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 | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2021, Laurence Lundblade. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 4 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following |
| 13 | disclaimer in the documentation and/or other materials provided |
| 14 | with the distribution. |
| 15 | * Neither the name of The Linux Foundation nor the names of its |
| 16 | contributors, nor the name "Laurence Lundblade" may be used to |
| 17 | endorse or promote products derived from this software without |
| 18 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 31 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 32 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 33 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 34 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 35 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 36 | #include "ieee754.h" /* Does not use math.h */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 37 | |
| 38 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 39 | |
| 40 | #include <math.h> /* For isnan(), llround(), llroudf(), round(), roundf(), |
| 41 | * pow(), exp2() |
| 42 | */ |
| 43 | #include <fenv.h> /* feclearexcept(), fetestexcept() */ |
| 44 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 45 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 46 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 47 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 48 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 49 | #define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type)) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 50 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 51 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 52 | |
| 53 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 54 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 55 | QCBORItem_IsMapOrArray(const QCBORItem *pMe) |
| 56 | { |
| 57 | const uint8_t uDataType = pMe->uDataType; |
| 58 | return uDataType == QCBOR_TYPE_MAP || |
| 59 | uDataType == QCBOR_TYPE_ARRAY || |
| 60 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
| 61 | } |
| 62 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 63 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 64 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 65 | { |
| 66 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if(pMe->val.uCount != 0) { |
| 71 | return false; |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 76 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 77 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 78 | { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 79 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 80 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if(pMe->val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
| 85 | return false; |
| 86 | } |
| 87 | return true; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 88 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 89 | (void)pMe; |
| 90 | return false; |
| 91 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 95 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 96 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 97 | ===========================================================================*/ |
| 98 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 99 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 100 | * See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
| 101 | * the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 102 | */ |
| 103 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 104 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 105 | static inline uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 106 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 107 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 108 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 109 | /* Limit in DecodeNesting_Descend against more than |
| 110 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 111 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 112 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 115 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 116 | static inline uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 117 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 118 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 119 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 120 | /* Limit in DecodeNesting_Descend against more than |
| 121 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 122 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 123 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 126 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 127 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 128 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 129 | { |
| 130 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 131 | } |
| 132 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 133 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 134 | static inline bool |
| 135 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 136 | { |
| 137 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 138 | return true; |
| 139 | } else { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 145 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 146 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 147 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 148 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 149 | return true; |
| 150 | } else { |
| 151 | return false; |
| 152 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 155 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 156 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 157 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 158 | { |
| 159 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 160 | /* Not a map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 161 | return false; |
| 162 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 163 | |
| 164 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 165 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 166 | /* Is indefinite */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 167 | return false; |
| 168 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 169 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 170 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 171 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 172 | /* All checks passed; is a definte length map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 176 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 177 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 178 | { |
| 179 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 180 | /* is a byte string */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 186 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 187 | static inline bool DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 188 | { |
| 189 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 190 | return true; |
| 191 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 192 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 198 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 199 | static inline void DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 200 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 201 | /* Should be only called on maps and arrays */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 202 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 203 | * DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 204 | * larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 205 | * uin32_t so the cast is safe. |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 206 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 207 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 208 | |
| 209 | if(bIsEmpty) { |
| 210 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 211 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 214 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 215 | static inline void DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 216 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 217 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 220 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 221 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 222 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 223 | { |
| 224 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 225 | /* No bounded map or array set up */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 229 | /* Not a map or array; end of those is by byte count */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 232 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 233 | /* In a traveral at a level deeper than the bounded level */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 234 | return false; |
| 235 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 236 | /* Works for both definite- and indefinitelength maps/arrays */ |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 237 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0 && |
| 238 | pNesting->pCurrentBounded->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 239 | /* Count is not zero, still unconsumed item */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 242 | /* All checks passed, got to the end of an array or map*/ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 243 | return true; |
| 244 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 245 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 246 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 247 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 248 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 249 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 250 | /* Must only be called on map / array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 251 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 252 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 253 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 254 | return false; |
| 255 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 258 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 259 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 260 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 261 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 262 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 263 | return true; |
| 264 | } else { |
| 265 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 266 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 269 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 270 | static inline bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 271 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 272 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 273 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 274 | return false; |
| 275 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 276 | |
| 277 | if(pNesting->pCurrentBounded->uLevelType != uType) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 284 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 285 | static inline void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 286 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 287 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 288 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 289 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 290 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 291 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 292 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 293 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 294 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 295 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 296 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 297 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 298 | } |
| 299 | |
| 300 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 301 | static inline void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 302 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 303 | { |
| 304 | pNesting->pCurrent--; |
| 305 | } |
| 306 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 307 | |
| 308 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 309 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 310 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 311 | /* Error out if nesting is too deep */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 312 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 313 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 316 | /* The actual descend */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 317 | pNesting->pCurrent++; |
| 318 | |
| 319 | pNesting->pCurrent->uLevelType = uType; |
| 320 | |
| 321 | return QCBOR_SUCCESS; |
| 322 | } |
| 323 | |
| 324 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 325 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 326 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, |
| 327 | bool bIsEmpty, |
| 328 | size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 329 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 330 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 331 | * Should only be called on map/array. |
| 332 | * |
| 333 | * Have descended into this before this is called. The job here is |
| 334 | * just to mark it in bounded mode. |
| 335 | * |
| 336 | * Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
| 337 | * uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET. |
| 338 | * |
| 339 | * 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] | 340 | */ |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 341 | if((uint32_t)uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 342 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 345 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 346 | |
| 347 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 348 | |
| 349 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 352 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 353 | static inline QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 354 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 355 | uint8_t uQCBORType, |
| 356 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 357 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 358 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 359 | |
| 360 | if(uCount == 0) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 361 | /* Nothing to do for empty definite-length arrays. They are just are |
| 362 | * effectively the same as an item that is not a map or array. |
| 363 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 364 | goto Done; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 365 | /* Empty indefinite-length maps and arrays are handled elsewhere */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 368 | /* Error out if arrays is too long to handle */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 369 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 370 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 371 | uError = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 372 | goto Done; |
| 373 | } |
| 374 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 375 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 376 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 377 | goto Done; |
| 378 | } |
| 379 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 380 | /* Fill in the new map/array level. Check above makes casts OK. */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 381 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 382 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 383 | |
| 384 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 385 | |
| 386 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 387 | return uError;; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | static inline void |
| 392 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 393 | { |
| 394 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | static inline void |
| 399 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 400 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 401 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 402 | pNesting->pCurrentBounded--; |
| 403 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 404 | break; |
| 405 | } |
| 406 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 409 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 410 | static inline void |
| 411 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 412 | { |
| 413 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 414 | } |
| 415 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 416 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 417 | static inline QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 418 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 419 | uint32_t uEndOffset, |
| 420 | uint32_t uStartOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 421 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 422 | QCBORError uError; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 423 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 424 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 425 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 426 | goto Done; |
| 427 | } |
| 428 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 429 | /* Fill in the new byte string level */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 430 | pNesting->pCurrent->u.bs.uSavedEndOffset = uEndOffset; |
| 431 | pNesting->pCurrent->u.bs.uBstrStartOffset = uStartOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 432 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 433 | /* Bstr wrapped levels are always bounded */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 434 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 435 | |
| 436 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 437 | return uError;; |
| 438 | } |
| 439 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 440 | |
| 441 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 442 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 443 | { |
| 444 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 447 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 448 | static inline void |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 449 | DecodeNesting_ResetMapOrArrayCount(QCBORDecodeNesting *pNesting) |
| 450 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 451 | if(pNesting->pCurrent->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 452 | pNesting->pCurrentBounded->u.ma.uCountCursor = pNesting->pCurrentBounded->u.ma.uCountTotal; |
| 453 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | static inline void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 458 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 459 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 460 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 461 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 462 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 466 | static inline void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 467 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, |
| 468 | QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 469 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 470 | *pSave = *pNesting; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 473 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 474 | static inline void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 475 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, |
| 476 | const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 477 | { |
| 478 | *pNesting = *pSave; |
| 479 | } |
| 480 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 481 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 482 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 483 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 484 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 485 | return pMe->pCurrentBounded->u.bs.uSavedEndOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 489 | |
| 490 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 491 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 492 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 493 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 494 | |
| 495 | The following four functions are pretty wrappers for invocation of |
| 496 | the string allocator supplied by the caller. |
| 497 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 498 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 499 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 500 | static inline void |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 501 | StringAllocator_Free(const QCBORInternalAllocator *pMe, const void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 502 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 503 | /* These pragmas allow the "-Wcast-qual" warnings flag to be set for |
| 504 | * gcc and clang. This is the one place where the const needs to be |
| 505 | * cast away so const can be use in the rest of the code. |
| 506 | */ |
| 507 | #pragma GCC diagnostic push |
| 508 | #pragma GCC diagnostic ignored "-Wcast-qual" |
| 509 | (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)pMem, 0); |
| 510 | #pragma GCC diagnostic pop |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 513 | // StringAllocator_Reallocate called with pMem NULL is |
| 514 | // equal to StringAllocator_Allocate() |
| 515 | static inline UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 516 | StringAllocator_Reallocate(const QCBORInternalAllocator *pMe, |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 517 | const void *pMem, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 518 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 519 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 520 | /* See comment in StringAllocator_Free() */ |
| 521 | #pragma GCC diagnostic push |
| 522 | #pragma GCC diagnostic ignored "-Wcast-qual" |
| 523 | return (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)pMem, uSize); |
| 524 | #pragma GCC diagnostic pop |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 527 | static inline UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 528 | StringAllocator_Allocate(const QCBORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 529 | { |
| 530 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 531 | } |
| 532 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 533 | static inline void |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 534 | StringAllocator_Destruct(const QCBORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 535 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 536 | /* See comment in StringAllocator_Free() */ |
| 537 | #pragma GCC diagnostic push |
| 538 | #pragma GCC diagnostic ignored "-Wcast-qual" |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 539 | if(pMe->pfAllocator) { |
| 540 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 541 | } |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 542 | #pragma GCC diagnostic pop |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 543 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 544 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 545 | |
| 546 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 547 | |
| 548 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 549 | /*=========================================================================== |
| 550 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 551 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 552 | See qcbor/qcbor_decode.h for definition of the object |
| 553 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 554 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 555 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 556 | * Public function, see header file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 557 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 558 | void QCBORDecode_Init(QCBORDecodeContext *pMe, |
| 559 | UsefulBufC EncodedCBOR, |
| 560 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 561 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 562 | memset(pMe, 0, sizeof(QCBORDecodeContext)); |
| 563 | UsefulInputBuf_Init(&(pMe->InBuf), EncodedCBOR); |
| 564 | /* Don't bother with error check on decode mode. If a bad value is |
| 565 | * passed it will just act as if the default normal mode of 0 was set. |
| 566 | */ |
| 567 | pMe->uDecodeMode = (uint8_t)nDecodeMode; |
| 568 | DecodeNesting_Init(&(pMe->nesting)); |
| 569 | |
| 570 | /* Inialize me->auMappedTags to CBOR_TAG_INVALID16. See |
| 571 | * GetNext_TaggedItem() and MapTagNumber(). */ |
| 572 | memset(pMe->auMappedTags, 0xff, sizeof(pMe->auMappedTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 576 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 577 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 578 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 579 | * Public function, see header file |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 580 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 581 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 582 | QCBORStringAllocate pfAllocateFunction, |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 583 | void *pAllocateContext, |
| 584 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 585 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 586 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 587 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 588 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 589 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 590 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 591 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 592 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 593 | |
| 594 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 595 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 596 | * Deprecated public function, see header file |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 597 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 598 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 599 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 600 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 601 | /* This does nothing now. It is retained for backwards compatibility */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 602 | (void)pMe; |
| 603 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 604 | } |
| 605 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 606 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 607 | |
| 608 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 609 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 610 | * Decoding items is done in six layers, one calling the next one |
| 611 | * down. If a layer has no work to do for a particular item, it |
| 612 | * returns quickly. |
| 613 | * |
| 614 | * 1. QCBORDecode_GetNextTagContent - The top layer processes tagged |
| 615 | * data items, turning them into the local C representation. For the |
| 616 | * most simple it is just associating a QCBOR_TYPE with the data. For |
| 617 | * the complex ones that an aggregate of data items, there is some |
| 618 | * further decoding and some limited recursion. |
| 619 | * |
| 620 | * 2. QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
| 621 | * ends of maps and arrays. It tracks descending into and ascending |
| 622 | * out of maps/arrays. It processes breaks that terminate |
| 623 | * indefinite-length maps and arrays. |
| 624 | * |
| 625 | * 3. QCBORDecode_GetNextMapEntry - This handles the combining of two |
| 626 | * items, the label and the data, that make up a map entry. It only |
| 627 | * does work on maps. It combines the label and data items into one |
| 628 | * labeled item. |
| 629 | * |
| 630 | * 4. QCBORDecode_GetNextTagNumber - This decodes type 6 tag |
| 631 | * numbers. It turns the tag numbers into bit flags associated with |
| 632 | * the data item. No actual decoding of the contents of the tag is |
| 633 | * performed here. |
| 634 | * |
| 635 | * 5. QCBORDecode_GetNextFullString - This assembles the sub-items |
| 636 | * that make up an indefinite-length string into one string item. It |
| 637 | * uses the string allocator to create contiguous space for the |
| 638 | * item. It processes all breaks that are part of indefinite-length |
| 639 | * strings. |
| 640 | * |
| 641 | * 6. DecodeAtomicDataItem - This decodes the atomic data items in |
| 642 | * CBOR. Each atomic data item has a "major type", an integer |
| 643 | * "argument" and optionally some content. For text and byte strings, |
| 644 | * the content is the bytes that make up the string. These are the |
| 645 | * smallest data items that are considered to be well-formed. The |
| 646 | * content may also be other data items in the case of aggregate |
| 647 | * types. They are not handled in this layer. |
| 648 | * |
| 649 | * Roughly this takes 300 bytes of stack for vars. TODO: evaluate this |
| 650 | * more carefully and correctly. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 651 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 652 | |
| 653 | |
| 654 | /* |
| 655 | * Note about use of int and unsigned variables. |
| 656 | * |
| 657 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 658 | * used carefully here, and in particular why it isn't used in the |
| 659 | * public interface. Also see |
| 660 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 661 | * |
| 662 | * Int is used for values that need less than 16-bits and would be |
| 663 | * subject to integer promotion and result in complaining from static |
| 664 | * analyzers. |
| 665 | */ |
| 666 | |
| 667 | |
| 668 | /** |
| 669 | * @brief Decode the CBOR head, the type and argument. |
| 670 | * |
| 671 | * @param[in] pUInBuf The input buffer to read from. |
| 672 | * @param[out] pnMajorType The decoded major type. |
| 673 | * @param[out] puArgument The decoded argument. |
| 674 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 675 | * |
| 676 | * @retval QCBOR_ERR_UNSUPPORTED |
| 677 | * @retval QCBOR_ERR_HIT_END |
| 678 | * |
| 679 | * This decodes the CBOR "head" that every CBOR data item has. See |
| 680 | * longer explaination of the head in documentation for |
| 681 | * QCBOREncode_EncodeHead(). |
| 682 | * |
| 683 | * This does the network->host byte order conversion. The conversion |
| 684 | * here also results in the conversion for floats in addition to that |
| 685 | * for lengths, tags and integer values. |
| 686 | * |
| 687 | * The int type is preferred to uint8_t for some variables as this |
| 688 | * avoids integer promotions, can reduce code size and makes static |
| 689 | * analyzers happier. |
| 690 | */ |
| 691 | static inline QCBORError |
| 692 | DecodeHead(UsefulInputBuf *pUInBuf, |
| 693 | int *pnMajorType, |
| 694 | uint64_t *puArgument, |
| 695 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 696 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 697 | QCBORError uReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 698 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 699 | /* Get the initial byte that every CBOR data item has and break it |
| 700 | * down. */ |
| 701 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 702 | const int nTmpMajorType = nInitialByte >> 5; |
| 703 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 704 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 705 | /* Where the argument accumulates */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 706 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 707 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 708 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 709 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 710 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 711 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 712 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 713 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 714 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 715 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 716 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 717 | /* This shift and add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 718 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 719 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 720 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 721 | /* The reserved and thus-far unused additional info values */ |
| 722 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 723 | goto Done; |
| 724 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 725 | /* Less than 24, additional info is argument or 31, an |
| 726 | * indefinite-length. No more bytes to get. |
| 727 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 728 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 729 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 730 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 731 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 732 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 733 | goto Done; |
| 734 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 735 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 736 | /* All successful if arrived here. */ |
| 737 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 738 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 739 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 740 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 741 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 742 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 743 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 746 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 747 | /** |
| 748 | * @brief Decode integer types, major types 0 and 1. |
| 749 | * |
| 750 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 751 | * @param[in] uArgument The argument from the head. |
| 752 | * @param[out] pDecodedItem The filled in decoded item. |
| 753 | * |
| 754 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 755 | * |
| 756 | * Must only be called when major type is 0 or 1. |
| 757 | * |
| 758 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 759 | * all CPUs use it these days and the test vectors in the RFC are |
| 760 | * so. All integers in the CBOR structure are positive and the major |
| 761 | * type indicates positive or negative. CBOR can express positive |
| 762 | * integers up to 2^x - 1 where x is the number of bits and negative |
| 763 | * integers down to 2^x. Note that negative numbers can be one more |
| 764 | * away from zero than positive. Stdint, as far as I can tell, uses |
| 765 | * two's compliment to represent negative integers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 766 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 767 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 768 | DecodeInteger(int nMajorType, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 769 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 770 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 771 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 772 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 773 | if (uArgument <= INT64_MAX) { |
| 774 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 775 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 776 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 777 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 778 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 779 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 780 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 781 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 782 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 783 | if(uArgument <= INT64_MAX) { |
| 784 | /* CBOR's representation of negative numbers lines up with |
| 785 | * the two-compliment representation. A negative integer has |
| 786 | * one more in range than a positive integer. INT64_MIN is |
| 787 | * equal to (-INT64_MAX) - 1. |
| 788 | */ |
| 789 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 790 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 791 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 792 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 793 | /* C can't represent a negative integer in this range so it |
| 794 | * is an error. |
| 795 | */ |
| 796 | uReturn = QCBOR_ERR_INT_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 797 | } |
| 798 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 799 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 800 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 803 | |
| 804 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 805 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 806 | #error QCBOR_TYPE_FALSE macro value wrong |
| 807 | #endif |
| 808 | |
| 809 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 810 | #error QCBOR_TYPE_TRUE macro value wrong |
| 811 | #endif |
| 812 | |
| 813 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 814 | #error QCBOR_TYPE_NULL macro value wrong |
| 815 | #endif |
| 816 | |
| 817 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 818 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 819 | #endif |
| 820 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 821 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 822 | #error QCBOR_TYPE_BREAK macro value wrong |
| 823 | #endif |
| 824 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 825 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 826 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 827 | #endif |
| 828 | |
| 829 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 830 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 831 | #endif |
| 832 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 833 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 834 | /** |
| 835 | * @brief Decode major type 7 -- true, false, floating-point, break... |
| 836 | * |
| 837 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 838 | * @param[in] uArgument The argument from the head. |
| 839 | * @param[out] pDecodedItem The filled in decoded item. |
| 840 | * |
| 841 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 842 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 843 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 844 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 845 | static inline QCBORError |
| 846 | DecodeType7(int nAdditionalInfo, uint64_t uArgument, QCBORItem *pDecodedItem) |
| 847 | { |
| 848 | QCBORError uReturn = QCBOR_SUCCESS; |
| 849 | |
| 850 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 851 | * checks above make sure uAdditionalInfo values line up with |
| 852 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 853 | * > 0x1f so cast is safe. |
| 854 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 855 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 856 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 857 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 858 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 859 | * are caught before this is called. |
| 860 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 861 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 862 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 863 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 864 | /* Half-precision is returned as a double. The cast to |
| 865 | * uint16_t is safe because the encoded value was 16 bits. It |
| 866 | * was widened to 64 bits to be passed in here. |
| 867 | */ |
| 868 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 869 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 870 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 871 | uReturn = QCBOR_ERR_HALF_PRECISION_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 872 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 873 | break; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 874 | case SINGLE_PREC_FLOAT: /* 26 */ |
| 875 | /* Single precision is normally returned as a double since |
| 876 | * double is widely supported, there is no loss of precision, |
| 877 | * it makes it easy for the caller in most cases and it can |
| 878 | * be converted back to single with no loss of precision |
| 879 | * |
| 880 | * The cast to uint32_t is safe because the encoded value was |
| 881 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 882 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 883 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 884 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 885 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 886 | /* In the normal case, use HW to convert float to |
| 887 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 888 | pDecodedItem->val.dfnum = (double)f; |
| 889 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 890 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 891 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 892 | pDecodedItem->val.fnum = f; |
| 893 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 894 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 895 | /* IEEE754_FloatToDouble() could be used here to return as |
| 896 | * a double, but it adds object code and most likely |
| 897 | * anyone disabling FLOAT HW use doesn't care about floats |
| 898 | * and wants to save object code. |
| 899 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 900 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 901 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 902 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 903 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 904 | case DOUBLE_PREC_FLOAT: /* 27 */ |
| 905 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 906 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 907 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 908 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 909 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 910 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 911 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 912 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 913 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 914 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 915 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 916 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 917 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 918 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 919 | * as e0 … f7 |
| 920 | */ |
| 921 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 922 | goto Done; |
| 923 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 924 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 925 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 926 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 927 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 928 | /* DecodeHead() will make uArgument equal to |
| 929 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 930 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 931 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 932 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 933 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 934 | break; |
| 935 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 936 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 937 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 938 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 942 | /** |
| 943 | * @brief Decode text and byte strings |
| 944 | * |
| 945 | * @param[in] pAllocator The string allocator or NULL. |
| 946 | * @param[in] uStrLen The length of the string. |
| 947 | * @param[in] pUInBuf The surce from which to read the string's bytes. |
| 948 | * @param[out] pDecodedItem The filled in decoded item. |
| 949 | * |
| 950 | * @retval QCBOR_ERR_HIT_END |
| 951 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 952 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 953 | * |
| 954 | * The reads @c uStrlen bytes from @c pUInBuf and fills in @c |
| 955 | * pDecodedItem. If @c pAllocator is not NULL then memory for the |
| 956 | * string is allocated. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 957 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 958 | static inline QCBORError |
| 959 | DecodeBytes(const QCBORInternalAllocator *pAllocator, |
| 960 | uint64_t uStrLen, |
| 961 | UsefulInputBuf *pUInBuf, |
| 962 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 963 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 964 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 965 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 966 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 967 | * CPUs. This check makes the casts to size_t below safe. |
| 968 | * |
| 969 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 970 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 971 | * one will care the limit on strings is 4 bytes shorter). |
| 972 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 973 | if(uStrLen > SIZE_MAX-4) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 974 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 975 | goto Done; |
| 976 | } |
| 977 | |
| 978 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 979 | if(UsefulBuf_IsNULLC(Bytes)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 980 | /* Failed to get the bytes for this string item */ |
| 981 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 982 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 983 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 984 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 985 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 986 | /* Note that this is not where allocation to coalesce |
| 987 | * indefinite-length strings is done. This is for when the caller |
| 988 | * has requested all strings be allocated. Disabling indefinite |
| 989 | * length strings also disables this allocate-all option. |
| 990 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 991 | if(pAllocator) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 992 | /* request to use the string allocator to make a copy */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 993 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 994 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 995 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 996 | goto Done; |
| 997 | } |
| 998 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 999 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1000 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1001 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1002 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1003 | (void)pAllocator; |
| 1004 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1005 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1006 | /* Normal case with no string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1007 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1008 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1009 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1010 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1013 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1014 | /** |
| 1015 | * @brief Map the CBOR major types for strings to the QCBOR types. |
| 1016 | * |
| 1017 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1018 | * @retturns QCBOR type number. |
| 1019 | * |
| 1020 | * This only works for the two string types. |
| 1021 | */ |
| 1022 | static inline uint8_t ConvertStringMajorTypes(int nCBORMajorType) |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1023 | { |
| 1024 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 1025 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 1026 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1027 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1028 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 1029 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 1030 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1031 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1032 | return (uint8_t)(nCBORMajorType + 4); |
| 1033 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1034 | |
| 1035 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1036 | /** |
| 1037 | * @brief Map the CBOR major types for arrays/maps to the QCBOR types. |
| 1038 | * |
| 1039 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1040 | * @retturns QCBOR type number. |
| 1041 | * |
| 1042 | * This only works for the two aggregate types. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1043 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1044 | static inline uint8_t ConvertArrayOrMapType(int nCBORMajorType) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1045 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1046 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1047 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1048 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1049 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1050 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1051 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1052 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1053 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1054 | return (uint8_t)(nCBORMajorType); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1058 | /** |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1059 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1060 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1061 | * @param[in] pUInBuf Input buffer to read data item from. |
| 1062 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1063 | * @param[in] pAllocator The allocator to use for strings or NULL. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1064 | * |
| 1065 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1066 | * @retval QCBOR_ERR_HIT_END |
| 1067 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1068 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1069 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1070 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1071 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1072 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1073 | * |
| 1074 | * This decodes the most primitive / atomic data item. It does |
| 1075 | * no combing of data items. |
| 1076 | */ |
| 1077 | static QCBORError |
| 1078 | DecodeAtomicDataItem(UsefulInputBuf *pUInBuf, |
| 1079 | QCBORItem *pDecodedItem, |
| 1080 | const QCBORInternalAllocator *pAllocator) |
| 1081 | { |
| 1082 | QCBORError uReturn; |
| 1083 | |
| 1084 | /* Get the major type and the argument. The argument could be |
| 1085 | * length of more bytes or the value depending on the major |
| 1086 | * type. nAdditionalInfo is an encoding of the length of the |
| 1087 | * uNumber and is needed to decode floats and doubles. |
| 1088 | */ |
| 1089 | int nMajorType = 0; |
| 1090 | uint64_t uArgument = 0; |
| 1091 | int nAdditionalInfo = 0; |
| 1092 | |
| 1093 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1094 | |
| 1095 | uReturn = DecodeHead(pUInBuf, &nMajorType, &uArgument, &nAdditionalInfo); |
| 1096 | if(uReturn) { |
| 1097 | goto Done; |
| 1098 | } |
| 1099 | |
| 1100 | /* At this point the major type and the argument are valid. We've |
| 1101 | * got the type and the argument that starts every CBOR data item. |
| 1102 | */ |
| 1103 | switch (nMajorType) { |
| 1104 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1105 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
| 1106 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1107 | uReturn = QCBOR_ERR_BAD_INT; |
| 1108 | } else { |
| 1109 | uReturn = DecodeInteger(nMajorType, uArgument, pDecodedItem); |
| 1110 | } |
| 1111 | break; |
| 1112 | |
| 1113 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1114 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
| 1115 | pDecodedItem->uDataType = ConvertStringMajorTypes(nMajorType); |
| 1116 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1117 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
| 1118 | } else { |
| 1119 | uReturn = DecodeBytes(pAllocator, uArgument, pUInBuf, pDecodedItem); |
| 1120 | } |
| 1121 | break; |
| 1122 | |
| 1123 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1124 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
| 1125 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1126 | /* Indefinite-length string. */ |
| 1127 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 1128 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
| 1129 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1130 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
| 1131 | break; |
| 1132 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1133 | } else { |
| 1134 | /* Definite-length string. */ |
| 1135 | if(uArgument > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1136 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1137 | goto Done; |
| 1138 | } |
| 1139 | /* cast OK because of check above */ |
| 1140 | pDecodedItem->val.uCount = (uint16_t)uArgument; |
| 1141 | } |
| 1142 | pDecodedItem->uDataType = ConvertArrayOrMapType(nMajorType); |
| 1143 | break; |
| 1144 | |
| 1145 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
| 1146 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1147 | uReturn = QCBOR_ERR_BAD_INT; |
| 1148 | } else { |
| 1149 | pDecodedItem->val.uTagV = uArgument; |
| 1150 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
| 1151 | } |
| 1152 | break; |
| 1153 | |
| 1154 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1155 | /* Major type 7: float, double, true, false, null... */ |
| 1156 | uReturn = DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
| 1157 | break; |
| 1158 | |
| 1159 | default: |
| 1160 | /* Never happens because DecodeHead() should never return > 7 */ |
| 1161 | uReturn = QCBOR_ERR_UNSUPPORTED; |
| 1162 | break; |
| 1163 | } |
| 1164 | |
| 1165 | Done: |
| 1166 | return uReturn; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /** |
| 1171 | * @brief Process indefinite-length strings (decode layer 5). |
| 1172 | * |
| 1173 | * @param[in] pMe Decoder context |
| 1174 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1175 | * |
| 1176 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1177 | * @retval QCBOR_ERR_HIT_END |
| 1178 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1179 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1180 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1181 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1182 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1183 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1184 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1185 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1186 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1187 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1188 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1189 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1190 | * If it is, this loops getting the subsequent chunk data items that |
| 1191 | * make up the string. The string allocator is used to make a |
| 1192 | * contiguous buffer for the chunks. When this completes @c |
| 1193 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1194 | * |
| 1195 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1196 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1197 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1198 | QCBORDecode_GetNextFullString(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1199 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1200 | /* Aproximate stack usage |
| 1201 | * 64-bit 32-bit |
| 1202 | * local vars 32 16 |
| 1203 | * 2 UsefulBufs 32 16 |
| 1204 | * QCBORItem 56 52 |
| 1205 | * TOTAL 120 74 |
| 1206 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1207 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1208 | /* The string allocator is used here for two purposes: 1) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1209 | * coalescing the chunks of an indefinite-length string, 2) |
| 1210 | * allocating storage for every string returned when requested. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1211 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1212 | * The first use is below in this function. Indefinite-length |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1213 | * strings cannot be processed at all without a string allocator. |
| 1214 | * |
| 1215 | * The second used is in DecodeBytes() which is called by |
| 1216 | * GetNext_Item() below. This second use unneccessary for most use |
| 1217 | * and only happens when requested in the call to |
| 1218 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1219 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1220 | * |
| 1221 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1222 | * allocator altogether and thus both of these uses. It reduced the |
| 1223 | * decoder object code by about 400 bytes. |
| 1224 | */ |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1225 | const QCBORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1226 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1227 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1228 | const QCBORInternalAllocator *pAllocator = NULL; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1229 | |
| 1230 | if(pMe->StringAllocator.pfAllocator) { |
| 1231 | pAllocator = &(pMe->StringAllocator); |
| 1232 | if(pMe->bStringAllocateAll) { |
| 1233 | pAllocatorForGetNext = pAllocator; |
| 1234 | } |
| 1235 | } |
| 1236 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1237 | |
| 1238 | QCBORError uReturn; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1239 | uReturn = DecodeAtomicDataItem(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1240 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1241 | goto Done; |
| 1242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1243 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1244 | /* Only do indefinite-length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1245 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1246 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1247 | goto Done; |
| 1248 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1249 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1250 | /* Is this a string with an indefinite length? */ |
| 1251 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1252 | goto Done; |
| 1253 | } |
| 1254 | |
| 1255 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1256 | /* Can't decode indefinite-length strings without a string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1257 | if(pAllocator == NULL) { |
| 1258 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1259 | goto Done; |
| 1260 | } |
| 1261 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1262 | /* Loop getting chunks of the indefinite-length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1263 | UsefulBufC FullString = NULLUsefulBufC; |
| 1264 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1265 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1266 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1267 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1268 | /* Pass a NULL string allocator to GetNext_Item() because the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1269 | * individual string chunks in an indefinite-length should not |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1270 | * be allocated. They are always copied in the the contiguous |
| 1271 | * buffer allocated here. |
| 1272 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1273 | uReturn = DecodeAtomicDataItem(&(pMe->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1274 | if(uReturn) { |
| 1275 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1276 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1277 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1278 | /* Is item is the marker for end of the indefinite-length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1279 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1280 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1281 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1282 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1283 | break; |
| 1284 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1285 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1286 | /* 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] | 1287 | * that introduces the indefinite-length string. This also |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1288 | * 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] | 1289 | * indefinite-length string inside an indefinite-length string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1290 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1291 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1292 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1293 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1294 | break; |
| 1295 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1296 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1297 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1298 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1299 | * not NULL and a reallocation happens. |
| 1300 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1301 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 1302 | FullString.ptr, |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1303 | FullString.len + StringChunkItem.val.string.len); |
| 1304 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1305 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1306 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1307 | break; |
| 1308 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1309 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1310 | /* Copy new string chunk to the end of accumulated string */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1311 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1312 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1313 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1314 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1315 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 1316 | StringAllocator_Free(pAllocator, FullString.ptr); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1317 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1318 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1319 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1320 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1321 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1322 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1323 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1326 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1327 | /** |
| 1328 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1329 | * |
| 1330 | * @param[in] pMe The decode context. |
| 1331 | * @param[in] uUnMappedTag The tag number to map |
| 1332 | * @param[out] puMappedTagNumer The stored tag number. |
| 1333 | * |
| 1334 | * @return error code. |
| 1335 | * |
| 1336 | * The main point of mapping tag numbers is make QCBORItem |
| 1337 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1338 | * bytes. Without, it would take up 32 bytes. |
| 1339 | * |
| 1340 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1341 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1342 | * |
| 1343 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1344 | */ |
| 1345 | static inline QCBORError |
| 1346 | MapTagNumber(QCBORDecodeContext *pMe, uint64_t uUnMappedTag, uint16_t *puMappedTagNumer) |
| 1347 | { |
| 1348 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1349 | unsigned uTagMapIndex; |
| 1350 | /* Is there room in the tag map, or is it in it already? */ |
| 1351 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1352 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1353 | break; |
| 1354 | } |
| 1355 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1356 | break; |
| 1357 | } |
| 1358 | } |
| 1359 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1360 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1361 | } |
| 1362 | |
| 1363 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1364 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1365 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1366 | |
| 1367 | } else { |
| 1368 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1369 | } |
| 1370 | |
| 1371 | return QCBOR_SUCCESS; |
| 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | /** |
| 1376 | * @brief This converts a mapped tag number to the actual tag number. |
| 1377 | * |
| 1378 | * @param[in] pMe The decode context. |
| 1379 | * @param[in] uMappedTagNumber The stored tag number. |
| 1380 | * |
| 1381 | * @return The actual tag number is returned or |
| 1382 | * @ref CBOR_TAG_INVALID64 on error. |
| 1383 | * |
| 1384 | * This is the reverse of MapTagNumber() |
| 1385 | */ |
| 1386 | static uint64_t |
| 1387 | UnMapTagNumber(const QCBORDecodeContext *pMe, uint16_t uMappedTagNumber) |
| 1388 | { |
| 1389 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1390 | return uMappedTagNumber; |
| 1391 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1392 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1393 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1394 | /* This won't be negative because of code below in |
| 1395 | * MapTagNumber() |
| 1396 | */ |
| 1397 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1398 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1402 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1403 | /** |
| 1404 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
| 1405 | * |
| 1406 | * @param[in] pMe Decoder context |
| 1407 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1408 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1409 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1410 | * @retval QCBOR_ERR_HIT_END |
| 1411 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1412 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1413 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1414 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1415 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1416 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1417 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1418 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1419 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1420 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1421 | * |
| 1422 | * This loops getting atomic data items until one is not a tag |
| 1423 | * number. Usually this is largely pass-through because most |
| 1424 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1425 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1426 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1427 | QCBORDecode_GetNextTagNumber(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1428 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1429 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1430 | |
| 1431 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1432 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1433 | /* Be sure the memset does the right thing. */ |
| 1434 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1435 | #endif |
| 1436 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1437 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1438 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1439 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1440 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1441 | for(;;) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1442 | QCBORError uErr = QCBORDecode_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1443 | if(uErr != QCBOR_SUCCESS) { |
| 1444 | uReturn = uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1445 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1446 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1447 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1448 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1449 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1450 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1451 | break; |
| 1452 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1453 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1454 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1455 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1456 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1457 | /* Continue on to get all tags wrapping this item even though |
| 1458 | * it is erroring out in the end. This allows decoding to |
| 1459 | * continue. This is a resource limit error, not a problem |
| 1460 | * with being well-formed CBOR. |
| 1461 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1462 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1463 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1464 | /* Slide tags over one in the array to make room at index 0. |
| 1465 | * Must use memmove because the move source and destination |
| 1466 | * overlap. |
| 1467 | */ |
| 1468 | memmove(&auItemsTags[1], auItemsTags, sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1469 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1470 | /* Map the tag */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 1471 | uint16_t uMappedTagNumer = 0; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1472 | uReturn = MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumer); |
| 1473 | /* Continue even on error so as to consume all tags wrapping |
| 1474 | * this data item so decoding can go on. If MapTagNumber() |
| 1475 | * errors once it will continue to error. |
| 1476 | */ |
| 1477 | auItemsTags[0] = uMappedTagNumer; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1478 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1479 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1480 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1481 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1485 | /** |
| 1486 | * @brief Combine a map entry label and value into one item (decode layer 3). |
| 1487 | * |
| 1488 | * @param[in] pMe Decoder context |
| 1489 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1490 | * |
| 1491 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1492 | * @retval QCBOR_ERR_HIT_END |
| 1493 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1494 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1495 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1496 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1497 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1498 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1499 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1500 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1501 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1502 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1503 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1504 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1505 | * |
| 1506 | * If a the current nesting level is a map, then this |
| 1507 | * combines pairs of items into one data item with a label |
| 1508 | * and value. |
| 1509 | * |
| 1510 | * This is pass-through if the current nesting leve is |
| 1511 | * not a map. |
| 1512 | * |
| 1513 | * This also implements maps-as-array mode where a map |
| 1514 | * is treated like an array to allow caller to do their |
| 1515 | * own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1516 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1517 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1518 | QCBORDecode_GetNextMapEntry(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1519 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1520 | QCBORError uReturn = QCBORDecode_GetNextTagNumber(pMe, pDecodedItem); |
| 1521 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1522 | goto Done; |
| 1523 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1524 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1525 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
| 1526 | /* Break can't be a map entry */ |
| 1527 | goto Done; |
| 1528 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1529 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1530 | if(pMe->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1531 | /* Normal decoding of maps -- combine label and value into one item. */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1532 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1533 | if(DecodeNesting_IsCurrentTypeMap(&(pMe->nesting))) { |
| 1534 | /* Save label in pDecodedItem and get the next which will |
| 1535 | * be the real data item. |
| 1536 | */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1537 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1538 | uReturn = QCBORDecode_GetNextTagNumber(pMe, pDecodedItem); |
| 1539 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1540 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1541 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1542 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1543 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1544 | |
| 1545 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1546 | /* strings are always good labels */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1547 | pDecodedItem->label.string = LabelItem.val.string; |
| 1548 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1549 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == pMe->uDecodeMode) { |
| 1550 | /* It's not a string and we only want strings */ |
| 1551 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1552 | goto Done; |
| 1553 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1554 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1555 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1556 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1557 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1558 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1559 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1560 | pDecodedItem->label.string = LabelItem.val.string; |
| 1561 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1562 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1563 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1564 | /* label is not an int or a string. It is an arrray |
| 1565 | * or a float or such and this implementation doesn't handle that. |
| 1566 | * Also, tags on labels are ignored. |
| 1567 | */ |
| 1568 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1569 | goto Done; |
| 1570 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1571 | } |
| 1572 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1573 | /* Decoding of maps as arrays to let the caller decide what to do |
| 1574 | * about labels, particularly lables that are not integers or |
| 1575 | * strings. |
| 1576 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1577 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1578 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1579 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1580 | goto Done; |
| 1581 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1582 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1583 | /* Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2. |
| 1584 | * Cast is needed because of integer promotion. |
| 1585 | */ |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1586 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1587 | } |
| 1588 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1589 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1590 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1591 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1595 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1596 | /** |
| 1597 | * @brief Peek and see if next data item is a break; |
| 1598 | * |
| 1599 | * @param[in] pUIB UsefulInputBuf to read from. |
| 1600 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1601 | * |
| 1602 | * @return Any decoding error. |
| 1603 | * |
| 1604 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1605 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1606 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1607 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1608 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1609 | { |
| 1610 | *pbNextIsBreak = false; |
| 1611 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1612 | QCBORItem Peek; |
| 1613 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1614 | QCBORError uReturn = DecodeAtomicDataItem(pUIB, &Peek, NULL); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1615 | if(uReturn != QCBOR_SUCCESS) { |
| 1616 | return uReturn; |
| 1617 | } |
| 1618 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1619 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1620 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1621 | } else { |
| 1622 | *pbNextIsBreak = true; |
| 1623 | } |
| 1624 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1625 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1626 | return QCBOR_SUCCESS; |
| 1627 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1628 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1629 | |
| 1630 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1631 | /** |
| 1632 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1633 | * |
| 1634 | * @param[in] pMe The decode context. |
| 1635 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
| 1636 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1637 | * An item was just consumed, now figure out if it was the |
| 1638 | * end of an array/map map that can be closed out. That |
| 1639 | * may in turn close out the above array/map... |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1640 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1641 | static QCBORError |
| 1642 | QCBORDecode_NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1643 | { |
| 1644 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1645 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1646 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1647 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1648 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1649 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1650 | /* Nesting level is bstr-wrapped CBOR */ |
| 1651 | |
| 1652 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1653 | * so no further ascending can happen. |
| 1654 | */ |
| 1655 | break; |
| 1656 | |
| 1657 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1658 | /* Level is a definite-length array/map */ |
| 1659 | |
| 1660 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1661 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1662 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1663 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1664 | break; |
| 1665 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1666 | /* All items in a definite-length array were consumed so it |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1667 | * is time to ascend one level. This happens below. |
| 1668 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1669 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1670 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1671 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1672 | /* Level is an indefinite-length array/map. */ |
| 1673 | |
| 1674 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1675 | bool bIsBreak = false; |
| 1676 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1677 | if(uReturn != QCBOR_SUCCESS) { |
| 1678 | goto Done; |
| 1679 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1680 | |
| 1681 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1682 | /* 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] | 1683 | break; |
| 1684 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1685 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1686 | /* It was a break in an indefinitelength map / array so |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1687 | * it is time to ascend one level. |
| 1688 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1689 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1690 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1691 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1692 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1693 | |
| 1694 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1695 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1696 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1697 | * QCBORDecode_ExitBoundedMode(). |
| 1698 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1699 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1700 | /* Set the count to zero for definite-length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1701 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1702 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1703 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1704 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1705 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1706 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1707 | break; |
| 1708 | } |
| 1709 | |
| 1710 | /* Finally, actually ascend one level. */ |
| 1711 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1712 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1713 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1714 | uReturn = QCBOR_SUCCESS; |
| 1715 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1716 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1717 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1718 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1719 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1720 | return uReturn; |
| 1721 | } |
| 1722 | |
| 1723 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1724 | /** |
| 1725 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
| 1726 | * |
| 1727 | * @param[in] pMe Decoder context |
| 1728 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1729 | * |
| 1730 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1731 | * @retval QCBOR_ERR_HIT_END |
| 1732 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1733 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1734 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1735 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1736 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1737 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1738 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1739 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1740 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1741 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1742 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1743 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1744 | * @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1745 | * @retval QCBOR_ERR_BAD_BREAK |
| 1746 | * @retval QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
| 1747 | * |
| 1748 | * This handles the traversal descending into and asecnding out of |
| 1749 | * maps, arrays and bstr-wrapped CBOR. It figures out the ends of |
| 1750 | * definite- and indefinte-length maps and arrays by looking at the |
| 1751 | * item count or finding CBOR breaks. It detects the ends of the |
| 1752 | * top-level sequence and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1753 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1754 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1755 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1756 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1757 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1758 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1759 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1760 | /* If out of bytes to consume, it is either the end of the |
| 1761 | * top-level sequence of some bstr-wrapped CBOR that was entered. |
| 1762 | * |
| 1763 | * In the case of bstr-wrapped CBOR, the length of the |
| 1764 | * UsefulInputBuf was set to that of the bstr-wrapped CBOR. When |
| 1765 | * the bstr-wrapped CBOR is exited, the length is set back to the |
| 1766 | * top-level's length or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1767 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1768 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1769 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1770 | goto Done; |
| 1771 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1772 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1773 | /* Check to see if at the end of a bounded definite-length map or |
| 1774 | * array. The check for a break ending indefinite-length array is |
| 1775 | * later in QCBORDecode_NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1776 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1777 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1778 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1779 | goto Done; |
| 1780 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1781 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1782 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1783 | uReturn = QCBORDecode_GetNextMapEntry(pMe, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1784 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1785 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1786 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1787 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1788 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1789 | /* Breaks ending arrays/maps are processed later in the call to |
| 1790 | * QCBORDecode_NestLevelAscender(). They should never show up here. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1791 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1792 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1793 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1794 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1795 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1796 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1797 | /* Record the nesting level for this data item before processing |
| 1798 | * any of decrementing and descending. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1799 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1800 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1801 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1802 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1803 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1804 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1805 | /* If the new item is a map or array, descend. |
| 1806 | * |
| 1807 | * Empty indefinite-length maps and arrays are descended into, |
| 1808 | * but then ascended out of in the next chunk of code. |
| 1809 | * |
| 1810 | * Maps and arrays do count as items in the map/array that |
| 1811 | * encloses them so a decrement needs to be done for them too, |
| 1812 | * but that is done only when all the items in them have been |
| 1813 | * processed, not when they are opened with the exception of an |
| 1814 | * empty map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1815 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1816 | QCBORError uDescendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1817 | uDescendErr = DecodeNesting_DescendMapOrArray(&(pMe->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1818 | pDecodedItem->uDataType, |
| 1819 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1820 | if(uDescendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1821 | /* This error is probably a traversal error and it overrides |
| 1822 | * the non-traversal error. |
| 1823 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1824 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1825 | goto Done; |
| 1826 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1829 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1830 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1831 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1832 | /* The following cases are handled here: |
| 1833 | * - A non-aggregate item like an integer or string |
| 1834 | * - An empty definite-length map or array |
| 1835 | * - An indefinite-length map or array that might be empty or might not. |
| 1836 | * |
| 1837 | * QCBORDecode_NestLevelAscender() does the work of decrementing the count |
| 1838 | * for an definite-length map/array and break detection for an |
| 1839 | * indefinite-0length map/array. If the end of the map/array was |
| 1840 | * reached, then it ascends nesting levels, possibly all the way |
| 1841 | * to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1842 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1843 | QCBORError uAscendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1844 | uAscendErr = QCBORDecode_NestLevelAscender(pMe, true); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1845 | if(uAscendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1846 | /* This error is probably a traversal error and it overrides |
| 1847 | * the non-traversal error. |
| 1848 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1849 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1850 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1851 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1852 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1853 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1854 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1855 | /* Tell the caller what level is next. This tells them what |
| 1856 | * maps/arrays were closed out and makes it possible for them to |
| 1857 | * reconstruct the tree with just the information returned in a |
| 1858 | * QCBORItem. |
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 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1861 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1862 | pDecodedItem->uNextNestLevel = 0; |
| 1863 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1864 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1865 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1866 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1867 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1868 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1871 | |
| 1872 | /** |
| 1873 | * @brief Shift 0th tag out of the tag list. |
| 1874 | * |
| 1875 | * pDecodedItem[in,out] The data item to convert. |
| 1876 | * |
| 1877 | * The 0th tag is discarded. \ref CBOR_TAG_INVALID16 is |
| 1878 | * shifted into empty slot at the end of the tag list. |
| 1879 | */ |
| 1880 | static inline void ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1881 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1882 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 1883 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 1884 | } |
| 1885 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1888 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1889 | /** |
| 1890 | * @brief Convert different epoch date formats in to the QCBOR epoch date format |
| 1891 | * |
| 1892 | * pDecodedItem[in,out] The data item to convert. |
| 1893 | * |
| 1894 | * @retval QCBOR_ERR_DATE_OVERFLOW |
| 1895 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED |
| 1896 | * @retval QCBOR_ERR_BAD_TAG_CONTENT |
| 1897 | * |
| 1898 | * The epoch date tag defined in QCBOR allows for floating-point |
| 1899 | * dates. It even allows a protocol to flop between date formats when |
| 1900 | * ever it wants. Floating-point dates aren't that useful as they are |
| 1901 | * only needed for dates beyond the age of the earth. |
| 1902 | * |
| 1903 | * This converts all the date formats into one format of an unsigned |
| 1904 | * integer plus a floating-point fraction. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1905 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1906 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1907 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1908 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1909 | |
| 1910 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1911 | |
| 1912 | switch (pDecodedItem->uDataType) { |
| 1913 | |
| 1914 | case QCBOR_TYPE_INT64: |
| 1915 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1916 | break; |
| 1917 | |
| 1918 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1919 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 1920 | * always an overflow. |
| 1921 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1922 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1923 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1924 | break; |
| 1925 | |
| 1926 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1927 | case QCBOR_TYPE_FLOAT: |
| 1928 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1929 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1930 | /* Convert working value to double if input was a float */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1931 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1932 | pDecodedItem->val.dfnum : |
| 1933 | (double)pDecodedItem->val.fnum; |
| 1934 | |
| 1935 | /* The conversion from float to integer requires overflow |
| 1936 | * detection since floats can be much larger than integers. |
| 1937 | * This implementation errors out on these large float values |
| 1938 | * since they are beyond the age of the earth. |
| 1939 | * |
| 1940 | * These constants for the overflow check are computed by the |
| 1941 | * compiler. They are not computed at run time. |
| 1942 | * |
| 1943 | * The factor of 0x7ff is added/subtracted to avoid a |
| 1944 | * rounding error in the wrong direction when the compiler |
| 1945 | * computes these constants. There is rounding because an |
| 1946 | * 64-bit integer has 63 bits of precision where a double |
| 1947 | * only has 53 bits. Without the 0x7ff factor, the compiler |
| 1948 | * may round up and produce a double for the bounds check |
| 1949 | * that is larger than can be stored in a 64-bit integer. The |
| 1950 | * amount of 0x7ff is picked because it has 11 bits set. |
| 1951 | * |
| 1952 | * Without the 0x7ff there is a ~30 minute range of time |
| 1953 | * values 10 billion years in the past and in the future |
| 1954 | * where this code could go wrong. Some compilers correctly |
| 1955 | * generate a warning or error without the 0x7ff. |
| 1956 | */ |
| 1957 | const double dDateMax = (double)(INT64_MAX - 0x7ff); |
| 1958 | const double dDateMin = (double)(INT64_MIN + 0x7ff); |
| 1959 | |
| 1960 | if(isnan(d) || d > dDateMax || d < dDateMin) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1961 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1962 | goto Done; |
| 1963 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1964 | |
| 1965 | /* The actual conversion */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1966 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1967 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 1968 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1969 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1970 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1971 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1972 | uReturn = QCBOR_ERR_FLOAT_DATE_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1973 | goto Done; |
| 1974 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1975 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1976 | break; |
| 1977 | |
| 1978 | default: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1979 | uReturn = QCBOR_ERR_BAD_TAG_CONTENT; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1980 | goto Done; |
| 1981 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1982 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1983 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1984 | |
| 1985 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1986 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 1990 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1991 | /** |
| 1992 | * @brief Decode decimal fractions and big floats. |
| 1993 | * |
| 1994 | * @param[in] pMe The decode context. |
| 1995 | * @param[in,out] pDecodedItem On input the array data item that |
| 1996 | * holds the mantissa and exponent. On |
| 1997 | * output the decoded mantissa and |
| 1998 | * exponent. |
| 1999 | * |
| 2000 | * @returns Decoding errors from getting primitive data items or |
| 2001 | * \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA. |
| 2002 | * |
| 2003 | * When called pDecodedItem must be the array that is tagged as a big |
| 2004 | * float or decimal fraction, the array that has the two members, the |
| 2005 | * exponent and mantissa. |
| 2006 | * |
| 2007 | * This will fetch and decode the exponent and mantissa and put the |
| 2008 | * result back into pDecodedItem. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2009 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2010 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2011 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2012 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2013 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2014 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2015 | /* --- Make sure it is an array; track nesting level of members --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2016 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2017 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2018 | goto Done; |
| 2019 | } |
| 2020 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2021 | /* A check for pDecodedItem->val.uCount == 2 would work for |
| 2022 | * definite-length arrays, but not for indefnite. Instead remember |
| 2023 | * the nesting level the two integers must be at, which is one |
| 2024 | * deeper than that of the array. |
| 2025 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2026 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 2027 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2028 | /* --- Which is it, decimal fraction or a bigfloat? --- */ |
| 2029 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(pMe, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2030 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 2031 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2032 | /* --- Get the exponent --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2033 | QCBORItem exponentItem; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2034 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, &exponentItem); |
| 2035 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2036 | goto Done; |
| 2037 | } |
| 2038 | if(exponentItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2039 | /* Array is empty or a map/array encountered when expecting an int */ |
| 2040 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2041 | goto Done; |
| 2042 | } |
| 2043 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2044 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2045 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2046 | * also means that the only data arriving here of type |
| 2047 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2048 | * and thus an error that will get handled in the next else. |
| 2049 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2050 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2051 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2052 | /* Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2053 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2054 | goto Done; |
| 2055 | } |
| 2056 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2057 | /* --- Get the mantissa --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2058 | QCBORItem mantissaItem; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2059 | uReturn = QCBORDecode_GetNextWithTags(pMe, &mantissaItem, NULL); |
| 2060 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2061 | goto Done; |
| 2062 | } |
| 2063 | if(mantissaItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2064 | /* Mantissa missing or map/array encountered when expecting number */ |
| 2065 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2066 | goto Done; |
| 2067 | } |
| 2068 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2069 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2070 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2071 | * also means that the only data arriving here of type |
| 2072 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2073 | * and thus an error that will get handled in an else below. |
| 2074 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2075 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2076 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2077 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2078 | /* Got a good big num mantissa */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2079 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2080 | /* Depends on numbering of QCBOR_TYPE_XXX */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2081 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 2082 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 2083 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2084 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2085 | /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2086 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2087 | goto Done; |
| 2088 | } |
| 2089 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2090 | /* --- Check that array only has the two numbers --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2091 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2092 | /* Extra items in the decimal fraction / big float */ |
| 2093 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2094 | goto Done; |
| 2095 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2096 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2097 | |
| 2098 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2099 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2100 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2101 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2102 | |
| 2103 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2104 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2105 | /** |
| 2106 | * @brief Decode the MIME type tag |
| 2107 | * |
| 2108 | * @param[in,out] pDecodedItem The item to decode. |
| 2109 | * |
| 2110 | * Handle the text and binary MIME type tags. Slightly too complicated |
| 2111 | * f or ProcessTaggedString() because the RFC 7049 MIME type was |
| 2112 | * incorreclty text-only. |
| 2113 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2114 | static inline QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2115 | { |
| 2116 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2117 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2118 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2119 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2120 | } else { |
| 2121 | return QCBOR_ERR_BAD_OPT_TAG; |
| 2122 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2123 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2124 | return QCBOR_SUCCESS; |
| 2125 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2126 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2127 | |
| 2128 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2129 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2130 | * Table of CBOR tags whose content is either a text string or a byte |
| 2131 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2132 | * of uQCBORtype indicates the content should be a byte string rather |
| 2133 | * than a text string |
| 2134 | */ |
| 2135 | struct StringTagMapEntry { |
| 2136 | uint16_t uTagNumber; |
| 2137 | uint8_t uQCBORtype; |
| 2138 | }; |
| 2139 | |
| 2140 | #define IS_BYTE_STRING_BIT 0x80 |
| 2141 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2142 | |
| 2143 | static const struct StringTagMapEntry StringTagMap[] = { |
| 2144 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
| 2145 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2146 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2147 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2148 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2149 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2150 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2151 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2152 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2153 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2154 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2155 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2156 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2157 | }; |
| 2158 | |
| 2159 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2160 | /** |
| 2161 | * @brief Process standard CBOR tags whose content is a string |
| 2162 | * |
| 2163 | * @param[in] uTag The tag. |
| 2164 | * @param[in,out] pDecodedItem The data item. |
| 2165 | * |
| 2166 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2167 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
| 2168 | * \ref QCBOR_ERR_BAD_OPT_TAG if the content type was wrong for the tag. |
| 2169 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2170 | * Process the CBOR tags that whose content is a byte string or a text |
| 2171 | * string and for which the string is just passed on to the caller. |
| 2172 | * |
| 2173 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2174 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2175 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2176 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2177 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2178 | static inline QCBORError |
| 2179 | ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2180 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2181 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2182 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2183 | return QCBOR_ERR_UNSUPPORTED; |
| 2184 | } |
| 2185 | |
| 2186 | unsigned uIndex; |
| 2187 | for(uIndex = 0; StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2188 | if(StringTagMap[uIndex].uTagNumber == uTag) { |
| 2189 | break; |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | const uint8_t uQCBORType = StringTagMap[uIndex].uQCBORtype; |
| 2194 | if(uQCBORType == QCBOR_TYPE_NONE) { |
| 2195 | /* repurpose this error to mean, not handled here */ |
| 2196 | return QCBOR_ERR_UNSUPPORTED; |
| 2197 | } |
| 2198 | |
| 2199 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2200 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2201 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2202 | } |
| 2203 | |
| 2204 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2205 | return QCBOR_ERR_BAD_OPT_TAG; |
| 2206 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2207 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2208 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2209 | return QCBOR_SUCCESS; |
| 2210 | } |
| 2211 | |
| 2212 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2213 | /** |
| 2214 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2215 | * |
| 2216 | * @param[in] pMe The decode context. |
| 2217 | * @param[out] pDecodedItem The decoded item. |
| 2218 | * |
| 2219 | * @return Decoding error code. |
| 2220 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2221 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2222 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2223 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2224 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2225 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2226 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2227 | QCBORDecode_GetNextTagContent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2228 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2229 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2230 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2231 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2232 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2233 | goto Done; |
| 2234 | } |
| 2235 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2236 | /* When there are no tag numbers for the item, this exits first |
| 2237 | * thing and effectively does nothing. |
| 2238 | * |
| 2239 | * This loops over all the tag numbers accumulated for this item |
| 2240 | * trying to decode and interpret them. This stops at the end of |
| 2241 | * the list or at the first tag number that can't be interpreted by |
| 2242 | * this code. This is effectively a recursive processing of the |
| 2243 | * tags number list that handles nested tags. |
| 2244 | */ |
| 2245 | while(1) { |
| 2246 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2247 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2248 | */ |
| 2249 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2250 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2251 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2252 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2253 | break; |
| 2254 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2255 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2256 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2257 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2258 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2259 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2260 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2261 | uReturn = QCBORDecode_MantissaAndExponent(pMe, pDecodedItem); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2262 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2263 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2264 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2265 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2266 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2267 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2268 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2269 | } else { |
| 2270 | /* See if it is a pass-through byte/text string tag; process if so */ |
| 2271 | uReturn = ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2272 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2273 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
| 2274 | /* It wasn't a pass-through byte/text string tag so it is |
| 2275 | * an unknown tag. This is the exit from the loop on the |
| 2276 | * first unknown tag. It is a successful exit. |
| 2277 | */ |
| 2278 | uReturn = QCBOR_SUCCESS; |
| 2279 | break; |
| 2280 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2281 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2282 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2283 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2284 | /* Error exit from the loop */ |
| 2285 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2286 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2287 | |
| 2288 | /* A tag was successfully processed, shift it out of the list of |
| 2289 | * tags returned. This is the loop increment. |
| 2290 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2291 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2295 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2299 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2300 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2301 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2302 | QCBORError |
| 2303 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2304 | { |
| 2305 | QCBORError uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2306 | uErr = QCBORDecode_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2307 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2308 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2309 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2310 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2311 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | |
| 2315 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2316 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2317 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2318 | QCBORError |
| 2319 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2320 | { |
| 2321 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2322 | const UsefulInputBuf Save = pMe->InBuf; |
| 2323 | |
| 2324 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2325 | |
| 2326 | pMe->nesting = SaveNesting; |
| 2327 | pMe->InBuf = Save; |
| 2328 | |
| 2329 | return uErr; |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2334 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2335 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2336 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2337 | { |
| 2338 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2339 | return; |
| 2340 | } |
| 2341 | |
| 2342 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2343 | } |
| 2344 | |
| 2345 | |
| 2346 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2347 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2348 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2349 | QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2350 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2351 | QCBORItem *pDecodedItem, |
| 2352 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2353 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2354 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2355 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2356 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2357 | if(uReturn != QCBOR_SUCCESS) { |
| 2358 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | if(pTags != NULL) { |
| 2362 | pTags->uNumUsed = 0; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2363 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2364 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2365 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2366 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2367 | } |
| 2368 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2369 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2370 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2371 | pTags->puTags[pTags->uNumUsed] = UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2372 | pTags->uNumUsed++; |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2380 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2381 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2382 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2383 | bool QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2384 | const QCBORItem *pItem, |
| 2385 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2386 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2387 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2388 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2389 | break; |
| 2390 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2391 | if(UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2392 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2393 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2394 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2395 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2396 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2401 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2402 | */ |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2403 | QCBORError QCBORDecode_PartialFinish(QCBORDecodeContext *pMe, size_t *puConsumed) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2404 | { |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2405 | if(puConsumed != NULL) { |
| 2406 | *puConsumed = pMe->InBuf.cursor; |
| 2407 | } |
| 2408 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2409 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2410 | |
| 2411 | if(uReturn != QCBOR_SUCCESS) { |
| 2412 | goto Done; |
| 2413 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2414 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2415 | /* Error out if all the maps/arrays are not closed out */ |
| 2416 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2417 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2418 | goto Done; |
| 2419 | } |
| 2420 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2421 | /* Error out if not all the bytes are consumed */ |
| 2422 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2423 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2424 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2425 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2426 | Done: |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2427 | return uReturn; |
| 2428 | } |
| 2429 | |
| 2430 | |
| 2431 | /* |
| 2432 | * Public function, see header qcbor/qcbor_decode.h file |
| 2433 | */ |
| 2434 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *pMe) |
| 2435 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2436 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2437 | /* Call the destructor for the string allocator if there is one. |
| 2438 | * Always called, even if there are errors; always have to clean up. |
| 2439 | */ |
| 2440 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2441 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2442 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2443 | return QCBORDecode_PartialFinish(pMe, NULL); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2447 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2448 | * Public function, see header qcbor/qcbor_decode.h file |
| 2449 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2450 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2451 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2452 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2453 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2454 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2455 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2456 | return CBOR_TAG_INVALID64; |
| 2457 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2458 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2459 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2460 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2461 | return UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2465 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2466 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2467 | * Public function, see header qcbor/qcbor_decode.h file |
| 2468 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2469 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2470 | uint32_t uIndex) |
| 2471 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2472 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2473 | return CBOR_TAG_INVALID64; |
| 2474 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2475 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2476 | return CBOR_TAG_INVALID64; |
| 2477 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2478 | return UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2479 | } |
| 2480 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2481 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2482 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2483 | |
| 2484 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2485 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2486 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2487 | /* =========================================================================== |
| 2488 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2489 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2490 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2491 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2492 | implements the function type QCBORStringAllocate and allows easy |
| 2493 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2494 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2495 | This particular allocator is built-in for convenience. The caller |
| 2496 | can implement their own. All of this following code will get |
| 2497 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2498 | |
| 2499 | This is a very primitive memory allocator. It does not track |
| 2500 | individual allocations, only a high-water mark. A free or |
| 2501 | reallocation must be of the last chunk allocated. |
| 2502 | |
| 2503 | The size of the pool and offset to free memory are packed into the |
| 2504 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2505 | the decode context. Since the address of the pool may not be |
| 2506 | aligned, they have to be packed and unpacked as if they were |
| 2507 | serialized data of the wire or such. |
| 2508 | |
| 2509 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2510 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2511 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2512 | |
| 2513 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2514 | static inline int |
| 2515 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2516 | { |
| 2517 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2518 | UsefulInputBuf UIB; |
| 2519 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2520 | // Just assume the size here. It was checked during SetUp so |
| 2521 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2522 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2523 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2524 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2525 | return UsefulInputBuf_GetError(&UIB); |
| 2526 | } |
| 2527 | |
| 2528 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2529 | static inline int |
| 2530 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2531 | { |
| 2532 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2533 | // length check performed here is useful. |
| 2534 | UsefulOutBuf UOB; |
| 2535 | |
| 2536 | UsefulOutBuf_Init(&UOB, Pool); |
| 2537 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2538 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2539 | return UsefulOutBuf_GetError(&UOB); |
| 2540 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2541 | |
| 2542 | |
| 2543 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2544 | Internal function for an allocation, reallocation free and destuct. |
| 2545 | |
| 2546 | Having only one function rather than one each per mode saves space in |
| 2547 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2548 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2549 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2550 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2551 | static UsefulBuf |
| 2552 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2553 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2554 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2555 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2556 | uint32_t uPoolSize; |
| 2557 | uint32_t uFreeOffset; |
| 2558 | |
| 2559 | if(uNewSize > UINT32_MAX) { |
| 2560 | // This allocator is only good up to 4GB. This check should |
| 2561 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2562 | goto Done; |
| 2563 | } |
| 2564 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2565 | |
| 2566 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2567 | goto Done; |
| 2568 | } |
| 2569 | |
| 2570 | if(uNewSize) { |
| 2571 | if(pMem) { |
| 2572 | // REALLOCATION MODE |
| 2573 | // Calculate pointer to the end of the memory pool. It is |
| 2574 | // assumed that pPool + uPoolSize won't wrap around by |
| 2575 | // assuming the caller won't pass a pool buffer in that is |
| 2576 | // not in legitimate memory space. |
| 2577 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2578 | |
| 2579 | // Check that the pointer for reallocation is in the range of the |
| 2580 | // pool. This also makes sure that pointer math further down |
| 2581 | // doesn't wrap under or over. |
| 2582 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2583 | // Offset to start of chunk for reallocation. This won't |
| 2584 | // wrap under because of check that pMem >= pPool. Cast |
| 2585 | // is safe because the pool is always less than UINT32_MAX |
| 2586 | // because of check in QCBORDecode_SetMemPool(). |
| 2587 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2588 | |
| 2589 | // Check to see if the allocation will fit. uPoolSize - |
| 2590 | // uMemOffset will not wrap under because of check that |
| 2591 | // pMem is in the range of the uPoolSize by check above. |
| 2592 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2593 | ReturnValue.ptr = pMem; |
| 2594 | ReturnValue.len = uNewSize; |
| 2595 | |
| 2596 | // Addition won't wrap around over because uNewSize was |
| 2597 | // checked to be sure it is less than the pool size. |
| 2598 | uFreeOffset = uMemOffset + uNewSize32; |
| 2599 | } |
| 2600 | } |
| 2601 | } else { |
| 2602 | // ALLOCATION MODE |
| 2603 | // uPoolSize - uFreeOffset will not underflow because this |
| 2604 | // pool implementation makes sure uFreeOffset is always |
| 2605 | // smaller than uPoolSize through this check here and |
| 2606 | // reallocation case. |
| 2607 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2608 | ReturnValue.len = uNewSize; |
| 2609 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2610 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2611 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2612 | } |
| 2613 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2614 | if(pMem) { |
| 2615 | // FREE MODE |
| 2616 | // Cast is safe because of limit on pool size in |
| 2617 | // QCBORDecode_SetMemPool() |
| 2618 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2619 | } else { |
| 2620 | // DESTRUCT MODE |
| 2621 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2622 | } |
| 2623 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2624 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2625 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2626 | MemPool_Pack(Pool, uFreeOffset); |
| 2627 | |
| 2628 | Done: |
| 2629 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2632 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2633 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2634 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2635 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2636 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2637 | UsefulBuf Pool, |
| 2638 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2639 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2640 | // The pool size and free mem offset are packed into the beginning |
| 2641 | // of the pool memory. This compile time check make sure the |
| 2642 | // constant in the header is correct. This check should optimize |
| 2643 | // down to nothing. |
| 2644 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2645 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2646 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2647 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2648 | // The pool size and free offset packed in to the beginning of pool |
| 2649 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2650 | // machines. |
| 2651 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2652 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2653 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2654 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2655 | // This checks that the pool buffer given is big enough. |
| 2656 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2657 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2658 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2659 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2660 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2661 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2662 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2663 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2664 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2665 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2666 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2667 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2668 | |
| 2669 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2670 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2671 | { |
| 2672 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2673 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2674 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2675 | |
| 2676 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2677 | Consume an entire map or array (and do next to |
| 2678 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2679 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2680 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2681 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2682 | const QCBORItem *pItemToConsume, |
| 2683 | uint_fast8_t *puNextNestLevel) |
| 2684 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2685 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2686 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2687 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2688 | // If it is a map or array, this will tell if it is empty. |
| 2689 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2690 | |
| 2691 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2692 | /* 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] | 2693 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2694 | /* This works for definite- and indefinite- length |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2695 | * maps and arrays by using the nesting level |
| 2696 | */ |
| 2697 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2698 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2699 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2700 | goto Done; |
| 2701 | } |
| 2702 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2703 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2704 | *puNextNestLevel = Item.uNextNestLevel; |
| 2705 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2706 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2707 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2708 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2709 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2710 | /* Just pass the nesting level through */ |
| 2711 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2712 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2713 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2714 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2715 | |
| 2716 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2717 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2718 | } |
| 2719 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 2720 | |
| 2721 | void QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2722 | { |
| 2723 | uint8_t uNextNestLevel; |
| 2724 | |
| 2725 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 2726 | |
| 2727 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2728 | pMe->uLastError = (uint8_t)ConsumeItem(pMe, pDecodedItem, &uNextNestLevel); |
| 2729 | pDecodedItem->uNextNestLevel = uNextNestLevel; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | |
| 2734 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 2735 | /* Call only on maps and arrays. Rewinds the cursor |
| 2736 | * to the start as if it was just entered. |
| 2737 | */ |
| 2738 | static void RewindMapOrArray(QCBORDecodeContext *pMe) |
| 2739 | { |
| 2740 | /* Reset nesting tracking to the deepest bounded level */ |
| 2741 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 2742 | |
| 2743 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 2744 | |
| 2745 | /* Reposition traversal cursor to the start of the map/array */ |
| 2746 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2747 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 2748 | } |
| 2749 | |
| 2750 | |
| 2751 | /* |
| 2752 | Public function, see header qcbor/qcbor_decode.h file |
| 2753 | */ |
| 2754 | void QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
| 2755 | { |
| 2756 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 2757 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 2758 | |
| 2759 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 2760 | /* In bstr-wrapped CBOR. */ |
| 2761 | |
| 2762 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 2763 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2764 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 2765 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 2766 | |
| 2767 | } else { |
| 2768 | /* In a map or array */ |
| 2769 | RewindMapOrArray(pMe); |
| 2770 | } |
| 2771 | |
| 2772 | } else { |
| 2773 | /* Not in anything bounded */ |
| 2774 | |
| 2775 | /* Reposition traversal cursor to the start of input CBOR */ |
| 2776 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 2777 | |
| 2778 | /* Reset nesting tracking to beginning of input. */ |
| 2779 | DecodeNesting_Init(&(pMe->nesting)); |
| 2780 | } |
| 2781 | |
| 2782 | pMe->uLastError = QCBOR_SUCCESS; |
| 2783 | } |
| 2784 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2785 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2786 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2787 | Works only for integer and string labels. Returns false |
| 2788 | for any other type. */ |
| 2789 | static inline bool |
| 2790 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2791 | { |
| 2792 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2793 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2794 | return true; |
| 2795 | } |
| 2796 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2797 | 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] | 2798 | return true; |
| 2799 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2800 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2801 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2802 | return true; |
| 2803 | } |
| 2804 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2805 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2806 | return true; |
| 2807 | } |
| 2808 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2809 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2810 | /* Other label types are never matched */ |
| 2811 | return false; |
| 2812 | } |
| 2813 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2814 | |
| 2815 | /* |
| 2816 | Returns true if Item1 and Item2 are the same type |
| 2817 | or if either are of QCBOR_TYPE_ANY. |
| 2818 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2819 | static inline bool |
| 2820 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2821 | { |
| 2822 | if(Item1.uDataType == Item2.uDataType) { |
| 2823 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2824 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2825 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2826 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2827 | return true; |
| 2828 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2829 | return false; |
| 2830 | } |
| 2831 | |
| 2832 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2833 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2834 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2835 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2836 | @param[in] pMe The decode context to search. |
| 2837 | @param[in,out] pItemArray The items to search for and the items found. |
| 2838 | @param[out] puOffset Byte offset of last item matched. |
| 2839 | @param[in] pCBContext Context for the not-found item call back. |
| 2840 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2841 | |
| 2842 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2843 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2844 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2845 | were found for one of the labels being |
| 2846 | search for. This duplicate detection is |
| 2847 | only performed for items in pItemArray, |
| 2848 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2849 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2850 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2851 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2852 | |
| 2853 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2854 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2855 | On input pItemArray contains a list of labels and data types |
| 2856 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2857 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2858 | On output the fully retrieved items are filled in with |
| 2859 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2860 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2861 | 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] | 2862 | |
| 2863 | 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] | 2864 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2865 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2866 | MapSearch(QCBORDecodeContext *pMe, |
| 2867 | QCBORItem *pItemArray, |
| 2868 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2869 | void *pCBContext, |
| 2870 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2871 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2872 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2873 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2874 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2875 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2876 | uReturn = pMe->uLastError; |
| 2877 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2878 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2879 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2880 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2881 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2882 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2883 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2884 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2885 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2888 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2889 | // It is an empty bounded array or map |
| 2890 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2891 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2892 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2893 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2894 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2895 | // Nothing is ever found in an empty array or map. All items |
| 2896 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2897 | uReturn = QCBOR_SUCCESS; |
| 2898 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2899 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2900 | } |
| 2901 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2902 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2903 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2904 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2905 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 2906 | RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2907 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2908 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2909 | Loop over all the items in the map or array. Each item |
| 2910 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2911 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2912 | length maps and arrays. The only reason this is ever |
| 2913 | called on arrays is to find their end position. |
| 2914 | |
| 2915 | This will always run over all items in order to do |
| 2916 | duplicate detection. |
| 2917 | |
| 2918 | This will exit with failure if it encounters an |
| 2919 | unrecoverable error, but continue on for recoverable |
| 2920 | errors. |
| 2921 | |
| 2922 | If a recoverable error occurs on a matched item, then |
| 2923 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2924 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2925 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2926 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2927 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2928 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2929 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2930 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2931 | /* Get the item */ |
| 2932 | QCBORItem Item; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2933 | QCBORError uResult = QCBORDecode_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2934 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2935 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2936 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2937 | goto Done; |
| 2938 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2939 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2940 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2941 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2942 | goto Done; |
| 2943 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2944 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2945 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2946 | bool bMatched = false; |
| 2947 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 2948 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2949 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2950 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2951 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2952 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2953 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2954 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2955 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2956 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2957 | goto Done; |
| 2958 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2959 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2960 | if(uResult != QCBOR_SUCCESS) { |
| 2961 | uReturn = uResult; |
| 2962 | goto Done; |
| 2963 | } |
| 2964 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2965 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2966 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2967 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2968 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2969 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2970 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2971 | bMatched = true; |
| 2972 | } |
| 2973 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2974 | |
| 2975 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2976 | if(!bMatched && pfCallback != NULL) { |
| 2977 | /* |
| 2978 | Call the callback on unmatched labels. |
| 2979 | (It is tempting to do duplicate detection here, but that would |
| 2980 | require dynamic memory allocation because the number of labels |
| 2981 | that might be encountered is unbounded.) |
| 2982 | */ |
| 2983 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2984 | if(uReturn != QCBOR_SUCCESS) { |
| 2985 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2986 | } |
| 2987 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2988 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2989 | /* |
| 2990 | Consume the item whether matched or not. This |
| 2991 | does the work of traversing maps and array and |
| 2992 | everything in them. In this loop only the |
| 2993 | items at the current nesting level are examined |
| 2994 | to match the labels. |
| 2995 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2996 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2997 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2998 | goto Done; |
| 2999 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3000 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3001 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3002 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3003 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3004 | |
| 3005 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3006 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3007 | // Check here makes sure that this won't accidentally be |
| 3008 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3009 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3010 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3011 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3012 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3013 | goto Done; |
| 3014 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3015 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3016 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3017 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3018 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3019 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3020 | |
| 3021 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3022 | /* 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] | 3023 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3024 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3025 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3026 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3027 | } |
| 3028 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3029 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3030 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3031 | } |
| 3032 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3033 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3034 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3035 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3036 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3037 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3038 | int64_t nLabel, |
| 3039 | uint8_t uQcborType, |
| 3040 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3041 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3042 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3043 | return; |
| 3044 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3045 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3046 | QCBORItem OneItemSeach[2]; |
| 3047 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3048 | OneItemSeach[0].label.int64 = nLabel; |
| 3049 | OneItemSeach[0].uDataType = uQcborType; |
| 3050 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3051 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3052 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3053 | |
| 3054 | *pItem = OneItemSeach[0]; |
| 3055 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3056 | if(uReturn != QCBOR_SUCCESS) { |
| 3057 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3058 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3059 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3060 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3063 | Done: |
| 3064 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3068 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3069 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3070 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3071 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3072 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3073 | uint8_t uQcborType, |
| 3074 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3075 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3076 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3077 | return; |
| 3078 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3079 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3080 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3081 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3082 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3083 | OneItemSeach[0].uDataType = uQcborType; |
| 3084 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3085 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3086 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 3087 | if(uReturn != QCBOR_SUCCESS) { |
| 3088 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3089 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3090 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3091 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3092 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3095 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3096 | |
| 3097 | Done: |
| 3098 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3102 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3103 | static QCBORError |
| 3104 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3105 | { |
| 3106 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 3107 | if(uDataType == puTypeList[i]) { |
| 3108 | return QCBOR_SUCCESS; |
| 3109 | } |
| 3110 | } |
| 3111 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3112 | } |
| 3113 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3114 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3115 | /** |
| 3116 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3117 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3118 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3119 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3120 | @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3121 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3122 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered |
| 3123 | tag value. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3124 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3125 | static QCBORError |
| 3126 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3127 | { |
| 3128 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3129 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3130 | /* There are tags that QCBOR couldn't process on this item and |
| 3131 | the caller has told us there should not be. */ |
| 3132 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3133 | } |
| 3134 | |
| 3135 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3136 | const int nItemType = pItem->uDataType; |
| 3137 | |
| 3138 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3139 | // Must match the tag and only the tag |
| 3140 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3141 | } |
| 3142 | |
| 3143 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3144 | if(uReturn == QCBOR_SUCCESS) { |
| 3145 | return QCBOR_SUCCESS; |
| 3146 | } |
| 3147 | |
| 3148 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3149 | /* Must match the content type and only the content type. |
| 3150 | There was no match just above so it is a fail. */ |
| 3151 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3152 | } |
| 3153 | |
| 3154 | /* If here it can match either the tag or the content |
| 3155 | and it hasn't matched the content, so the end |
| 3156 | result is whether it matches the tag. This is |
| 3157 | also the case that the CBOR standard discourages. */ |
| 3158 | |
| 3159 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3160 | } |
| 3161 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3162 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3163 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3164 | // This could be semi-private if need be |
| 3165 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3166 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3167 | int64_t nLabel, |
| 3168 | TagSpecification TagSpec, |
| 3169 | QCBORItem *pItem) |
| 3170 | { |
| 3171 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3172 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3173 | return; |
| 3174 | } |
| 3175 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3176 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3177 | } |
| 3178 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3179 | |
| 3180 | // This could be semi-private if need be |
| 3181 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3182 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3183 | const char *szLabel, |
| 3184 | TagSpecification TagSpec, |
| 3185 | QCBORItem *pItem) |
| 3186 | { |
| 3187 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3188 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3189 | return; |
| 3190 | } |
| 3191 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3192 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3195 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3196 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3197 | int64_t nLabel, |
| 3198 | TagSpecification TagSpec, |
| 3199 | UsefulBufC *pString) |
| 3200 | { |
| 3201 | QCBORItem Item; |
| 3202 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3203 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3204 | *pString = Item.val.string; |
| 3205 | } |
| 3206 | } |
| 3207 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3208 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3209 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3210 | const char * szLabel, |
| 3211 | TagSpecification TagSpec, |
| 3212 | UsefulBufC *pString) |
| 3213 | { |
| 3214 | QCBORItem Item; |
| 3215 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3216 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3217 | *pString = Item.val.string; |
| 3218 | } |
| 3219 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3220 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3221 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3222 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3223 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3224 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3225 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3226 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 3227 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3230 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3231 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3232 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3233 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3234 | QCBORItem *pItemList, |
| 3235 | void *pCallbackCtx, |
| 3236 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3237 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3238 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 3239 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3243 | /** |
| 3244 | * @brief Search for a map/array by label and enter it |
| 3245 | * |
| 3246 | * @param[in] pMe The decode context. |
| 3247 | * @param[in] pSearch The map/array to search for. |
| 3248 | * |
| 3249 | * @c pSearch is expected to contain one item of type map or array |
| 3250 | * with the label specified. The current bounded map will be searched for |
| 3251 | * this and if found will be entered. |
| 3252 | * |
| 3253 | * If the label is not found, or the item found is not a map or array, |
| 3254 | * the error state is set. |
| 3255 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3256 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3257 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3258 | // The first item in pSearch is the one that is to be |
| 3259 | // entered. It should be the only one filled in. Any other |
| 3260 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3261 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3262 | return; |
| 3263 | } |
| 3264 | |
| 3265 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3266 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3267 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3268 | return; |
| 3269 | } |
| 3270 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3271 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3272 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3273 | return; |
| 3274 | } |
| 3275 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3276 | |
| 3277 | /* The map or array was found. Now enter it. |
| 3278 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3279 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3280 | * next item for the pre-order traversal cursor to be the map/array |
| 3281 | * found by MapSearch(). The next few lines of code force the |
| 3282 | * cursor to that. |
| 3283 | * |
| 3284 | * There is no need to retain the old cursor because |
| 3285 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3286 | * beginning of the map/array being entered. |
| 3287 | * |
| 3288 | * The cursor is forced by: 1) setting the input buffer position to |
| 3289 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3290 | * counter to the total in the map/array, 3) setting the nesting |
| 3291 | * level. Setting the map/array counter to the total is not |
| 3292 | * strictly correct, but this is OK because this cursor only needs |
| 3293 | * to be used to get one item and MapSearch() has already found it |
| 3294 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3295 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3296 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3297 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3298 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3299 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3300 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3301 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3302 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3306 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3307 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3308 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3309 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3310 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3311 | QCBORItem OneItemSeach[2]; |
| 3312 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3313 | OneItemSeach[0].label.int64 = nLabel; |
| 3314 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3315 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3316 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3317 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3318 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3322 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3323 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3324 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3325 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3326 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3327 | QCBORItem OneItemSeach[2]; |
| 3328 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3329 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3330 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3331 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3332 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3333 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3334 | } |
| 3335 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3336 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3337 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3338 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3339 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3340 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3341 | QCBORItem OneItemSeach[2]; |
| 3342 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3343 | OneItemSeach[0].label.int64 = nLabel; |
| 3344 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3345 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3346 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3347 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3350 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3351 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3352 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3353 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3354 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3355 | QCBORItem OneItemSeach[2]; |
| 3356 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3357 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3358 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3359 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3360 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3361 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3365 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3366 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3367 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3368 | QCBORError uErr; |
| 3369 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3370 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3371 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3372 | // Already in error state; do nothing. |
| 3373 | return; |
| 3374 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3375 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3376 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3377 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3378 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3379 | if(uErr != QCBOR_SUCCESS) { |
| 3380 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3381 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3382 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3383 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3384 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3387 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3388 | |
| 3389 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3390 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3391 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3392 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3393 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3394 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3395 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3396 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3397 | // Special case to increment nesting level for zero-length maps |
| 3398 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3399 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3400 | } |
| 3401 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3402 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3403 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3404 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3405 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3406 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3407 | if(pItem != NULL) { |
| 3408 | *pItem = Item; |
| 3409 | } |
| 3410 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3411 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3412 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3413 | } |
| 3414 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3415 | |
| 3416 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3417 | This is the common work for exiting a level that is a bounded map, |
| 3418 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3419 | |
| 3420 | One chunk of work is to set up the pre-order traversal so it is at |
| 3421 | the item just after the bounded map, array or bstr that is being |
| 3422 | exited. This is somewhat complex. |
| 3423 | |
| 3424 | The other work is to level-up the bounded mode to next higest bounded |
| 3425 | mode or the top level if there isn't one. |
| 3426 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3427 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3428 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3429 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3430 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3431 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3432 | /* |
| 3433 | First the pre-order-traversal byte offset is positioned to the |
| 3434 | item just after the bounded mode item that was just consumed. |
| 3435 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3436 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3437 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3438 | /* |
| 3439 | Next, set the current nesting level to one above the bounded level |
| 3440 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3441 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3442 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3443 | makes sure pCurrentBounded is valid. |
| 3444 | */ |
| 3445 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3446 | |
| 3447 | /* |
| 3448 | This does the complex work of leveling up the pre-order traversal |
| 3449 | when the end of a map or array or another bounded level is |
| 3450 | reached. It may do nothing, or ascend all the way to the top |
| 3451 | level. |
| 3452 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3453 | uErr = QCBORDecode_NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3454 | if(uErr != QCBOR_SUCCESS) { |
| 3455 | goto Done; |
| 3456 | } |
| 3457 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3458 | /* |
| 3459 | This makes the next highest bounded level the current bounded |
| 3460 | level. If there is no next highest level, then no bounded mode is |
| 3461 | in effect. |
| 3462 | */ |
| 3463 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3464 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3465 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3466 | |
| 3467 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3468 | return uErr; |
| 3469 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3470 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3471 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3472 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3473 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3474 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3475 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3476 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3477 | return; |
| 3478 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3479 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3480 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3481 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3482 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3483 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3484 | goto Done; |
| 3485 | } |
| 3486 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3487 | /* |
| 3488 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3489 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3490 | from previous map search, then do a dummy search. |
| 3491 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3492 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3493 | QCBORItem Dummy; |
| 3494 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3495 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3496 | if(uErr != QCBOR_SUCCESS) { |
| 3497 | goto Done; |
| 3498 | } |
| 3499 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3500 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3501 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3502 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3503 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3504 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3508 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3509 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3510 | const QCBORItem *pItem, |
| 3511 | uint8_t uTagRequirement, |
| 3512 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3513 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3514 | if(pBstr) { |
| 3515 | *pBstr = NULLUsefulBufC; |
| 3516 | } |
| 3517 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3518 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3519 | /* Already in error state; do nothing. */ |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3520 | return pMe->uLastError; |
| 3521 | } |
| 3522 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3523 | QCBORError uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3524 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3525 | const TagSpecification TagSpec = |
| 3526 | { |
| 3527 | uTagRequirement, |
| 3528 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3529 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3530 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3531 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3532 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3533 | if(uError != QCBOR_SUCCESS) { |
| 3534 | goto Done; |
| 3535 | } |
| 3536 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3537 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3538 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 3539 | * increment in QCBORDecode_NestLevelAscender() called by |
| 3540 | * ExitBoundedLevel() will work right. |
| 3541 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3542 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3543 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3544 | |
| 3545 | if(pBstr) { |
| 3546 | *pBstr = pItem->val.string; |
| 3547 | } |
| 3548 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3549 | /* This saves the current length of the UsefulInputBuf and then |
| 3550 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 3551 | * CBOR that is being entered. |
| 3552 | * |
| 3553 | * Most of these calls are simple inline accessors so this doesn't |
| 3554 | * amount to much code. |
| 3555 | */ |
| 3556 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3557 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3558 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 3559 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3560 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3561 | goto Done; |
| 3562 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3563 | |
| 3564 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 3565 | pItem->val.string.ptr); |
| 3566 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 3567 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 3568 | /* This should never happen because pItem->val.string.ptr should |
| 3569 | * always be valid since it was just returned. |
| 3570 | */ |
| 3571 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3572 | goto Done; |
| 3573 | } |
| 3574 | |
| 3575 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 3576 | |
| 3577 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3578 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3579 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3580 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3581 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3582 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3583 | Done: |
| 3584 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3588 | /* |
| 3589 | Public function, see header qcbor/qcbor_decode.h file |
| 3590 | */ |
| 3591 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3592 | uint8_t uTagRequirement, |
| 3593 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3594 | { |
| 3595 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3596 | // Already in error state; do nothing. |
| 3597 | return; |
| 3598 | } |
| 3599 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3600 | /* Get the data item that is the byte string being entered */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3601 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3602 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3603 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3604 | return; |
| 3605 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3606 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3607 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3608 | &Item, |
| 3609 | uTagRequirement, |
| 3610 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3614 | /* |
| 3615 | Public function, see header qcbor/qcbor_decode.h file |
| 3616 | */ |
| 3617 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3618 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3619 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3620 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3621 | { |
| 3622 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3623 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3624 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3625 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3626 | &Item, |
| 3627 | uTagRequirement, |
| 3628 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3629 | } |
| 3630 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3631 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3632 | /* |
| 3633 | Public function, see header qcbor/qcbor_decode.h file |
| 3634 | */ |
| 3635 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3636 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3637 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3638 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3639 | { |
| 3640 | QCBORItem Item; |
| 3641 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3642 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3643 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3644 | &Item, |
| 3645 | uTagRequirement, |
| 3646 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3647 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3648 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3649 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3650 | /* |
| 3651 | Public function, see header qcbor/qcbor_decode.h file |
| 3652 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3653 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3654 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3655 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3656 | // Already in error state; do nothing. |
| 3657 | return; |
| 3658 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3659 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3660 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3661 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3662 | return; |
| 3663 | } |
| 3664 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3665 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 3666 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3667 | /* |
| 3668 | Reset the length of the UsefulInputBuf to what it was before |
| 3669 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3670 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3671 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3672 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3673 | |
| 3674 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3675 | QCBORError uErr = ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3676 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3679 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3680 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3681 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3682 | static inline void |
| 3683 | ProcessBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3684 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3685 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3686 | /* Already in error state, do nothing */ |
| 3687 | return; |
| 3688 | } |
| 3689 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3690 | switch(pItem->uDataType) { |
| 3691 | case QCBOR_TYPE_TRUE: |
| 3692 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3693 | break; |
| 3694 | |
| 3695 | case QCBOR_TYPE_FALSE: |
| 3696 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3697 | break; |
| 3698 | |
| 3699 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3700 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3701 | break; |
| 3702 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3703 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3704 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3705 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3706 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3707 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3708 | * Public function, see header qcbor/qcbor_decode.h file |
| 3709 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3710 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3711 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3712 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3713 | /* Already in error state, do nothing */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3714 | return; |
| 3715 | } |
| 3716 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3717 | QCBORItem Item; |
| 3718 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3719 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3720 | |
| 3721 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3722 | } |
| 3723 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3724 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3725 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3726 | * Public function, see header qcbor/qcbor_decode.h file |
| 3727 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3728 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3729 | { |
| 3730 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3731 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3732 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3733 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3734 | } |
| 3735 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3736 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3737 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3738 | * Public function, see header qcbor/qcbor_decode.h file |
| 3739 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3740 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3741 | { |
| 3742 | QCBORItem Item; |
| 3743 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3744 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame^] | 3745 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | |
| 3749 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3750 | |
| 3751 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3752 | QCBORItem *pItem, |
| 3753 | uint8_t uTagRequirement, |
| 3754 | int64_t *pnTime) |
| 3755 | { |
| 3756 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3757 | // Already in error state, do nothing |
| 3758 | return; |
| 3759 | } |
| 3760 | |
| 3761 | QCBORError uErr; |
| 3762 | |
| 3763 | const TagSpecification TagSpec = |
| 3764 | { |
| 3765 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3766 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3767 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3768 | }; |
| 3769 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3770 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3771 | if(uErr != QCBOR_SUCCESS) { |
| 3772 | goto Done; |
| 3773 | } |
| 3774 | |
| 3775 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3776 | uErr = DecodeDateEpoch(pItem); |
| 3777 | if(uErr != QCBOR_SUCCESS) { |
| 3778 | goto Done; |
| 3779 | } |
| 3780 | } |
| 3781 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3782 | // Save the tags in the last item's tags in the decode context |
| 3783 | // for QCBORDecode_GetNthTagOfLast() |
| 3784 | CopyTags(pMe, pItem); |
| 3785 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3786 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3787 | |
| 3788 | Done: |
| 3789 | pMe->uLastError = (uint8_t)uErr; |
| 3790 | } |
| 3791 | |
| 3792 | |
| 3793 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3794 | uint8_t uTagRequirement, |
| 3795 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3796 | { |
| 3797 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3798 | // Already in error state, do nothing |
| 3799 | return; |
| 3800 | } |
| 3801 | |
| 3802 | QCBORItem Item; |
| 3803 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3804 | |
| 3805 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3806 | } |
| 3807 | |
| 3808 | |
| 3809 | void |
| 3810 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3811 | int64_t nLabel, |
| 3812 | uint8_t uTagRequirement, |
| 3813 | int64_t *pnTime) |
| 3814 | { |
| 3815 | QCBORItem Item; |
| 3816 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3817 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3818 | } |
| 3819 | |
| 3820 | |
| 3821 | void |
| 3822 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3823 | const char *szLabel, |
| 3824 | uint8_t uTagRequirement, |
| 3825 | int64_t *pnTime) |
| 3826 | { |
| 3827 | QCBORItem Item; |
| 3828 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3829 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3830 | } |
| 3831 | |
| 3832 | |
| 3833 | |
| 3834 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3835 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3836 | TagSpecification TagSpec, |
| 3837 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3838 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3839 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3840 | // Already in error state, do nothing |
| 3841 | return; |
| 3842 | } |
| 3843 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3844 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3845 | QCBORItem Item; |
| 3846 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3847 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3848 | if(uError != QCBOR_SUCCESS) { |
| 3849 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3850 | return; |
| 3851 | } |
| 3852 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3853 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3854 | |
| 3855 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3856 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 3857 | } else { |
| 3858 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3862 | |
| 3863 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3864 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3865 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3866 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3867 | UsefulBufC *pValue, |
| 3868 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3869 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3870 | const TagSpecification TagSpec = |
| 3871 | { |
| 3872 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3873 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3874 | {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] | 3875 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3876 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3877 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3878 | if(uErr != QCBOR_SUCCESS) { |
| 3879 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3880 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3881 | |
| 3882 | *pValue = pItem->val.string; |
| 3883 | |
| 3884 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3885 | *pbIsNegative = false; |
| 3886 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3887 | *pbIsNegative = true; |
| 3888 | } |
| 3889 | |
| 3890 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3894 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3895 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3896 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3897 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3898 | uint8_t uTagRequirement, |
| 3899 | UsefulBufC *pValue, |
| 3900 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3901 | { |
| 3902 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3903 | // Already in error state, do nothing |
| 3904 | return; |
| 3905 | } |
| 3906 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3907 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3908 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3909 | if(uError != QCBOR_SUCCESS) { |
| 3910 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3911 | return; |
| 3912 | } |
| 3913 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3914 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3915 | } |
| 3916 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3917 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3918 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3919 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3920 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3921 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3922 | int64_t nLabel, |
| 3923 | uint8_t uTagRequirement, |
| 3924 | UsefulBufC *pValue, |
| 3925 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3926 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3927 | QCBORItem Item; |
| 3928 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3929 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3930 | return; |
| 3931 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3932 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3933 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3934 | } |
| 3935 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3936 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3937 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3938 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3939 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3940 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3941 | const char *szLabel, |
| 3942 | uint8_t uTagRequirement, |
| 3943 | UsefulBufC *pValue, |
| 3944 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3945 | { |
| 3946 | QCBORItem Item; |
| 3947 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3948 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3949 | return; |
| 3950 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3951 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3952 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | |
| 3956 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3957 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3958 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3959 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3960 | const QCBORItem *pItem, |
| 3961 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3962 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3963 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3964 | const TagSpecification TagSpecText = |
| 3965 | { |
| 3966 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3967 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3968 | {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] | 3969 | }; |
| 3970 | const TagSpecification TagSpecBinary = |
| 3971 | { |
| 3972 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3973 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3974 | {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] | 3975 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3976 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3977 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3978 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3979 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3980 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3981 | if(pbIsTag257 != NULL) { |
| 3982 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3983 | } |
| 3984 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3985 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3986 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3987 | if(pbIsTag257 != NULL) { |
| 3988 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3989 | } |
| 3990 | uReturn = QCBOR_SUCCESS; |
| 3991 | |
| 3992 | } else { |
| 3993 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3994 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3995 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3996 | return uReturn; |
| 3997 | } |
| 3998 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3999 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 4000 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4001 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4002 | |
| 4003 | |
| 4004 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4005 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4006 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4007 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4008 | |
| 4009 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4010 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4011 | static QCBORError |
| 4012 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4013 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4014 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4015 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4016 | if(uResult != 0) { |
| 4017 | /* This loop will run a maximum of 19 times because |
| 4018 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 4019 | * exit with the overflow error |
| 4020 | */ |
| 4021 | for(; nExponent > 0; nExponent--) { |
| 4022 | if(uResult > UINT64_MAX / 10) { |
| 4023 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 4024 | } |
| 4025 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4026 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4027 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4028 | for(; nExponent < 0; nExponent++) { |
| 4029 | uResult = uResult / 10; |
| 4030 | if(uResult == 0) { |
| 4031 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 4032 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4033 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4034 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4035 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4036 | |
| 4037 | *puResult = uResult; |
| 4038 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4039 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4043 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4044 | static QCBORError |
| 4045 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4046 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4047 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4048 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4049 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4050 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4051 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4052 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4053 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4054 | */ |
| 4055 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4056 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4057 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4058 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4059 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4060 | nExponent--; |
| 4061 | } |
| 4062 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4063 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4064 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4065 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 4066 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4067 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4068 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4069 | } |
| 4070 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4071 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4072 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4073 | return QCBOR_SUCCESS; |
| 4074 | } |
| 4075 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4076 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4077 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4078 | Compute value with signed mantissa and signed result. Works with |
| 4079 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4080 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4081 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 4082 | int64_t nExponent, |
| 4083 | int64_t *pnResult, |
| 4084 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4085 | { |
| 4086 | uint64_t uResult; |
| 4087 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4088 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4089 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4090 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 4091 | |
| 4092 | // Do the exponentiation of the positive mantissa |
| 4093 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4094 | if(uReturn) { |
| 4095 | return uReturn; |
| 4096 | } |
| 4097 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4098 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4099 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4100 | of INT64_MIN. This assumes two's compliment representation where |
| 4101 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 4102 | Trying to write -INT64_MIN doesn't work to get this because the |
| 4103 | compiler tries to work with an int64_t which can't represent |
| 4104 | -INT64_MIN. |
| 4105 | */ |
| 4106 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 4107 | |
| 4108 | // Error out if too large |
| 4109 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4110 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4111 | } |
| 4112 | |
| 4113 | // Casts are safe because of checks above |
| 4114 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 4115 | |
| 4116 | return QCBOR_SUCCESS; |
| 4117 | } |
| 4118 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4119 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4120 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4121 | Compute value with signed mantissa and unsigned result. Works with |
| 4122 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4123 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4124 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 4125 | int64_t nExponent, |
| 4126 | uint64_t *puResult, |
| 4127 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4128 | { |
| 4129 | if(nMantissa < 0) { |
| 4130 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4131 | } |
| 4132 | |
| 4133 | // Cast to unsigned is OK because of check for negative |
| 4134 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 4135 | // Exponentiation is straight forward |
| 4136 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4137 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4138 | |
| 4139 | |
| 4140 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4141 | Compute value with signed mantissa and unsigned result. Works with |
| 4142 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4143 | */ |
| 4144 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 4145 | int64_t nExponent, |
| 4146 | uint64_t *puResult, |
| 4147 | fExponentiator pfExp) |
| 4148 | { |
| 4149 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4150 | } |
| 4151 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4152 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4153 | |
| 4154 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4155 | |
| 4156 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4157 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4158 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4159 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4160 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4161 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4162 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4163 | const uint8_t *pByte = BigNum.ptr; |
| 4164 | size_t uLen = BigNum.len; |
| 4165 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4166 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4167 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4168 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4169 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4172 | *pResult = uResult; |
| 4173 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4174 | } |
| 4175 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4176 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4177 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4178 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4179 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4180 | } |
| 4181 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4182 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4183 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4184 | { |
| 4185 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4186 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 4187 | if(uError) { |
| 4188 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4189 | } |
| 4190 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 4191 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4192 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4196 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4197 | { |
| 4198 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4199 | /* The negative integer furthest from zero for a C int64_t is |
| 4200 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4201 | negative number in CBOR is computed as -n - 1 where n is the |
| 4202 | encoded integer, where n is what is in the variable BigNum. When |
| 4203 | converting BigNum to a uint64_t, the maximum value is thus |
| 4204 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 4205 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4206 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4207 | -n - 1 <= INT64_MIN. |
| 4208 | -n - 1 <= -INT64_MAX - 1 |
| 4209 | n <= INT64_MAX. |
| 4210 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4211 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4212 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4213 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4214 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4215 | |
| 4216 | /// Now apply -n - 1. The cast is safe because |
| 4217 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4218 | // is the largest positive integer that an int64_t can |
| 4219 | // represent. */ |
| 4220 | *pnResult = -(int64_t)uResult - 1; |
| 4221 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4222 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4223 | } |
| 4224 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4225 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4226 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4227 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4228 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4229 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4230 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4231 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4232 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4233 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4234 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4235 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4236 | |
| 4237 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4238 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4239 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4240 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4241 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4242 | static QCBORError |
| 4243 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4244 | { |
| 4245 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4246 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4247 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4248 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4249 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4250 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 4251 | http://www.cplusplus.com/reference/cmath/llround/ |
| 4252 | */ |
| 4253 | // Not interested in FE_INEXACT |
| 4254 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4255 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4256 | *pnValue = llround(pItem->val.dfnum); |
| 4257 | } else { |
| 4258 | *pnValue = lroundf(pItem->val.fnum); |
| 4259 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4260 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4261 | // llround() shouldn't result in divide by zero, but catch |
| 4262 | // it here in case it unexpectedly does. Don't try to |
| 4263 | // distinguish between the various exceptions because it seems |
| 4264 | // they vary by CPU, compiler and OS. |
| 4265 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4266 | } |
| 4267 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4268 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4269 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4270 | #else |
| 4271 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4272 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4273 | break; |
| 4274 | |
| 4275 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4276 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4277 | *pnValue = pItem->val.int64; |
| 4278 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4279 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4280 | } |
| 4281 | break; |
| 4282 | |
| 4283 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4284 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4285 | if(pItem->val.uint64 < INT64_MAX) { |
| 4286 | *pnValue = pItem->val.int64; |
| 4287 | } else { |
| 4288 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4289 | } |
| 4290 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4291 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4292 | } |
| 4293 | break; |
| 4294 | |
| 4295 | default: |
| 4296 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4297 | } |
| 4298 | return QCBOR_SUCCESS; |
| 4299 | } |
| 4300 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4301 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4302 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4303 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4304 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4305 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4306 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4307 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4308 | return; |
| 4309 | } |
| 4310 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4311 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4312 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4313 | if(uError) { |
| 4314 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4315 | return; |
| 4316 | } |
| 4317 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4318 | if(pItem) { |
| 4319 | *pItem = Item; |
| 4320 | } |
| 4321 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4322 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4323 | } |
| 4324 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4325 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4326 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4327 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4328 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4329 | int64_t *pnValue, |
| 4330 | QCBORItem *pItem) |
| 4331 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4332 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4333 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4334 | return; |
| 4335 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4336 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4337 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4340 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4341 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4342 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4343 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4344 | int64_t *pnValue, |
| 4345 | QCBORItem *pItem) |
| 4346 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4347 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4348 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4349 | return; |
| 4350 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4351 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4352 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4356 | /* |
| 4357 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4358 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4359 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4360 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4361 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4362 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4363 | |
| 4364 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4365 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4366 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4367 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4368 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4369 | static QCBORError |
| 4370 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4371 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4372 | switch(pItem->uDataType) { |
| 4373 | |
| 4374 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4375 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4376 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4377 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4378 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4379 | } |
| 4380 | break; |
| 4381 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4382 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4383 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4384 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4385 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4386 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4387 | } |
| 4388 | break; |
| 4389 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4390 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4391 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4392 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4393 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4394 | pItem->val.expAndMantissa.nExponent, |
| 4395 | pnValue, |
| 4396 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4397 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4398 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4399 | } |
| 4400 | break; |
| 4401 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4402 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4403 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4404 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4405 | pItem->val.expAndMantissa.nExponent, |
| 4406 | pnValue, |
| 4407 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4408 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4409 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4410 | } |
| 4411 | break; |
| 4412 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4413 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4414 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4415 | int64_t nMantissa; |
| 4416 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4417 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4418 | if(uErr) { |
| 4419 | return uErr; |
| 4420 | } |
| 4421 | return ExponentiateNN(nMantissa, |
| 4422 | pItem->val.expAndMantissa.nExponent, |
| 4423 | pnValue, |
| 4424 | Exponentitate10); |
| 4425 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4426 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4427 | } |
| 4428 | break; |
| 4429 | |
| 4430 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4431 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4432 | int64_t nMantissa; |
| 4433 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4434 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4435 | if(uErr) { |
| 4436 | return uErr; |
| 4437 | } |
| 4438 | return ExponentiateNN(nMantissa, |
| 4439 | pItem->val.expAndMantissa.nExponent, |
| 4440 | pnValue, |
| 4441 | Exponentitate10); |
| 4442 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4443 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4444 | } |
| 4445 | break; |
| 4446 | |
| 4447 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4448 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4449 | int64_t nMantissa; |
| 4450 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4451 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4452 | if(uErr) { |
| 4453 | return uErr; |
| 4454 | } |
| 4455 | return ExponentiateNN(nMantissa, |
| 4456 | pItem->val.expAndMantissa.nExponent, |
| 4457 | pnValue, |
| 4458 | Exponentitate2); |
| 4459 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4460 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4461 | } |
| 4462 | break; |
| 4463 | |
| 4464 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4465 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4466 | int64_t nMantissa; |
| 4467 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4468 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4469 | if(uErr) { |
| 4470 | return uErr; |
| 4471 | } |
| 4472 | return ExponentiateNN(nMantissa, |
| 4473 | pItem->val.expAndMantissa.nExponent, |
| 4474 | pnValue, |
| 4475 | Exponentitate2); |
| 4476 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4477 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4478 | } |
| 4479 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4480 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4481 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4482 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4483 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4484 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4485 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4486 | |
| 4487 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4488 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4489 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4490 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4491 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4492 | { |
| 4493 | QCBORItem Item; |
| 4494 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4495 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4496 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4497 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4498 | // The above conversion succeeded |
| 4499 | return; |
| 4500 | } |
| 4501 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4502 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4503 | // 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] | 4504 | return; |
| 4505 | } |
| 4506 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4507 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4508 | } |
| 4509 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4510 | |
| 4511 | /* |
| 4512 | Public function, see header qcbor/qcbor_decode.h file |
| 4513 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4514 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4515 | int64_t nLabel, |
| 4516 | uint32_t uConvertTypes, |
| 4517 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4518 | { |
| 4519 | QCBORItem Item; |
| 4520 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4521 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4522 | nLabel, |
| 4523 | uConvertTypes, |
| 4524 | pnValue, |
| 4525 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4526 | |
| 4527 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4528 | // The above conversion succeeded |
| 4529 | return; |
| 4530 | } |
| 4531 | |
| 4532 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4533 | // The above conversion failed in a way that code below can't correct |
| 4534 | return; |
| 4535 | } |
| 4536 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4537 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4541 | /* |
| 4542 | Public function, see header qcbor/qcbor_decode.h file |
| 4543 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4544 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4545 | const char *szLabel, |
| 4546 | uint32_t uConvertTypes, |
| 4547 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4548 | { |
| 4549 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4550 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4551 | szLabel, |
| 4552 | uConvertTypes, |
| 4553 | pnValue, |
| 4554 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4555 | |
| 4556 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4557 | // The above conversion succeeded |
| 4558 | return; |
| 4559 | } |
| 4560 | |
| 4561 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4562 | // The above conversion failed in a way that code below can't correct |
| 4563 | return; |
| 4564 | } |
| 4565 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4566 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4570 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4571 | { |
| 4572 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4573 | case QCBOR_TYPE_DOUBLE: |
| 4574 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4575 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4576 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4577 | // Can't use llround here because it will not convert values |
| 4578 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4579 | // need to be converted so it is more complicated. |
| 4580 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4581 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4582 | if(isnan(pItem->val.dfnum)) { |
| 4583 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4584 | } else if(pItem->val.dfnum < 0) { |
| 4585 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4586 | } else { |
| 4587 | double dRounded = round(pItem->val.dfnum); |
| 4588 | // See discussion in DecodeDateEpoch() for |
| 4589 | // explanation of - 0x7ff |
| 4590 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4591 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4592 | } |
| 4593 | *puValue = (uint64_t)dRounded; |
| 4594 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4595 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4596 | if(isnan(pItem->val.fnum)) { |
| 4597 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4598 | } else if(pItem->val.fnum < 0) { |
| 4599 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4600 | } else { |
| 4601 | float fRounded = roundf(pItem->val.fnum); |
| 4602 | // See discussion in DecodeDateEpoch() for |
| 4603 | // explanation of - 0x7ff |
| 4604 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4605 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4606 | } |
| 4607 | *puValue = (uint64_t)fRounded; |
| 4608 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4609 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4610 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4611 | // round() and roundf() shouldn't result in exceptions here, but |
| 4612 | // catch them to be robust and thorough. Don't try to |
| 4613 | // distinguish between the various exceptions because it seems |
| 4614 | // they vary by CPU, compiler and OS. |
| 4615 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4616 | } |
| 4617 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4618 | } else { |
| 4619 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4620 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4621 | #else |
| 4622 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4623 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4624 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4625 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4626 | case QCBOR_TYPE_INT64: |
| 4627 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4628 | if(pItem->val.int64 >= 0) { |
| 4629 | *puValue = (uint64_t)pItem->val.int64; |
| 4630 | } else { |
| 4631 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4632 | } |
| 4633 | } else { |
| 4634 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4635 | } |
| 4636 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4637 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4638 | case QCBOR_TYPE_UINT64: |
| 4639 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4640 | *puValue = pItem->val.uint64; |
| 4641 | } else { |
| 4642 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4643 | } |
| 4644 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4645 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4646 | default: |
| 4647 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4648 | } |
| 4649 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4650 | return QCBOR_SUCCESS; |
| 4651 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4652 | |
| 4653 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4654 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4655 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4656 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4657 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4658 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4659 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4660 | return; |
| 4661 | } |
| 4662 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4663 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4664 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4665 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4666 | if(uError) { |
| 4667 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4668 | return; |
| 4669 | } |
| 4670 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4671 | if(pItem) { |
| 4672 | *pItem = Item; |
| 4673 | } |
| 4674 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4675 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4676 | } |
| 4677 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4678 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4679 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4680 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4681 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4682 | uint64_t *puValue, |
| 4683 | QCBORItem *pItem) |
| 4684 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4685 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4686 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4687 | return; |
| 4688 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4689 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4690 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4694 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4695 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4696 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4697 | uint64_t *puValue, |
| 4698 | QCBORItem *pItem) |
| 4699 | { |
| 4700 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4701 | return; |
| 4702 | } |
| 4703 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4704 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4705 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4706 | return; |
| 4707 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4708 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4709 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4712 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4713 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4714 | static QCBORError |
| 4715 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4716 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4717 | switch(pItem->uDataType) { |
| 4718 | |
| 4719 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4720 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4721 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4722 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4723 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4724 | } |
| 4725 | break; |
| 4726 | |
| 4727 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4728 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4729 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4730 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4731 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4732 | } |
| 4733 | break; |
| 4734 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4735 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4736 | |
| 4737 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4738 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4739 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4740 | pItem->val.expAndMantissa.nExponent, |
| 4741 | puValue, |
| 4742 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4743 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4744 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4745 | } |
| 4746 | break; |
| 4747 | |
| 4748 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4749 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4750 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4751 | pItem->val.expAndMantissa.nExponent, |
| 4752 | puValue, |
| 4753 | Exponentitate2); |
| 4754 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4755 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4756 | } |
| 4757 | break; |
| 4758 | |
| 4759 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4760 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4761 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4762 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4763 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4764 | if(uErr != QCBOR_SUCCESS) { |
| 4765 | return uErr; |
| 4766 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4767 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4768 | pItem->val.expAndMantissa.nExponent, |
| 4769 | puValue, |
| 4770 | Exponentitate10); |
| 4771 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4772 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4773 | } |
| 4774 | break; |
| 4775 | |
| 4776 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4777 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4778 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4779 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4780 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4781 | } |
| 4782 | break; |
| 4783 | |
| 4784 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4785 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4786 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4787 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4788 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4789 | if(uErr != QCBOR_SUCCESS) { |
| 4790 | return uErr; |
| 4791 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4792 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4793 | pItem->val.expAndMantissa.nExponent, |
| 4794 | puValue, |
| 4795 | Exponentitate2); |
| 4796 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4797 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4798 | } |
| 4799 | break; |
| 4800 | |
| 4801 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4802 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4803 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4804 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4805 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4806 | } |
| 4807 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4808 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4809 | default: |
| 4810 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4811 | } |
| 4812 | } |
| 4813 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4814 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4815 | /* |
| 4816 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4817 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4818 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4819 | { |
| 4820 | QCBORItem Item; |
| 4821 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4822 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4823 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4824 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4825 | // The above conversion succeeded |
| 4826 | return; |
| 4827 | } |
| 4828 | |
| 4829 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4830 | // 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] | 4831 | return; |
| 4832 | } |
| 4833 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4834 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4835 | } |
| 4836 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4837 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4838 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4839 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4840 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4841 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4842 | int64_t nLabel, |
| 4843 | uint32_t uConvertTypes, |
| 4844 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4845 | { |
| 4846 | QCBORItem Item; |
| 4847 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4848 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 4849 | nLabel, |
| 4850 | uConvertTypes, |
| 4851 | puValue, |
| 4852 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4853 | |
| 4854 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4855 | // The above conversion succeeded |
| 4856 | return; |
| 4857 | } |
| 4858 | |
| 4859 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4860 | // The above conversion failed in a way that code below can't correct |
| 4861 | return; |
| 4862 | } |
| 4863 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4864 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | |
| 4868 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4869 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4870 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4871 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4872 | const char *szLabel, |
| 4873 | uint32_t uConvertTypes, |
| 4874 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4875 | { |
| 4876 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4877 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 4878 | szLabel, |
| 4879 | uConvertTypes, |
| 4880 | puValue, |
| 4881 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4882 | |
| 4883 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4884 | // The above conversion succeeded |
| 4885 | return; |
| 4886 | } |
| 4887 | |
| 4888 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4889 | // The above conversion failed in a way that code below can't correct |
| 4890 | return; |
| 4891 | } |
| 4892 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4893 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4897 | |
| 4898 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4899 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4900 | uint32_t uConvertTypes, |
| 4901 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4902 | { |
| 4903 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4904 | case QCBOR_TYPE_FLOAT: |
| 4905 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4906 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4907 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4908 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4909 | *pdValue = (double)pItem->val.fnum; |
| 4910 | } else { |
| 4911 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4912 | } |
| 4913 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4914 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4915 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4916 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4917 | break; |
| 4918 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4919 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4920 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4921 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4922 | *pdValue = pItem->val.dfnum; |
| 4923 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4924 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4925 | } |
| 4926 | } |
| 4927 | break; |
| 4928 | |
| 4929 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4930 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4931 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4932 | // A simple cast seems to do the job with no worry of exceptions. |
| 4933 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4934 | *pdValue = (double)pItem->val.int64; |
| 4935 | |
| 4936 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4937 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4938 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4939 | #else |
| 4940 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4941 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4942 | break; |
| 4943 | |
| 4944 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4945 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4946 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4947 | // A simple cast seems to do the job with no worry of exceptions. |
| 4948 | // There will be precision loss for some values. |
| 4949 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4950 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4951 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4952 | } |
| 4953 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4954 | #else |
| 4955 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4956 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4957 | |
| 4958 | default: |
| 4959 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4960 | } |
| 4961 | |
| 4962 | return QCBOR_SUCCESS; |
| 4963 | } |
| 4964 | |
| 4965 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4966 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4967 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4968 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4969 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4970 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4971 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4972 | return; |
| 4973 | } |
| 4974 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4975 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4976 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4977 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4978 | if(uError) { |
| 4979 | pMe->uLastError = (uint8_t)uError; |
| 4980 | return; |
| 4981 | } |
| 4982 | |
| 4983 | if(pItem) { |
| 4984 | *pItem = Item; |
| 4985 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4986 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4987 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4988 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4989 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4990 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4991 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4992 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4993 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4994 | double *pdValue, |
| 4995 | QCBORItem *pItem) |
| 4996 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4997 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4998 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4999 | return; |
| 5000 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5001 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5002 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5003 | } |
| 5004 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5005 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5006 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 5007 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5008 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5009 | double *pdValue, |
| 5010 | QCBORItem *pItem) |
| 5011 | { |
| 5012 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5013 | return; |
| 5014 | } |
| 5015 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5016 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5017 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5018 | return; |
| 5019 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5020 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5021 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5025 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5026 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 5027 | { |
| 5028 | double dResult; |
| 5029 | |
| 5030 | dResult = 0.0; |
| 5031 | const uint8_t *pByte = BigNum.ptr; |
| 5032 | size_t uLen = BigNum.len; |
| 5033 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5034 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5035 | while(uLen--) { |
| 5036 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 5037 | } |
| 5038 | |
| 5039 | return dResult; |
| 5040 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5041 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5042 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5043 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5044 | static QCBORError |
| 5045 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5046 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5047 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5048 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5049 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 5050 | |
| 5051 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5052 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5053 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5054 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5055 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5056 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5057 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5058 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5059 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 5060 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5061 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5062 | } |
| 5063 | break; |
| 5064 | |
| 5065 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5066 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5067 | // Underflow gives 0, overflow gives infinity |
| 5068 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5069 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5070 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5071 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5072 | } |
| 5073 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5074 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5075 | |
| 5076 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5077 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5078 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 5079 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5080 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5081 | } |
| 5082 | break; |
| 5083 | |
| 5084 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5085 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5086 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5087 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5088 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5089 | } |
| 5090 | break; |
| 5091 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5092 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5093 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5094 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5095 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5096 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5097 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5098 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5099 | } |
| 5100 | break; |
| 5101 | |
| 5102 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5103 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5104 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5105 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5106 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5107 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5108 | } |
| 5109 | break; |
| 5110 | |
| 5111 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5112 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5113 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5114 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5115 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5116 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5117 | } |
| 5118 | break; |
| 5119 | |
| 5120 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5121 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5122 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5123 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5124 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5125 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5126 | } |
| 5127 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5128 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5129 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5130 | default: |
| 5131 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5132 | } |
| 5133 | |
| 5134 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5135 | |
| 5136 | #else |
| 5137 | (void)pItem; |
| 5138 | (void)uConvertTypes; |
| 5139 | (void)pdValue; |
| 5140 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5141 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5142 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | |
| 5146 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5147 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5148 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5149 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 5150 | uint32_t uConvertTypes, |
| 5151 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5152 | { |
| 5153 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5154 | QCBORItem Item; |
| 5155 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5156 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5157 | |
| 5158 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5159 | // The above conversion succeeded |
| 5160 | return; |
| 5161 | } |
| 5162 | |
| 5163 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5164 | // The above conversion failed in a way that code below can't correct |
| 5165 | return; |
| 5166 | } |
| 5167 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5168 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5171 | |
| 5172 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5173 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5174 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5175 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5176 | int64_t nLabel, |
| 5177 | uint32_t uConvertTypes, |
| 5178 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5179 | { |
| 5180 | QCBORItem Item; |
| 5181 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5182 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5183 | |
| 5184 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5185 | // The above conversion succeeded |
| 5186 | return; |
| 5187 | } |
| 5188 | |
| 5189 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5190 | // The above conversion failed in a way that code below can't correct |
| 5191 | return; |
| 5192 | } |
| 5193 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5194 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | |
| 5198 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5199 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5200 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5201 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5202 | const char *szLabel, |
| 5203 | uint32_t uConvertTypes, |
| 5204 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5205 | { |
| 5206 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5207 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5208 | |
| 5209 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5210 | // The above conversion succeeded |
| 5211 | return; |
| 5212 | } |
| 5213 | |
| 5214 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5215 | // The above conversion failed in a way that code below can't correct |
| 5216 | return; |
| 5217 | } |
| 5218 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5219 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5220 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5221 | |
| 5222 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5223 | |
| 5224 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5225 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5226 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 5227 | { |
| 5228 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5229 | uInt = uInt << 8; |
| 5230 | }; |
| 5231 | |
| 5232 | UsefulOutBuf UOB; |
| 5233 | |
| 5234 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5235 | |
| 5236 | while(uInt) { |
| 5237 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5238 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5239 | uInt = uInt << 8; |
| 5240 | (void)xx; |
| 5241 | } |
| 5242 | |
| 5243 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5244 | } |
| 5245 | |
| 5246 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5247 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 5248 | TagSpecification TagSpec, |
| 5249 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5250 | { |
| 5251 | QCBORError uErr; |
| 5252 | // Loops runs at most 1.5 times. Making it a loop saves object code. |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5253 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5254 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5255 | if(uErr != QCBOR_SUCCESS) { |
| 5256 | goto Done; |
| 5257 | } |
| 5258 | |
| 5259 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 5260 | break; // Successful exit. Moving on to finish decoding. |
| 5261 | } |
| 5262 | |
| 5263 | // The item is an array, which means an undecoded |
| 5264 | // mantissa and exponent, so decode it. It will then |
| 5265 | // have a different type and exit the loop if. |
| 5266 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5267 | if(uErr != QCBOR_SUCCESS) { |
| 5268 | goto Done; |
| 5269 | } |
| 5270 | |
| 5271 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5272 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5273 | } |
| 5274 | Done: |
| 5275 | return uErr; |
| 5276 | } |
| 5277 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5278 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5279 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5280 | TagSpecification TagSpec, |
| 5281 | QCBORItem *pItem, |
| 5282 | int64_t *pnMantissa, |
| 5283 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5284 | { |
| 5285 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5286 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5287 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5288 | if(uErr != QCBOR_SUCCESS) { |
| 5289 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5290 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5291 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5292 | switch (pItem->uDataType) { |
| 5293 | |
| 5294 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5295 | case QCBOR_TYPE_BIGFLOAT: |
| 5296 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 5297 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5298 | break; |
| 5299 | |
| 5300 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5301 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5302 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5303 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5304 | break; |
| 5305 | |
| 5306 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5307 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5308 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5309 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5310 | break; |
| 5311 | |
| 5312 | default: |
| 5313 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5314 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5315 | |
| 5316 | Done: |
| 5317 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5318 | } |
| 5319 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5320 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5321 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5322 | TagSpecification TagSpec, |
| 5323 | QCBORItem *pItem, |
| 5324 | UsefulBuf BufferForMantissa, |
| 5325 | UsefulBufC *pMantissa, |
| 5326 | bool *pbIsNegative, |
| 5327 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5328 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5329 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5330 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5331 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5332 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5333 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5334 | } |
| 5335 | |
| 5336 | uint64_t uMantissa; |
| 5337 | |
| 5338 | switch (pItem->uDataType) { |
| 5339 | |
| 5340 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5341 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5342 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5343 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5344 | *pbIsNegative = false; |
| 5345 | } else { |
| 5346 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5347 | *pbIsNegative = true; |
| 5348 | } |
| 5349 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5350 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5351 | break; |
| 5352 | |
| 5353 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5354 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5355 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5356 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5357 | *pbIsNegative = false; |
| 5358 | break; |
| 5359 | |
| 5360 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5361 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5362 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5363 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5364 | *pbIsNegative = true; |
| 5365 | break; |
| 5366 | |
| 5367 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5368 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5369 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5370 | |
| 5371 | Done: |
| 5372 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5376 | /* |
| 5377 | Public function, see header qcbor/qcbor_decode.h file |
| 5378 | */ |
| 5379 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5380 | uint8_t uTagRequirement, |
| 5381 | int64_t *pnMantissa, |
| 5382 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5383 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5384 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5385 | return; |
| 5386 | } |
| 5387 | |
| 5388 | QCBORItem Item; |
| 5389 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5390 | if(uError) { |
| 5391 | pMe->uLastError = (uint8_t)uError; |
| 5392 | return; |
| 5393 | } |
| 5394 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5395 | const TagSpecification TagSpec = |
| 5396 | { |
| 5397 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5398 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5399 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5400 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5401 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5402 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5403 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5404 | } |
| 5405 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5406 | |
| 5407 | /* |
| 5408 | Public function, see header qcbor/qcbor_decode.h file |
| 5409 | */ |
| 5410 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5411 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5412 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5413 | int64_t *pnMantissa, |
| 5414 | int64_t *pnExponent) |
| 5415 | { |
| 5416 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5417 | return; |
| 5418 | } |
| 5419 | |
| 5420 | QCBORItem Item; |
| 5421 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5422 | |
| 5423 | const TagSpecification TagSpec = |
| 5424 | { |
| 5425 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5426 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5427 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5428 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5429 | }; |
| 5430 | |
| 5431 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5432 | } |
| 5433 | |
| 5434 | |
| 5435 | /* |
| 5436 | Public function, see header qcbor/qcbor_decode.h file |
| 5437 | */ |
| 5438 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5439 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5440 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5441 | int64_t *pnMantissa, |
| 5442 | int64_t *pnExponent) |
| 5443 | { |
| 5444 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5445 | return; |
| 5446 | } |
| 5447 | |
| 5448 | QCBORItem Item; |
| 5449 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5450 | |
| 5451 | const TagSpecification TagSpec = |
| 5452 | { |
| 5453 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5454 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5455 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5456 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5457 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5458 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5459 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5460 | } |
| 5461 | |
| 5462 | |
| 5463 | /* |
| 5464 | Public function, see header qcbor/qcbor_decode.h file |
| 5465 | */ |
| 5466 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5467 | uint8_t uTagRequirement, |
| 5468 | UsefulBuf MantissaBuffer, |
| 5469 | UsefulBufC *pMantissa, |
| 5470 | bool *pbMantissaIsNegative, |
| 5471 | int64_t *pnExponent) |
| 5472 | { |
| 5473 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5474 | return; |
| 5475 | } |
| 5476 | |
| 5477 | QCBORItem Item; |
| 5478 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5479 | if(uError) { |
| 5480 | pMe->uLastError = (uint8_t)uError; |
| 5481 | return; |
| 5482 | } |
| 5483 | |
| 5484 | const TagSpecification TagSpec = |
| 5485 | { |
| 5486 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5487 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5488 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5489 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5490 | }; |
| 5491 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5492 | ProcessMantissaAndExponentBig(pMe, |
| 5493 | TagSpec, |
| 5494 | &Item, |
| 5495 | MantissaBuffer, |
| 5496 | pMantissa, |
| 5497 | pbMantissaIsNegative, |
| 5498 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5499 | } |
| 5500 | |
| 5501 | |
| 5502 | /* |
| 5503 | Public function, see header qcbor/qcbor_decode.h file |
| 5504 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5505 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5506 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5507 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5508 | UsefulBuf BufferForMantissa, |
| 5509 | UsefulBufC *pMantissa, |
| 5510 | bool *pbIsNegative, |
| 5511 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5512 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5513 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5514 | return; |
| 5515 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5516 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5517 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5518 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5519 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5520 | return; |
| 5521 | } |
| 5522 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5523 | const TagSpecification TagSpec = |
| 5524 | { |
| 5525 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5526 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5527 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5528 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5529 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5530 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5531 | ProcessMantissaAndExponentBig(pMe, |
| 5532 | TagSpec, |
| 5533 | &Item, |
| 5534 | BufferForMantissa, |
| 5535 | pMantissa, |
| 5536 | pbIsNegative, |
| 5537 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5538 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5539 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5540 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5541 | /* |
| 5542 | Public function, see header qcbor/qcbor_decode.h file |
| 5543 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5544 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5545 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5546 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5547 | UsefulBuf BufferForMantissa, |
| 5548 | UsefulBufC *pMantissa, |
| 5549 | bool *pbIsNegative, |
| 5550 | int64_t *pnExponent) |
| 5551 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5552 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5553 | return; |
| 5554 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5555 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5556 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5557 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5558 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5559 | return; |
| 5560 | } |
| 5561 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5562 | const TagSpecification TagSpec = |
| 5563 | { |
| 5564 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5565 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5566 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5567 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5568 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5569 | |
| 5570 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5571 | } |
| 5572 | |
| 5573 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5574 | /* |
| 5575 | Public function, see header qcbor/qcbor_decode.h file |
| 5576 | */ |
| 5577 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5578 | uint8_t uTagRequirement, |
| 5579 | int64_t *pnMantissa, |
| 5580 | int64_t *pnExponent) |
| 5581 | { |
| 5582 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5583 | return; |
| 5584 | } |
| 5585 | |
| 5586 | QCBORItem Item; |
| 5587 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5588 | if(uError) { |
| 5589 | pMe->uLastError = (uint8_t)uError; |
| 5590 | return; |
| 5591 | } |
| 5592 | const TagSpecification TagSpec = |
| 5593 | { |
| 5594 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5595 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5596 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5597 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5598 | }; |
| 5599 | |
| 5600 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5601 | } |
| 5602 | |
| 5603 | |
| 5604 | /* |
| 5605 | Public function, see header qcbor/qcbor_decode.h file |
| 5606 | */ |
| 5607 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5608 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5609 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5610 | int64_t *pnMantissa, |
| 5611 | int64_t *pnExponent) |
| 5612 | { |
| 5613 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5614 | return; |
| 5615 | } |
| 5616 | |
| 5617 | QCBORItem Item; |
| 5618 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5619 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5620 | return; |
| 5621 | } |
| 5622 | |
| 5623 | const TagSpecification TagSpec = |
| 5624 | { |
| 5625 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5626 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5627 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5628 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5629 | }; |
| 5630 | |
| 5631 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5632 | } |
| 5633 | |
| 5634 | |
| 5635 | /* |
| 5636 | Public function, see header qcbor/qcbor_decode.h file |
| 5637 | */ |
| 5638 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5639 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5640 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5641 | int64_t *pnMantissa, |
| 5642 | int64_t *pnExponent) |
| 5643 | { |
| 5644 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5645 | return; |
| 5646 | } |
| 5647 | |
| 5648 | QCBORItem Item; |
| 5649 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5650 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5651 | return; |
| 5652 | } |
| 5653 | |
| 5654 | const TagSpecification TagSpec = |
| 5655 | { |
| 5656 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5657 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5658 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5659 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5660 | }; |
| 5661 | |
| 5662 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5663 | } |
| 5664 | |
| 5665 | |
| 5666 | /* |
| 5667 | Public function, see header qcbor/qcbor_decode.h file |
| 5668 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5669 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5670 | uint8_t uTagRequirement, |
| 5671 | UsefulBuf MantissaBuffer, |
| 5672 | UsefulBufC *pMantissa, |
| 5673 | bool *pbMantissaIsNegative, |
| 5674 | int64_t *pnExponent) |
| 5675 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5676 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5677 | return; |
| 5678 | } |
| 5679 | |
| 5680 | QCBORItem Item; |
| 5681 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5682 | if(uError) { |
| 5683 | pMe->uLastError = (uint8_t)uError; |
| 5684 | return; |
| 5685 | } |
| 5686 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5687 | const TagSpecification TagSpec = |
| 5688 | { |
| 5689 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5690 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5691 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5692 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5693 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5694 | |
| 5695 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5696 | } |
| 5697 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5698 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5699 | /* |
| 5700 | Public function, see header qcbor/qcbor_decode.h file |
| 5701 | */ |
| 5702 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5703 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5704 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5705 | UsefulBuf BufferForMantissa, |
| 5706 | UsefulBufC *pMantissa, |
| 5707 | bool *pbIsNegative, |
| 5708 | int64_t *pnExponent) |
| 5709 | { |
| 5710 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5711 | return; |
| 5712 | } |
| 5713 | |
| 5714 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5715 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5716 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5717 | return; |
| 5718 | } |
| 5719 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5720 | const TagSpecification TagSpec = |
| 5721 | { |
| 5722 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5723 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5724 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5725 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5726 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5727 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5728 | ProcessMantissaAndExponentBig(pMe, |
| 5729 | TagSpec, |
| 5730 | &Item, |
| 5731 | BufferForMantissa, |
| 5732 | pMantissa, |
| 5733 | pbIsNegative, |
| 5734 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5738 | /* |
| 5739 | Public function, see header qcbor/qcbor_decode.h file |
| 5740 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5741 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5742 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5743 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5744 | UsefulBuf BufferForMantissa, |
| 5745 | UsefulBufC *pMantissa, |
| 5746 | bool *pbIsNegative, |
| 5747 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5748 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5749 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5750 | return; |
| 5751 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5752 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5753 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5754 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5755 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5756 | return; |
| 5757 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5758 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5759 | const TagSpecification TagSpec = |
| 5760 | { |
| 5761 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5762 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5763 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5764 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5765 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5766 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5767 | ProcessMantissaAndExponentBig(pMe, |
| 5768 | TagSpec, |
| 5769 | &Item, |
| 5770 | BufferForMantissa, |
| 5771 | pMantissa, |
| 5772 | pbIsNegative, |
| 5773 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5774 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5775 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5776 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |