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 | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3 | Copyright (c) 2018-2022, Laurence Lundblade. |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 4 | Copyright (c) 2021, Arm Limited. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 5 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 6 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are |
| 9 | met: |
| 10 | * Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above |
| 13 | copyright notice, this list of conditions and the following |
| 14 | disclaimer in the documentation and/or other materials provided |
| 15 | with the distribution. |
| 16 | * Neither the name of The Linux Foundation nor the names of its |
| 17 | contributors, nor the name "Laurence Lundblade" may be used to |
| 18 | endorse or promote products derived from this software without |
| 19 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 20 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 21 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 25 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 30 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 32 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 33 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 34 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 35 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 36 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 37 | #include "ieee754.h" /* Does not use math.h */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 38 | |
| 39 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 40 | |
| 41 | #include <math.h> /* For isnan(), llround(), llroudf(), round(), roundf(), |
| 42 | * pow(), exp2() |
| 43 | */ |
| 44 | #include <fenv.h> /* feclearexcept(), fetestexcept() */ |
| 45 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 46 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 47 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 48 | |
Laurence Lundblade | d3adb7c | 2023-02-08 12:43:11 -0700 | [diff] [blame] | 49 | #if (defined(__GNUC__) && !defined(__clang__)) |
| 50 | /* -Wmaybe-uninitialized will generate false warnings because it |
| 51 | * doesn't do a thorough static analysis. It is known that The |
| 52 | * warnings generated for this code are false positives because 1) |
| 53 | * each case was examined and 2) this code has been run through proper |
| 54 | * static analyzers. |
| 55 | * |
| 56 | * What is reported by -Wmaybe-uninitialized varies by GCC version, |
| 57 | * target OS and optimizer settings. Sometimes it's on with -Wall and |
| 58 | * some times it's not. Compilers that aren't GCC (e.g., clang) |
| 59 | * pretend to be GCC and don't know about this warn option. It's kind |
| 60 | * of a mess... :-( |
| 61 | * |
| 62 | * There are no warnings for qcbode_decode.c with |
| 63 | * -Wuninitialized. Presumably the difference is that -Wuninitialized |
| 64 | * only reports issues that are clearly issues that have no |
| 65 | * possibility of a false positive. |
| 66 | * |
| 67 | * This pragma disables the warning for -Wmaybe-uninitialized for the whole file so the source |
| 68 | * code is kept a bit cleaner and nicer, in particularly because what |
| 69 | * this warns about is so variable. |
| 70 | * |
| 71 | * Another way to solve this is to provide initializations. The |
| 72 | * problem with this is that they make the code bigger, |
| 73 | * but even worse, just initializing without understanding what |
| 74 | * the code does and what it should be initialized to understand if it really |
| 75 | * isn't initialized is just as bad for the end correctness as |
| 76 | * suppressing the warning without checking the code. |
| 77 | * |
| 78 | * Most of the warnings are about Item.Item->uDataType being |
| 79 | * unitialized. QCBORDecode_GetNext() *always* sets this value. -Wmaybe-uninitialized |
| 80 | * just can't tell. |
| 81 | * |
| 82 | * https://stackoverflow.com/questions/5080848/disable-gcc-may-be-used-uninitialized-on-a-particular-variable |
| 83 | */ |
| 84 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 85 | #endif |
| 86 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 87 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 88 | #define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type)) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 89 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 90 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 91 | |
| 92 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 93 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 94 | QCBORItem_IsMapOrArray(const QCBORItem *pMe) |
| 95 | { |
| 96 | const uint8_t uDataType = pMe->uDataType; |
| 97 | return uDataType == QCBOR_TYPE_MAP || |
| 98 | uDataType == QCBOR_TYPE_ARRAY || |
| 99 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
| 100 | } |
| 101 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 102 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 103 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 104 | { |
| 105 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if(pMe->val.uCount != 0) { |
| 110 | return false; |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 115 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 116 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 117 | { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 118 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 119 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if(pMe->val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
| 124 | return false; |
| 125 | } |
| 126 | return true; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 127 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 128 | (void)pMe; |
| 129 | return false; |
| 130 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 134 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 135 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 136 | ===========================================================================*/ |
| 137 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 138 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 139 | * See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
| 140 | * the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 141 | */ |
| 142 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 143 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 144 | static inline uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 145 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 146 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 147 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 148 | /* Limit in DecodeNesting_Descend against more than |
| 149 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 150 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 151 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 154 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 155 | static inline uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 156 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 157 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 158 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 159 | /* Limit in DecodeNesting_Descend against more than |
| 160 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 161 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 162 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 165 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 166 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 167 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 168 | { |
| 169 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 170 | } |
| 171 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 172 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 173 | static inline bool |
| 174 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 175 | { |
| 176 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 177 | return true; |
| 178 | } else { |
| 179 | return false; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 184 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 185 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 186 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 187 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 188 | return true; |
| 189 | } else { |
| 190 | return false; |
| 191 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 194 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 195 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 196 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 197 | { |
| 198 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 199 | /* Not a map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 202 | |
| 203 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 204 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 205 | /* Is indefinite */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 208 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 209 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 210 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 211 | /* All checks passed; is a definte length map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 215 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 216 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 217 | { |
| 218 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 219 | /* is a byte string */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 225 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 226 | static inline bool DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 227 | { |
| 228 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 229 | return true; |
| 230 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 231 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 237 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 238 | static inline void DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 239 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 240 | /* Should be only called on maps and arrays */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 241 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 242 | * DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 243 | * larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 244 | * uin32_t so the cast is safe. |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 245 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 246 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 247 | |
| 248 | if(bIsEmpty) { |
| 249 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 250 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 253 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 254 | static inline void DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 255 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 256 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 259 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 260 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 261 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 262 | { |
| 263 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 264 | /* No bounded map or array set up */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 268 | /* Not a map or array; end of those is by byte count */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 269 | return false; |
| 270 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 271 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 272 | /* In a traveral at a level deeper than the bounded level */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 273 | return false; |
| 274 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 275 | /* Works for both definite- and indefinitelength maps/arrays */ |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 276 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0 && |
| 277 | pNesting->pCurrentBounded->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 278 | /* Count is not zero, still unconsumed item */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 279 | return false; |
| 280 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 281 | /* All checks passed, got to the end of an array or map*/ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 282 | return true; |
| 283 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 284 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 285 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 286 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 287 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 288 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 289 | /* Must only be called on map / array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 290 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 291 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 292 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 297 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 298 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 299 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 300 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 301 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 302 | return true; |
| 303 | } else { |
| 304 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 305 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 308 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 309 | static inline bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 310 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 311 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 312 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 313 | return false; |
| 314 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 315 | |
| 316 | if(pNesting->pCurrentBounded->uLevelType != uType) { |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 323 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 324 | static inline void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 325 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 326 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 327 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 328 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 329 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 330 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 331 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 332 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 333 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 334 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 335 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 336 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 337 | } |
| 338 | |
| 339 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 340 | static inline void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 341 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 342 | { |
| 343 | pNesting->pCurrent--; |
| 344 | } |
| 345 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 346 | |
| 347 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 348 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 349 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 350 | /* Error out if nesting is too deep */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 351 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 352 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 355 | /* The actual descend */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 356 | pNesting->pCurrent++; |
| 357 | |
| 358 | pNesting->pCurrent->uLevelType = uType; |
| 359 | |
| 360 | return QCBOR_SUCCESS; |
| 361 | } |
| 362 | |
| 363 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 364 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 365 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, |
| 366 | bool bIsEmpty, |
| 367 | size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 368 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 369 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 370 | * Should only be called on map/array. |
| 371 | * |
| 372 | * Have descended into this before this is called. The job here is |
| 373 | * just to mark it in bounded mode. |
| 374 | * |
| 375 | * Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
| 376 | * uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET. |
| 377 | * |
| 378 | * 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] | 379 | */ |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 380 | if((uint32_t)uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 381 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 384 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 385 | |
| 386 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 387 | |
| 388 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 391 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 392 | static inline QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 393 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 394 | uint8_t uQCBORType, |
| 395 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 396 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 397 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 398 | |
| 399 | if(uCount == 0) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 400 | /* Nothing to do for empty definite-length arrays. They are just are |
| 401 | * effectively the same as an item that is not a map or array. |
| 402 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 403 | goto Done; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 404 | /* Empty indefinite-length maps and arrays are handled elsewhere */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 407 | /* Error out if arrays is too long to handle */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 408 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 409 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 410 | uError = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 411 | goto Done; |
| 412 | } |
| 413 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 414 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 415 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 416 | goto Done; |
| 417 | } |
| 418 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 419 | /* Fill in the new map/array level. Check above makes casts OK. */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 420 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 421 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 422 | |
| 423 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 424 | |
| 425 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 426 | return uError;; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | static inline void |
| 431 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 432 | { |
| 433 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | static inline void |
| 438 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 439 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 440 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 441 | pNesting->pCurrentBounded--; |
| 442 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 443 | break; |
| 444 | } |
| 445 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 448 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 449 | static inline void |
| 450 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 451 | { |
| 452 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 453 | } |
| 454 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 455 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 456 | static inline QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 457 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 458 | uint32_t uEndOffset, |
| 459 | uint32_t uStartOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 460 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 461 | QCBORError uError; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 462 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 463 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 464 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 465 | goto Done; |
| 466 | } |
| 467 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 468 | /* Fill in the new byte string level */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 469 | pNesting->pCurrent->u.bs.uSavedEndOffset = uEndOffset; |
| 470 | pNesting->pCurrent->u.bs.uBstrStartOffset = uStartOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 471 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 472 | /* Bstr wrapped levels are always bounded */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 473 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 474 | |
| 475 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 476 | return uError;; |
| 477 | } |
| 478 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 479 | |
| 480 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 481 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 482 | { |
| 483 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 486 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 487 | static inline void |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 488 | DecodeNesting_ResetMapOrArrayCount(QCBORDecodeNesting *pNesting) |
| 489 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 490 | if(pNesting->pCurrent->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 491 | pNesting->pCurrentBounded->u.ma.uCountCursor = pNesting->pCurrentBounded->u.ma.uCountTotal; |
| 492 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
| 496 | static inline void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 497 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 498 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 499 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 500 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 501 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 505 | static inline void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 506 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, |
| 507 | QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 508 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 509 | *pSave = *pNesting; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 512 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 513 | static inline void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 514 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, |
| 515 | const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 516 | { |
| 517 | *pNesting = *pSave; |
| 518 | } |
| 519 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 520 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 521 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 522 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 523 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 524 | return pMe->pCurrentBounded->u.bs.uSavedEndOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 528 | |
| 529 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 530 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 531 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 532 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 533 | |
| 534 | The following four functions are pretty wrappers for invocation of |
| 535 | the string allocator supplied by the caller. |
| 536 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 537 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 538 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 539 | static inline void |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 540 | StringAllocator_Free(const QCBORInternalAllocator *pMe, const void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 541 | { |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 542 | /* This cast to uintptr_t suppresses the "-Wcast-qual" warnings. |
| 543 | * This is the one place where the const needs to be cast away so const can |
| 544 | * be use in the rest of the code. |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 545 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 546 | (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 549 | // StringAllocator_Reallocate called with pMem NULL is |
| 550 | // equal to StringAllocator_Allocate() |
| 551 | static inline UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 552 | StringAllocator_Reallocate(const QCBORInternalAllocator *pMe, |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 553 | const void *pMem, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 554 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 555 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 556 | /* See comment in StringAllocator_Free() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 557 | return (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, uSize); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 560 | static inline UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 561 | StringAllocator_Allocate(const QCBORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 562 | { |
| 563 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 564 | } |
| 565 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 566 | static inline void |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 567 | StringAllocator_Destruct(const QCBORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 568 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 569 | /* See comment in StringAllocator_Free() */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 570 | if(pMe->pfAllocator) { |
| 571 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 572 | } |
| 573 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 574 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 575 | |
| 576 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 577 | |
| 578 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 579 | /*=========================================================================== |
| 580 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 581 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 582 | See qcbor/qcbor_decode.h for definition of the object |
| 583 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 584 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 585 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 586 | * Public function, see header file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 587 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 588 | void QCBORDecode_Init(QCBORDecodeContext *pMe, |
| 589 | UsefulBufC EncodedCBOR, |
| 590 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 591 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 592 | memset(pMe, 0, sizeof(QCBORDecodeContext)); |
| 593 | UsefulInputBuf_Init(&(pMe->InBuf), EncodedCBOR); |
| 594 | /* Don't bother with error check on decode mode. If a bad value is |
| 595 | * passed it will just act as if the default normal mode of 0 was set. |
| 596 | */ |
| 597 | pMe->uDecodeMode = (uint8_t)nDecodeMode; |
| 598 | DecodeNesting_Init(&(pMe->nesting)); |
| 599 | |
| 600 | /* Inialize me->auMappedTags to CBOR_TAG_INVALID16. See |
| 601 | * GetNext_TaggedItem() and MapTagNumber(). */ |
| 602 | memset(pMe->auMappedTags, 0xff, sizeof(pMe->auMappedTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 606 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 607 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 608 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 609 | * Public function, see header file |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 610 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 611 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 612 | QCBORStringAllocate pfAllocateFunction, |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 613 | void *pAllocateContext, |
| 614 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 615 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 616 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 617 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 618 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 619 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 620 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 621 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 622 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 623 | |
| 624 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 625 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 626 | * Deprecated public function, see header file |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 627 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 628 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 629 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 630 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 631 | /* This does nothing now. It is retained for backwards compatibility */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 632 | (void)pMe; |
| 633 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 634 | } |
| 635 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 636 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 637 | |
| 638 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 639 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 640 | * Decoding items is done in six layers, one calling the next one |
| 641 | * down. If a layer has no work to do for a particular item, it |
| 642 | * returns quickly. |
| 643 | * |
| 644 | * 1. QCBORDecode_GetNextTagContent - The top layer processes tagged |
| 645 | * data items, turning them into the local C representation. For the |
| 646 | * most simple it is just associating a QCBOR_TYPE with the data. For |
| 647 | * the complex ones that an aggregate of data items, there is some |
| 648 | * further decoding and some limited recursion. |
| 649 | * |
| 650 | * 2. QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
| 651 | * ends of maps and arrays. It tracks descending into and ascending |
| 652 | * out of maps/arrays. It processes breaks that terminate |
| 653 | * indefinite-length maps and arrays. |
| 654 | * |
| 655 | * 3. QCBORDecode_GetNextMapEntry - This handles the combining of two |
| 656 | * items, the label and the data, that make up a map entry. It only |
| 657 | * does work on maps. It combines the label and data items into one |
| 658 | * labeled item. |
| 659 | * |
| 660 | * 4. QCBORDecode_GetNextTagNumber - This decodes type 6 tag |
| 661 | * numbers. It turns the tag numbers into bit flags associated with |
| 662 | * the data item. No actual decoding of the contents of the tag is |
| 663 | * performed here. |
| 664 | * |
| 665 | * 5. QCBORDecode_GetNextFullString - This assembles the sub-items |
| 666 | * that make up an indefinite-length string into one string item. It |
| 667 | * uses the string allocator to create contiguous space for the |
| 668 | * item. It processes all breaks that are part of indefinite-length |
| 669 | * strings. |
| 670 | * |
| 671 | * 6. DecodeAtomicDataItem - This decodes the atomic data items in |
| 672 | * CBOR. Each atomic data item has a "major type", an integer |
| 673 | * "argument" and optionally some content. For text and byte strings, |
| 674 | * the content is the bytes that make up the string. These are the |
| 675 | * smallest data items that are considered to be well-formed. The |
| 676 | * content may also be other data items in the case of aggregate |
| 677 | * types. They are not handled in this layer. |
| 678 | * |
| 679 | * Roughly this takes 300 bytes of stack for vars. TODO: evaluate this |
| 680 | * more carefully and correctly. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 681 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 682 | |
| 683 | |
| 684 | /* |
| 685 | * Note about use of int and unsigned variables. |
| 686 | * |
| 687 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 688 | * used carefully here, and in particular why it isn't used in the |
| 689 | * public interface. Also see |
| 690 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 691 | * |
| 692 | * Int is used for values that need less than 16-bits and would be |
| 693 | * subject to integer promotion and result in complaining from static |
| 694 | * analyzers. |
| 695 | */ |
| 696 | |
| 697 | |
| 698 | /** |
| 699 | * @brief Decode the CBOR head, the type and argument. |
| 700 | * |
| 701 | * @param[in] pUInBuf The input buffer to read from. |
| 702 | * @param[out] pnMajorType The decoded major type. |
| 703 | * @param[out] puArgument The decoded argument. |
| 704 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 705 | * |
| 706 | * @retval QCBOR_ERR_UNSUPPORTED |
| 707 | * @retval QCBOR_ERR_HIT_END |
| 708 | * |
| 709 | * This decodes the CBOR "head" that every CBOR data item has. See |
| 710 | * longer explaination of the head in documentation for |
| 711 | * QCBOREncode_EncodeHead(). |
| 712 | * |
| 713 | * This does the network->host byte order conversion. The conversion |
| 714 | * here also results in the conversion for floats in addition to that |
| 715 | * for lengths, tags and integer values. |
| 716 | * |
| 717 | * The int type is preferred to uint8_t for some variables as this |
| 718 | * avoids integer promotions, can reduce code size and makes static |
| 719 | * analyzers happier. |
| 720 | */ |
| 721 | static inline QCBORError |
| 722 | DecodeHead(UsefulInputBuf *pUInBuf, |
| 723 | int *pnMajorType, |
| 724 | uint64_t *puArgument, |
| 725 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 726 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 727 | QCBORError uReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 728 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 729 | /* Get the initial byte that every CBOR data item has and break it |
| 730 | * down. */ |
| 731 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 732 | const int nTmpMajorType = nInitialByte >> 5; |
| 733 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 734 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 735 | /* Where the argument accumulates */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 736 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 737 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 738 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 739 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 740 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 741 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 742 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 743 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 744 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 745 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 746 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 747 | /* This shift and add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 748 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 749 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 750 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 751 | /* The reserved and thus-far unused additional info values */ |
| 752 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 753 | goto Done; |
| 754 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 755 | /* Less than 24, additional info is argument or 31, an |
| 756 | * indefinite-length. No more bytes to get. |
| 757 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 758 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 759 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 760 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 761 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 762 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 763 | goto Done; |
| 764 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 765 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 766 | /* All successful if arrived here. */ |
| 767 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 768 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 769 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 770 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 771 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 772 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 773 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 776 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 777 | /** |
| 778 | * @brief Decode integer types, major types 0 and 1. |
| 779 | * |
| 780 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 781 | * @param[in] uArgument The argument from the head. |
| 782 | * @param[out] pDecodedItem The filled in decoded item. |
| 783 | * |
| 784 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 785 | * |
| 786 | * Must only be called when major type is 0 or 1. |
| 787 | * |
| 788 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 789 | * all CPUs use it these days and the test vectors in the RFC are |
| 790 | * so. All integers in the CBOR structure are positive and the major |
| 791 | * type indicates positive or negative. CBOR can express positive |
| 792 | * integers up to 2^x - 1 where x is the number of bits and negative |
| 793 | * integers down to 2^x. Note that negative numbers can be one more |
| 794 | * away from zero than positive. Stdint, as far as I can tell, uses |
| 795 | * two's compliment to represent negative integers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 796 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 797 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 798 | DecodeInteger(int nMajorType, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 799 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 800 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 801 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 802 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 803 | if (uArgument <= INT64_MAX) { |
| 804 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 805 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 806 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 807 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 808 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 809 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 810 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 811 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 812 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 813 | if(uArgument <= INT64_MAX) { |
| 814 | /* CBOR's representation of negative numbers lines up with |
| 815 | * the two-compliment representation. A negative integer has |
| 816 | * one more in range than a positive integer. INT64_MIN is |
| 817 | * equal to (-INT64_MAX) - 1. |
| 818 | */ |
| 819 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 820 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 821 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 822 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 823 | /* C can't represent a negative integer in this range so it |
| 824 | * is an error. |
| 825 | */ |
| 826 | uReturn = QCBOR_ERR_INT_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 829 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 830 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 833 | |
| 834 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 835 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 836 | #error QCBOR_TYPE_FALSE macro value wrong |
| 837 | #endif |
| 838 | |
| 839 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 840 | #error QCBOR_TYPE_TRUE macro value wrong |
| 841 | #endif |
| 842 | |
| 843 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 844 | #error QCBOR_TYPE_NULL macro value wrong |
| 845 | #endif |
| 846 | |
| 847 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 848 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 849 | #endif |
| 850 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 851 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 852 | #error QCBOR_TYPE_BREAK macro value wrong |
| 853 | #endif |
| 854 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 855 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 856 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 857 | #endif |
| 858 | |
| 859 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 860 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 861 | #endif |
| 862 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 863 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 864 | /** |
| 865 | * @brief Decode major type 7 -- true, false, floating-point, break... |
| 866 | * |
| 867 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 868 | * @param[in] uArgument The argument from the head. |
| 869 | * @param[out] pDecodedItem The filled in decoded item. |
| 870 | * |
| 871 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 872 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 873 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 874 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 875 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 876 | static inline QCBORError |
| 877 | DecodeType7(int nAdditionalInfo, uint64_t uArgument, QCBORItem *pDecodedItem) |
| 878 | { |
| 879 | QCBORError uReturn = QCBOR_SUCCESS; |
| 880 | |
| 881 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 882 | * checks above make sure uAdditionalInfo values line up with |
| 883 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 884 | * > 0x1f so cast is safe. |
| 885 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 886 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 887 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 888 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 889 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 890 | * are caught before this is called. |
| 891 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 892 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 893 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 894 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 895 | /* Half-precision is returned as a double. The cast to |
| 896 | * uint16_t is safe because the encoded value was 16 bits. It |
| 897 | * was widened to 64 bits to be passed in here. |
| 898 | */ |
| 899 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 900 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 901 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 902 | uReturn = FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 903 | break; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 904 | case SINGLE_PREC_FLOAT: /* 26 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 905 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 906 | /* Single precision is normally returned as a double since |
| 907 | * double is widely supported, there is no loss of precision, |
| 908 | * it makes it easy for the caller in most cases and it can |
| 909 | * be converted back to single with no loss of precision |
| 910 | * |
| 911 | * The cast to uint32_t is safe because the encoded value was |
| 912 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 913 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 914 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 915 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 916 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 917 | /* In the normal case, use HW to convert float to |
| 918 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 919 | pDecodedItem->val.dfnum = (double)f; |
| 920 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 921 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 922 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 923 | pDecodedItem->val.fnum = f; |
| 924 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 925 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 926 | /* IEEE754_FloatToDouble() could be used here to return as |
| 927 | * a double, but it adds object code and most likely |
| 928 | * anyone disabling FLOAT HW use doesn't care about floats |
| 929 | * and wants to save object code. |
| 930 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 931 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 932 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 933 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 934 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 935 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 936 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 937 | case DOUBLE_PREC_FLOAT: /* 27 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 938 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 939 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 940 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 941 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 942 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 943 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 944 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 945 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 946 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 947 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 948 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 949 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 950 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 951 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 952 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 953 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 954 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 955 | * as e0 … f7 |
| 956 | */ |
| 957 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 958 | goto Done; |
| 959 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 960 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 961 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 962 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 963 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 964 | /* DecodeHead() will make uArgument equal to |
| 965 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 966 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 967 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 968 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 969 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 970 | break; |
| 971 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 972 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 973 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 974 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 978 | /** |
| 979 | * @brief Decode text and byte strings |
| 980 | * |
| 981 | * @param[in] pAllocator The string allocator or NULL. |
| 982 | * @param[in] uStrLen The length of the string. |
| 983 | * @param[in] pUInBuf The surce from which to read the string's bytes. |
| 984 | * @param[out] pDecodedItem The filled in decoded item. |
| 985 | * |
| 986 | * @retval QCBOR_ERR_HIT_END |
| 987 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 988 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 989 | * |
| 990 | * The reads @c uStrlen bytes from @c pUInBuf and fills in @c |
| 991 | * pDecodedItem. If @c pAllocator is not NULL then memory for the |
| 992 | * string is allocated. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 993 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 994 | static inline QCBORError |
| 995 | DecodeBytes(const QCBORInternalAllocator *pAllocator, |
| 996 | uint64_t uStrLen, |
| 997 | UsefulInputBuf *pUInBuf, |
| 998 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 999 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1000 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1001 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1002 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 1003 | * CPUs. This check makes the casts to size_t below safe. |
| 1004 | * |
| 1005 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 1006 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 1007 | * one will care the limit on strings is 4 bytes shorter). |
| 1008 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1009 | if(uStrLen > SIZE_MAX-4) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1010 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1011 | goto Done; |
| 1012 | } |
| 1013 | |
| 1014 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1015 | if(UsefulBuf_IsNULLC(Bytes)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1016 | /* Failed to get the bytes for this string item */ |
| 1017 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1018 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1019 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1020 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1021 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1022 | /* Note that this is not where allocation to coalesce |
| 1023 | * indefinite-length strings is done. This is for when the caller |
| 1024 | * has requested all strings be allocated. Disabling indefinite |
| 1025 | * length strings also disables this allocate-all option. |
| 1026 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1027 | if(pAllocator) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1028 | /* request to use the string allocator to make a copy */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1029 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1030 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1031 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1032 | goto Done; |
| 1033 | } |
| 1034 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1035 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1036 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1037 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1038 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1039 | (void)pAllocator; |
| 1040 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1041 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1042 | /* Normal case with no string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1043 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1044 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1045 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1046 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1049 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1050 | /** |
| 1051 | * @brief Map the CBOR major types for strings to the QCBOR types. |
| 1052 | * |
| 1053 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1054 | * @retturns QCBOR type number. |
| 1055 | * |
| 1056 | * This only works for the two string types. |
| 1057 | */ |
| 1058 | static inline uint8_t ConvertStringMajorTypes(int nCBORMajorType) |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1059 | { |
| 1060 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 1061 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 1062 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1063 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1064 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 1065 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 1066 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1067 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1068 | return (uint8_t)(nCBORMajorType + 4); |
| 1069 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1070 | |
| 1071 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1072 | /** |
| 1073 | * @brief Map the CBOR major types for arrays/maps to the QCBOR types. |
| 1074 | * |
| 1075 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1076 | * @retturns QCBOR type number. |
| 1077 | * |
| 1078 | * This only works for the two aggregate types. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1079 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1080 | static inline uint8_t ConvertArrayOrMapType(int nCBORMajorType) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1081 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1082 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1083 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1084 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1085 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1086 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1087 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1088 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1089 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1090 | return (uint8_t)(nCBORMajorType); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1094 | /** |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1095 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1096 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1097 | * @param[in] pUInBuf Input buffer to read data item from. |
| 1098 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1099 | * @param[in] pAllocator The allocator to use for strings or NULL. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1100 | * |
| 1101 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1102 | * @retval QCBOR_ERR_HIT_END |
| 1103 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1104 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1105 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1106 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1107 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1108 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1109 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1110 | * |
| 1111 | * This decodes the most primitive / atomic data item. It does |
| 1112 | * no combing of data items. |
| 1113 | */ |
| 1114 | static QCBORError |
| 1115 | DecodeAtomicDataItem(UsefulInputBuf *pUInBuf, |
| 1116 | QCBORItem *pDecodedItem, |
| 1117 | const QCBORInternalAllocator *pAllocator) |
| 1118 | { |
| 1119 | QCBORError uReturn; |
| 1120 | |
| 1121 | /* Get the major type and the argument. The argument could be |
| 1122 | * length of more bytes or the value depending on the major |
| 1123 | * type. nAdditionalInfo is an encoding of the length of the |
| 1124 | * uNumber and is needed to decode floats and doubles. |
| 1125 | */ |
| 1126 | int nMajorType = 0; |
| 1127 | uint64_t uArgument = 0; |
| 1128 | int nAdditionalInfo = 0; |
| 1129 | |
| 1130 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1131 | |
| 1132 | uReturn = DecodeHead(pUInBuf, &nMajorType, &uArgument, &nAdditionalInfo); |
| 1133 | if(uReturn) { |
| 1134 | goto Done; |
| 1135 | } |
| 1136 | |
| 1137 | /* At this point the major type and the argument are valid. We've |
| 1138 | * got the type and the argument that starts every CBOR data item. |
| 1139 | */ |
| 1140 | switch (nMajorType) { |
| 1141 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1142 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
| 1143 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1144 | uReturn = QCBOR_ERR_BAD_INT; |
| 1145 | } else { |
| 1146 | uReturn = DecodeInteger(nMajorType, uArgument, pDecodedItem); |
| 1147 | } |
| 1148 | break; |
| 1149 | |
| 1150 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1151 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
| 1152 | pDecodedItem->uDataType = ConvertStringMajorTypes(nMajorType); |
| 1153 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1154 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
| 1155 | } else { |
| 1156 | uReturn = DecodeBytes(pAllocator, uArgument, pUInBuf, pDecodedItem); |
| 1157 | } |
| 1158 | break; |
| 1159 | |
| 1160 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1161 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
| 1162 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1163 | /* Indefinite-length string. */ |
| 1164 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 1165 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
| 1166 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1167 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
| 1168 | break; |
| 1169 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1170 | } else { |
| 1171 | /* Definite-length string. */ |
| 1172 | if(uArgument > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1173 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1174 | goto Done; |
| 1175 | } |
| 1176 | /* cast OK because of check above */ |
| 1177 | pDecodedItem->val.uCount = (uint16_t)uArgument; |
| 1178 | } |
| 1179 | pDecodedItem->uDataType = ConvertArrayOrMapType(nMajorType); |
| 1180 | break; |
| 1181 | |
| 1182 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1183 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1184 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1185 | uReturn = QCBOR_ERR_BAD_INT; |
| 1186 | } else { |
| 1187 | pDecodedItem->val.uTagV = uArgument; |
| 1188 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
| 1189 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1190 | #else /* QCBOR_DISABLE_TAGS */ |
| 1191 | uReturn = QCBOR_ERR_TAGS_DISABLED; |
| 1192 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1193 | break; |
| 1194 | |
| 1195 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1196 | /* Major type 7: float, double, true, false, null... */ |
| 1197 | uReturn = DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
| 1198 | break; |
| 1199 | |
| 1200 | default: |
| 1201 | /* Never happens because DecodeHead() should never return > 7 */ |
| 1202 | uReturn = QCBOR_ERR_UNSUPPORTED; |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | Done: |
| 1207 | return uReturn; |
| 1208 | } |
| 1209 | |
| 1210 | |
| 1211 | /** |
| 1212 | * @brief Process indefinite-length strings (decode layer 5). |
| 1213 | * |
| 1214 | * @param[in] pMe Decoder context |
| 1215 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1216 | * |
| 1217 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1218 | * @retval QCBOR_ERR_HIT_END |
| 1219 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1220 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1221 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1222 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1223 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1224 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1225 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1226 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1227 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1228 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1229 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1230 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1231 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1232 | * If it is, this loops getting the subsequent chunk data items that |
| 1233 | * make up the string. The string allocator is used to make a |
| 1234 | * contiguous buffer for the chunks. When this completes @c |
| 1235 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1236 | * |
| 1237 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1238 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1239 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1240 | QCBORDecode_GetNextFullString(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1241 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1242 | /* Aproximate stack usage |
| 1243 | * 64-bit 32-bit |
| 1244 | * local vars 32 16 |
| 1245 | * 2 UsefulBufs 32 16 |
| 1246 | * QCBORItem 56 52 |
| 1247 | * TOTAL 120 74 |
| 1248 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1249 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1250 | /* The string allocator is used here for two purposes: 1) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1251 | * coalescing the chunks of an indefinite-length string, 2) |
| 1252 | * allocating storage for every string returned when requested. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1253 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1254 | * The first use is below in this function. Indefinite-length |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1255 | * strings cannot be processed at all without a string allocator. |
| 1256 | * |
| 1257 | * The second used is in DecodeBytes() which is called by |
| 1258 | * GetNext_Item() below. This second use unneccessary for most use |
| 1259 | * and only happens when requested in the call to |
| 1260 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1261 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1262 | * |
| 1263 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1264 | * allocator altogether and thus both of these uses. It reduced the |
| 1265 | * decoder object code by about 400 bytes. |
| 1266 | */ |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1267 | const QCBORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1268 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1269 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1270 | const QCBORInternalAllocator *pAllocator = NULL; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1271 | |
| 1272 | if(pMe->StringAllocator.pfAllocator) { |
| 1273 | pAllocator = &(pMe->StringAllocator); |
| 1274 | if(pMe->bStringAllocateAll) { |
| 1275 | pAllocatorForGetNext = pAllocator; |
| 1276 | } |
| 1277 | } |
| 1278 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1279 | |
| 1280 | QCBORError uReturn; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1281 | uReturn = DecodeAtomicDataItem(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1282 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1283 | goto Done; |
| 1284 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1285 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1286 | /* Only do indefinite-length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1287 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1288 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1289 | goto Done; |
| 1290 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1291 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1292 | /* Is this a string with an indefinite length? */ |
| 1293 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1294 | goto Done; |
| 1295 | } |
| 1296 | |
| 1297 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1298 | /* Can't decode indefinite-length strings without a string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1299 | if(pAllocator == NULL) { |
| 1300 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1301 | goto Done; |
| 1302 | } |
| 1303 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1304 | /* Loop getting chunks of the indefinite-length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1305 | UsefulBufC FullString = NULLUsefulBufC; |
| 1306 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1307 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1308 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1309 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1310 | /* Pass a NULL string allocator to GetNext_Item() because the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1311 | * individual string chunks in an indefinite-length should not |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1312 | * be allocated. They are always copied in the the contiguous |
| 1313 | * buffer allocated here. |
| 1314 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1315 | uReturn = DecodeAtomicDataItem(&(pMe->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1316 | if(uReturn) { |
| 1317 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1318 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1319 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1320 | /* Is item is the marker for end of the indefinite-length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1321 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1322 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1323 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1324 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1325 | break; |
| 1326 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1327 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1328 | /* 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] | 1329 | * that introduces the indefinite-length string. This also |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1330 | * 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] | 1331 | * indefinite-length string inside an indefinite-length string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1332 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1333 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1334 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1335 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1336 | break; |
| 1337 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1338 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1339 | if (StringChunkItem.val.string.len > 0) { |
| 1340 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1341 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1342 | * not NULL and a reallocation happens. |
| 1343 | */ |
| 1344 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1345 | FullString.ptr, |
| 1346 | FullString.len + StringChunkItem.val.string.len); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1347 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1348 | if(UsefulBuf_IsNULL(NewMem)) { |
| 1349 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
| 1350 | break; |
| 1351 | } |
| 1352 | |
| 1353 | /* Copy new string chunk to the end of accumulated string */ |
| 1354 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1355 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1356 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1357 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1358 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1359 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 1360 | StringAllocator_Free(pAllocator, FullString.ptr); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1361 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1362 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1363 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1364 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1365 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1366 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1367 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1370 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1371 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1372 | /** |
| 1373 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1374 | * |
| 1375 | * @param[in] pMe The decode context. |
| 1376 | * @param[in] uUnMappedTag The tag number to map |
| 1377 | * @param[out] puMappedTagNumer The stored tag number. |
| 1378 | * |
| 1379 | * @return error code. |
| 1380 | * |
| 1381 | * The main point of mapping tag numbers is make QCBORItem |
| 1382 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1383 | * bytes. Without, it would take up 32 bytes. |
| 1384 | * |
| 1385 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1386 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1387 | * |
| 1388 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1389 | */ |
| 1390 | static inline QCBORError |
| 1391 | MapTagNumber(QCBORDecodeContext *pMe, uint64_t uUnMappedTag, uint16_t *puMappedTagNumer) |
| 1392 | { |
| 1393 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1394 | unsigned uTagMapIndex; |
| 1395 | /* Is there room in the tag map, or is it in it already? */ |
| 1396 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1397 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1398 | break; |
| 1399 | } |
| 1400 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1401 | break; |
| 1402 | } |
| 1403 | } |
| 1404 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1405 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1406 | } |
| 1407 | |
| 1408 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1409 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1410 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1411 | |
| 1412 | } else { |
| 1413 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1414 | } |
| 1415 | |
| 1416 | return QCBOR_SUCCESS; |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | /** |
| 1421 | * @brief This converts a mapped tag number to the actual tag number. |
| 1422 | * |
| 1423 | * @param[in] pMe The decode context. |
| 1424 | * @param[in] uMappedTagNumber The stored tag number. |
| 1425 | * |
| 1426 | * @return The actual tag number is returned or |
| 1427 | * @ref CBOR_TAG_INVALID64 on error. |
| 1428 | * |
| 1429 | * This is the reverse of MapTagNumber() |
| 1430 | */ |
| 1431 | static uint64_t |
| 1432 | UnMapTagNumber(const QCBORDecodeContext *pMe, uint16_t uMappedTagNumber) |
| 1433 | { |
| 1434 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1435 | return uMappedTagNumber; |
| 1436 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1437 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1438 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1439 | /* This won't be negative because of code below in |
| 1440 | * MapTagNumber() |
| 1441 | */ |
| 1442 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1443 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1444 | } |
| 1445 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1446 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1447 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1448 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1449 | /** |
| 1450 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
| 1451 | * |
| 1452 | * @param[in] pMe Decoder context |
| 1453 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1454 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1455 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1456 | * @retval QCBOR_ERR_HIT_END |
| 1457 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1458 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1459 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1460 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1461 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1462 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1463 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1464 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1465 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1466 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1467 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1468 | * |
| 1469 | * This loops getting atomic data items until one is not a tag |
| 1470 | * number. Usually this is largely pass-through because most |
| 1471 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1472 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1473 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1474 | QCBORDecode_GetNextTagNumber(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1475 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1476 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1477 | /* Accummulate the tags from multiple items here and then copy them |
| 1478 | * into the last item, the non-tag item. |
| 1479 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1480 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1481 | |
| 1482 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1483 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1484 | /* Be sure the memset does the right thing. */ |
| 1485 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1486 | #endif |
| 1487 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1488 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1489 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1490 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1491 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1492 | for(;;) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1493 | QCBORError uErr = QCBORDecode_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1494 | if(uErr != QCBOR_SUCCESS) { |
| 1495 | uReturn = uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1496 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1497 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1498 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1499 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1500 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1501 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1502 | break; |
| 1503 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1504 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1505 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1506 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1507 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1508 | /* Continue on to get all tags wrapping this item even though |
| 1509 | * it is erroring out in the end. This allows decoding to |
| 1510 | * continue. This is a resource limit error, not a problem |
| 1511 | * with being well-formed CBOR. |
| 1512 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1513 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1514 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1515 | /* Slide tags over one in the array to make room at index 0. |
| 1516 | * Must use memmove because the move source and destination |
| 1517 | * overlap. |
| 1518 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1519 | memmove(&auItemsTags[1], |
| 1520 | auItemsTags, |
| 1521 | sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1522 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1523 | /* Map the tag */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1524 | uint16_t uMappedTagNumber = 0; |
| 1525 | uReturn = MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumber); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1526 | /* Continue even on error so as to consume all tags wrapping |
| 1527 | * this data item so decoding can go on. If MapTagNumber() |
| 1528 | * errors once it will continue to error. |
| 1529 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1530 | auItemsTags[0] = uMappedTagNumber; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1531 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1532 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1533 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1534 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1535 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1536 | #else /* QCBOR_DISABLE_TAGS */ |
| 1537 | |
| 1538 | return QCBORDecode_GetNextFullString(pMe, pDecodedItem); |
| 1539 | |
| 1540 | #endif /* QCBOR_DISABLE_TAGS */ |
| 1541 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1542 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1543 | /** |
| 1544 | * @brief Combine a map entry label and value into one item (decode layer 3). |
| 1545 | * |
| 1546 | * @param[in] pMe Decoder context |
| 1547 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1548 | * |
| 1549 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1550 | * @retval QCBOR_ERR_HIT_END |
| 1551 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1552 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1553 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1554 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1555 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1556 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1557 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1558 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1559 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1560 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1561 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1562 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1563 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1564 | * |
| 1565 | * If a the current nesting level is a map, then this |
| 1566 | * combines pairs of items into one data item with a label |
| 1567 | * and value. |
| 1568 | * |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 1569 | * This is passthrough if the current nesting level is |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1570 | * not a map. |
| 1571 | * |
| 1572 | * This also implements maps-as-array mode where a map |
| 1573 | * is treated like an array to allow caller to do their |
| 1574 | * own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1575 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1576 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1577 | QCBORDecode_GetNextMapEntry(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1578 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1579 | QCBORError uReturn = QCBORDecode_GetNextTagNumber(pMe, pDecodedItem); |
| 1580 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1581 | goto Done; |
| 1582 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1583 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1584 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
| 1585 | /* Break can't be a map entry */ |
| 1586 | goto Done; |
| 1587 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1588 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1589 | if(pMe->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1590 | /* Normal decoding of maps -- combine label and value into one item. */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1591 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1592 | if(DecodeNesting_IsCurrentTypeMap(&(pMe->nesting))) { |
| 1593 | /* Save label in pDecodedItem and get the next which will |
| 1594 | * be the real data item. |
| 1595 | */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1596 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1597 | uReturn = QCBORDecode_GetNextTagNumber(pMe, pDecodedItem); |
| 1598 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1599 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1600 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1601 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1602 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1603 | |
| 1604 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1605 | /* strings are always good labels */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1606 | pDecodedItem->label.string = LabelItem.val.string; |
| 1607 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1608 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == pMe->uDecodeMode) { |
| 1609 | /* It's not a string and we only want strings */ |
| 1610 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1611 | goto Done; |
| 1612 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1613 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1614 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1615 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1616 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1617 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1618 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1619 | pDecodedItem->label.string = LabelItem.val.string; |
| 1620 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1621 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1622 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1623 | /* label is not an int or a string. It is an arrray |
| 1624 | * or a float or such and this implementation doesn't handle that. |
| 1625 | * Also, tags on labels are ignored. |
| 1626 | */ |
| 1627 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1628 | goto Done; |
| 1629 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1630 | } |
| 1631 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1632 | /* Decoding of maps as arrays to let the caller decide what to do |
| 1633 | * about labels, particularly lables that are not integers or |
| 1634 | * strings. |
| 1635 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1636 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1637 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1638 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1639 | goto Done; |
| 1640 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1641 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1642 | /* Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2. |
| 1643 | * Cast is needed because of integer promotion. |
| 1644 | */ |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1645 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1646 | } |
| 1647 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1648 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1649 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1650 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1654 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1655 | /** |
| 1656 | * @brief Peek and see if next data item is a break; |
| 1657 | * |
| 1658 | * @param[in] pUIB UsefulInputBuf to read from. |
| 1659 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1660 | * |
| 1661 | * @return Any decoding error. |
| 1662 | * |
| 1663 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1664 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1665 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1666 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1667 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1668 | { |
| 1669 | *pbNextIsBreak = false; |
| 1670 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1671 | QCBORItem Peek; |
| 1672 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1673 | QCBORError uReturn = DecodeAtomicDataItem(pUIB, &Peek, NULL); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1674 | if(uReturn != QCBOR_SUCCESS) { |
| 1675 | return uReturn; |
| 1676 | } |
| 1677 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1678 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1679 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1680 | } else { |
| 1681 | *pbNextIsBreak = true; |
| 1682 | } |
| 1683 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1684 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1685 | return QCBOR_SUCCESS; |
| 1686 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1687 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1688 | |
| 1689 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1690 | /** |
| 1691 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1692 | * |
| 1693 | * @param[in] pMe The decode context. |
| 1694 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
| 1695 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1696 | * An item was just consumed, now figure out if it was the |
| 1697 | * end of an array/map map that can be closed out. That |
| 1698 | * may in turn close out the above array/map... |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1699 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1700 | static QCBORError |
| 1701 | QCBORDecode_NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1702 | { |
| 1703 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1704 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1705 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1706 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1707 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1708 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1709 | /* Nesting level is bstr-wrapped CBOR */ |
| 1710 | |
| 1711 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1712 | * so no further ascending can happen. |
| 1713 | */ |
| 1714 | break; |
| 1715 | |
| 1716 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1717 | /* Level is a definite-length array/map */ |
| 1718 | |
| 1719 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1720 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1721 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1722 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1723 | break; |
| 1724 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1725 | /* All items in a definite-length array were consumed so it |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1726 | * is time to ascend one level. This happens below. |
| 1727 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1728 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1729 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1730 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1731 | /* Level is an indefinite-length array/map. */ |
| 1732 | |
| 1733 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1734 | bool bIsBreak = false; |
| 1735 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1736 | if(uReturn != QCBOR_SUCCESS) { |
| 1737 | goto Done; |
| 1738 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1739 | |
| 1740 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1741 | /* 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] | 1742 | break; |
| 1743 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1744 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1745 | /* It was a break in an indefinitelength map / array so |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1746 | * it is time to ascend one level. |
| 1747 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1748 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1749 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1750 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1751 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1752 | |
| 1753 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1754 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1755 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1756 | * QCBORDecode_ExitBoundedMode(). |
| 1757 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1758 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1759 | /* Set the count to zero for definite-length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1760 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1761 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1762 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1763 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1764 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1765 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1766 | break; |
| 1767 | } |
| 1768 | |
| 1769 | /* Finally, actually ascend one level. */ |
| 1770 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1771 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1772 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1773 | uReturn = QCBOR_SUCCESS; |
| 1774 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1775 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1776 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1777 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1778 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1779 | return uReturn; |
| 1780 | } |
| 1781 | |
| 1782 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1783 | /** |
| 1784 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
| 1785 | * |
| 1786 | * @param[in] pMe Decoder context |
| 1787 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1788 | * |
| 1789 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1790 | * @retval QCBOR_ERR_HIT_END |
| 1791 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1792 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1793 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1794 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1795 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1796 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1797 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1798 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1799 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1800 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1801 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1802 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1803 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1804 | * @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1805 | * @retval QCBOR_ERR_BAD_BREAK |
| 1806 | * @retval QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
| 1807 | * |
| 1808 | * This handles the traversal descending into and asecnding out of |
| 1809 | * maps, arrays and bstr-wrapped CBOR. It figures out the ends of |
| 1810 | * definite- and indefinte-length maps and arrays by looking at the |
| 1811 | * item count or finding CBOR breaks. It detects the ends of the |
| 1812 | * top-level sequence and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1813 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1814 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1815 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1816 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1817 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1818 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1819 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1820 | /* If out of bytes to consume, it is either the end of the |
| 1821 | * top-level sequence of some bstr-wrapped CBOR that was entered. |
| 1822 | * |
| 1823 | * In the case of bstr-wrapped CBOR, the length of the |
| 1824 | * UsefulInputBuf was set to that of the bstr-wrapped CBOR. When |
| 1825 | * the bstr-wrapped CBOR is exited, the length is set back to the |
| 1826 | * top-level's length or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1827 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1828 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1829 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1830 | goto Done; |
| 1831 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1832 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1833 | /* Check to see if at the end of a bounded definite-length map or |
| 1834 | * array. The check for a break ending indefinite-length array is |
| 1835 | * later in QCBORDecode_NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1836 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1837 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1838 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1839 | goto Done; |
| 1840 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1841 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1842 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1843 | uReturn = QCBORDecode_GetNextMapEntry(pMe, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1844 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1845 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1846 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1847 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1848 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1849 | /* Breaks ending arrays/maps are processed later in the call to |
| 1850 | * QCBORDecode_NestLevelAscender(). They should never show up here. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1851 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1852 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1853 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1854 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1855 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1856 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1857 | /* Record the nesting level for this data item before processing |
| 1858 | * any of decrementing and descending. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1859 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1860 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1861 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1862 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1863 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1864 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1865 | /* If the new item is a map or array, descend. |
| 1866 | * |
| 1867 | * Empty indefinite-length maps and arrays are descended into, |
| 1868 | * but then ascended out of in the next chunk of code. |
| 1869 | * |
| 1870 | * Maps and arrays do count as items in the map/array that |
| 1871 | * encloses them so a decrement needs to be done for them too, |
| 1872 | * but that is done only when all the items in them have been |
| 1873 | * processed, not when they are opened with the exception of an |
| 1874 | * empty map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1875 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1876 | QCBORError uDescendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1877 | uDescendErr = DecodeNesting_DescendMapOrArray(&(pMe->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1878 | pDecodedItem->uDataType, |
| 1879 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1880 | if(uDescendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1881 | /* This error is probably a traversal error and it overrides |
| 1882 | * the non-traversal error. |
| 1883 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1884 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1885 | goto Done; |
| 1886 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1889 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1890 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1891 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1892 | /* The following cases are handled here: |
| 1893 | * - A non-aggregate item like an integer or string |
| 1894 | * - An empty definite-length map or array |
| 1895 | * - An indefinite-length map or array that might be empty or might not. |
| 1896 | * |
| 1897 | * QCBORDecode_NestLevelAscender() does the work of decrementing the count |
| 1898 | * for an definite-length map/array and break detection for an |
| 1899 | * indefinite-0length map/array. If the end of the map/array was |
| 1900 | * reached, then it ascends nesting levels, possibly all the way |
| 1901 | * to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1902 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1903 | QCBORError uAscendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1904 | uAscendErr = QCBORDecode_NestLevelAscender(pMe, true); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1905 | if(uAscendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1906 | /* This error is probably a traversal error and it overrides |
| 1907 | * the non-traversal error. |
| 1908 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1909 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1910 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1911 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1912 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1913 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1914 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1915 | /* Tell the caller what level is next. This tells them what |
| 1916 | * maps/arrays were closed out and makes it possible for them to |
| 1917 | * reconstruct the tree with just the information returned in a |
| 1918 | * QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1919 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1920 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1921 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1922 | pDecodedItem->uNextNestLevel = 0; |
| 1923 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1924 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1925 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1926 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1927 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1928 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1931 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1932 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1933 | /** |
| 1934 | * @brief Shift 0th tag out of the tag list. |
| 1935 | * |
| 1936 | * pDecodedItem[in,out] The data item to convert. |
| 1937 | * |
| 1938 | * The 0th tag is discarded. \ref CBOR_TAG_INVALID16 is |
| 1939 | * shifted into empty slot at the end of the tag list. |
| 1940 | */ |
| 1941 | static inline void ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1942 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1943 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 1944 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 1945 | } |
| 1946 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1949 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1950 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1951 | /** |
| 1952 | * @brief Convert different epoch date formats in to the QCBOR epoch date format |
| 1953 | * |
| 1954 | * pDecodedItem[in,out] The data item to convert. |
| 1955 | * |
| 1956 | * @retval QCBOR_ERR_DATE_OVERFLOW |
| 1957 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1958 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 1959 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1960 | * |
| 1961 | * The epoch date tag defined in QCBOR allows for floating-point |
| 1962 | * dates. It even allows a protocol to flop between date formats when |
| 1963 | * ever it wants. Floating-point dates aren't that useful as they are |
| 1964 | * only needed for dates beyond the age of the earth. |
| 1965 | * |
| 1966 | * This converts all the date formats into one format of an unsigned |
| 1967 | * integer plus a floating-point fraction. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1968 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1969 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1970 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1971 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1972 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1973 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1974 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1975 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1976 | |
| 1977 | switch (pDecodedItem->uDataType) { |
| 1978 | |
| 1979 | case QCBOR_TYPE_INT64: |
| 1980 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1981 | break; |
| 1982 | |
| 1983 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1984 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 1985 | * always an overflow. |
| 1986 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1987 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1988 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1989 | break; |
| 1990 | |
| 1991 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1992 | case QCBOR_TYPE_FLOAT: |
| 1993 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1994 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1995 | /* Convert working value to double if input was a float */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1996 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1997 | pDecodedItem->val.dfnum : |
| 1998 | (double)pDecodedItem->val.fnum; |
| 1999 | |
| 2000 | /* The conversion from float to integer requires overflow |
| 2001 | * detection since floats can be much larger than integers. |
| 2002 | * This implementation errors out on these large float values |
| 2003 | * since they are beyond the age of the earth. |
| 2004 | * |
| 2005 | * These constants for the overflow check are computed by the |
| 2006 | * compiler. They are not computed at run time. |
| 2007 | * |
| 2008 | * The factor of 0x7ff is added/subtracted to avoid a |
| 2009 | * rounding error in the wrong direction when the compiler |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2010 | * computes these constants. There is rounding because a |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2011 | * 64-bit integer has 63 bits of precision where a double |
| 2012 | * only has 53 bits. Without the 0x7ff factor, the compiler |
| 2013 | * may round up and produce a double for the bounds check |
| 2014 | * that is larger than can be stored in a 64-bit integer. The |
| 2015 | * amount of 0x7ff is picked because it has 11 bits set. |
| 2016 | * |
| 2017 | * Without the 0x7ff there is a ~30 minute range of time |
| 2018 | * values 10 billion years in the past and in the future |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2019 | * where this code could go wrong. Some compilers |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2020 | * generate a warning or error without the 0x7ff. |
| 2021 | */ |
| 2022 | const double dDateMax = (double)(INT64_MAX - 0x7ff); |
| 2023 | const double dDateMin = (double)(INT64_MIN + 0x7ff); |
| 2024 | |
| 2025 | if(isnan(d) || d > dDateMax || d < dDateMin) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2026 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2027 | goto Done; |
| 2028 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2029 | |
| 2030 | /* The actual conversion */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2031 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 2032 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 2033 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2034 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2035 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2036 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2037 | uReturn = QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2038 | goto Done; |
| 2039 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2040 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2041 | break; |
| 2042 | |
| 2043 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2044 | /* It's the arrays and maps that are unrecoverable because |
| 2045 | * they are not consumed here. Since this is just an error |
| 2046 | * condition, no extra code is added here to make the error |
| 2047 | * recoverable for non-arrays and maps like strings. */ |
| 2048 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2049 | goto Done; |
| 2050 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2051 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2052 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 2053 | |
| 2054 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2055 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2059 | /** |
| 2060 | * @brief Convert the days epoch date. |
| 2061 | * |
| 2062 | * pDecodedItem[in,out] The data item to convert. |
| 2063 | * |
| 2064 | * @retval QCBOR_ERR_DATE_OVERFLOW |
| 2065 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2066 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2067 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2068 | * |
| 2069 | * This is much simpler than the other epoch date format because |
| 2070 | * floating-porint is not allowed. This is mostly a simple type check. |
| 2071 | */ |
| 2072 | static QCBORError DecodeDaysEpoch(QCBORItem *pDecodedItem) |
| 2073 | { |
| 2074 | QCBORError uReturn = QCBOR_SUCCESS; |
| 2075 | |
| 2076 | switch (pDecodedItem->uDataType) { |
| 2077 | |
| 2078 | case QCBOR_TYPE_INT64: |
| 2079 | pDecodedItem->val.epochDays = pDecodedItem->val.int64; |
| 2080 | break; |
| 2081 | |
| 2082 | case QCBOR_TYPE_UINT64: |
| 2083 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2084 | * always an overflow. |
| 2085 | */ |
| 2086 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2087 | goto Done; |
| 2088 | break; |
| 2089 | |
| 2090 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2091 | /* It's the arrays and maps that are unrecoverable because |
| 2092 | * they are not consumed here. Since this is just an error |
| 2093 | * condition, no extra code is added here to make the error |
| 2094 | * recoverable for non-arrays and maps like strings. */ |
| 2095 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2096 | goto Done; |
| 2097 | break; |
| 2098 | } |
| 2099 | |
| 2100 | pDecodedItem->uDataType = QCBOR_TYPE_DAYS_EPOCH; |
| 2101 | |
| 2102 | Done: |
| 2103 | return uReturn; |
| 2104 | } |
| 2105 | |
| 2106 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2107 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2108 | |
| 2109 | /* Forward declaration is necessary for |
| 2110 | * QCBORDecode_MantissaAndExponent(). to be able to decode bignum |
| 2111 | * tags in the mantissa. If the mantissa is a decimal fraction or big |
| 2112 | * float in error, this will result in a recurive call to |
| 2113 | * QCBORDecode_MantissaAndExponent(), but the recursion will unwined |
| 2114 | * correctly and the correct error is returned. |
| 2115 | */ |
| 2116 | static QCBORError |
| 2117 | QCBORDecode_GetNextTagContent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem); |
| 2118 | |
| 2119 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2120 | /** |
| 2121 | * @brief Decode decimal fractions and big floats. |
| 2122 | * |
| 2123 | * @param[in] pMe The decode context. |
| 2124 | * @param[in,out] pDecodedItem On input the array data item that |
| 2125 | * holds the mantissa and exponent. On |
| 2126 | * output the decoded mantissa and |
| 2127 | * exponent. |
| 2128 | * |
| 2129 | * @returns Decoding errors from getting primitive data items or |
| 2130 | * \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA. |
| 2131 | * |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2132 | * When called pDecodedItem must be the array with two members, the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2133 | * exponent and mantissa. |
| 2134 | * |
| 2135 | * This will fetch and decode the exponent and mantissa and put the |
| 2136 | * result back into pDecodedItem. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2137 | * |
| 2138 | * This does no checking or processing of tag numbers. That is to be |
| 2139 | * done by the code that calls this. |
| 2140 | * |
| 2141 | * This stuffs the type of the mantissa into pDecodedItem with the expectation |
| 2142 | * the caller will process it. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2143 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2144 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2145 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2146 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2147 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2148 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2149 | /* --- Make sure it is an array; track nesting level of members --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2150 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2151 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2152 | goto Done; |
| 2153 | } |
| 2154 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2155 | /* A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2156 | * definite-length arrays, but not for indefinite. Instead remember |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2157 | * the nesting level the two integers must be at, which is one |
| 2158 | * deeper than that of the array. |
| 2159 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2160 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 2161 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2162 | /* --- Get the exponent --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2163 | QCBORItem exponentItem; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2164 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, &exponentItem); |
| 2165 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2166 | goto Done; |
| 2167 | } |
| 2168 | if(exponentItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2169 | /* Array is empty or a map/array encountered when expecting an int */ |
| 2170 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2171 | goto Done; |
| 2172 | } |
| 2173 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2174 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2175 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2176 | * also means that the only data arriving here of type |
| 2177 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2178 | * and thus an error that will get handled in the next else. |
| 2179 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2180 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2181 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2182 | /* Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2183 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2184 | goto Done; |
| 2185 | } |
| 2186 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2187 | /* --- Get the mantissa --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2188 | QCBORItem mantissaItem; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2189 | uReturn = QCBORDecode_GetNextTagContent(pMe, &mantissaItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2190 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2191 | goto Done; |
| 2192 | } |
| 2193 | if(mantissaItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2194 | /* Mantissa missing or map/array encountered when expecting number */ |
| 2195 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2196 | goto Done; |
| 2197 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2198 | /* Stuff the mantissa data type into the item to send it up to the |
| 2199 | * the next level. */ |
| 2200 | pDecodedItem->uDataType = mantissaItem.uDataType; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2201 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2202 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2203 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2204 | * also means that the only data arriving here of type |
| 2205 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2206 | * and thus an error that will get handled in an else below. |
| 2207 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2208 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2209 | #ifndef QCBOR_DISABLE_TAGS |
| 2210 | /* With tags fully disabled a big number mantissa will error out |
| 2211 | * in the call to QCBORDecode_GetNextWithTags() because it has |
| 2212 | * a tag number. |
| 2213 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2214 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2215 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2216 | /* Got a good big num mantissa */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2217 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2218 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2219 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2220 | /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2221 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2222 | goto Done; |
| 2223 | } |
| 2224 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2225 | /* --- Check that array only has the two numbers --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2226 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2227 | /* Extra items in the decimal fraction / big float */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2228 | /* Improvement: this should probably be an unrecoverable error. */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2229 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2230 | goto Done; |
| 2231 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2232 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2233 | |
| 2234 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2235 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2236 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2237 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2238 | |
| 2239 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2240 | #ifndef QCBOR_DISABLE_TAGS |
| 2241 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2242 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2243 | /** |
| 2244 | * @brief Decode the MIME type tag |
| 2245 | * |
| 2246 | * @param[in,out] pDecodedItem The item to decode. |
| 2247 | * |
| 2248 | * Handle the text and binary MIME type tags. Slightly too complicated |
| 2249 | * f or ProcessTaggedString() because the RFC 7049 MIME type was |
| 2250 | * incorreclty text-only. |
| 2251 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2252 | static inline QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2253 | { |
| 2254 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2255 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2256 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2257 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2258 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2259 | /* It's the arrays and maps that are unrecoverable because |
| 2260 | * they are not consumed here. Since this is just an error |
| 2261 | * condition, no extra code is added here to make the error |
| 2262 | * recoverable for non-arrays and maps like strings. */ |
| 2263 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2264 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2265 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2266 | return QCBOR_SUCCESS; |
| 2267 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2268 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2269 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2270 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2271 | * Table of CBOR tags whose content is either a text string or a byte |
| 2272 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2273 | * of uQCBORtype indicates the content should be a byte string rather |
| 2274 | * than a text string |
| 2275 | */ |
| 2276 | struct StringTagMapEntry { |
| 2277 | uint16_t uTagNumber; |
| 2278 | uint8_t uQCBORtype; |
| 2279 | }; |
| 2280 | |
| 2281 | #define IS_BYTE_STRING_BIT 0x80 |
| 2282 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2283 | |
| 2284 | static const struct StringTagMapEntry StringTagMap[] = { |
| 2285 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2286 | {CBOR_TAG_DAYS_STRING, QCBOR_TYPE_DAYS_STRING}, |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2287 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2288 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2289 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2290 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2291 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2292 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2293 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2294 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2295 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2296 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2297 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2298 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2299 | }; |
| 2300 | |
| 2301 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2302 | /** |
| 2303 | * @brief Process standard CBOR tags whose content is a string |
| 2304 | * |
| 2305 | * @param[in] uTag The tag. |
| 2306 | * @param[in,out] pDecodedItem The data item. |
| 2307 | * |
| 2308 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2309 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2310 | * \ref QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT if the content type was wrong for the tag. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2311 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2312 | * Process the CBOR tags that whose content is a byte string or a text |
| 2313 | * string and for which the string is just passed on to the caller. |
| 2314 | * |
| 2315 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2316 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2317 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2318 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2319 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2320 | static inline QCBORError |
| 2321 | ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2322 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2323 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2324 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2325 | return QCBOR_ERR_UNSUPPORTED; |
| 2326 | } |
| 2327 | |
| 2328 | unsigned uIndex; |
| 2329 | for(uIndex = 0; StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2330 | if(StringTagMap[uIndex].uTagNumber == uTag) { |
| 2331 | break; |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | const uint8_t uQCBORType = StringTagMap[uIndex].uQCBORtype; |
| 2336 | if(uQCBORType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2337 | /* repurpose this error to mean not handled here */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2338 | return QCBOR_ERR_UNSUPPORTED; |
| 2339 | } |
| 2340 | |
| 2341 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2342 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2343 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2344 | } |
| 2345 | |
| 2346 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2347 | /* It's the arrays and maps that are unrecoverable because |
| 2348 | * they are not consumed here. Since this is just an error |
| 2349 | * condition, no extra code is added here to make the error |
| 2350 | * recoverable for non-arrays and maps like strings. */ |
| 2351 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2352 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2353 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2354 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2355 | return QCBOR_SUCCESS; |
| 2356 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2357 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2358 | |
| 2359 | |
| 2360 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 2361 | /* |
| 2362 | * This returns the QCBOR_TYPE for a mantissa and exponent. |
| 2363 | |
| 2364 | Called in one context where there is always a tag |
| 2365 | |
| 2366 | Called in another context where there might be a tag or the caller might say what they are expecting. |
| 2367 | |
| 2368 | 6 possible outputs |
| 2369 | */ |
| 2370 | static inline uint8_t |
| 2371 | MantissaExponentDataType(const uint16_t uTagToProcess, const QCBORItem *pDecodedItem) |
| 2372 | { |
| 2373 | uint8_t uBase = uTagToProcess == CBOR_TAG_DECIMAL_FRACTION ? |
| 2374 | QCBOR_TYPE_DECIMAL_FRACTION : |
| 2375 | QCBOR_TYPE_BIGFLOAT; |
| 2376 | if(pDecodedItem->uDataType != QCBOR_TYPE_INT64) { |
| 2377 | uBase = (uint8_t)(uBase + pDecodedItem->uDataType - QCBOR_TYPE_POSBIGNUM + 1); |
| 2378 | } |
| 2379 | return uBase; |
| 2380 | } |
| 2381 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2382 | |
| 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 |
| 2394 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 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 | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2398 | QCBORDecode_GetNextTagContent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2399 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2400 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2401 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2402 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2403 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2404 | goto Done; |
| 2405 | } |
| 2406 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2407 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2408 | /* When there are no tag numbers for the item, this exits first |
| 2409 | * thing and effectively does nothing. |
| 2410 | * |
| 2411 | * This loops over all the tag numbers accumulated for this item |
| 2412 | * trying to decode and interpret them. This stops at the end of |
| 2413 | * the list or at the first tag number that can't be interpreted by |
| 2414 | * this code. This is effectively a recursive processing of the |
| 2415 | * tags number list that handles nested tags. |
| 2416 | */ |
| 2417 | while(1) { |
| 2418 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2419 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2420 | */ |
| 2421 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2422 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2423 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2424 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2425 | break; |
| 2426 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2427 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2428 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2429 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2430 | } else if(uTagToProcess == CBOR_TAG_DAYS_EPOCH) { |
| 2431 | uReturn = DecodeDaysEpoch(pDecodedItem); |
| 2432 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2433 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2434 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2435 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2436 | uReturn = QCBORDecode_MantissaAndExponent(pMe, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2437 | /* --- Which is it, decimal fraction or a bigfloat? --- */ |
| 2438 | pDecodedItem->uDataType = MantissaExponentDataType(uTagToProcess, pDecodedItem); |
| 2439 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2440 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2441 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2442 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2443 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2444 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2445 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2446 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2447 | } else { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2448 | /* See if it is a passthrough byte/text string tag; process if so */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2449 | uReturn = ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2450 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2451 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2452 | /* It wasn't a passthrough byte/text string tag so it is |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2453 | * an unknown tag. This is the exit from the loop on the |
| 2454 | * first unknown tag. It is a successful exit. |
| 2455 | */ |
| 2456 | uReturn = QCBOR_SUCCESS; |
| 2457 | break; |
| 2458 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2459 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2460 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2461 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2462 | /* Error exit from the loop */ |
| 2463 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2464 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2465 | |
| 2466 | /* A tag was successfully processed, shift it out of the list of |
| 2467 | * tags returned. This is the loop increment. |
| 2468 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2469 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2470 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2471 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2472 | |
| 2473 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2474 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2478 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2479 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2480 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2481 | QCBORError |
| 2482 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2483 | { |
| 2484 | QCBORError uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2485 | uErr = QCBORDecode_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2486 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2487 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2488 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2489 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2490 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | |
| 2494 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2495 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2496 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2497 | QCBORError |
| 2498 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2499 | { |
| 2500 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2501 | const UsefulInputBuf Save = pMe->InBuf; |
| 2502 | |
| 2503 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2504 | |
| 2505 | pMe->nesting = SaveNesting; |
| 2506 | pMe->InBuf = Save; |
| 2507 | |
| 2508 | return uErr; |
| 2509 | } |
| 2510 | |
| 2511 | |
| 2512 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2513 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2514 | */ |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2515 | void |
| 2516 | QCBORDecode_VPeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2517 | { |
| 2518 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2519 | return; |
| 2520 | } |
| 2521 | |
| 2522 | pMe->uLastError = (uint8_t)QCBORDecode_PeekNext(pMe, pDecodedItem); |
| 2523 | } |
| 2524 | |
| 2525 | |
| 2526 | /* |
| 2527 | * Public function, see header qcbor/qcbor_decode.h file |
| 2528 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2529 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2530 | { |
| 2531 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2532 | return; |
| 2533 | } |
| 2534 | |
| 2535 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2536 | } |
| 2537 | |
| 2538 | |
| 2539 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2540 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2541 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2542 | QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2543 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2544 | QCBORItem *pDecodedItem, |
| 2545 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2546 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2547 | #ifndef QCBOR_DISABLE_TAGS |
| 2548 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2549 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2550 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2551 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2552 | if(uReturn != QCBOR_SUCCESS) { |
| 2553 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | if(pTags != NULL) { |
| 2557 | pTags->uNumUsed = 0; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2558 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2559 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2560 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2561 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2562 | } |
| 2563 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2564 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2565 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2566 | pTags->puTags[pTags->uNumUsed] = UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2567 | pTags->uNumUsed++; |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | return QCBOR_SUCCESS; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2572 | |
| 2573 | #else /* QCBOR_DISABLE_TAGS */ |
| 2574 | (void)pMe; |
| 2575 | (void)pDecodedItem; |
| 2576 | (void)pTags; |
| 2577 | return QCBOR_ERR_TAGS_DISABLED; |
| 2578 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2582 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2583 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2584 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2585 | bool QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2586 | const QCBORItem *pItem, |
| 2587 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2588 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2589 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2590 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2591 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2592 | break; |
| 2593 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2594 | if(UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2595 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2596 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2597 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2598 | #else /* QCBOR_TAGS_DISABLED */ |
| 2599 | (void)pMe; |
| 2600 | (void)pItem; |
| 2601 | (void)uTag; |
| 2602 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2603 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2604 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | |
| 2608 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2609 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2610 | */ |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2611 | QCBORError QCBORDecode_PartialFinish(QCBORDecodeContext *pMe, size_t *puConsumed) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2612 | { |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2613 | if(puConsumed != NULL) { |
| 2614 | *puConsumed = pMe->InBuf.cursor; |
| 2615 | } |
| 2616 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2617 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2618 | |
| 2619 | if(uReturn != QCBOR_SUCCESS) { |
| 2620 | goto Done; |
| 2621 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2622 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2623 | /* Error out if all the maps/arrays are not closed out */ |
| 2624 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2625 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2626 | goto Done; |
| 2627 | } |
| 2628 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2629 | /* Error out if not all the bytes are consumed */ |
| 2630 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2631 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2632 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2633 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2634 | Done: |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2635 | return uReturn; |
| 2636 | } |
| 2637 | |
| 2638 | |
| 2639 | /* |
| 2640 | * Public function, see header qcbor/qcbor_decode.h file |
| 2641 | */ |
| 2642 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *pMe) |
| 2643 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2644 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2645 | /* Call the destructor for the string allocator if there is one. |
| 2646 | * Always called, even if there are errors; always have to clean up. |
| 2647 | */ |
| 2648 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2649 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2650 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2651 | return QCBORDecode_PartialFinish(pMe, NULL); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2655 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2656 | * Public function, see header qcbor/qcbor_decode.h file |
| 2657 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2658 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2659 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2660 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2661 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2662 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2663 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2664 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2665 | return CBOR_TAG_INVALID64; |
| 2666 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2667 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2668 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2669 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2670 | return UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2671 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2672 | #else /* QCBOR_DISABLE_TAGS */ |
| 2673 | (void)pMe; |
| 2674 | (void)pItem; |
| 2675 | (void)uIndex; |
| 2676 | |
| 2677 | return CBOR_TAG_INVALID64; |
| 2678 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2679 | } |
| 2680 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2681 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2682 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2683 | * Public function, see header qcbor/qcbor_decode.h file |
| 2684 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2685 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2686 | uint32_t uIndex) |
| 2687 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2688 | #ifndef QCBOR_DISABLE_TAGS |
| 2689 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2690 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2691 | return CBOR_TAG_INVALID64; |
| 2692 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2693 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2694 | return CBOR_TAG_INVALID64; |
| 2695 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2696 | return UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2697 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2698 | #else /* QCBOR_DISABLE_TAGS */ |
| 2699 | (void)pMe; |
| 2700 | (void)uIndex; |
| 2701 | |
| 2702 | return CBOR_TAG_INVALID64; |
| 2703 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2704 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2705 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2706 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2707 | |
| 2708 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2709 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2710 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2711 | /* =========================================================================== |
| 2712 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2713 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2714 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2715 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2716 | implements the function type QCBORStringAllocate and allows easy |
| 2717 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2718 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2719 | This particular allocator is built-in for convenience. The caller |
| 2720 | can implement their own. All of this following code will get |
| 2721 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2722 | |
| 2723 | This is a very primitive memory allocator. It does not track |
| 2724 | individual allocations, only a high-water mark. A free or |
| 2725 | reallocation must be of the last chunk allocated. |
| 2726 | |
| 2727 | The size of the pool and offset to free memory are packed into the |
| 2728 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2729 | the decode context. Since the address of the pool may not be |
| 2730 | aligned, they have to be packed and unpacked as if they were |
| 2731 | serialized data of the wire or such. |
| 2732 | |
| 2733 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2734 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2735 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2736 | |
| 2737 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2738 | static inline int |
| 2739 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2740 | { |
| 2741 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2742 | UsefulInputBuf UIB; |
| 2743 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2744 | // Just assume the size here. It was checked during SetUp so |
| 2745 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2746 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2747 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2748 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2749 | return UsefulInputBuf_GetError(&UIB); |
| 2750 | } |
| 2751 | |
| 2752 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2753 | static inline int |
| 2754 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2755 | { |
| 2756 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2757 | // length check performed here is useful. |
| 2758 | UsefulOutBuf UOB; |
| 2759 | |
| 2760 | UsefulOutBuf_Init(&UOB, Pool); |
| 2761 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2762 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2763 | return UsefulOutBuf_GetError(&UOB); |
| 2764 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2765 | |
| 2766 | |
| 2767 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2768 | Internal function for an allocation, reallocation free and destuct. |
| 2769 | |
| 2770 | Having only one function rather than one each per mode saves space in |
| 2771 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2772 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2773 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2774 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2775 | static UsefulBuf |
| 2776 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2777 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2778 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2779 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2780 | uint32_t uPoolSize; |
| 2781 | uint32_t uFreeOffset; |
| 2782 | |
| 2783 | if(uNewSize > UINT32_MAX) { |
| 2784 | // This allocator is only good up to 4GB. This check should |
| 2785 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2786 | goto Done; |
| 2787 | } |
| 2788 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2789 | |
| 2790 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2791 | goto Done; |
| 2792 | } |
| 2793 | |
| 2794 | if(uNewSize) { |
| 2795 | if(pMem) { |
| 2796 | // REALLOCATION MODE |
| 2797 | // Calculate pointer to the end of the memory pool. It is |
| 2798 | // assumed that pPool + uPoolSize won't wrap around by |
| 2799 | // assuming the caller won't pass a pool buffer in that is |
| 2800 | // not in legitimate memory space. |
| 2801 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2802 | |
| 2803 | // Check that the pointer for reallocation is in the range of the |
| 2804 | // pool. This also makes sure that pointer math further down |
| 2805 | // doesn't wrap under or over. |
| 2806 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2807 | // Offset to start of chunk for reallocation. This won't |
| 2808 | // wrap under because of check that pMem >= pPool. Cast |
| 2809 | // is safe because the pool is always less than UINT32_MAX |
| 2810 | // because of check in QCBORDecode_SetMemPool(). |
| 2811 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2812 | |
| 2813 | // Check to see if the allocation will fit. uPoolSize - |
| 2814 | // uMemOffset will not wrap under because of check that |
| 2815 | // pMem is in the range of the uPoolSize by check above. |
| 2816 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2817 | ReturnValue.ptr = pMem; |
| 2818 | ReturnValue.len = uNewSize; |
| 2819 | |
| 2820 | // Addition won't wrap around over because uNewSize was |
| 2821 | // checked to be sure it is less than the pool size. |
| 2822 | uFreeOffset = uMemOffset + uNewSize32; |
| 2823 | } |
| 2824 | } |
| 2825 | } else { |
| 2826 | // ALLOCATION MODE |
| 2827 | // uPoolSize - uFreeOffset will not underflow because this |
| 2828 | // pool implementation makes sure uFreeOffset is always |
| 2829 | // smaller than uPoolSize through this check here and |
| 2830 | // reallocation case. |
| 2831 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2832 | ReturnValue.len = uNewSize; |
| 2833 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2834 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2835 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2836 | } |
| 2837 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2838 | if(pMem) { |
| 2839 | // FREE MODE |
| 2840 | // Cast is safe because of limit on pool size in |
| 2841 | // QCBORDecode_SetMemPool() |
| 2842 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2843 | } else { |
| 2844 | // DESTRUCT MODE |
| 2845 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2846 | } |
| 2847 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2848 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2849 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2850 | MemPool_Pack(Pool, uFreeOffset); |
| 2851 | |
| 2852 | Done: |
| 2853 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2854 | } |
| 2855 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2856 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2857 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2858 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2859 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2860 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2861 | UsefulBuf Pool, |
| 2862 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2863 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2864 | // The pool size and free mem offset are packed into the beginning |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 2865 | // of the pool memory. This compile time check makes sure the |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2866 | // constant in the header is correct. This check should optimize |
| 2867 | // down to nothing. |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 2868 | #ifdef _MSC_VER |
| 2869 | #pragma warning(push) |
| 2870 | #pragma warning(disable:4127) // conditional expression is constant |
| 2871 | #endif |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2872 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2873 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2874 | } |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 2875 | #ifdef _MSC_VER |
| 2876 | #pragma warning(pop) |
| 2877 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2878 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2879 | // The pool size and free offset packed in to the beginning of pool |
| 2880 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2881 | // machines. |
| 2882 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2883 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2884 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2885 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2886 | // This checks that the pool buffer given is big enough. |
| 2887 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2888 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2889 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2890 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2891 | QCBORDecode_SetUpAllocator(pMe, MemPool_Function, Pool.ptr, bAllStrings); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2892 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2893 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2894 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2895 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2896 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2897 | |
| 2898 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2899 | static inline void |
| 2900 | CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2901 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2902 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2903 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2904 | #else |
| 2905 | (void)pMe; |
| 2906 | (void)pItem; |
| 2907 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2908 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2909 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2910 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2911 | /** |
| 2912 | * @brief Consume an entire map or array including its contents. |
| 2913 | * |
| 2914 | * @param[in] pMe The decoder context. |
| 2915 | * @param[in] pItemToConsume The array/map whose contents are to be |
| 2916 | * consumed. |
| 2917 | * @param[out] puNextNestLevel The next nesting level after the item was |
| 2918 | * fully consumed. |
| 2919 | * |
| 2920 | * This may be called when @c pItemToConsume is not an array or |
| 2921 | * map. In that case, this is just a pass through for @c puNextNestLevel |
| 2922 | * since there is nothing to do. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2923 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2924 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2925 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2926 | const QCBORItem *pItemToConsume, |
Máté Tóth-Pál | 2f2aa5f | 2021-05-17 21:35:09 +0200 | [diff] [blame] | 2927 | uint8_t *puNextNestLevel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2928 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2929 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2930 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2931 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2932 | /* If it is a map or array, this will tell if it is empty. */ |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2933 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2934 | |
| 2935 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2936 | /* There is only real work to do for non-empty maps and arrays */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2937 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2938 | /* This works for definite- and indefinite-length maps and |
| 2939 | * arrays by using the nesting level |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2940 | */ |
| 2941 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2942 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2943 | if(QCBORDecode_IsUnrecoverableError(uReturn) || |
| 2944 | uReturn == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2945 | goto Done; |
| 2946 | } |
| 2947 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2948 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2949 | *puNextNestLevel = Item.uNextNestLevel; |
| 2950 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2951 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2952 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2953 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2954 | /* pItemToConsume is not a map or array. Just pass the nesting |
| 2955 | * level through. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2956 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2957 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2958 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2959 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2960 | |
| 2961 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2962 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2963 | } |
| 2964 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 2965 | |
| 2966 | void QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2967 | { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 2968 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 2969 | |
| 2970 | if(pMe->uLastError == QCBOR_SUCCESS) { |
Máté Tóth-Pál | c6d5968 | 2021-05-26 18:55:30 +0200 | [diff] [blame] | 2971 | pMe->uLastError = (uint8_t)ConsumeItem(pMe, pDecodedItem, |
| 2972 | &pDecodedItem->uNextNestLevel); |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | |
| 2977 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 2978 | /* Call only on maps and arrays. Rewinds the cursor |
| 2979 | * to the start as if it was just entered. |
| 2980 | */ |
| 2981 | static void RewindMapOrArray(QCBORDecodeContext *pMe) |
| 2982 | { |
| 2983 | /* Reset nesting tracking to the deepest bounded level */ |
| 2984 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 2985 | |
| 2986 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 2987 | |
| 2988 | /* Reposition traversal cursor to the start of the map/array */ |
| 2989 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2990 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 2991 | } |
| 2992 | |
| 2993 | |
| 2994 | /* |
| 2995 | Public function, see header qcbor/qcbor_decode.h file |
| 2996 | */ |
| 2997 | void QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
| 2998 | { |
| 2999 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 3000 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 3001 | |
| 3002 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 3003 | /* In bstr-wrapped CBOR. */ |
| 3004 | |
| 3005 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 3006 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3007 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 3008 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3009 | |
| 3010 | } else { |
| 3011 | /* In a map or array */ |
| 3012 | RewindMapOrArray(pMe); |
| 3013 | } |
| 3014 | |
| 3015 | } else { |
| 3016 | /* Not in anything bounded */ |
| 3017 | |
| 3018 | /* Reposition traversal cursor to the start of input CBOR */ |
| 3019 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 3020 | |
| 3021 | /* Reset nesting tracking to beginning of input. */ |
| 3022 | DecodeNesting_Init(&(pMe->nesting)); |
| 3023 | } |
| 3024 | |
| 3025 | pMe->uLastError = QCBOR_SUCCESS; |
| 3026 | } |
| 3027 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3028 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3029 | /* Return true if the labels in Item1 and Item2 are the same. |
| 3030 | Works only for integer and string labels. Returns false |
| 3031 | for any other type. */ |
| 3032 | static inline bool |
| 3033 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 3034 | { |
| 3035 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 3036 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 3037 | return true; |
| 3038 | } |
| 3039 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3040 | if(Item2.uLabelType == QCBOR_TYPE_TEXT_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3041 | return true; |
| 3042 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3043 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 3044 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 3045 | return true; |
| 3046 | } |
| 3047 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 3048 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 3049 | return true; |
| 3050 | } |
| 3051 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3052 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3053 | /* Other label types are never matched */ |
| 3054 | return false; |
| 3055 | } |
| 3056 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3057 | |
| 3058 | /* |
| 3059 | Returns true if Item1 and Item2 are the same type |
| 3060 | or if either are of QCBOR_TYPE_ANY. |
| 3061 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3062 | static inline bool |
| 3063 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 3064 | { |
| 3065 | if(Item1.uDataType == Item2.uDataType) { |
| 3066 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3067 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3068 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3069 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3070 | return true; |
| 3071 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3072 | return false; |
| 3073 | } |
| 3074 | |
| 3075 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3076 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3077 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3078 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3079 | @param[in] pMe The decode context to search. |
| 3080 | @param[in,out] pItemArray The items to search for and the items found. |
| 3081 | @param[out] puOffset Byte offset of last item matched. |
| 3082 | @param[in] pCBContext Context for the not-found item call back. |
| 3083 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3084 | |
| 3085 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 3086 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3087 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 3088 | were found for one of the labels being |
| 3089 | search for. This duplicate detection is |
| 3090 | only performed for items in pItemArray, |
| 3091 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3092 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3093 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 3094 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3095 | |
| 3096 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 3097 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3098 | On input pItemArray contains a list of labels and data types |
| 3099 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3100 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3101 | On output the fully retrieved items are filled in with |
| 3102 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3103 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3104 | If an item was not found, its data type is set to QCBOR_TYPE_NONE. |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3105 | |
| 3106 | 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] | 3107 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3108 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3109 | MapSearch(QCBORDecodeContext *pMe, |
| 3110 | QCBORItem *pItemArray, |
| 3111 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3112 | void *pCBContext, |
| 3113 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3114 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3115 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3116 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 3117 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3118 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3119 | uReturn = pMe->uLastError; |
| 3120 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3121 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3122 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3123 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3124 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 3125 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 3126 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3127 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 3128 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3131 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 3132 | // It is an empty bounded array or map |
| 3133 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 3134 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3135 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3136 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3137 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3138 | // Nothing is ever found in an empty array or map. All items |
| 3139 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3140 | uReturn = QCBOR_SUCCESS; |
| 3141 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3142 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3143 | } |
| 3144 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3145 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3146 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3147 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3148 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3149 | RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3150 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3151 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3152 | Loop over all the items in the map or array. Each item |
| 3153 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3154 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3155 | length maps and arrays. The only reason this is ever |
| 3156 | called on arrays is to find their end position. |
| 3157 | |
| 3158 | This will always run over all items in order to do |
| 3159 | duplicate detection. |
| 3160 | |
| 3161 | This will exit with failure if it encounters an |
| 3162 | unrecoverable error, but continue on for recoverable |
| 3163 | errors. |
| 3164 | |
| 3165 | If a recoverable error occurs on a matched item, then |
| 3166 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3167 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 3168 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Máté Tóth-Pál | 2f2aa5f | 2021-05-17 21:35:09 +0200 | [diff] [blame] | 3169 | uint8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3170 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3171 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3172 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3173 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3174 | /* Get the item */ |
| 3175 | QCBORItem Item; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3176 | QCBORError uResult = QCBORDecode_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3177 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3178 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3179 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3180 | goto Done; |
| 3181 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3182 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3183 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3184 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3185 | goto Done; |
| 3186 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3187 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3188 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3189 | bool bMatched = false; |
| 3190 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 3191 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3192 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3193 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 3194 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3195 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3196 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3197 | if(uResult != QCBOR_SUCCESS) { |
| 3198 | /* The label matches, but the data item is in error */ |
| 3199 | uReturn = uResult; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3200 | goto Done; |
| 3201 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3202 | if(!MatchType(Item, pItemArray[nIndex])) { |
| 3203 | /* The data item is not of the type(s) requested */ |
| 3204 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3205 | goto Done; |
| 3206 | } |
| 3207 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3208 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3209 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3210 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3211 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3212 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3213 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3214 | bMatched = true; |
| 3215 | } |
| 3216 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3217 | |
| 3218 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3219 | if(!bMatched && pfCallback != NULL) { |
| 3220 | /* |
| 3221 | Call the callback on unmatched labels. |
| 3222 | (It is tempting to do duplicate detection here, but that would |
| 3223 | require dynamic memory allocation because the number of labels |
| 3224 | that might be encountered is unbounded.) |
| 3225 | */ |
| 3226 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 3227 | if(uReturn != QCBOR_SUCCESS) { |
| 3228 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3229 | } |
| 3230 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3231 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3232 | /* |
| 3233 | Consume the item whether matched or not. This |
| 3234 | does the work of traversing maps and array and |
| 3235 | everything in them. In this loop only the |
| 3236 | items at the current nesting level are examined |
| 3237 | to match the labels. |
| 3238 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3239 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3240 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3241 | goto Done; |
| 3242 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3243 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3244 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3245 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3246 | |
| 3247 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3248 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3249 | // Check here makes sure that this won't accidentally be |
| 3250 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3251 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3252 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3253 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3254 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3255 | goto Done; |
| 3256 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3257 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3258 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3259 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3260 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3261 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3262 | |
| 3263 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3264 | /* 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] | 3265 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3266 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3267 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3268 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3269 | } |
| 3270 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3271 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3272 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3273 | } |
| 3274 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3275 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3276 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3277 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3278 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3279 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3280 | int64_t nLabel, |
| 3281 | uint8_t uQcborType, |
| 3282 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3283 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3284 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3285 | return; |
| 3286 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3287 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3288 | QCBORItem OneItemSeach[2]; |
| 3289 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3290 | OneItemSeach[0].label.int64 = nLabel; |
| 3291 | OneItemSeach[0].uDataType = uQcborType; |
| 3292 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3293 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3294 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3295 | |
| 3296 | *pItem = OneItemSeach[0]; |
| 3297 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3298 | if(uReturn != QCBOR_SUCCESS) { |
| 3299 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3300 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3301 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3302 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3305 | Done: |
| 3306 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3310 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3311 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3312 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3313 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3314 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3315 | uint8_t uQcborType, |
| 3316 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3317 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3318 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3319 | return; |
| 3320 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3321 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3322 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3323 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3324 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3325 | OneItemSeach[0].uDataType = uQcborType; |
| 3326 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3327 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3328 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 3329 | if(uReturn != QCBOR_SUCCESS) { |
| 3330 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3331 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3332 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3333 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3334 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3335 | } |
| 3336 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3337 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3338 | |
| 3339 | Done: |
| 3340 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3344 | static QCBORError |
| 3345 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3346 | { |
| 3347 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
Laurence Lundblade | d3adb7c | 2023-02-08 12:43:11 -0700 | [diff] [blame] | 3348 | if(uDataType == puTypeList[i]) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3349 | return QCBOR_SUCCESS; |
| 3350 | } |
| 3351 | } |
| 3352 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3353 | } |
| 3354 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3355 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3356 | /** |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3357 | * Match a tag/type specification against the type of the item. |
| 3358 | * |
| 3359 | * @param[in] TagSpec Specification for matching tags. |
| 3360 | * @param[in] pItem The item to check. |
| 3361 | * |
| 3362 | * @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3363 | * @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec |
| 3364 | * |
| 3365 | * This checks the item data type of untagged items as well as of |
| 3366 | * tagged items against a specification to see if decoding should |
| 3367 | * proceed. |
| 3368 | * |
| 3369 | * This relies on the automatic tag decoding done by QCBOR that turns |
| 3370 | * tag numbers into particular QCBOR_TYPEs so there is no actual |
| 3371 | * comparsion of tag numbers, just of QCBOR_TYPEs. |
| 3372 | * |
| 3373 | * This checks the data item type as possibly representing the tag |
| 3374 | * number or as the tag content type. |
| 3375 | * |
| 3376 | * If QCBOR_DISABLE_TAGS is #defined, this primarily checks the item |
| 3377 | * data type against the allowed tag content types. It will also error out |
| 3378 | * if the caller tries to require a tag because there is no way that can |
| 3379 | * ever be fulfilled. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3380 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3381 | static QCBORError |
| 3382 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3383 | { |
Laurence Lundblade | d3adb7c | 2023-02-08 12:43:11 -0700 | [diff] [blame] | 3384 | const int nItemType = pItem->uDataType; /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3385 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3386 | |
| 3387 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | d3adb7c | 2023-02-08 12:43:11 -0700 | [diff] [blame] | 3388 | /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3389 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3390 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3391 | /* There are tags that QCBOR couldn't process on this item and |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3392 | * the caller has told us there should not be. |
| 3393 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3394 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3395 | } |
| 3396 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3397 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3398 | /* Must match the tag number and only the tag */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3399 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3400 | } |
| 3401 | |
| 3402 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3403 | if(uReturn == QCBOR_SUCCESS) { |
| 3404 | return QCBOR_SUCCESS; |
| 3405 | } |
| 3406 | |
| 3407 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3408 | /* Must match the content type and only the content type. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3409 | * There was no match just above so it is a fail. */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3410 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3411 | } |
| 3412 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3413 | /* QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG: If here it can match either the tag or the content |
| 3414 | * and it hasn't matched the content, so the end |
| 3415 | * result is whether it matches the tag. This is |
| 3416 | * the tag optional case that the CBOR standard discourages. |
| 3417 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3418 | |
| 3419 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3420 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3421 | #else /* QCBOR_DISABLE_TAGS */ |
| 3422 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3423 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3424 | } |
| 3425 | |
| 3426 | return CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3427 | |
| 3428 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3429 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3430 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3431 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3432 | // This could be semi-private if need be |
| 3433 | static inline |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3434 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3435 | const int64_t nLabel, |
| 3436 | const TagSpecification TagSpec, |
| 3437 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3438 | { |
| 3439 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3440 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3441 | return; |
| 3442 | } |
| 3443 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3444 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3445 | } |
| 3446 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3447 | |
| 3448 | // This could be semi-private if need be |
| 3449 | static inline |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3450 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3451 | const char *szLabel, |
| 3452 | const TagSpecification TagSpec, |
| 3453 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3454 | { |
| 3455 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3456 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3457 | return; |
| 3458 | } |
| 3459 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3460 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3461 | } |
| 3462 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3463 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3464 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3465 | int64_t nLabel, |
| 3466 | TagSpecification TagSpec, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3467 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3468 | { |
| 3469 | QCBORItem Item; |
| 3470 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3471 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3472 | *pString = Item.val.string; |
| 3473 | } |
| 3474 | } |
| 3475 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3476 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3477 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3478 | const char * szLabel, |
| 3479 | TagSpecification TagSpec, |
| 3480 | UsefulBufC *pString) |
| 3481 | { |
| 3482 | QCBORItem Item; |
| 3483 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3484 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3485 | *pString = Item.val.string; |
| 3486 | } |
| 3487 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3488 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3489 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3490 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3491 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3492 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3493 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3494 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 3495 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3498 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3499 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3500 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3501 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3502 | QCBORItem *pItemList, |
| 3503 | void *pCallbackCtx, |
| 3504 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3505 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3506 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 3507 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3511 | /** |
| 3512 | * @brief Search for a map/array by label and enter it |
| 3513 | * |
| 3514 | * @param[in] pMe The decode context. |
| 3515 | * @param[in] pSearch The map/array to search for. |
| 3516 | * |
| 3517 | * @c pSearch is expected to contain one item of type map or array |
| 3518 | * with the label specified. The current bounded map will be searched for |
| 3519 | * this and if found will be entered. |
| 3520 | * |
| 3521 | * If the label is not found, or the item found is not a map or array, |
| 3522 | * the error state is set. |
| 3523 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3524 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3525 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3526 | // The first item in pSearch is the one that is to be |
| 3527 | // entered. It should be the only one filled in. Any other |
| 3528 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3529 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3530 | return; |
| 3531 | } |
| 3532 | |
| 3533 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3534 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3535 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3536 | return; |
| 3537 | } |
| 3538 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3539 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3540 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3541 | return; |
| 3542 | } |
| 3543 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3544 | |
| 3545 | /* The map or array was found. Now enter it. |
| 3546 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3547 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3548 | * next item for the pre-order traversal cursor to be the map/array |
| 3549 | * found by MapSearch(). The next few lines of code force the |
| 3550 | * cursor to that. |
| 3551 | * |
| 3552 | * There is no need to retain the old cursor because |
| 3553 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3554 | * beginning of the map/array being entered. |
| 3555 | * |
| 3556 | * The cursor is forced by: 1) setting the input buffer position to |
| 3557 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3558 | * counter to the total in the map/array, 3) setting the nesting |
| 3559 | * level. Setting the map/array counter to the total is not |
| 3560 | * strictly correct, but this is OK because this cursor only needs |
| 3561 | * to be used to get one item and MapSearch() has already found it |
| 3562 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3563 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3564 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3565 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3566 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3567 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3568 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3569 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3570 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3574 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3575 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3576 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3577 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3578 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3579 | QCBORItem OneItemSeach[2]; |
| 3580 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3581 | OneItemSeach[0].label.int64 = nLabel; |
| 3582 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3583 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3584 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3585 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3586 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3590 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3591 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3592 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3593 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3594 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3595 | QCBORItem OneItemSeach[2]; |
| 3596 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3597 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3598 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3599 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3600 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3601 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3602 | } |
| 3603 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3604 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3605 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3606 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3607 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3608 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3609 | QCBORItem OneItemSeach[2]; |
| 3610 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3611 | OneItemSeach[0].label.int64 = nLabel; |
| 3612 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3613 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3614 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3615 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3616 | } |
| 3617 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3618 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3619 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3620 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3621 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3622 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3623 | QCBORItem OneItemSeach[2]; |
| 3624 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3625 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3626 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3627 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3628 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3629 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3633 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3634 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3635 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3636 | QCBORError uErr; |
| 3637 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3638 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3639 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3640 | // Already in error state; do nothing. |
| 3641 | return; |
| 3642 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3643 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3644 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3645 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3646 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3647 | if(uErr != QCBOR_SUCCESS) { |
| 3648 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3649 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3650 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3651 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3652 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3653 | } |
| 3654 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3655 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3656 | |
| 3657 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3658 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3659 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3660 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3661 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3662 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3663 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3664 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3665 | // Special case to increment nesting level for zero-length maps |
| 3666 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3667 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3668 | } |
| 3669 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3670 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3671 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3672 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3673 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3674 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3675 | if(pItem != NULL) { |
| 3676 | *pItem = Item; |
| 3677 | } |
| 3678 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3679 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3680 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3681 | } |
| 3682 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3683 | |
| 3684 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3685 | This is the common work for exiting a level that is a bounded map, |
| 3686 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3687 | |
| 3688 | One chunk of work is to set up the pre-order traversal so it is at |
| 3689 | the item just after the bounded map, array or bstr that is being |
| 3690 | exited. This is somewhat complex. |
| 3691 | |
| 3692 | The other work is to level-up the bounded mode to next higest bounded |
| 3693 | mode or the top level if there isn't one. |
| 3694 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3695 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3696 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3697 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3698 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3699 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3700 | /* |
| 3701 | First the pre-order-traversal byte offset is positioned to the |
| 3702 | item just after the bounded mode item that was just consumed. |
| 3703 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3704 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3705 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3706 | /* |
| 3707 | Next, set the current nesting level to one above the bounded level |
| 3708 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3709 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3710 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3711 | makes sure pCurrentBounded is valid. |
| 3712 | */ |
| 3713 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3714 | |
| 3715 | /* |
| 3716 | This does the complex work of leveling up the pre-order traversal |
| 3717 | when the end of a map or array or another bounded level is |
| 3718 | reached. It may do nothing, or ascend all the way to the top |
| 3719 | level. |
| 3720 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3721 | uErr = QCBORDecode_NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3722 | if(uErr != QCBOR_SUCCESS) { |
| 3723 | goto Done; |
| 3724 | } |
| 3725 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3726 | /* |
| 3727 | This makes the next highest bounded level the current bounded |
| 3728 | level. If there is no next highest level, then no bounded mode is |
| 3729 | in effect. |
| 3730 | */ |
| 3731 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3732 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3733 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3734 | |
| 3735 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3736 | return uErr; |
| 3737 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3738 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3739 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3740 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3741 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3742 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3743 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3744 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3745 | return; |
| 3746 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3747 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3748 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3749 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3750 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3751 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3752 | goto Done; |
| 3753 | } |
| 3754 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3755 | /* |
| 3756 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3757 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3758 | from previous map search, then do a dummy search. |
| 3759 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3760 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3761 | QCBORItem Dummy; |
| 3762 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3763 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3764 | if(uErr != QCBOR_SUCCESS) { |
| 3765 | goto Done; |
| 3766 | } |
| 3767 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3768 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3769 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3770 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3771 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3772 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3776 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3777 | static QCBORError |
| 3778 | InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
| 3779 | const QCBORItem *pItem, |
| 3780 | const uint8_t uTagRequirement, |
| 3781 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3782 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3783 | if(pBstr) { |
| 3784 | *pBstr = NULLUsefulBufC; |
| 3785 | } |
| 3786 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3787 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3788 | /* Already in error state; do nothing. */ |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3789 | return pMe->uLastError; |
| 3790 | } |
| 3791 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3792 | QCBORError uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3793 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3794 | const TagSpecification TagSpec = |
| 3795 | { |
| 3796 | uTagRequirement, |
| 3797 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3798 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3799 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3800 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3801 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3802 | if(uError != QCBOR_SUCCESS) { |
| 3803 | goto Done; |
| 3804 | } |
| 3805 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3806 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3807 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 3808 | * increment in QCBORDecode_NestLevelAscender() called by |
| 3809 | * ExitBoundedLevel() will work right. |
| 3810 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3811 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3812 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3813 | |
| 3814 | if(pBstr) { |
| 3815 | *pBstr = pItem->val.string; |
| 3816 | } |
| 3817 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3818 | /* This saves the current length of the UsefulInputBuf and then |
| 3819 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 3820 | * CBOR that is being entered. |
| 3821 | * |
| 3822 | * Most of these calls are simple inline accessors so this doesn't |
| 3823 | * amount to much code. |
| 3824 | */ |
| 3825 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3826 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3827 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 3828 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3829 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3830 | goto Done; |
| 3831 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3832 | |
| 3833 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 3834 | pItem->val.string.ptr); |
| 3835 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 3836 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 3837 | /* This should never happen because pItem->val.string.ptr should |
| 3838 | * always be valid since it was just returned. |
| 3839 | */ |
| 3840 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3841 | goto Done; |
| 3842 | } |
| 3843 | |
| 3844 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 3845 | |
| 3846 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3847 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3848 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3849 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3850 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3851 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3852 | Done: |
| 3853 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3857 | /* |
| 3858 | Public function, see header qcbor/qcbor_decode.h file |
| 3859 | */ |
| 3860 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3861 | uint8_t uTagRequirement, |
| 3862 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3863 | { |
| 3864 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3865 | // Already in error state; do nothing. |
| 3866 | return; |
| 3867 | } |
| 3868 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3869 | /* Get the data item that is the byte string being entered */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3870 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3871 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3872 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3873 | return; |
| 3874 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3875 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3876 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3877 | &Item, |
| 3878 | uTagRequirement, |
| 3879 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3883 | /* |
| 3884 | Public function, see header qcbor/qcbor_decode.h file |
| 3885 | */ |
| 3886 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3887 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3888 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3889 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3890 | { |
| 3891 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3892 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3893 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3894 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3895 | &Item, |
| 3896 | uTagRequirement, |
| 3897 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3898 | } |
| 3899 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3900 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3901 | /* |
| 3902 | Public function, see header qcbor/qcbor_decode.h file |
| 3903 | */ |
| 3904 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3905 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3906 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3907 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3908 | { |
| 3909 | QCBORItem Item; |
| 3910 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3911 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3912 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3913 | &Item, |
| 3914 | uTagRequirement, |
| 3915 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3916 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3917 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3918 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3919 | /* |
| 3920 | Public function, see header qcbor/qcbor_decode.h file |
| 3921 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3922 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3923 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3924 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3925 | // Already in error state; do nothing. |
| 3926 | return; |
| 3927 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3928 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3929 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3930 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3931 | return; |
| 3932 | } |
| 3933 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3934 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 3935 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3936 | /* |
| 3937 | Reset the length of the UsefulInputBuf to what it was before |
| 3938 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3939 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3940 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3941 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3942 | |
| 3943 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3944 | QCBORError uErr = ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3945 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3946 | } |
| 3947 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3948 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3949 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3950 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3951 | static inline void |
| 3952 | ProcessBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3953 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3954 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3955 | /* Already in error state, do nothing */ |
| 3956 | return; |
| 3957 | } |
| 3958 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3959 | switch(pItem->uDataType) { |
| 3960 | case QCBOR_TYPE_TRUE: |
| 3961 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3962 | break; |
| 3963 | |
| 3964 | case QCBOR_TYPE_FALSE: |
| 3965 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3966 | break; |
| 3967 | |
| 3968 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3969 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3970 | break; |
| 3971 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3972 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3973 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3974 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3975 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3976 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3977 | * Public function, see header qcbor/qcbor_decode.h file |
| 3978 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3979 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3980 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3981 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3982 | /* Already in error state, do nothing */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3983 | return; |
| 3984 | } |
| 3985 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3986 | QCBORItem Item; |
| 3987 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3988 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3989 | |
| 3990 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3991 | } |
| 3992 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3993 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3994 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3995 | * Public function, see header qcbor/qcbor_decode.h file |
| 3996 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3997 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3998 | { |
| 3999 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4000 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4001 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4002 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4005 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4006 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4007 | * Public function, see header qcbor/qcbor_decode.h file |
| 4008 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4009 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 4010 | { |
| 4011 | QCBORItem Item; |
| 4012 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4013 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4014 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | |
| 4018 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4019 | |
| 4020 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 4021 | QCBORItem *pItem, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4022 | const uint8_t uTagRequirement, |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4023 | int64_t *pnTime) |
| 4024 | { |
| 4025 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4026 | // Already in error state, do nothing |
| 4027 | return; |
| 4028 | } |
| 4029 | |
| 4030 | QCBORError uErr; |
| 4031 | |
| 4032 | const TagSpecification TagSpec = |
| 4033 | { |
| 4034 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4035 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4036 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4037 | }; |
| 4038 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4039 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4040 | if(uErr != QCBOR_SUCCESS) { |
| 4041 | goto Done; |
| 4042 | } |
| 4043 | |
| 4044 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 4045 | uErr = DecodeDateEpoch(pItem); |
| 4046 | if(uErr != QCBOR_SUCCESS) { |
| 4047 | goto Done; |
| 4048 | } |
| 4049 | } |
| 4050 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4051 | // Save the tags in the last item's tags in the decode context |
| 4052 | // for QCBORDecode_GetNthTagOfLast() |
| 4053 | CopyTags(pMe, pItem); |
| 4054 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4055 | *pnTime = pItem->val.epochDate.nSeconds; |
| 4056 | |
| 4057 | Done: |
| 4058 | pMe->uLastError = (uint8_t)uErr; |
| 4059 | } |
| 4060 | |
| 4061 | |
| 4062 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4063 | uint8_t uTagRequirement, |
| 4064 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4065 | { |
| 4066 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4067 | // Already in error state, do nothing |
| 4068 | return; |
| 4069 | } |
| 4070 | |
| 4071 | QCBORItem Item; |
| 4072 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4073 | |
| 4074 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 4075 | } |
| 4076 | |
| 4077 | |
| 4078 | void |
| 4079 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 4080 | int64_t nLabel, |
| 4081 | uint8_t uTagRequirement, |
| 4082 | int64_t *pnTime) |
| 4083 | { |
| 4084 | QCBORItem Item; |
| 4085 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 4086 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 4087 | } |
| 4088 | |
| 4089 | |
| 4090 | void |
| 4091 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 4092 | const char *szLabel, |
| 4093 | uint8_t uTagRequirement, |
| 4094 | int64_t *pnTime) |
| 4095 | { |
| 4096 | QCBORItem Item; |
| 4097 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4098 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 4099 | } |
| 4100 | |
| 4101 | |
| 4102 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4103 | /* |
| 4104 | * Common processing for the RFC 8943 day-count tag. Mostly |
| 4105 | * make sure the tag content is correct and copy forward any |
| 4106 | * further other tag numbers. |
| 4107 | */ |
| 4108 | static void ProcessEpochDays(QCBORDecodeContext *pMe, |
| 4109 | QCBORItem *pItem, |
| 4110 | uint8_t uTagRequirement, |
| 4111 | int64_t *pnDays) |
| 4112 | { |
| 4113 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4114 | /* Already in error state, do nothing */ |
| 4115 | return; |
| 4116 | } |
| 4117 | |
| 4118 | QCBORError uErr; |
| 4119 | |
| 4120 | const TagSpecification TagSpec = |
| 4121 | { |
| 4122 | uTagRequirement, |
| 4123 | {QCBOR_TYPE_DAYS_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4124 | {QCBOR_TYPE_INT64, QCBOR_TYPE_UINT64, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4125 | }; |
| 4126 | |
| 4127 | uErr = CheckTagRequirement(TagSpec, pItem); |
| 4128 | if(uErr != QCBOR_SUCCESS) { |
| 4129 | goto Done; |
| 4130 | } |
| 4131 | |
| 4132 | if(pItem->uDataType != QCBOR_TYPE_DAYS_EPOCH) { |
| 4133 | uErr = DecodeDaysEpoch(pItem); |
| 4134 | if(uErr != QCBOR_SUCCESS) { |
| 4135 | goto Done; |
| 4136 | } |
| 4137 | } |
| 4138 | |
| 4139 | /* Save the tags in the last item's tags in the decode context |
| 4140 | * for QCBORDecode_GetNthTagOfLast() |
| 4141 | */ |
| 4142 | CopyTags(pMe, pItem); |
| 4143 | |
| 4144 | *pnDays = pItem->val.epochDays; |
| 4145 | |
| 4146 | Done: |
| 4147 | pMe->uLastError = (uint8_t)uErr; |
| 4148 | } |
| 4149 | |
| 4150 | |
| 4151 | /* |
| 4152 | * Public function, see header qcbor/qcbor_decode.h |
| 4153 | */ |
| 4154 | void QCBORDecode_GetEpochDays(QCBORDecodeContext *pMe, |
| 4155 | uint8_t uTagRequirement, |
| 4156 | int64_t *pnDays) |
| 4157 | { |
| 4158 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4159 | /* Already in error state, do nothing */ |
| 4160 | return; |
| 4161 | } |
| 4162 | |
| 4163 | QCBORItem Item; |
| 4164 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4165 | |
| 4166 | ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
| 4167 | } |
| 4168 | |
| 4169 | |
| 4170 | /* |
| 4171 | * Public function, see header qcbor/qcbor_decode.h |
| 4172 | */ |
| 4173 | void |
| 4174 | QCBORDecode_GetEpochDaysInMapN(QCBORDecodeContext *pMe, |
| 4175 | int64_t nLabel, |
| 4176 | uint8_t uTagRequirement, |
| 4177 | int64_t *pnDays) |
| 4178 | { |
| 4179 | QCBORItem Item; |
| 4180 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 4181 | ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
| 4182 | } |
| 4183 | |
| 4184 | |
| 4185 | /* |
| 4186 | * Public function, see header qcbor/qcbor_decode.h |
| 4187 | */ |
| 4188 | void |
| 4189 | QCBORDecode_GetEpochDaysInMapSZ(QCBORDecodeContext *pMe, |
| 4190 | const char *szLabel, |
| 4191 | uint8_t uTagRequirement, |
| 4192 | int64_t *pnDays) |
| 4193 | { |
| 4194 | QCBORItem Item; |
| 4195 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4196 | ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
| 4197 | } |
| 4198 | |
| 4199 | |
| 4200 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4201 | /* |
| 4202 | * @brief Get a string that matches the type/tag specification. |
| 4203 | */ |
| 4204 | void |
| 4205 | QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 4206 | const TagSpecification TagSpec, |
| 4207 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4208 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4209 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4210 | /* Already in error state, do nothing */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4211 | return; |
| 4212 | } |
| 4213 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4214 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4215 | QCBORItem Item; |
| 4216 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4217 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 4218 | if(uError != QCBOR_SUCCESS) { |
| 4219 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4220 | return; |
| 4221 | } |
| 4222 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4223 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4224 | |
| 4225 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4226 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 4227 | } else { |
| 4228 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4229 | } |
| 4230 | } |
| 4231 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4232 | |
| 4233 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4234 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4235 | static QCBORError |
| 4236 | ProcessBigNum(const uint8_t uTagRequirement, |
| 4237 | const QCBORItem *pItem, |
| 4238 | UsefulBufC *pValue, |
| 4239 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4240 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4241 | const TagSpecification TagSpec = |
| 4242 | { |
| 4243 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4244 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4245 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4246 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4247 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4248 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4249 | if(uErr != QCBOR_SUCCESS) { |
| 4250 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4251 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4252 | |
| 4253 | *pValue = pItem->val.string; |
| 4254 | |
| 4255 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 4256 | *pbIsNegative = false; |
| 4257 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 4258 | *pbIsNegative = true; |
| 4259 | } |
| 4260 | |
| 4261 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4265 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4266 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4267 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4268 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 4269 | uint8_t uTagRequirement, |
| 4270 | UsefulBufC *pValue, |
| 4271 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4272 | { |
| 4273 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4274 | // Already in error state, do nothing |
| 4275 | return; |
| 4276 | } |
| 4277 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4278 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4279 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4280 | if(uError != QCBOR_SUCCESS) { |
| 4281 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4282 | return; |
| 4283 | } |
| 4284 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4285 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4286 | } |
| 4287 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4288 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4289 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4290 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4291 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4292 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 4293 | int64_t nLabel, |
| 4294 | uint8_t uTagRequirement, |
| 4295 | UsefulBufC *pValue, |
| 4296 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4297 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4298 | QCBORItem Item; |
| 4299 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4300 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4301 | return; |
| 4302 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4303 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4304 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4307 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4308 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4309 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4310 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4311 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 4312 | const char *szLabel, |
| 4313 | uint8_t uTagRequirement, |
| 4314 | UsefulBufC *pValue, |
| 4315 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4316 | { |
| 4317 | QCBORItem Item; |
| 4318 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4319 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4320 | return; |
| 4321 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4322 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4323 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | |
| 4327 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4328 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4329 | // Semi private |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4330 | QCBORError |
| 4331 | QCBORDecode_GetMIMEInternal(const uint8_t uTagRequirement, |
| 4332 | const QCBORItem *pItem, |
| 4333 | UsefulBufC *pMessage, |
| 4334 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4335 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4336 | const TagSpecification TagSpecText = |
| 4337 | { |
| 4338 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4339 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4340 | {QCBOR_TYPE_TEXT_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4341 | }; |
| 4342 | const TagSpecification TagSpecBinary = |
| 4343 | { |
| 4344 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4345 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4346 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4347 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4348 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4349 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4350 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4351 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4352 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4353 | if(pbIsTag257 != NULL) { |
| 4354 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4355 | } |
| 4356 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4357 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4358 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4359 | if(pbIsTag257 != NULL) { |
| 4360 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4361 | } |
| 4362 | uReturn = QCBOR_SUCCESS; |
| 4363 | |
| 4364 | } else { |
| 4365 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4366 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4367 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4368 | return uReturn; |
| 4369 | } |
| 4370 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4371 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 4372 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4373 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4374 | |
| 4375 | |
| 4376 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4377 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4378 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4379 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4380 | |
| 4381 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4382 | /** |
| 4383 | * @brief Base 10 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4384 | * |
| 4385 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4386 | * @param[in] nExponent The signed integer exponent. |
| 4387 | * @param[out] puResult Place to return the unsigned integer result. |
| 4388 | * |
| 4389 | * This computes: mantissa * 10 ^^ exponent as for a decimal fraction. The output is a 64-bit |
| 4390 | * unsigned integer. |
| 4391 | * |
| 4392 | * There are many inputs for which the result will not fit in the |
| 4393 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4394 | * be returned. |
| 4395 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4396 | static QCBORError |
| 4397 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4398 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4399 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4400 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4401 | if(uResult != 0) { |
| 4402 | /* This loop will run a maximum of 19 times because |
| 4403 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 4404 | * exit with the overflow error |
| 4405 | */ |
| 4406 | for(; nExponent > 0; nExponent--) { |
| 4407 | if(uResult > UINT64_MAX / 10) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4408 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4409 | } |
| 4410 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4411 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4412 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4413 | for(; nExponent < 0; nExponent++) { |
| 4414 | uResult = uResult / 10; |
| 4415 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4416 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4417 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4418 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4419 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4420 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4421 | |
| 4422 | *puResult = uResult; |
| 4423 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4424 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4428 | /** |
| 4429 | * @brief Base 2 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4430 | * |
| 4431 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4432 | * @param[in] nExponent The signed integer exponent. |
| 4433 | * @param[out] puResult Place to return the unsigned integer result. |
| 4434 | * |
| 4435 | * This computes: mantissa * 2 ^^ exponent as for a big float. The |
| 4436 | * output is a 64-bit unsigned integer. |
| 4437 | * |
| 4438 | * There are many inputs for which the result will not fit in the |
| 4439 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4440 | * be returned. |
| 4441 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4442 | static QCBORError |
| 4443 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4444 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4445 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4446 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4447 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4448 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4449 | /* This loop will run a maximum of 64 times because INT64_MAX < |
| 4450 | * 2^31. More than that will cause exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4451 | */ |
| 4452 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4453 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4454 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4455 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4456 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4457 | nExponent--; |
| 4458 | } |
| 4459 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4460 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4461 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4462 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4463 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4464 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4465 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4466 | } |
| 4467 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4468 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4469 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4470 | return QCBOR_SUCCESS; |
| 4471 | } |
| 4472 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4473 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4474 | /** |
| 4475 | * @brief Exponentiate a signed mantissa and signed exponent to produce a signed result. |
| 4476 | * |
| 4477 | * @param[in] nMantissa Signed integer mantissa. |
| 4478 | * @param[in] nExponent Signed integer exponent. |
| 4479 | * @param[out] pnResult Place to put the signed integer result. |
| 4480 | * @param[in] pfExp Exponentiation function. |
| 4481 | * |
| 4482 | * @returns Error code |
| 4483 | * |
| 4484 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4485 | * produces an unsigned result. This converts the mantissa from signed |
| 4486 | * and converts the result to signed. The exponentiation function is |
| 4487 | * 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] | 4488 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4489 | static QCBORError |
| 4490 | ExponentiateNN(int64_t nMantissa, |
| 4491 | int64_t nExponent, |
| 4492 | int64_t *pnResult, |
| 4493 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4494 | { |
| 4495 | uint64_t uResult; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4496 | uint64_t uMantissa; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4497 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4498 | /* Take the absolute value and put it into an unsigned. */ |
| 4499 | if(nMantissa >= 0) { |
| 4500 | /* Positive case is straightforward */ |
| 4501 | uMantissa = (uint64_t)nMantissa; |
| 4502 | } else if(nMantissa != INT64_MIN) { |
| 4503 | /* The common negative case. See next. */ |
| 4504 | uMantissa = (uint64_t)-nMantissa; |
| 4505 | } else { |
| 4506 | /* int64_t and uint64_t are always two's complement per the |
| 4507 | * C standard (and since QCBOR uses these it only works with |
| 4508 | * two's complement, which is pretty much universal these |
| 4509 | * days). The range of a negative two's complement integer is |
| 4510 | * one more that than a positive, so the simple code above might |
| 4511 | * not work all the time because you can't simply negate the |
| 4512 | * value INT64_MIN because it can't be represented in an |
| 4513 | * int64_t. -INT64_MIN can however be represented in a |
| 4514 | * uint64_t. Some compilers seem to recognize this case for the |
| 4515 | * above code and put the correct value in uMantissa, however |
| 4516 | * they are not required to do this by the C standard. This next |
| 4517 | * line does however work for all compilers. |
| 4518 | * |
| 4519 | * This does assume two's complement where -INT64_MIN == |
| 4520 | * INT64_MAX + 1 (which wouldn't be true for one's complement or |
| 4521 | * sign and magnitude (but we know we're using two's complement |
| 4522 | * because int64_t requires it)). |
| 4523 | * |
| 4524 | * See these, particularly the detailed commentary: |
| 4525 | * https://stackoverflow.com/questions/54915742/does-c99-mandate-a-int64-t-type-be-available-always |
| 4526 | * https://stackoverflow.com/questions/37301078/is-negating-int-min-undefined-behaviour |
| 4527 | */ |
| 4528 | uMantissa = (uint64_t)INT64_MAX+1; |
| 4529 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4530 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4531 | /* Call the exponentiator passed for either base 2 or base 10. |
| 4532 | * Here is where most of the overflow errors are caught. */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4533 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4534 | if(uReturn) { |
| 4535 | return uReturn; |
| 4536 | } |
| 4537 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4538 | /* Convert back to the sign of the original mantissa */ |
| 4539 | if(nMantissa >= 0) { |
| 4540 | if(uResult > INT64_MAX) { |
| 4541 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4542 | } |
| 4543 | *pnResult = (int64_t)uResult; |
| 4544 | } else { |
| 4545 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4546 | * of INT64_MIN. This assumes two's compliment representation |
| 4547 | * where INT64_MIN is one increment farther from 0 than |
| 4548 | * INT64_MAX. Trying to write -INT64_MIN doesn't work to get |
| 4549 | * this because the compiler makes it an int64_t which can't |
| 4550 | * represent -INT64_MIN. Also see above. |
| 4551 | */ |
| 4552 | if(uResult > (uint64_t)INT64_MAX+1) { |
| 4553 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4554 | } |
| 4555 | *pnResult = -(int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4558 | return QCBOR_SUCCESS; |
| 4559 | } |
| 4560 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4561 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4562 | /** |
| 4563 | * @brief Exponentiate an unsigned mantissa and signed exponent to produce an unsigned result. |
| 4564 | * |
| 4565 | * @param[in] nMantissa Signed integer mantissa. |
| 4566 | * @param[in] nExponent Signed integer exponent. |
| 4567 | * @param[out] puResult Place to put the signed integer result. |
| 4568 | * @param[in] pfExp Exponentiation function. |
| 4569 | * |
| 4570 | * @returns Error code |
| 4571 | * |
| 4572 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4573 | * produces an unsigned result. This errors out if the mantissa |
| 4574 | * is negative because the output is unsigned. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4575 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4576 | static QCBORError |
| 4577 | ExponentitateNU(int64_t nMantissa, |
| 4578 | int64_t nExponent, |
| 4579 | uint64_t *puResult, |
| 4580 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4581 | { |
| 4582 | if(nMantissa < 0) { |
| 4583 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4584 | } |
| 4585 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4586 | /* Cast to unsigned is OK because of check for negative. |
| 4587 | * Cast to unsigned is OK because UINT64_MAX > INT64_MAX. |
| 4588 | * Exponentiation is straight forward |
| 4589 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4590 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4591 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4592 | |
| 4593 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4594 | /** |
| 4595 | * @brief Exponentiate an usnigned mantissa and unsigned exponent to produce an unsigned result. |
| 4596 | * |
| 4597 | * @param[in] uMantissa Unsigned integer mantissa. |
| 4598 | * @param[in] nExponent Unsigned integer exponent. |
| 4599 | * @param[out] puResult Place to put the unsigned integer result. |
| 4600 | * @param[in] pfExp Exponentiation function. |
| 4601 | * |
| 4602 | * @returns Error code |
| 4603 | * |
| 4604 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4605 | * produces an unsigned result so this is just a wrapper that does |
| 4606 | * nothing (and is likely inlined). |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4607 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4608 | static QCBORError |
| 4609 | ExponentitateUU(uint64_t uMantissa, |
| 4610 | int64_t nExponent, |
| 4611 | uint64_t *puResult, |
| 4612 | fExponentiator pfExp) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4613 | { |
| 4614 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4615 | } |
| 4616 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4617 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4618 | |
| 4619 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4620 | |
| 4621 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4622 | /** |
| 4623 | * @brief Convert a CBOR big number to a uint64_t. |
| 4624 | * |
| 4625 | * @param[in] BigNum Bytes of the big number to convert. |
| 4626 | * @param[in] uMax Maximum value allowed for the result. |
| 4627 | * @param[out] pResult Place to put the unsigned integer result. |
| 4628 | * |
| 4629 | * @returns Error code |
| 4630 | * |
| 4631 | * Many values will overflow because a big num can represent a much |
| 4632 | * larger range than uint64_t. |
| 4633 | */ |
| 4634 | static QCBORError |
| 4635 | ConvertBigNumToUnsigned(const UsefulBufC BigNum, const uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4636 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4637 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4638 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4639 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4640 | const uint8_t *pByte = BigNum.ptr; |
| 4641 | size_t uLen = BigNum.len; |
| 4642 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4643 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4644 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4645 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4646 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4647 | } |
| 4648 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4649 | *pResult = uResult; |
| 4650 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4653 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4654 | /** |
| 4655 | * @brief Convert a CBOR postive big number to a uint64_t. |
| 4656 | * |
| 4657 | * @param[in] BigNum Bytes of the big number to convert. |
| 4658 | * @param[out] pResult Place to put the unsigned integer result. |
| 4659 | * |
| 4660 | * @returns Error code |
| 4661 | * |
| 4662 | * Many values will overflow because a big num can represent a much |
| 4663 | * larger range than uint64_t. |
| 4664 | */ |
| 4665 | static QCBORError |
| 4666 | ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4667 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4668 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4671 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4672 | /** |
| 4673 | * @brief Convert a CBOR positive big number to an int64_t. |
| 4674 | * |
| 4675 | * @param[in] BigNum Bytes of the big number to convert. |
| 4676 | * @param[out] pResult Place to put the signed integer result. |
| 4677 | * |
| 4678 | * @returns Error code |
| 4679 | * |
| 4680 | * Many values will overflow because a big num can represent a much |
| 4681 | * larger range than int64_t. |
| 4682 | */ |
| 4683 | static QCBORError |
| 4684 | ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4685 | { |
| 4686 | uint64_t uResult; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4687 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4688 | if(uError) { |
| 4689 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4690 | } |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4691 | /* Cast is safe because ConvertBigNumToUnsigned is told to limit to INT64_MAX */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4692 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4693 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4697 | /** |
| 4698 | * @brief Convert a CBOR negative big number to an int64_t. |
| 4699 | * |
| 4700 | * @param[in] BigNum Bytes of the big number to convert. |
| 4701 | * @param[out] pnResult Place to put the signed integer result. |
| 4702 | * |
| 4703 | * @returns Error code |
| 4704 | * |
| 4705 | * Many values will overflow because a big num can represent a much |
| 4706 | * larger range than int64_t. |
| 4707 | */ |
| 4708 | static QCBORError |
| 4709 | ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4710 | { |
| 4711 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4712 | /* The negative integer furthest from zero for a C int64_t is |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4713 | * INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4714 | * negative number in CBOR is computed as -n - 1 where n is the |
| 4715 | * encoded integer, where n is what is in the variable BigNum. When |
| 4716 | * converting BigNum to a uint64_t, the maximum value is thus |
| 4717 | * INT64_MAX, so that when it -n - 1 is applied to it the result |
| 4718 | * will never be further from 0 than INT64_MIN. |
| 4719 | * |
| 4720 | * -n - 1 <= INT64_MIN. |
| 4721 | * -n - 1 <= -INT64_MAX - 1 |
| 4722 | * n <= INT64_MAX. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4723 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4724 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4725 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4726 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4727 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4728 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4729 | /* Now apply -n - 1. The cast is safe because |
| 4730 | * ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4731 | * is the largest positive integer that an int64_t can |
| 4732 | * represent. */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4733 | *pnResult = -(int64_t)uResult - 1; |
| 4734 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4735 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4736 | } |
| 4737 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4738 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4739 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4740 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4741 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4742 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4743 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4744 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4745 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4746 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4747 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4748 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4749 | |
| 4750 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4751 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4752 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4753 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4754 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4755 | static QCBORError |
| 4756 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4757 | { |
| 4758 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4759 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4760 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4761 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4762 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4763 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 4764 | http://www.cplusplus.com/reference/cmath/llround/ |
| 4765 | */ |
| 4766 | // Not interested in FE_INEXACT |
| 4767 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4768 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4769 | *pnValue = llround(pItem->val.dfnum); |
| 4770 | } else { |
| 4771 | *pnValue = lroundf(pItem->val.fnum); |
| 4772 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4773 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4774 | // llround() shouldn't result in divide by zero, but catch |
| 4775 | // it here in case it unexpectedly does. Don't try to |
| 4776 | // distinguish between the various exceptions because it seems |
| 4777 | // they vary by CPU, compiler and OS. |
| 4778 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4779 | } |
| 4780 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4781 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4782 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4783 | #else |
| 4784 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4785 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4786 | break; |
| 4787 | |
| 4788 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4789 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4790 | *pnValue = pItem->val.int64; |
| 4791 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4792 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4793 | } |
| 4794 | break; |
| 4795 | |
| 4796 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4797 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4798 | if(pItem->val.uint64 < INT64_MAX) { |
| 4799 | *pnValue = pItem->val.int64; |
| 4800 | } else { |
| 4801 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4802 | } |
| 4803 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4804 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4805 | } |
| 4806 | break; |
| 4807 | |
| 4808 | default: |
| 4809 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4810 | } |
| 4811 | return QCBOR_SUCCESS; |
| 4812 | } |
| 4813 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4814 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4815 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4816 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4817 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4818 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4819 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4820 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4821 | return; |
| 4822 | } |
| 4823 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4824 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4825 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4826 | if(uError) { |
| 4827 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4828 | return; |
| 4829 | } |
| 4830 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4831 | if(pItem) { |
| 4832 | *pItem = Item; |
| 4833 | } |
| 4834 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4835 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4836 | } |
| 4837 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4838 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4839 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4840 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4841 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4842 | int64_t *pnValue, |
| 4843 | QCBORItem *pItem) |
| 4844 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4845 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4846 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4847 | return; |
| 4848 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4849 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4850 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4851 | } |
| 4852 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4853 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4854 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4855 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4856 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4857 | int64_t *pnValue, |
| 4858 | QCBORItem *pItem) |
| 4859 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4860 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4861 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4862 | return; |
| 4863 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4864 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4865 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4869 | /* |
| 4870 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4871 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4872 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4873 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4874 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4875 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4876 | |
| 4877 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4878 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4879 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4880 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4881 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4882 | static QCBORError |
| 4883 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4884 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4885 | switch(pItem->uDataType) { |
| 4886 | |
| 4887 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4888 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4889 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4890 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4891 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4892 | } |
| 4893 | break; |
| 4894 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4895 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4896 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4897 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4898 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4899 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4900 | } |
| 4901 | break; |
| 4902 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4903 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4904 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4905 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4906 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4907 | pItem->val.expAndMantissa.nExponent, |
| 4908 | pnValue, |
| 4909 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4910 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4911 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4912 | } |
| 4913 | break; |
| 4914 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4915 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4916 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4917 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4918 | pItem->val.expAndMantissa.nExponent, |
| 4919 | pnValue, |
| 4920 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4921 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4922 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4923 | } |
| 4924 | break; |
| 4925 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4926 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4927 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4928 | int64_t nMantissa; |
| 4929 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4930 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4931 | if(uErr) { |
| 4932 | return uErr; |
| 4933 | } |
| 4934 | return ExponentiateNN(nMantissa, |
| 4935 | pItem->val.expAndMantissa.nExponent, |
| 4936 | pnValue, |
| 4937 | Exponentitate10); |
| 4938 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4939 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4940 | } |
| 4941 | break; |
| 4942 | |
| 4943 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4944 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4945 | int64_t nMantissa; |
| 4946 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4947 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4948 | if(uErr) { |
| 4949 | return uErr; |
| 4950 | } |
| 4951 | return ExponentiateNN(nMantissa, |
| 4952 | pItem->val.expAndMantissa.nExponent, |
| 4953 | pnValue, |
| 4954 | Exponentitate10); |
| 4955 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4956 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4957 | } |
| 4958 | break; |
| 4959 | |
| 4960 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4961 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4962 | int64_t nMantissa; |
| 4963 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4964 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4965 | if(uErr) { |
| 4966 | return uErr; |
| 4967 | } |
| 4968 | return ExponentiateNN(nMantissa, |
| 4969 | pItem->val.expAndMantissa.nExponent, |
| 4970 | pnValue, |
| 4971 | Exponentitate2); |
| 4972 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4973 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4974 | } |
| 4975 | break; |
| 4976 | |
| 4977 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4978 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4979 | int64_t nMantissa; |
| 4980 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4981 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4982 | if(uErr) { |
| 4983 | return uErr; |
| 4984 | } |
| 4985 | return ExponentiateNN(nMantissa, |
| 4986 | pItem->val.expAndMantissa.nExponent, |
| 4987 | pnValue, |
| 4988 | Exponentitate2); |
| 4989 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4990 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4991 | } |
| 4992 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4993 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4994 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4995 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4996 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4997 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4998 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4999 | |
| 5000 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5001 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5002 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5003 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5004 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5005 | { |
| 5006 | QCBORItem Item; |
| 5007 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5008 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5009 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5010 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5011 | // The above conversion succeeded |
| 5012 | return; |
| 5013 | } |
| 5014 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5015 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5016 | // 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] | 5017 | return; |
| 5018 | } |
| 5019 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5020 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5021 | } |
| 5022 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5023 | |
| 5024 | /* |
| 5025 | Public function, see header qcbor/qcbor_decode.h file |
| 5026 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5027 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5028 | int64_t nLabel, |
| 5029 | uint32_t uConvertTypes, |
| 5030 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5031 | { |
| 5032 | QCBORItem Item; |
| 5033 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5034 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 5035 | nLabel, |
| 5036 | uConvertTypes, |
| 5037 | pnValue, |
| 5038 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5039 | |
| 5040 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5041 | // The above conversion succeeded |
| 5042 | return; |
| 5043 | } |
| 5044 | |
| 5045 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5046 | // The above conversion failed in a way that code below can't correct |
| 5047 | return; |
| 5048 | } |
| 5049 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5050 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5054 | /* |
| 5055 | Public function, see header qcbor/qcbor_decode.h file |
| 5056 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5057 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5058 | const char *szLabel, |
| 5059 | uint32_t uConvertTypes, |
| 5060 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5061 | { |
| 5062 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5063 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 5064 | szLabel, |
| 5065 | uConvertTypes, |
| 5066 | pnValue, |
| 5067 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5068 | |
| 5069 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5070 | // The above conversion succeeded |
| 5071 | return; |
| 5072 | } |
| 5073 | |
| 5074 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5075 | // The above conversion failed in a way that code below can't correct |
| 5076 | return; |
| 5077 | } |
| 5078 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5079 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5083 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5084 | { |
| 5085 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5086 | case QCBOR_TYPE_DOUBLE: |
| 5087 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5088 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5089 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5090 | // Can't use llround here because it will not convert values |
| 5091 | // greater than INT64_MAX and less than UINT64_MAX that |
| 5092 | // need to be converted so it is more complicated. |
| 5093 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 5094 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5095 | if(isnan(pItem->val.dfnum)) { |
| 5096 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5097 | } else if(pItem->val.dfnum < 0) { |
| 5098 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5099 | } else { |
| 5100 | double dRounded = round(pItem->val.dfnum); |
| 5101 | // See discussion in DecodeDateEpoch() for |
| 5102 | // explanation of - 0x7ff |
| 5103 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 5104 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5105 | } |
| 5106 | *puValue = (uint64_t)dRounded; |
| 5107 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5108 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5109 | if(isnan(pItem->val.fnum)) { |
| 5110 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5111 | } else if(pItem->val.fnum < 0) { |
| 5112 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5113 | } else { |
| 5114 | float fRounded = roundf(pItem->val.fnum); |
| 5115 | // See discussion in DecodeDateEpoch() for |
| 5116 | // explanation of - 0x7ff |
| 5117 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 5118 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5119 | } |
| 5120 | *puValue = (uint64_t)fRounded; |
| 5121 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5122 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5123 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5124 | // round() and roundf() shouldn't result in exceptions here, but |
| 5125 | // catch them to be robust and thorough. Don't try to |
| 5126 | // distinguish between the various exceptions because it seems |
| 5127 | // they vary by CPU, compiler and OS. |
| 5128 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5129 | } |
| 5130 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5131 | } else { |
| 5132 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5133 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5134 | #else |
| 5135 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5136 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5137 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5138 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5139 | case QCBOR_TYPE_INT64: |
| 5140 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5141 | if(pItem->val.int64 >= 0) { |
| 5142 | *puValue = (uint64_t)pItem->val.int64; |
| 5143 | } else { |
| 5144 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5145 | } |
| 5146 | } else { |
| 5147 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5148 | } |
| 5149 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5150 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5151 | case QCBOR_TYPE_UINT64: |
| 5152 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5153 | *puValue = pItem->val.uint64; |
| 5154 | } else { |
| 5155 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5156 | } |
| 5157 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5158 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5159 | default: |
| 5160 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5161 | } |
| 5162 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5163 | return QCBOR_SUCCESS; |
| 5164 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5165 | |
| 5166 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5167 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5168 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5169 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5170 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5171 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5172 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5173 | return; |
| 5174 | } |
| 5175 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5176 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5177 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5178 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5179 | if(uError) { |
| 5180 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5181 | return; |
| 5182 | } |
| 5183 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 5184 | if(pItem) { |
| 5185 | *pItem = Item; |
| 5186 | } |
| 5187 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5188 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5189 | } |
| 5190 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5191 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 5192 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5193 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5194 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5195 | uint64_t *puValue, |
| 5196 | QCBORItem *pItem) |
| 5197 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5198 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5199 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5200 | return; |
| 5201 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5202 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5203 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5207 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5208 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5209 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5210 | uint64_t *puValue, |
| 5211 | QCBORItem *pItem) |
| 5212 | { |
| 5213 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5214 | return; |
| 5215 | } |
| 5216 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5217 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5218 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5219 | return; |
| 5220 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5221 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5222 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5223 | } |
| 5224 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5225 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5226 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5227 | static QCBORError |
| 5228 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5229 | { |
Laurence Lundblade | d3adb7c | 2023-02-08 12:43:11 -0700 | [diff] [blame] | 5230 | switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5231 | |
| 5232 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5233 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5234 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 5235 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5236 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5237 | } |
| 5238 | break; |
| 5239 | |
| 5240 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5241 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5242 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5243 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5244 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5245 | } |
| 5246 | break; |
| 5247 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5248 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5249 | |
| 5250 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5251 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5252 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5253 | pItem->val.expAndMantissa.nExponent, |
| 5254 | puValue, |
| 5255 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5256 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5257 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5258 | } |
| 5259 | break; |
| 5260 | |
| 5261 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5262 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5263 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 5264 | pItem->val.expAndMantissa.nExponent, |
| 5265 | puValue, |
| 5266 | Exponentitate2); |
| 5267 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5268 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5269 | } |
| 5270 | break; |
| 5271 | |
| 5272 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5273 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5274 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5275 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5276 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5277 | if(uErr != QCBOR_SUCCESS) { |
| 5278 | return uErr; |
| 5279 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5280 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5281 | pItem->val.expAndMantissa.nExponent, |
| 5282 | puValue, |
| 5283 | Exponentitate10); |
| 5284 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5285 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5286 | } |
| 5287 | break; |
| 5288 | |
| 5289 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5290 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5291 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5292 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5293 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5294 | } |
| 5295 | break; |
| 5296 | |
| 5297 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5298 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5299 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5300 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5301 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5302 | if(uErr != QCBOR_SUCCESS) { |
| 5303 | return uErr; |
| 5304 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5305 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5306 | pItem->val.expAndMantissa.nExponent, |
| 5307 | puValue, |
| 5308 | Exponentitate2); |
| 5309 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5310 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5311 | } |
| 5312 | break; |
| 5313 | |
| 5314 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5315 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5316 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5317 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5318 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5319 | } |
| 5320 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5321 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5322 | default: |
| 5323 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5324 | } |
| 5325 | } |
| 5326 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5327 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5328 | /* |
| 5329 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5330 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5331 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5332 | { |
| 5333 | QCBORItem Item; |
| 5334 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5335 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5336 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5337 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5338 | // The above conversion succeeded |
| 5339 | return; |
| 5340 | } |
| 5341 | |
| 5342 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5343 | // 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] | 5344 | return; |
| 5345 | } |
| 5346 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5347 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5348 | } |
| 5349 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5350 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5351 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5352 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5353 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 5354 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5355 | int64_t nLabel, |
| 5356 | uint32_t uConvertTypes, |
| 5357 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5358 | { |
| 5359 | QCBORItem Item; |
| 5360 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5361 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 5362 | nLabel, |
| 5363 | uConvertTypes, |
| 5364 | puValue, |
| 5365 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5366 | |
| 5367 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5368 | // The above conversion succeeded |
| 5369 | return; |
| 5370 | } |
| 5371 | |
| 5372 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5373 | // The above conversion failed in a way that code below can't correct |
| 5374 | return; |
| 5375 | } |
| 5376 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5377 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | |
| 5381 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5382 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5383 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 5384 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5385 | const char *szLabel, |
| 5386 | uint32_t uConvertTypes, |
| 5387 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5388 | { |
| 5389 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5390 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 5391 | szLabel, |
| 5392 | uConvertTypes, |
| 5393 | puValue, |
| 5394 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5395 | |
| 5396 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5397 | // The above conversion succeeded |
| 5398 | return; |
| 5399 | } |
| 5400 | |
| 5401 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5402 | // The above conversion failed in a way that code below can't correct |
| 5403 | return; |
| 5404 | } |
| 5405 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5406 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5410 | |
| 5411 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5412 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5413 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 5414 | uint32_t uConvertTypes, |
| 5415 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5416 | { |
| 5417 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5418 | case QCBOR_TYPE_FLOAT: |
| 5419 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5420 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5421 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5422 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5423 | *pdValue = (double)pItem->val.fnum; |
| 5424 | } else { |
| 5425 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5426 | } |
| 5427 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5428 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5429 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5430 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5431 | break; |
| 5432 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5433 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5434 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5435 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5436 | *pdValue = pItem->val.dfnum; |
| 5437 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5438 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5439 | } |
| 5440 | } |
| 5441 | break; |
| 5442 | |
| 5443 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5444 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5445 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5446 | // A simple cast seems to do the job with no worry of exceptions. |
| 5447 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5448 | *pdValue = (double)pItem->val.int64; |
| 5449 | |
| 5450 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5451 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5452 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5453 | #else |
| 5454 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5455 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5456 | break; |
| 5457 | |
| 5458 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5459 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5460 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5461 | // A simple cast seems to do the job with no worry of exceptions. |
| 5462 | // There will be precision loss for some values. |
| 5463 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5464 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5465 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5466 | } |
| 5467 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5468 | #else |
| 5469 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5470 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5471 | |
| 5472 | default: |
| 5473 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5474 | } |
| 5475 | |
| 5476 | return QCBOR_SUCCESS; |
| 5477 | } |
| 5478 | |
| 5479 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5480 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5481 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5482 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5483 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5484 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5485 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5486 | return; |
| 5487 | } |
| 5488 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5489 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5490 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5491 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5492 | if(uError) { |
| 5493 | pMe->uLastError = (uint8_t)uError; |
| 5494 | return; |
| 5495 | } |
| 5496 | |
| 5497 | if(pItem) { |
| 5498 | *pItem = Item; |
| 5499 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5500 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5501 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5502 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5503 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5504 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5505 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 5506 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5507 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5508 | double *pdValue, |
| 5509 | QCBORItem *pItem) |
| 5510 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5511 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5512 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5513 | return; |
| 5514 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5515 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5516 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5517 | } |
| 5518 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5519 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5520 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 5521 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5522 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5523 | double *pdValue, |
| 5524 | QCBORItem *pItem) |
| 5525 | { |
| 5526 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5527 | return; |
| 5528 | } |
| 5529 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5530 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5531 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5532 | return; |
| 5533 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5534 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5535 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5539 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5540 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 5541 | { |
| 5542 | double dResult; |
| 5543 | |
| 5544 | dResult = 0.0; |
| 5545 | const uint8_t *pByte = BigNum.ptr; |
| 5546 | size_t uLen = BigNum.len; |
| 5547 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5548 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5549 | while(uLen--) { |
| 5550 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 5551 | } |
| 5552 | |
| 5553 | return dResult; |
| 5554 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5555 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5556 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5557 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5558 | static QCBORError |
| 5559 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5560 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5561 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5562 | /* |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5563 | * What Every Computer Scientist Should Know About Floating-Point Arithmetic |
| 5564 | * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 5565 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5566 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5567 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5568 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5569 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5570 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5571 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5572 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5573 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 5574 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5575 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5576 | } |
| 5577 | break; |
| 5578 | |
| 5579 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5580 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5581 | // Underflow gives 0, overflow gives infinity |
| 5582 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5583 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5584 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5585 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5586 | } |
| 5587 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5588 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5589 | |
| 5590 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5591 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5592 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 5593 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5594 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5595 | } |
| 5596 | break; |
| 5597 | |
| 5598 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5599 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5600 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5601 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5602 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5603 | } |
| 5604 | break; |
| 5605 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5606 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5607 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5608 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5609 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5610 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5611 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5612 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5613 | } |
| 5614 | break; |
| 5615 | |
| 5616 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5617 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5618 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5619 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5620 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5621 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5622 | } |
| 5623 | break; |
| 5624 | |
| 5625 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5626 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5627 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5628 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5629 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5630 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5631 | } |
| 5632 | break; |
| 5633 | |
| 5634 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5635 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5636 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5637 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5638 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5639 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5640 | } |
| 5641 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5642 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5643 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5644 | default: |
| 5645 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5646 | } |
| 5647 | |
| 5648 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5649 | |
| 5650 | #else |
| 5651 | (void)pItem; |
| 5652 | (void)uConvertTypes; |
| 5653 | (void)pdValue; |
| 5654 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5655 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5656 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | |
| 5660 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5661 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5662 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5663 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 5664 | uint32_t uConvertTypes, |
| 5665 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5666 | { |
| 5667 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5668 | QCBORItem Item; |
| 5669 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5670 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5671 | |
| 5672 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5673 | // The above conversion succeeded |
| 5674 | return; |
| 5675 | } |
| 5676 | |
| 5677 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5678 | // The above conversion failed in a way that code below can't correct |
| 5679 | return; |
| 5680 | } |
| 5681 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5682 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5683 | } |
| 5684 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5685 | |
| 5686 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5687 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5688 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5689 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5690 | int64_t nLabel, |
| 5691 | uint32_t uConvertTypes, |
| 5692 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5693 | { |
| 5694 | QCBORItem Item; |
| 5695 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5696 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5697 | |
| 5698 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5699 | // The above conversion succeeded |
| 5700 | return; |
| 5701 | } |
| 5702 | |
| 5703 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5704 | // The above conversion failed in a way that code below can't correct |
| 5705 | return; |
| 5706 | } |
| 5707 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5708 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | |
| 5712 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5713 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5714 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5715 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5716 | const char *szLabel, |
| 5717 | uint32_t uConvertTypes, |
| 5718 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5719 | { |
| 5720 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5721 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5722 | |
| 5723 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5724 | // The above conversion succeeded |
| 5725 | return; |
| 5726 | } |
| 5727 | |
| 5728 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5729 | // The above conversion failed in a way that code below can't correct |
| 5730 | return; |
| 5731 | } |
| 5732 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5733 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5734 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5735 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5736 | |
| 5737 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5738 | |
| 5739 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5740 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5741 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 5742 | { |
| 5743 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5744 | uInt = uInt << 8; |
| 5745 | }; |
| 5746 | |
| 5747 | UsefulOutBuf UOB; |
| 5748 | |
| 5749 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5750 | |
| 5751 | while(uInt) { |
| 5752 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5753 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5754 | uInt = uInt << 8; |
| 5755 | (void)xx; |
| 5756 | } |
| 5757 | |
| 5758 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5759 | } |
| 5760 | |
| 5761 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5762 | /** |
| 5763 | * @brief Check and/or complete mantissa and exponent item. |
| 5764 | * |
| 5765 | * @param[in] pMe The decoder context |
| 5766 | * @param[in] TagSpec Expected type(s) |
| 5767 | * @param[in,out] pItem See below |
| 5768 | * |
| 5769 | * This is for decimal fractions and big floats, both of which are a |
| 5770 | * mantissa and exponent. |
| 5771 | * |
| 5772 | * The input item is either a fully decoded decimal faction or big |
| 5773 | * float, or a just the decoded first item of a decimal fraction or |
| 5774 | * big float. |
| 5775 | * |
| 5776 | * On output, the item is always a fully decoded decimal fraction or |
| 5777 | * big float. |
| 5778 | * |
| 5779 | * This errors out if the input type does not meet the TagSpec. |
| 5780 | */ |
| 5781 | // TODO: document and see tests for the bug that was fixed by this rewrite |
| 5782 | static QCBORError |
| 5783 | MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 5784 | const TagSpecification TagSpec, |
| 5785 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5786 | { |
| 5787 | QCBORError uErr; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5788 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5789 | /* pItem could either be an auto-decoded mantissa and exponent or |
| 5790 | * the opening array of an undecoded mantissa and exponent. This |
| 5791 | * check will succeed on either, but doesn't say which it was. |
| 5792 | */ |
| 5793 | uErr = CheckTagRequirement(TagSpec, pItem); |
| 5794 | if(uErr != QCBOR_SUCCESS) { |
| 5795 | goto Done; |
| 5796 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5797 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5798 | if(pItem->uDataType == QCBOR_TYPE_ARRAY) { |
| 5799 | /* The item is an array, which means is is an undecoded mantissa |
| 5800 | * and exponent. This call consumes the items in the array and |
| 5801 | * results in a decoded mantissa and exponent in pItem. This is |
| 5802 | * the case where there was no tag. |
| 5803 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5804 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5805 | if(uErr != QCBOR_SUCCESS) { |
| 5806 | goto Done; |
| 5807 | } |
| 5808 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5809 | /* The above decode didn't determine whether it is a decimal |
| 5810 | * fraction or big num. Which of these two depends on what the |
| 5811 | * caller wants it decoded as since there is no tag, so fish the |
| 5812 | * type out of the TagSpec. */ |
| 5813 | pItem->uDataType = MantissaExponentDataType(TagSpec.uTaggedTypes[0], pItem); |
| 5814 | |
| 5815 | /* No need to check the type again. All that we need to know was |
| 5816 | * that it decoded correctly as a mantissa and exponent. The |
| 5817 | * QCBOR type is set out by what was requested. |
| 5818 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5819 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5820 | |
| 5821 | /* If the item was not an array and the check passed, then |
| 5822 | * it is a fully decoded big float or decimal fraction and |
| 5823 | * matches what is requested. |
| 5824 | */ |
| 5825 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5826 | Done: |
| 5827 | return uErr; |
| 5828 | } |
| 5829 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5830 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5831 | /* Some notes from the work to disable tags. |
| 5832 | * |
| 5833 | * The API for big floats and decimal fractions seems good. |
| 5834 | * If there's any issue with it it's that the code size to |
| 5835 | * implement is a bit large because of the conversion |
| 5836 | * to/from int and bignum that is required. There is no API |
| 5837 | * that doesn't do the conversion so dead stripping will never |
| 5838 | * leave that code out. |
| 5839 | * |
| 5840 | * The implementation itself seems correct, but not as clean |
| 5841 | * and neat as it could be. It could probably be smaller too. |
| 5842 | * |
| 5843 | * The implementation has three main parts / functions |
| 5844 | * - The decoding of the array of two |
| 5845 | * - All the tag and type checking for the various API functions |
| 5846 | * - Conversion to/from bignum and int |
| 5847 | * |
| 5848 | * The type checking seems like it wastes the most code for |
| 5849 | * what it needs to do. |
| 5850 | * |
| 5851 | * The inlining for the conversion is probably making the |
| 5852 | * overall code base larger. |
| 5853 | * |
| 5854 | * The tests cases could be organized a lot better and be |
| 5855 | * more thorough. |
| 5856 | * |
| 5857 | * Seems also like there could be more common code in the |
| 5858 | * first tier part of the public API. Some functions only |
| 5859 | * vary by a TagSpec. |
| 5860 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5861 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5862 | TagSpecification TagSpec, |
| 5863 | QCBORItem *pItem, |
| 5864 | int64_t *pnMantissa, |
| 5865 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5866 | { |
| 5867 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5868 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5869 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5870 | if(uErr != QCBOR_SUCCESS) { |
| 5871 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5872 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5873 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5874 | switch (pItem->uDataType) { |
| 5875 | |
| 5876 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5877 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5878 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5879 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5880 | break; |
| 5881 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5882 | #ifndef QCBOR_DISABLE_TAGS |
| 5883 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5884 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5885 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5886 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5887 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5888 | break; |
| 5889 | |
| 5890 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5891 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5892 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5893 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5894 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5895 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5896 | |
| 5897 | default: |
| 5898 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5899 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5900 | |
| 5901 | Done: |
| 5902 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5903 | } |
| 5904 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5905 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5906 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5907 | TagSpecification TagSpec, |
| 5908 | QCBORItem *pItem, |
| 5909 | UsefulBuf BufferForMantissa, |
| 5910 | UsefulBufC *pMantissa, |
| 5911 | bool *pbIsNegative, |
| 5912 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5913 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5914 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5915 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5916 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5917 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5918 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5919 | } |
| 5920 | |
| 5921 | uint64_t uMantissa; |
| 5922 | |
| 5923 | switch (pItem->uDataType) { |
| 5924 | |
| 5925 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5926 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5927 | /* See comments in ExponentiateNN() on handling INT64_MIN */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5928 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5929 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5930 | *pbIsNegative = false; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5931 | } else if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5932 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5933 | *pbIsNegative = true; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5934 | } else { |
| 5935 | uMantissa = (uint64_t)INT64_MAX+1; |
| 5936 | *pbIsNegative = true; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5937 | } |
| 5938 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5939 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5940 | break; |
| 5941 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5942 | #ifndef QCBOR_DISABLE_TAGS |
| 5943 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5944 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5945 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5946 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5947 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5948 | *pbIsNegative = false; |
| 5949 | break; |
| 5950 | |
| 5951 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5952 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5953 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5954 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5955 | *pbIsNegative = true; |
| 5956 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5957 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5958 | |
| 5959 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5960 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5961 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5962 | |
| 5963 | Done: |
| 5964 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5965 | } |
| 5966 | |
| 5967 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5968 | /* |
| 5969 | Public function, see header qcbor/qcbor_decode.h file |
| 5970 | */ |
| 5971 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5972 | uint8_t uTagRequirement, |
| 5973 | int64_t *pnMantissa, |
| 5974 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5975 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5976 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5977 | return; |
| 5978 | } |
| 5979 | |
| 5980 | QCBORItem Item; |
| 5981 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5982 | if(uError) { |
| 5983 | pMe->uLastError = (uint8_t)uError; |
| 5984 | return; |
| 5985 | } |
| 5986 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5987 | const TagSpecification TagSpec = |
| 5988 | { |
| 5989 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5990 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5991 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5992 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5993 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5994 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5995 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5996 | } |
| 5997 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5998 | |
| 5999 | /* |
| 6000 | Public function, see header qcbor/qcbor_decode.h file |
| 6001 | */ |
| 6002 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6003 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6004 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6005 | int64_t *pnMantissa, |
| 6006 | int64_t *pnExponent) |
| 6007 | { |
| 6008 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6009 | return; |
| 6010 | } |
| 6011 | |
| 6012 | QCBORItem Item; |
| 6013 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6014 | |
| 6015 | const TagSpecification TagSpec = |
| 6016 | { |
| 6017 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6018 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6019 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6020 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6021 | }; |
| 6022 | |
| 6023 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 6024 | } |
| 6025 | |
| 6026 | |
| 6027 | /* |
| 6028 | Public function, see header qcbor/qcbor_decode.h file |
| 6029 | */ |
| 6030 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6031 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6032 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6033 | int64_t *pnMantissa, |
| 6034 | int64_t *pnExponent) |
| 6035 | { |
| 6036 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6037 | return; |
| 6038 | } |
| 6039 | |
| 6040 | QCBORItem Item; |
| 6041 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6042 | |
| 6043 | const TagSpecification TagSpec = |
| 6044 | { |
| 6045 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6046 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6047 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6048 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6049 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6050 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6051 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 6052 | } |
| 6053 | |
| 6054 | |
| 6055 | /* |
| 6056 | Public function, see header qcbor/qcbor_decode.h file |
| 6057 | */ |
| 6058 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 6059 | uint8_t uTagRequirement, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6060 | UsefulBuf MantissaBuffer, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6061 | UsefulBufC *pMantissa, |
| 6062 | bool *pbMantissaIsNegative, |
| 6063 | int64_t *pnExponent) |
| 6064 | { |
| 6065 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6066 | return; |
| 6067 | } |
| 6068 | |
| 6069 | QCBORItem Item; |
| 6070 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6071 | if(uError) { |
| 6072 | pMe->uLastError = (uint8_t)uError; |
| 6073 | return; |
| 6074 | } |
| 6075 | |
| 6076 | const TagSpecification TagSpec = |
| 6077 | { |
| 6078 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6079 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6080 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6081 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6082 | }; |
| 6083 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6084 | ProcessMantissaAndExponentBig(pMe, |
| 6085 | TagSpec, |
| 6086 | &Item, |
| 6087 | MantissaBuffer, |
| 6088 | pMantissa, |
| 6089 | pbMantissaIsNegative, |
| 6090 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6091 | } |
| 6092 | |
| 6093 | |
| 6094 | /* |
| 6095 | Public function, see header qcbor/qcbor_decode.h file |
| 6096 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6097 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6098 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6099 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6100 | UsefulBuf BufferForMantissa, |
| 6101 | UsefulBufC *pMantissa, |
| 6102 | bool *pbIsNegative, |
| 6103 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6104 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6105 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6106 | return; |
| 6107 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6108 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6109 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6110 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6111 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6112 | return; |
| 6113 | } |
| 6114 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6115 | const TagSpecification TagSpec = |
| 6116 | { |
| 6117 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6118 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6119 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6120 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6121 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6122 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6123 | ProcessMantissaAndExponentBig(pMe, |
| 6124 | TagSpec, |
| 6125 | &Item, |
| 6126 | BufferForMantissa, |
| 6127 | pMantissa, |
| 6128 | pbIsNegative, |
| 6129 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6130 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6131 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6132 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6133 | /* |
| 6134 | Public function, see header qcbor/qcbor_decode.h file |
| 6135 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6136 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6137 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6138 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6139 | UsefulBuf BufferForMantissa, |
| 6140 | UsefulBufC *pMantissa, |
| 6141 | bool *pbIsNegative, |
| 6142 | int64_t *pnExponent) |
| 6143 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6144 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6145 | return; |
| 6146 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6147 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6148 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6149 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6150 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6151 | return; |
| 6152 | } |
| 6153 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6154 | const TagSpecification TagSpec = |
| 6155 | { |
| 6156 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6157 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6158 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6159 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6160 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6161 | |
| 6162 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 6163 | } |
| 6164 | |
| 6165 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6166 | /* |
| 6167 | Public function, see header qcbor/qcbor_decode.h file |
| 6168 | */ |
| 6169 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 6170 | uint8_t uTagRequirement, |
| 6171 | int64_t *pnMantissa, |
| 6172 | int64_t *pnExponent) |
| 6173 | { |
| 6174 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6175 | return; |
| 6176 | } |
| 6177 | |
| 6178 | QCBORItem Item; |
| 6179 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6180 | if(uError) { |
| 6181 | pMe->uLastError = (uint8_t)uError; |
| 6182 | return; |
| 6183 | } |
| 6184 | const TagSpecification TagSpec = |
| 6185 | { |
| 6186 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6187 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6188 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6189 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6190 | }; |
| 6191 | |
| 6192 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 6193 | } |
| 6194 | |
| 6195 | |
| 6196 | /* |
| 6197 | Public function, see header qcbor/qcbor_decode.h file |
| 6198 | */ |
| 6199 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6200 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6201 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6202 | int64_t *pnMantissa, |
| 6203 | int64_t *pnExponent) |
| 6204 | { |
| 6205 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6206 | return; |
| 6207 | } |
| 6208 | |
| 6209 | QCBORItem Item; |
| 6210 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6211 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6212 | return; |
| 6213 | } |
| 6214 | |
| 6215 | const TagSpecification TagSpec = |
| 6216 | { |
| 6217 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6218 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6219 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6220 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6221 | }; |
| 6222 | |
| 6223 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 6224 | } |
| 6225 | |
| 6226 | |
| 6227 | /* |
| 6228 | Public function, see header qcbor/qcbor_decode.h file |
| 6229 | */ |
| 6230 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6231 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6232 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6233 | int64_t *pnMantissa, |
| 6234 | int64_t *pnExponent) |
| 6235 | { |
| 6236 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6237 | return; |
| 6238 | } |
| 6239 | |
| 6240 | QCBORItem Item; |
| 6241 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6242 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6243 | return; |
| 6244 | } |
| 6245 | |
| 6246 | const TagSpecification TagSpec = |
| 6247 | { |
| 6248 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6249 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6250 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6251 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6252 | }; |
| 6253 | |
| 6254 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 6255 | } |
| 6256 | |
| 6257 | |
| 6258 | /* |
| 6259 | Public function, see header qcbor/qcbor_decode.h file |
| 6260 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6261 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 6262 | uint8_t uTagRequirement, |
| 6263 | UsefulBuf MantissaBuffer, |
| 6264 | UsefulBufC *pMantissa, |
| 6265 | bool *pbMantissaIsNegative, |
| 6266 | int64_t *pnExponent) |
| 6267 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6268 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6269 | return; |
| 6270 | } |
| 6271 | |
| 6272 | QCBORItem Item; |
| 6273 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6274 | if(uError) { |
| 6275 | pMe->uLastError = (uint8_t)uError; |
| 6276 | return; |
| 6277 | } |
| 6278 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6279 | const TagSpecification TagSpec = |
| 6280 | { |
| 6281 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6282 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6283 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6284 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6285 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6286 | |
| 6287 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6288 | } |
| 6289 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6290 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6291 | /* |
| 6292 | Public function, see header qcbor/qcbor_decode.h file |
| 6293 | */ |
| 6294 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6295 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6296 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6297 | UsefulBuf BufferForMantissa, |
| 6298 | UsefulBufC *pMantissa, |
| 6299 | bool *pbIsNegative, |
| 6300 | int64_t *pnExponent) |
| 6301 | { |
| 6302 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6303 | return; |
| 6304 | } |
| 6305 | |
| 6306 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6307 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6308 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6309 | return; |
| 6310 | } |
| 6311 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6312 | const TagSpecification TagSpec = |
| 6313 | { |
| 6314 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6315 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6316 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6317 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6318 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6319 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6320 | ProcessMantissaAndExponentBig(pMe, |
| 6321 | TagSpec, |
| 6322 | &Item, |
| 6323 | BufferForMantissa, |
| 6324 | pMantissa, |
| 6325 | pbIsNegative, |
| 6326 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6327 | } |
| 6328 | |
| 6329 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6330 | /* |
| 6331 | Public function, see header qcbor/qcbor_decode.h file |
| 6332 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6333 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6334 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 6335 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6336 | UsefulBuf BufferForMantissa, |
| 6337 | UsefulBufC *pMantissa, |
| 6338 | bool *pbIsNegative, |
| 6339 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6340 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6341 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6342 | return; |
| 6343 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6344 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6345 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6346 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6347 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6348 | return; |
| 6349 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6350 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6351 | const TagSpecification TagSpec = |
| 6352 | { |
| 6353 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6354 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6355 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6356 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6357 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6358 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6359 | ProcessMantissaAndExponentBig(pMe, |
| 6360 | TagSpec, |
| 6361 | &Item, |
| 6362 | BufferForMantissa, |
| 6363 | pMantissa, |
| 6364 | pbIsNegative, |
| 6365 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6366 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6367 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6368 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |