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 | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 1990 | /** TODO: fix this documentation |
| 1991 | * @brief Convert different epoch date formats in to the QCBOR epoch date format |
| 1992 | * |
| 1993 | * pDecodedItem[in,out] The data item to convert. |
| 1994 | * |
| 1995 | * @retval QCBOR_ERR_DATE_OVERFLOW |
| 1996 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED |
| 1997 | * @retval QCBOR_ERR_BAD_TAG_CONTENT |
| 1998 | * |
| 1999 | * The epoch date tag defined in QCBOR allows for floating-point |
| 2000 | * dates. It even allows a protocol to flop between date formats when |
| 2001 | * ever it wants. Floating-point dates aren't that useful as they are |
| 2002 | * only needed for dates beyond the age of the earth. |
| 2003 | * |
| 2004 | * This converts all the date formats into one format of an unsigned |
| 2005 | * integer plus a floating-point fraction. |
| 2006 | */ |
| 2007 | static QCBORError DecodeDaysEpoch(QCBORItem *pDecodedItem) |
| 2008 | { |
| 2009 | QCBORError uReturn = QCBOR_SUCCESS; |
| 2010 | |
| 2011 | switch (pDecodedItem->uDataType) { |
| 2012 | |
| 2013 | case QCBOR_TYPE_INT64: |
| 2014 | pDecodedItem->val.epochDays = pDecodedItem->val.int64; |
| 2015 | break; |
| 2016 | |
| 2017 | case QCBOR_TYPE_UINT64: |
| 2018 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2019 | * always an overflow. |
| 2020 | */ |
| 2021 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2022 | goto Done; |
| 2023 | break; |
| 2024 | |
| 2025 | default: |
| 2026 | uReturn = QCBOR_ERR_BAD_TAG_CONTENT; |
| 2027 | goto Done; |
| 2028 | } |
| 2029 | |
| 2030 | pDecodedItem->uDataType = QCBOR_TYPE_DAYS_EPOCH; |
| 2031 | |
| 2032 | Done: |
| 2033 | return uReturn; |
| 2034 | } |
| 2035 | |
| 2036 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2037 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2038 | /** |
| 2039 | * @brief Decode decimal fractions and big floats. |
| 2040 | * |
| 2041 | * @param[in] pMe The decode context. |
| 2042 | * @param[in,out] pDecodedItem On input the array data item that |
| 2043 | * holds the mantissa and exponent. On |
| 2044 | * output the decoded mantissa and |
| 2045 | * exponent. |
| 2046 | * |
| 2047 | * @returns Decoding errors from getting primitive data items or |
| 2048 | * \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA. |
| 2049 | * |
| 2050 | * When called pDecodedItem must be the array that is tagged as a big |
| 2051 | * float or decimal fraction, the array that has the two members, the |
| 2052 | * exponent and mantissa. |
| 2053 | * |
| 2054 | * This will fetch and decode the exponent and mantissa and put the |
| 2055 | * result back into pDecodedItem. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2056 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2057 | static inline QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2058 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2059 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2060 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2061 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2062 | /* --- Make sure it is an array; track nesting level of members --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2063 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2064 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2065 | goto Done; |
| 2066 | } |
| 2067 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2068 | /* A check for pDecodedItem->val.uCount == 2 would work for |
| 2069 | * definite-length arrays, but not for indefnite. Instead remember |
| 2070 | * the nesting level the two integers must be at, which is one |
| 2071 | * deeper than that of the array. |
| 2072 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2073 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 2074 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2075 | /* --- Which is it, decimal fraction or a bigfloat? --- */ |
| 2076 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(pMe, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2077 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 2078 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2079 | /* --- Get the exponent --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2080 | QCBORItem exponentItem; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2081 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, &exponentItem); |
| 2082 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2083 | goto Done; |
| 2084 | } |
| 2085 | if(exponentItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2086 | /* Array is empty or a map/array encountered when expecting an int */ |
| 2087 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2088 | goto Done; |
| 2089 | } |
| 2090 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2091 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2092 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2093 | * also means that the only data arriving here of type |
| 2094 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2095 | * and thus an error that will get handled in the next else. |
| 2096 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2097 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2098 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2099 | /* Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2100 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2101 | goto Done; |
| 2102 | } |
| 2103 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2104 | /* --- Get the mantissa --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2105 | QCBORItem mantissaItem; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2106 | uReturn = QCBORDecode_GetNextWithTags(pMe, &mantissaItem, NULL); |
| 2107 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2108 | goto Done; |
| 2109 | } |
| 2110 | if(mantissaItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2111 | /* Mantissa missing or map/array encountered when expecting number */ |
| 2112 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2113 | goto Done; |
| 2114 | } |
| 2115 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2116 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2117 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2118 | * also means that the only data arriving here of type |
| 2119 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2120 | * and thus an error that will get handled in an else below. |
| 2121 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2122 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2123 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2124 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2125 | /* Got a good big num mantissa */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2126 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2127 | /* Depends on numbering of QCBOR_TYPE_XXX */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2128 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 2129 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 2130 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2131 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2132 | /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2133 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2134 | goto Done; |
| 2135 | } |
| 2136 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2137 | /* --- Check that array only has the two numbers --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2138 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2139 | /* Extra items in the decimal fraction / big float */ |
| 2140 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2141 | goto Done; |
| 2142 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2143 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2144 | |
| 2145 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2146 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2147 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2148 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2149 | |
| 2150 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2151 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2152 | /** |
| 2153 | * @brief Decode the MIME type tag |
| 2154 | * |
| 2155 | * @param[in,out] pDecodedItem The item to decode. |
| 2156 | * |
| 2157 | * Handle the text and binary MIME type tags. Slightly too complicated |
| 2158 | * f or ProcessTaggedString() because the RFC 7049 MIME type was |
| 2159 | * incorreclty text-only. |
| 2160 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2161 | static inline QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2162 | { |
| 2163 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2164 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2165 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2166 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2167 | } else { |
| 2168 | return QCBOR_ERR_BAD_OPT_TAG; |
| 2169 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2170 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2171 | return QCBOR_SUCCESS; |
| 2172 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2173 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2174 | |
| 2175 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2176 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2177 | * Table of CBOR tags whose content is either a text string or a byte |
| 2178 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2179 | * of uQCBORtype indicates the content should be a byte string rather |
| 2180 | * than a text string |
| 2181 | */ |
| 2182 | struct StringTagMapEntry { |
| 2183 | uint16_t uTagNumber; |
| 2184 | uint8_t uQCBORtype; |
| 2185 | }; |
| 2186 | |
| 2187 | #define IS_BYTE_STRING_BIT 0x80 |
| 2188 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2189 | |
| 2190 | static const struct StringTagMapEntry StringTagMap[] = { |
| 2191 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 2192 | {CBOR_TAG_DAYS_STRING, QCBOR_TYPE_DAYS_STRING}, |
| 2193 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2194 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2195 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2196 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2197 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2198 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2199 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2200 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2201 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2202 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2203 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2204 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2205 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2206 | }; |
| 2207 | |
| 2208 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2209 | /** |
| 2210 | * @brief Process standard CBOR tags whose content is a string |
| 2211 | * |
| 2212 | * @param[in] uTag The tag. |
| 2213 | * @param[in,out] pDecodedItem The data item. |
| 2214 | * |
| 2215 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2216 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
| 2217 | * \ref QCBOR_ERR_BAD_OPT_TAG if the content type was wrong for the tag. |
| 2218 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2219 | * Process the CBOR tags that whose content is a byte string or a text |
| 2220 | * string and for which the string is just passed on to the caller. |
| 2221 | * |
| 2222 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2223 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2224 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2225 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2226 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2227 | static inline QCBORError |
| 2228 | ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2229 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2230 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2231 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2232 | return QCBOR_ERR_UNSUPPORTED; |
| 2233 | } |
| 2234 | |
| 2235 | unsigned uIndex; |
| 2236 | for(uIndex = 0; StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2237 | if(StringTagMap[uIndex].uTagNumber == uTag) { |
| 2238 | break; |
| 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | const uint8_t uQCBORType = StringTagMap[uIndex].uQCBORtype; |
| 2243 | if(uQCBORType == QCBOR_TYPE_NONE) { |
| 2244 | /* repurpose this error to mean, not handled here */ |
| 2245 | return QCBOR_ERR_UNSUPPORTED; |
| 2246 | } |
| 2247 | |
| 2248 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2249 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2250 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2251 | } |
| 2252 | |
| 2253 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2254 | return QCBOR_ERR_BAD_OPT_TAG; |
| 2255 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2256 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2257 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2258 | return QCBOR_SUCCESS; |
| 2259 | } |
| 2260 | |
| 2261 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2262 | /** |
| 2263 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2264 | * |
| 2265 | * @param[in] pMe The decode context. |
| 2266 | * @param[out] pDecodedItem The decoded item. |
| 2267 | * |
| 2268 | * @return Decoding error code. |
| 2269 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2270 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2271 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2272 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2273 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2274 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2275 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2276 | QCBORDecode_GetNextTagContent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2277 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2278 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2279 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2280 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2281 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2282 | goto Done; |
| 2283 | } |
| 2284 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2285 | /* When there are no tag numbers for the item, this exits first |
| 2286 | * thing and effectively does nothing. |
| 2287 | * |
| 2288 | * This loops over all the tag numbers accumulated for this item |
| 2289 | * trying to decode and interpret them. This stops at the end of |
| 2290 | * the list or at the first tag number that can't be interpreted by |
| 2291 | * this code. This is effectively a recursive processing of the |
| 2292 | * tags number list that handles nested tags. |
| 2293 | */ |
| 2294 | while(1) { |
| 2295 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2296 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2297 | */ |
| 2298 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2299 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2300 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2301 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2302 | break; |
| 2303 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2304 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2305 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2306 | |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 2307 | } else if(uTagToProcess == CBOR_TAG_DAYS_EPOCH) { |
| 2308 | uReturn = DecodeDaysEpoch(pDecodedItem); |
| 2309 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2310 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2311 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2312 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2313 | uReturn = QCBORDecode_MantissaAndExponent(pMe, pDecodedItem); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2314 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2315 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2316 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2317 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2318 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2319 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2320 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2321 | } else { |
| 2322 | /* See if it is a pass-through byte/text string tag; process if so */ |
| 2323 | uReturn = ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2324 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2325 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
| 2326 | /* It wasn't a pass-through byte/text string tag so it is |
| 2327 | * an unknown tag. This is the exit from the loop on the |
| 2328 | * first unknown tag. It is a successful exit. |
| 2329 | */ |
| 2330 | uReturn = QCBOR_SUCCESS; |
| 2331 | break; |
| 2332 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2333 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2334 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2335 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2336 | /* Error exit from the loop */ |
| 2337 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2338 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2339 | |
| 2340 | /* A tag was successfully processed, shift it out of the list of |
| 2341 | * tags returned. This is the loop increment. |
| 2342 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2343 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2347 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2351 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2352 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2353 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2354 | QCBORError |
| 2355 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2356 | { |
| 2357 | QCBORError uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2358 | uErr = QCBORDecode_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2359 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2360 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2361 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2362 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2363 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2368 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2369 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2370 | QCBORError |
| 2371 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2372 | { |
| 2373 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2374 | const UsefulInputBuf Save = pMe->InBuf; |
| 2375 | |
| 2376 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2377 | |
| 2378 | pMe->nesting = SaveNesting; |
| 2379 | pMe->InBuf = Save; |
| 2380 | |
| 2381 | return uErr; |
| 2382 | } |
| 2383 | |
| 2384 | |
| 2385 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2386 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2387 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2388 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2389 | { |
| 2390 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2391 | return; |
| 2392 | } |
| 2393 | |
| 2394 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2395 | } |
| 2396 | |
| 2397 | |
| 2398 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2399 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2400 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2401 | QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2402 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2403 | QCBORItem *pDecodedItem, |
| 2404 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2405 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2406 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2407 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2408 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2409 | if(uReturn != QCBOR_SUCCESS) { |
| 2410 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | if(pTags != NULL) { |
| 2414 | pTags->uNumUsed = 0; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2415 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2416 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2417 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2418 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2419 | } |
| 2420 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2421 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2422 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2423 | pTags->puTags[pTags->uNumUsed] = UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2424 | pTags->uNumUsed++; |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2432 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2433 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2434 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2435 | bool QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2436 | const QCBORItem *pItem, |
| 2437 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2438 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2439 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2440 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2441 | break; |
| 2442 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2443 | if(UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2444 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2445 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2446 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2447 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2448 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | |
| 2452 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2453 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2454 | */ |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2455 | QCBORError QCBORDecode_PartialFinish(QCBORDecodeContext *pMe, size_t *puConsumed) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2456 | { |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2457 | if(puConsumed != NULL) { |
| 2458 | *puConsumed = pMe->InBuf.cursor; |
| 2459 | } |
| 2460 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2461 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2462 | |
| 2463 | if(uReturn != QCBOR_SUCCESS) { |
| 2464 | goto Done; |
| 2465 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2466 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2467 | /* Error out if all the maps/arrays are not closed out */ |
| 2468 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2469 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2470 | goto Done; |
| 2471 | } |
| 2472 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2473 | /* Error out if not all the bytes are consumed */ |
| 2474 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2475 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2476 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2477 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2478 | Done: |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2479 | return uReturn; |
| 2480 | } |
| 2481 | |
| 2482 | |
| 2483 | /* |
| 2484 | * Public function, see header qcbor/qcbor_decode.h file |
| 2485 | */ |
| 2486 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *pMe) |
| 2487 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2488 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2489 | /* Call the destructor for the string allocator if there is one. |
| 2490 | * Always called, even if there are errors; always have to clean up. |
| 2491 | */ |
| 2492 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2493 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2494 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2495 | return QCBORDecode_PartialFinish(pMe, NULL); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2499 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2500 | * Public function, see header qcbor/qcbor_decode.h file |
| 2501 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2502 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2503 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2504 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2505 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2506 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2507 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2508 | return CBOR_TAG_INVALID64; |
| 2509 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2510 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2511 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2512 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2513 | return UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2517 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2518 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2519 | * Public function, see header qcbor/qcbor_decode.h file |
| 2520 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2521 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2522 | uint32_t uIndex) |
| 2523 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2524 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2525 | return CBOR_TAG_INVALID64; |
| 2526 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2527 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2528 | return CBOR_TAG_INVALID64; |
| 2529 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2530 | return UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2531 | } |
| 2532 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2533 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2534 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2535 | |
| 2536 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2537 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2538 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2539 | /* =========================================================================== |
| 2540 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2541 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2542 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2543 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2544 | implements the function type QCBORStringAllocate and allows easy |
| 2545 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2546 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2547 | This particular allocator is built-in for convenience. The caller |
| 2548 | can implement their own. All of this following code will get |
| 2549 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2550 | |
| 2551 | This is a very primitive memory allocator. It does not track |
| 2552 | individual allocations, only a high-water mark. A free or |
| 2553 | reallocation must be of the last chunk allocated. |
| 2554 | |
| 2555 | The size of the pool and offset to free memory are packed into the |
| 2556 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2557 | the decode context. Since the address of the pool may not be |
| 2558 | aligned, they have to be packed and unpacked as if they were |
| 2559 | serialized data of the wire or such. |
| 2560 | |
| 2561 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2562 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2563 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2564 | |
| 2565 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2566 | static inline int |
| 2567 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2568 | { |
| 2569 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2570 | UsefulInputBuf UIB; |
| 2571 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2572 | // Just assume the size here. It was checked during SetUp so |
| 2573 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2574 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2575 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2576 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2577 | return UsefulInputBuf_GetError(&UIB); |
| 2578 | } |
| 2579 | |
| 2580 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2581 | static inline int |
| 2582 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2583 | { |
| 2584 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2585 | // length check performed here is useful. |
| 2586 | UsefulOutBuf UOB; |
| 2587 | |
| 2588 | UsefulOutBuf_Init(&UOB, Pool); |
| 2589 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2590 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2591 | return UsefulOutBuf_GetError(&UOB); |
| 2592 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2593 | |
| 2594 | |
| 2595 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2596 | Internal function for an allocation, reallocation free and destuct. |
| 2597 | |
| 2598 | Having only one function rather than one each per mode saves space in |
| 2599 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2600 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2601 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2602 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2603 | static UsefulBuf |
| 2604 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2605 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2606 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2607 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2608 | uint32_t uPoolSize; |
| 2609 | uint32_t uFreeOffset; |
| 2610 | |
| 2611 | if(uNewSize > UINT32_MAX) { |
| 2612 | // This allocator is only good up to 4GB. This check should |
| 2613 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2614 | goto Done; |
| 2615 | } |
| 2616 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2617 | |
| 2618 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2619 | goto Done; |
| 2620 | } |
| 2621 | |
| 2622 | if(uNewSize) { |
| 2623 | if(pMem) { |
| 2624 | // REALLOCATION MODE |
| 2625 | // Calculate pointer to the end of the memory pool. It is |
| 2626 | // assumed that pPool + uPoolSize won't wrap around by |
| 2627 | // assuming the caller won't pass a pool buffer in that is |
| 2628 | // not in legitimate memory space. |
| 2629 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2630 | |
| 2631 | // Check that the pointer for reallocation is in the range of the |
| 2632 | // pool. This also makes sure that pointer math further down |
| 2633 | // doesn't wrap under or over. |
| 2634 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2635 | // Offset to start of chunk for reallocation. This won't |
| 2636 | // wrap under because of check that pMem >= pPool. Cast |
| 2637 | // is safe because the pool is always less than UINT32_MAX |
| 2638 | // because of check in QCBORDecode_SetMemPool(). |
| 2639 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2640 | |
| 2641 | // Check to see if the allocation will fit. uPoolSize - |
| 2642 | // uMemOffset will not wrap under because of check that |
| 2643 | // pMem is in the range of the uPoolSize by check above. |
| 2644 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2645 | ReturnValue.ptr = pMem; |
| 2646 | ReturnValue.len = uNewSize; |
| 2647 | |
| 2648 | // Addition won't wrap around over because uNewSize was |
| 2649 | // checked to be sure it is less than the pool size. |
| 2650 | uFreeOffset = uMemOffset + uNewSize32; |
| 2651 | } |
| 2652 | } |
| 2653 | } else { |
| 2654 | // ALLOCATION MODE |
| 2655 | // uPoolSize - uFreeOffset will not underflow because this |
| 2656 | // pool implementation makes sure uFreeOffset is always |
| 2657 | // smaller than uPoolSize through this check here and |
| 2658 | // reallocation case. |
| 2659 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2660 | ReturnValue.len = uNewSize; |
| 2661 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2662 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2663 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2664 | } |
| 2665 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2666 | if(pMem) { |
| 2667 | // FREE MODE |
| 2668 | // Cast is safe because of limit on pool size in |
| 2669 | // QCBORDecode_SetMemPool() |
| 2670 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2671 | } else { |
| 2672 | // DESTRUCT MODE |
| 2673 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2674 | } |
| 2675 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2676 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2677 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2678 | MemPool_Pack(Pool, uFreeOffset); |
| 2679 | |
| 2680 | Done: |
| 2681 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2682 | } |
| 2683 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2684 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2685 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2686 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2687 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2688 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2689 | UsefulBuf Pool, |
| 2690 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2691 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2692 | // The pool size and free mem offset are packed into the beginning |
| 2693 | // of the pool memory. This compile time check make sure the |
| 2694 | // constant in the header is correct. This check should optimize |
| 2695 | // down to nothing. |
| 2696 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2697 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2698 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2699 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2700 | // The pool size and free offset packed in to the beginning of pool |
| 2701 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2702 | // machines. |
| 2703 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2704 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2705 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2706 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2707 | // This checks that the pool buffer given is big enough. |
| 2708 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2709 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2710 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2711 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2712 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2713 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2714 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2715 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2716 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2717 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2718 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2719 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2720 | |
| 2721 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2722 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2723 | { |
| 2724 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2725 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2726 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2727 | |
| 2728 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2729 | Consume an entire map or array (and do next to |
| 2730 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2731 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2732 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2733 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2734 | const QCBORItem *pItemToConsume, |
| 2735 | uint_fast8_t *puNextNestLevel) |
| 2736 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2737 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2738 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2739 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2740 | // If it is a map or array, this will tell if it is empty. |
| 2741 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2742 | |
| 2743 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2744 | /* 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] | 2745 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2746 | /* This works for definite- and indefinite- length |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2747 | * maps and arrays by using the nesting level |
| 2748 | */ |
| 2749 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2750 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2751 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2752 | goto Done; |
| 2753 | } |
| 2754 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2755 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2756 | *puNextNestLevel = Item.uNextNestLevel; |
| 2757 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2758 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2759 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2760 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2761 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2762 | /* Just pass the nesting level through */ |
| 2763 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2764 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2765 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2766 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2767 | |
| 2768 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2769 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 2772 | |
| 2773 | void QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2774 | { |
| 2775 | uint8_t uNextNestLevel; |
| 2776 | |
| 2777 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 2778 | |
| 2779 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2780 | pMe->uLastError = (uint8_t)ConsumeItem(pMe, pDecodedItem, &uNextNestLevel); |
| 2781 | pDecodedItem->uNextNestLevel = uNextNestLevel; |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | |
| 2786 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 2787 | /* Call only on maps and arrays. Rewinds the cursor |
| 2788 | * to the start as if it was just entered. |
| 2789 | */ |
| 2790 | static void RewindMapOrArray(QCBORDecodeContext *pMe) |
| 2791 | { |
| 2792 | /* Reset nesting tracking to the deepest bounded level */ |
| 2793 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 2794 | |
| 2795 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 2796 | |
| 2797 | /* Reposition traversal cursor to the start of the map/array */ |
| 2798 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2799 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 2800 | } |
| 2801 | |
| 2802 | |
| 2803 | /* |
| 2804 | Public function, see header qcbor/qcbor_decode.h file |
| 2805 | */ |
| 2806 | void QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
| 2807 | { |
| 2808 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 2809 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 2810 | |
| 2811 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 2812 | /* In bstr-wrapped CBOR. */ |
| 2813 | |
| 2814 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 2815 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2816 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 2817 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 2818 | |
| 2819 | } else { |
| 2820 | /* In a map or array */ |
| 2821 | RewindMapOrArray(pMe); |
| 2822 | } |
| 2823 | |
| 2824 | } else { |
| 2825 | /* Not in anything bounded */ |
| 2826 | |
| 2827 | /* Reposition traversal cursor to the start of input CBOR */ |
| 2828 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 2829 | |
| 2830 | /* Reset nesting tracking to beginning of input. */ |
| 2831 | DecodeNesting_Init(&(pMe->nesting)); |
| 2832 | } |
| 2833 | |
| 2834 | pMe->uLastError = QCBOR_SUCCESS; |
| 2835 | } |
| 2836 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2837 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2838 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2839 | Works only for integer and string labels. Returns false |
| 2840 | for any other type. */ |
| 2841 | static inline bool |
| 2842 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2843 | { |
| 2844 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2845 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2846 | return true; |
| 2847 | } |
| 2848 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2849 | 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] | 2850 | return true; |
| 2851 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2852 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2853 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2854 | return true; |
| 2855 | } |
| 2856 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2857 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2858 | return true; |
| 2859 | } |
| 2860 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2861 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2862 | /* Other label types are never matched */ |
| 2863 | return false; |
| 2864 | } |
| 2865 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2866 | |
| 2867 | /* |
| 2868 | Returns true if Item1 and Item2 are the same type |
| 2869 | or if either are of QCBOR_TYPE_ANY. |
| 2870 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2871 | static inline bool |
| 2872 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2873 | { |
| 2874 | if(Item1.uDataType == Item2.uDataType) { |
| 2875 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2876 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2877 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2878 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2879 | return true; |
| 2880 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2881 | return false; |
| 2882 | } |
| 2883 | |
| 2884 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2885 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2886 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2887 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2888 | @param[in] pMe The decode context to search. |
| 2889 | @param[in,out] pItemArray The items to search for and the items found. |
| 2890 | @param[out] puOffset Byte offset of last item matched. |
| 2891 | @param[in] pCBContext Context for the not-found item call back. |
| 2892 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2893 | |
| 2894 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2895 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2896 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2897 | were found for one of the labels being |
| 2898 | search for. This duplicate detection is |
| 2899 | only performed for items in pItemArray, |
| 2900 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2901 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2902 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2903 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2904 | |
| 2905 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2906 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2907 | On input pItemArray contains a list of labels and data types |
| 2908 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2909 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2910 | On output the fully retrieved items are filled in with |
| 2911 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2912 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2913 | 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] | 2914 | |
| 2915 | 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] | 2916 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2917 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2918 | MapSearch(QCBORDecodeContext *pMe, |
| 2919 | QCBORItem *pItemArray, |
| 2920 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2921 | void *pCBContext, |
| 2922 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2923 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2924 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2925 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2926 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2927 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2928 | uReturn = pMe->uLastError; |
| 2929 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2930 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2931 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2932 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2933 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2934 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2935 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2936 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2937 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2940 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2941 | // It is an empty bounded array or map |
| 2942 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2943 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2944 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2945 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2946 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2947 | // Nothing is ever found in an empty array or map. All items |
| 2948 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2949 | uReturn = QCBOR_SUCCESS; |
| 2950 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2951 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2954 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2955 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2956 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2957 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 2958 | RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2959 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2960 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2961 | Loop over all the items in the map or array. Each item |
| 2962 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2963 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2964 | length maps and arrays. The only reason this is ever |
| 2965 | called on arrays is to find their end position. |
| 2966 | |
| 2967 | This will always run over all items in order to do |
| 2968 | duplicate detection. |
| 2969 | |
| 2970 | This will exit with failure if it encounters an |
| 2971 | unrecoverable error, but continue on for recoverable |
| 2972 | errors. |
| 2973 | |
| 2974 | If a recoverable error occurs on a matched item, then |
| 2975 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2976 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2977 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2978 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2979 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2980 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2981 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2982 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2983 | /* Get the item */ |
| 2984 | QCBORItem Item; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2985 | QCBORError uResult = QCBORDecode_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2986 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2987 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2988 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2989 | goto Done; |
| 2990 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2991 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2992 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2993 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2994 | goto Done; |
| 2995 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2996 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2997 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2998 | bool bMatched = false; |
| 2999 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 3000 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3001 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3002 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 3003 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3004 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3005 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3006 | if(uResult != QCBOR_SUCCESS) { |
| 3007 | /* The label matches, but the data item is in error */ |
| 3008 | uReturn = uResult; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3009 | goto Done; |
| 3010 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3011 | if(!MatchType(Item, pItemArray[nIndex])) { |
| 3012 | /* The data item is not of the type(s) requested */ |
| 3013 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3014 | goto Done; |
| 3015 | } |
| 3016 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3017 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3018 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3019 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3020 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3021 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3022 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3023 | bMatched = true; |
| 3024 | } |
| 3025 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3026 | |
| 3027 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3028 | if(!bMatched && pfCallback != NULL) { |
| 3029 | /* |
| 3030 | Call the callback on unmatched labels. |
| 3031 | (It is tempting to do duplicate detection here, but that would |
| 3032 | require dynamic memory allocation because the number of labels |
| 3033 | that might be encountered is unbounded.) |
| 3034 | */ |
| 3035 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 3036 | if(uReturn != QCBOR_SUCCESS) { |
| 3037 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3038 | } |
| 3039 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3040 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3041 | /* |
| 3042 | Consume the item whether matched or not. This |
| 3043 | does the work of traversing maps and array and |
| 3044 | everything in them. In this loop only the |
| 3045 | items at the current nesting level are examined |
| 3046 | to match the labels. |
| 3047 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3048 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3049 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3050 | goto Done; |
| 3051 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3052 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3053 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3054 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3055 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3056 | |
| 3057 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3058 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3059 | // Check here makes sure that this won't accidentally be |
| 3060 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3061 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3062 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3063 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3064 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3065 | goto Done; |
| 3066 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3067 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3068 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3069 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3070 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3071 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3072 | |
| 3073 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3074 | /* 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] | 3075 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3076 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3077 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3078 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3079 | } |
| 3080 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3081 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3082 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3085 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3086 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3087 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3088 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3089 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3090 | int64_t nLabel, |
| 3091 | uint8_t uQcborType, |
| 3092 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3093 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3094 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3095 | return; |
| 3096 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3097 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3098 | QCBORItem OneItemSeach[2]; |
| 3099 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3100 | OneItemSeach[0].label.int64 = nLabel; |
| 3101 | OneItemSeach[0].uDataType = uQcborType; |
| 3102 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3103 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3104 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3105 | |
| 3106 | *pItem = OneItemSeach[0]; |
| 3107 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3108 | if(uReturn != QCBOR_SUCCESS) { |
| 3109 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3110 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3111 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3112 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3113 | } |
| 3114 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3115 | Done: |
| 3116 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3120 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3121 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3122 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3123 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3124 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3125 | uint8_t uQcborType, |
| 3126 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3127 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3128 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3129 | return; |
| 3130 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3131 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3132 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3133 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3134 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3135 | OneItemSeach[0].uDataType = uQcborType; |
| 3136 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3137 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3138 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 3139 | if(uReturn != QCBOR_SUCCESS) { |
| 3140 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3141 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3142 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3143 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3144 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3145 | } |
| 3146 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3147 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3148 | |
| 3149 | Done: |
| 3150 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3154 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3155 | static QCBORError |
| 3156 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3157 | { |
| 3158 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 3159 | if(uDataType == puTypeList[i]) { |
| 3160 | return QCBOR_SUCCESS; |
| 3161 | } |
| 3162 | } |
| 3163 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3164 | } |
| 3165 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3166 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3167 | /** |
| 3168 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3169 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3170 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3171 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3172 | @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] | 3173 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3174 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered |
| 3175 | tag value. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3176 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3177 | static QCBORError |
| 3178 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3179 | { |
| 3180 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3181 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3182 | /* There are tags that QCBOR couldn't process on this item and |
| 3183 | the caller has told us there should not be. */ |
| 3184 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3185 | } |
| 3186 | |
| 3187 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3188 | const int nItemType = pItem->uDataType; |
| 3189 | |
| 3190 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3191 | // Must match the tag and only the tag |
| 3192 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3193 | } |
| 3194 | |
| 3195 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3196 | if(uReturn == QCBOR_SUCCESS) { |
| 3197 | return QCBOR_SUCCESS; |
| 3198 | } |
| 3199 | |
| 3200 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3201 | /* Must match the content type and only the content type. |
| 3202 | There was no match just above so it is a fail. */ |
| 3203 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3204 | } |
| 3205 | |
| 3206 | /* If here it can match either the tag or the content |
| 3207 | and it hasn't matched the content, so the end |
| 3208 | result is whether it matches the tag. This is |
| 3209 | also the case that the CBOR standard discourages. */ |
| 3210 | |
| 3211 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3212 | } |
| 3213 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3214 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3215 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3216 | // This could be semi-private if need be |
| 3217 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3218 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3219 | int64_t nLabel, |
| 3220 | TagSpecification TagSpec, |
| 3221 | QCBORItem *pItem) |
| 3222 | { |
| 3223 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3224 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3225 | return; |
| 3226 | } |
| 3227 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3228 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3229 | } |
| 3230 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3231 | |
| 3232 | // This could be semi-private if need be |
| 3233 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3234 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3235 | const char *szLabel, |
| 3236 | TagSpecification TagSpec, |
| 3237 | QCBORItem *pItem) |
| 3238 | { |
| 3239 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3240 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3241 | return; |
| 3242 | } |
| 3243 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3244 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3247 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3248 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3249 | int64_t nLabel, |
| 3250 | TagSpecification TagSpec, |
| 3251 | UsefulBufC *pString) |
| 3252 | { |
| 3253 | QCBORItem Item; |
| 3254 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3255 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3256 | *pString = Item.val.string; |
| 3257 | } |
| 3258 | } |
| 3259 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3260 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3261 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3262 | const char * szLabel, |
| 3263 | TagSpecification TagSpec, |
| 3264 | UsefulBufC *pString) |
| 3265 | { |
| 3266 | QCBORItem Item; |
| 3267 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3268 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3269 | *pString = Item.val.string; |
| 3270 | } |
| 3271 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3272 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3273 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3274 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3275 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3276 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3277 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3278 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 3279 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3280 | } |
| 3281 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3282 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3283 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3284 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3285 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3286 | QCBORItem *pItemList, |
| 3287 | void *pCallbackCtx, |
| 3288 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3289 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3290 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 3291 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3295 | /** |
| 3296 | * @brief Search for a map/array by label and enter it |
| 3297 | * |
| 3298 | * @param[in] pMe The decode context. |
| 3299 | * @param[in] pSearch The map/array to search for. |
| 3300 | * |
| 3301 | * @c pSearch is expected to contain one item of type map or array |
| 3302 | * with the label specified. The current bounded map will be searched for |
| 3303 | * this and if found will be entered. |
| 3304 | * |
| 3305 | * If the label is not found, or the item found is not a map or array, |
| 3306 | * the error state is set. |
| 3307 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3308 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3309 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3310 | // The first item in pSearch is the one that is to be |
| 3311 | // entered. It should be the only one filled in. Any other |
| 3312 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3313 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3314 | return; |
| 3315 | } |
| 3316 | |
| 3317 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3318 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3319 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3320 | return; |
| 3321 | } |
| 3322 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3323 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3324 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3325 | return; |
| 3326 | } |
| 3327 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3328 | |
| 3329 | /* The map or array was found. Now enter it. |
| 3330 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3331 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3332 | * next item for the pre-order traversal cursor to be the map/array |
| 3333 | * found by MapSearch(). The next few lines of code force the |
| 3334 | * cursor to that. |
| 3335 | * |
| 3336 | * There is no need to retain the old cursor because |
| 3337 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3338 | * beginning of the map/array being entered. |
| 3339 | * |
| 3340 | * The cursor is forced by: 1) setting the input buffer position to |
| 3341 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3342 | * counter to the total in the map/array, 3) setting the nesting |
| 3343 | * level. Setting the map/array counter to the total is not |
| 3344 | * strictly correct, but this is OK because this cursor only needs |
| 3345 | * to be used to get one item and MapSearch() has already found it |
| 3346 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3347 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3348 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3349 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3350 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3351 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3352 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3353 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3354 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3358 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3359 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3360 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3361 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3362 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3363 | QCBORItem OneItemSeach[2]; |
| 3364 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3365 | OneItemSeach[0].label.int64 = nLabel; |
| 3366 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3367 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3368 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3369 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3370 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3374 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3375 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3376 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3377 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3378 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3379 | QCBORItem OneItemSeach[2]; |
| 3380 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3381 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3382 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3383 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3384 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3385 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3386 | } |
| 3387 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3388 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3389 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3390 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3391 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3392 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3393 | QCBORItem OneItemSeach[2]; |
| 3394 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3395 | OneItemSeach[0].label.int64 = nLabel; |
| 3396 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3397 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3398 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3399 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3402 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3403 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3404 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3405 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3406 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3407 | QCBORItem OneItemSeach[2]; |
| 3408 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3409 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3410 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3411 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3412 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3413 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3417 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3418 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3419 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3420 | QCBORError uErr; |
| 3421 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3422 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3423 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3424 | // Already in error state; do nothing. |
| 3425 | return; |
| 3426 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3427 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3428 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3429 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3430 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3431 | if(uErr != QCBOR_SUCCESS) { |
| 3432 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3433 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3434 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3435 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3436 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3439 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3440 | |
| 3441 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3442 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3443 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3444 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3445 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3446 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3447 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3448 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3449 | // Special case to increment nesting level for zero-length maps |
| 3450 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3451 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3452 | } |
| 3453 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3454 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3455 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3456 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3457 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3458 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3459 | if(pItem != NULL) { |
| 3460 | *pItem = Item; |
| 3461 | } |
| 3462 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3463 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3464 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3465 | } |
| 3466 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3467 | |
| 3468 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3469 | This is the common work for exiting a level that is a bounded map, |
| 3470 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3471 | |
| 3472 | One chunk of work is to set up the pre-order traversal so it is at |
| 3473 | the item just after the bounded map, array or bstr that is being |
| 3474 | exited. This is somewhat complex. |
| 3475 | |
| 3476 | The other work is to level-up the bounded mode to next higest bounded |
| 3477 | mode or the top level if there isn't one. |
| 3478 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3479 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3480 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3481 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3482 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3483 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3484 | /* |
| 3485 | First the pre-order-traversal byte offset is positioned to the |
| 3486 | item just after the bounded mode item that was just consumed. |
| 3487 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3488 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3489 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3490 | /* |
| 3491 | Next, set the current nesting level to one above the bounded level |
| 3492 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3493 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3494 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3495 | makes sure pCurrentBounded is valid. |
| 3496 | */ |
| 3497 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3498 | |
| 3499 | /* |
| 3500 | This does the complex work of leveling up the pre-order traversal |
| 3501 | when the end of a map or array or another bounded level is |
| 3502 | reached. It may do nothing, or ascend all the way to the top |
| 3503 | level. |
| 3504 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3505 | uErr = QCBORDecode_NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3506 | if(uErr != QCBOR_SUCCESS) { |
| 3507 | goto Done; |
| 3508 | } |
| 3509 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3510 | /* |
| 3511 | This makes the next highest bounded level the current bounded |
| 3512 | level. If there is no next highest level, then no bounded mode is |
| 3513 | in effect. |
| 3514 | */ |
| 3515 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3516 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3517 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3518 | |
| 3519 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3520 | return uErr; |
| 3521 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3522 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3523 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3524 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3525 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3526 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3527 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3528 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3529 | return; |
| 3530 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3531 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3532 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3533 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3534 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3535 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3536 | goto Done; |
| 3537 | } |
| 3538 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3539 | /* |
| 3540 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3541 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3542 | from previous map search, then do a dummy search. |
| 3543 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3544 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3545 | QCBORItem Dummy; |
| 3546 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3547 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3548 | if(uErr != QCBOR_SUCCESS) { |
| 3549 | goto Done; |
| 3550 | } |
| 3551 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3552 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3553 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3554 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3555 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3556 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3560 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3561 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3562 | const QCBORItem *pItem, |
| 3563 | uint8_t uTagRequirement, |
| 3564 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3565 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3566 | if(pBstr) { |
| 3567 | *pBstr = NULLUsefulBufC; |
| 3568 | } |
| 3569 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3570 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3571 | /* Already in error state; do nothing. */ |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3572 | return pMe->uLastError; |
| 3573 | } |
| 3574 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3575 | QCBORError uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3576 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3577 | const TagSpecification TagSpec = |
| 3578 | { |
| 3579 | uTagRequirement, |
| 3580 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3581 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3582 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3583 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3584 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3585 | if(uError != QCBOR_SUCCESS) { |
| 3586 | goto Done; |
| 3587 | } |
| 3588 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3589 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3590 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 3591 | * increment in QCBORDecode_NestLevelAscender() called by |
| 3592 | * ExitBoundedLevel() will work right. |
| 3593 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3594 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3595 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3596 | |
| 3597 | if(pBstr) { |
| 3598 | *pBstr = pItem->val.string; |
| 3599 | } |
| 3600 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3601 | /* This saves the current length of the UsefulInputBuf and then |
| 3602 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 3603 | * CBOR that is being entered. |
| 3604 | * |
| 3605 | * Most of these calls are simple inline accessors so this doesn't |
| 3606 | * amount to much code. |
| 3607 | */ |
| 3608 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3609 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3610 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 3611 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3612 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3613 | goto Done; |
| 3614 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3615 | |
| 3616 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 3617 | pItem->val.string.ptr); |
| 3618 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 3619 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 3620 | /* This should never happen because pItem->val.string.ptr should |
| 3621 | * always be valid since it was just returned. |
| 3622 | */ |
| 3623 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3624 | goto Done; |
| 3625 | } |
| 3626 | |
| 3627 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 3628 | |
| 3629 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3630 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3631 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3632 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3633 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3634 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3635 | Done: |
| 3636 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3640 | /* |
| 3641 | Public function, see header qcbor/qcbor_decode.h file |
| 3642 | */ |
| 3643 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3644 | uint8_t uTagRequirement, |
| 3645 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3646 | { |
| 3647 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3648 | // Already in error state; do nothing. |
| 3649 | return; |
| 3650 | } |
| 3651 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3652 | /* Get the data item that is the byte string being entered */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3653 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3654 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3655 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3656 | return; |
| 3657 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3658 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3659 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3660 | &Item, |
| 3661 | uTagRequirement, |
| 3662 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3666 | /* |
| 3667 | Public function, see header qcbor/qcbor_decode.h file |
| 3668 | */ |
| 3669 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3670 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3671 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3672 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3673 | { |
| 3674 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3675 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3676 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3677 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3678 | &Item, |
| 3679 | uTagRequirement, |
| 3680 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3681 | } |
| 3682 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3683 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3684 | /* |
| 3685 | Public function, see header qcbor/qcbor_decode.h file |
| 3686 | */ |
| 3687 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3688 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3689 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3690 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3691 | { |
| 3692 | QCBORItem Item; |
| 3693 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3694 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3695 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3696 | &Item, |
| 3697 | uTagRequirement, |
| 3698 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3699 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3700 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3701 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3702 | /* |
| 3703 | Public function, see header qcbor/qcbor_decode.h file |
| 3704 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3705 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3706 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3707 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3708 | // Already in error state; do nothing. |
| 3709 | return; |
| 3710 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3711 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3712 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3713 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3714 | return; |
| 3715 | } |
| 3716 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3717 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 3718 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3719 | /* |
| 3720 | Reset the length of the UsefulInputBuf to what it was before |
| 3721 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3722 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3723 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3724 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3725 | |
| 3726 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3727 | QCBORError uErr = ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3728 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3729 | } |
| 3730 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3731 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3732 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3733 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3734 | static inline void |
| 3735 | ProcessBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3736 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3737 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3738 | /* Already in error state, do nothing */ |
| 3739 | return; |
| 3740 | } |
| 3741 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3742 | switch(pItem->uDataType) { |
| 3743 | case QCBOR_TYPE_TRUE: |
| 3744 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3745 | break; |
| 3746 | |
| 3747 | case QCBOR_TYPE_FALSE: |
| 3748 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3749 | break; |
| 3750 | |
| 3751 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3752 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3753 | break; |
| 3754 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3755 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3756 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3757 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3758 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3759 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3760 | * Public function, see header qcbor/qcbor_decode.h file |
| 3761 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3762 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3763 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3764 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3765 | /* Already in error state, do nothing */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3766 | return; |
| 3767 | } |
| 3768 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3769 | QCBORItem Item; |
| 3770 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3771 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3772 | |
| 3773 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3774 | } |
| 3775 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3776 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3777 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3778 | * Public function, see header qcbor/qcbor_decode.h file |
| 3779 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3780 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3781 | { |
| 3782 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3783 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3784 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3785 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3788 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3789 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3790 | * Public function, see header qcbor/qcbor_decode.h file |
| 3791 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3792 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3793 | { |
| 3794 | QCBORItem Item; |
| 3795 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3796 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 3797 | ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | |
| 3801 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3802 | |
| 3803 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3804 | QCBORItem *pItem, |
| 3805 | uint8_t uTagRequirement, |
| 3806 | int64_t *pnTime) |
| 3807 | { |
| 3808 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3809 | // Already in error state, do nothing |
| 3810 | return; |
| 3811 | } |
| 3812 | |
| 3813 | QCBORError uErr; |
| 3814 | |
| 3815 | const TagSpecification TagSpec = |
| 3816 | { |
| 3817 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3818 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3819 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3820 | }; |
| 3821 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3822 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3823 | if(uErr != QCBOR_SUCCESS) { |
| 3824 | goto Done; |
| 3825 | } |
| 3826 | |
| 3827 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3828 | uErr = DecodeDateEpoch(pItem); |
| 3829 | if(uErr != QCBOR_SUCCESS) { |
| 3830 | goto Done; |
| 3831 | } |
| 3832 | } |
| 3833 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3834 | // Save the tags in the last item's tags in the decode context |
| 3835 | // for QCBORDecode_GetNthTagOfLast() |
| 3836 | CopyTags(pMe, pItem); |
| 3837 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3838 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3839 | |
| 3840 | Done: |
| 3841 | pMe->uLastError = (uint8_t)uErr; |
| 3842 | } |
| 3843 | |
| 3844 | |
| 3845 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3846 | uint8_t uTagRequirement, |
| 3847 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3848 | { |
| 3849 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3850 | // Already in error state, do nothing |
| 3851 | return; |
| 3852 | } |
| 3853 | |
| 3854 | QCBORItem Item; |
| 3855 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3856 | |
| 3857 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3858 | } |
| 3859 | |
| 3860 | |
| 3861 | void |
| 3862 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3863 | int64_t nLabel, |
| 3864 | uint8_t uTagRequirement, |
| 3865 | int64_t *pnTime) |
| 3866 | { |
| 3867 | QCBORItem Item; |
| 3868 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3869 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3870 | } |
| 3871 | |
| 3872 | |
| 3873 | void |
| 3874 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3875 | const char *szLabel, |
| 3876 | uint8_t uTagRequirement, |
| 3877 | int64_t *pnTime) |
| 3878 | { |
| 3879 | QCBORItem Item; |
| 3880 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3881 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3882 | } |
| 3883 | |
| 3884 | |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3885 | |
| 3886 | /* |
| 3887 | * Common processing for the RFC 8943 Day-count tag. Mostly |
| 3888 | * make sure the tag content is correct and copy forward any |
| 3889 | * further other tag numbers. |
| 3890 | */ |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3891 | static void ProcessEpochDays(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3892 | QCBORItem *pItem, |
| 3893 | uint8_t uTagRequirement, |
| 3894 | int64_t *pnDays) |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3895 | { |
| 3896 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3897 | /* Already in error state, do nothing */ |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3898 | return; |
| 3899 | } |
| 3900 | |
| 3901 | QCBORError uErr; |
| 3902 | |
| 3903 | const TagSpecification TagSpec = |
| 3904 | { |
| 3905 | uTagRequirement, |
| 3906 | {QCBOR_TYPE_DAYS_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3907 | {QCBOR_TYPE_INT64, QCBOR_TYPE_UINT64, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3908 | }; |
| 3909 | |
| 3910 | uErr = CheckTagRequirement(TagSpec, pItem); |
| 3911 | if(uErr != QCBOR_SUCCESS) { |
| 3912 | goto Done; |
| 3913 | } |
| 3914 | |
Laurence Lundblade | ac425ab | 2021-05-08 17:17:33 -0700 | [diff] [blame] | 3915 | if(pItem->uDataType != QCBOR_TYPE_DAYS_EPOCH) { |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3916 | uErr = DecodeDaysEpoch(pItem); |
| 3917 | if(uErr != QCBOR_SUCCESS) { |
| 3918 | goto Done; |
| 3919 | } |
| 3920 | } |
| 3921 | |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3922 | /* Save the tags in the last item's tags in the decode context |
| 3923 | * for QCBORDecode_GetNthTagOfLast() |
| 3924 | */ |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3925 | CopyTags(pMe, pItem); |
| 3926 | |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3927 | *pnDays = pItem->val.epochDays; |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3928 | |
| 3929 | Done: |
| 3930 | pMe->uLastError = (uint8_t)uErr; |
| 3931 | } |
| 3932 | |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3933 | |
| 3934 | /* |
| 3935 | * Public function, see header qcbor/qcbor_decode.h |
| 3936 | */ |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3937 | void QCBORDecode_GetEpochDays(QCBORDecodeContext *pMe, |
| 3938 | uint8_t uTagRequirement, |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3939 | int64_t *pnDays) |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3940 | { |
| 3941 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3942 | /* Already in error state, do nothing */ |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3943 | return; |
| 3944 | } |
| 3945 | |
| 3946 | QCBORItem Item; |
| 3947 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3948 | |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3949 | ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3953 | /* |
| 3954 | * Public function, see header qcbor/qcbor_decode.h |
| 3955 | */ |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3956 | void |
| 3957 | QCBORDecode_GetEpochDaysInMapN(QCBORDecodeContext *pMe, |
| 3958 | int64_t nLabel, |
| 3959 | uint8_t uTagRequirement, |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3960 | int64_t *pnDays) |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3961 | { |
| 3962 | QCBORItem Item; |
| 3963 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3964 | ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3968 | /* |
| 3969 | * Public function, see header qcbor/qcbor_decode.h |
| 3970 | */ |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3971 | void |
| 3972 | QCBORDecode_GetEpochDaysInMapSZ(QCBORDecodeContext *pMe, |
| 3973 | const char *szLabel, |
| 3974 | uint8_t uTagRequirement, |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3975 | int64_t *pnDays) |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3976 | { |
| 3977 | QCBORItem Item; |
| 3978 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 7d0f573 | 2021-05-12 00:06:06 -0700 | [diff] [blame^] | 3979 | ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 58541fe | 2021-05-07 01:24:49 -0700 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3983 | |
| 3984 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3985 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3986 | TagSpecification TagSpec, |
| 3987 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3988 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3989 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3990 | // Already in error state, do nothing |
| 3991 | return; |
| 3992 | } |
| 3993 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3994 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3995 | QCBORItem Item; |
| 3996 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3997 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3998 | if(uError != QCBOR_SUCCESS) { |
| 3999 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4000 | return; |
| 4001 | } |
| 4002 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4003 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4004 | |
| 4005 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4006 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 4007 | } else { |
| 4008 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4009 | } |
| 4010 | } |
| 4011 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4012 | |
| 4013 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4014 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4015 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4016 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4017 | UsefulBufC *pValue, |
| 4018 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4019 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4020 | const TagSpecification TagSpec = |
| 4021 | { |
| 4022 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4023 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4024 | {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] | 4025 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4026 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4027 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4028 | if(uErr != QCBOR_SUCCESS) { |
| 4029 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4030 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4031 | |
| 4032 | *pValue = pItem->val.string; |
| 4033 | |
| 4034 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 4035 | *pbIsNegative = false; |
| 4036 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 4037 | *pbIsNegative = true; |
| 4038 | } |
| 4039 | |
| 4040 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4044 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4045 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4046 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4047 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 4048 | uint8_t uTagRequirement, |
| 4049 | UsefulBufC *pValue, |
| 4050 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4051 | { |
| 4052 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4053 | // Already in error state, do nothing |
| 4054 | return; |
| 4055 | } |
| 4056 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4057 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4058 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4059 | if(uError != QCBOR_SUCCESS) { |
| 4060 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4061 | return; |
| 4062 | } |
| 4063 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4064 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4067 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4068 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4069 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4070 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4071 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 4072 | int64_t nLabel, |
| 4073 | uint8_t uTagRequirement, |
| 4074 | UsefulBufC *pValue, |
| 4075 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4076 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4077 | QCBORItem Item; |
| 4078 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4079 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4080 | return; |
| 4081 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4082 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4083 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4086 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4087 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4088 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4089 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4090 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 4091 | const char *szLabel, |
| 4092 | uint8_t uTagRequirement, |
| 4093 | UsefulBufC *pValue, |
| 4094 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4095 | { |
| 4096 | QCBORItem Item; |
| 4097 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4098 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4099 | return; |
| 4100 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4101 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4102 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | |
| 4106 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4107 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4108 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4109 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 4110 | const QCBORItem *pItem, |
| 4111 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4112 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4113 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4114 | const TagSpecification TagSpecText = |
| 4115 | { |
| 4116 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4117 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4118 | {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] | 4119 | }; |
| 4120 | const TagSpecification TagSpecBinary = |
| 4121 | { |
| 4122 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4123 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4124 | {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] | 4125 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4126 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4127 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4128 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4129 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4130 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4131 | if(pbIsTag257 != NULL) { |
| 4132 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4133 | } |
| 4134 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4135 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4136 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4137 | if(pbIsTag257 != NULL) { |
| 4138 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4139 | } |
| 4140 | uReturn = QCBOR_SUCCESS; |
| 4141 | |
| 4142 | } else { |
| 4143 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4144 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4145 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4146 | return uReturn; |
| 4147 | } |
| 4148 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4149 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 4150 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4151 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4152 | |
| 4153 | |
| 4154 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4155 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4156 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4157 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4158 | |
| 4159 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4160 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4161 | static QCBORError |
| 4162 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4163 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4164 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4165 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4166 | if(uResult != 0) { |
| 4167 | /* This loop will run a maximum of 19 times because |
| 4168 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 4169 | * exit with the overflow error |
| 4170 | */ |
| 4171 | for(; nExponent > 0; nExponent--) { |
| 4172 | if(uResult > UINT64_MAX / 10) { |
| 4173 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 4174 | } |
| 4175 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4176 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4177 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4178 | for(; nExponent < 0; nExponent++) { |
| 4179 | uResult = uResult / 10; |
| 4180 | if(uResult == 0) { |
| 4181 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 4182 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4183 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4184 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4185 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4186 | |
| 4187 | *puResult = uResult; |
| 4188 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4189 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4193 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4194 | static QCBORError |
| 4195 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4196 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4197 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4198 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4199 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4200 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4201 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4202 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4203 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4204 | */ |
| 4205 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4206 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4207 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4208 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4209 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4210 | nExponent--; |
| 4211 | } |
| 4212 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4213 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4214 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4215 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 4216 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4217 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4218 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4221 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4222 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4223 | return QCBOR_SUCCESS; |
| 4224 | } |
| 4225 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4226 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4227 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4228 | Compute value with signed mantissa and signed result. Works with |
| 4229 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4230 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4231 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 4232 | int64_t nExponent, |
| 4233 | int64_t *pnResult, |
| 4234 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4235 | { |
| 4236 | uint64_t uResult; |
| 4237 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4238 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4239 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4240 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 4241 | |
| 4242 | // Do the exponentiation of the positive mantissa |
| 4243 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4244 | if(uReturn) { |
| 4245 | return uReturn; |
| 4246 | } |
| 4247 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4248 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4249 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4250 | of INT64_MIN. This assumes two's compliment representation where |
| 4251 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 4252 | Trying to write -INT64_MIN doesn't work to get this because the |
| 4253 | compiler tries to work with an int64_t which can't represent |
| 4254 | -INT64_MIN. |
| 4255 | */ |
| 4256 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 4257 | |
| 4258 | // Error out if too large |
| 4259 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4260 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4261 | } |
| 4262 | |
| 4263 | // Casts are safe because of checks above |
| 4264 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 4265 | |
| 4266 | return QCBOR_SUCCESS; |
| 4267 | } |
| 4268 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4269 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4270 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4271 | Compute value with signed mantissa and unsigned result. Works with |
| 4272 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4273 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4274 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 4275 | int64_t nExponent, |
| 4276 | uint64_t *puResult, |
| 4277 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4278 | { |
| 4279 | if(nMantissa < 0) { |
| 4280 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4281 | } |
| 4282 | |
| 4283 | // Cast to unsigned is OK because of check for negative |
| 4284 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 4285 | // Exponentiation is straight forward |
| 4286 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4287 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4288 | |
| 4289 | |
| 4290 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4291 | Compute value with signed mantissa and unsigned result. Works with |
| 4292 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4293 | */ |
| 4294 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 4295 | int64_t nExponent, |
| 4296 | uint64_t *puResult, |
| 4297 | fExponentiator pfExp) |
| 4298 | { |
| 4299 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4300 | } |
| 4301 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4302 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4303 | |
| 4304 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4305 | |
| 4306 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4307 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4308 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4309 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4310 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4311 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4312 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4313 | const uint8_t *pByte = BigNum.ptr; |
| 4314 | size_t uLen = BigNum.len; |
| 4315 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4316 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4317 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4318 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4319 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4320 | } |
| 4321 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4322 | *pResult = uResult; |
| 4323 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4324 | } |
| 4325 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4326 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4327 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4328 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4329 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4330 | } |
| 4331 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4332 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4333 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4334 | { |
| 4335 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4336 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 4337 | if(uError) { |
| 4338 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4339 | } |
| 4340 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 4341 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4342 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4346 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4347 | { |
| 4348 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4349 | /* The negative integer furthest from zero for a C int64_t is |
| 4350 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4351 | negative number in CBOR is computed as -n - 1 where n is the |
| 4352 | encoded integer, where n is what is in the variable BigNum. When |
| 4353 | converting BigNum to a uint64_t, the maximum value is thus |
| 4354 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 4355 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4356 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4357 | -n - 1 <= INT64_MIN. |
| 4358 | -n - 1 <= -INT64_MAX - 1 |
| 4359 | n <= INT64_MAX. |
| 4360 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4361 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4362 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4363 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4364 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4365 | |
| 4366 | /// Now apply -n - 1. The cast is safe because |
| 4367 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4368 | // is the largest positive integer that an int64_t can |
| 4369 | // represent. */ |
| 4370 | *pnResult = -(int64_t)uResult - 1; |
| 4371 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4372 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4373 | } |
| 4374 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4375 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4376 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4377 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4378 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4379 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4380 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4381 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4382 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4383 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4384 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4385 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4386 | |
| 4387 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4388 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4389 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4390 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4391 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4392 | static QCBORError |
| 4393 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4394 | { |
| 4395 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4396 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4397 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4398 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4399 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4400 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 4401 | http://www.cplusplus.com/reference/cmath/llround/ |
| 4402 | */ |
| 4403 | // Not interested in FE_INEXACT |
| 4404 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4405 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4406 | *pnValue = llround(pItem->val.dfnum); |
| 4407 | } else { |
| 4408 | *pnValue = lroundf(pItem->val.fnum); |
| 4409 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4410 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4411 | // llround() shouldn't result in divide by zero, but catch |
| 4412 | // it here in case it unexpectedly does. Don't try to |
| 4413 | // distinguish between the various exceptions because it seems |
| 4414 | // they vary by CPU, compiler and OS. |
| 4415 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4416 | } |
| 4417 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4418 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4419 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4420 | #else |
| 4421 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4422 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4423 | break; |
| 4424 | |
| 4425 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4426 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4427 | *pnValue = pItem->val.int64; |
| 4428 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4429 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4430 | } |
| 4431 | break; |
| 4432 | |
| 4433 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4434 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4435 | if(pItem->val.uint64 < INT64_MAX) { |
| 4436 | *pnValue = pItem->val.int64; |
| 4437 | } else { |
| 4438 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4439 | } |
| 4440 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4441 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4442 | } |
| 4443 | break; |
| 4444 | |
| 4445 | default: |
| 4446 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4447 | } |
| 4448 | return QCBOR_SUCCESS; |
| 4449 | } |
| 4450 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4451 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4452 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4453 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4454 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4455 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4456 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4457 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4458 | return; |
| 4459 | } |
| 4460 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4461 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4462 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4463 | if(uError) { |
| 4464 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4465 | return; |
| 4466 | } |
| 4467 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4468 | if(pItem) { |
| 4469 | *pItem = Item; |
| 4470 | } |
| 4471 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4472 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4475 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4476 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4477 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4478 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4479 | int64_t *pnValue, |
| 4480 | QCBORItem *pItem) |
| 4481 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4482 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4483 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4484 | return; |
| 4485 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4486 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4487 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4488 | } |
| 4489 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4490 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4491 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4492 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4493 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4494 | int64_t *pnValue, |
| 4495 | QCBORItem *pItem) |
| 4496 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4497 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4498 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4499 | return; |
| 4500 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4501 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4502 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4503 | } |
| 4504 | |
| 4505 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4506 | /* |
| 4507 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4508 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4509 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4510 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4511 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4512 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4513 | |
| 4514 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4515 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4516 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4517 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4518 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4519 | static QCBORError |
| 4520 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4521 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4522 | switch(pItem->uDataType) { |
| 4523 | |
| 4524 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4525 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4526 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4527 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4528 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4529 | } |
| 4530 | break; |
| 4531 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4532 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4533 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4534 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4535 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4536 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4537 | } |
| 4538 | break; |
| 4539 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4540 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4541 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4542 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4543 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4544 | pItem->val.expAndMantissa.nExponent, |
| 4545 | pnValue, |
| 4546 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4547 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4548 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4549 | } |
| 4550 | break; |
| 4551 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4552 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4553 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4554 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4555 | pItem->val.expAndMantissa.nExponent, |
| 4556 | pnValue, |
| 4557 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4558 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4559 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4560 | } |
| 4561 | break; |
| 4562 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4563 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4564 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4565 | int64_t nMantissa; |
| 4566 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4567 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4568 | if(uErr) { |
| 4569 | return uErr; |
| 4570 | } |
| 4571 | return ExponentiateNN(nMantissa, |
| 4572 | pItem->val.expAndMantissa.nExponent, |
| 4573 | pnValue, |
| 4574 | Exponentitate10); |
| 4575 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4576 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4577 | } |
| 4578 | break; |
| 4579 | |
| 4580 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4581 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4582 | int64_t nMantissa; |
| 4583 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4584 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4585 | if(uErr) { |
| 4586 | return uErr; |
| 4587 | } |
| 4588 | return ExponentiateNN(nMantissa, |
| 4589 | pItem->val.expAndMantissa.nExponent, |
| 4590 | pnValue, |
| 4591 | Exponentitate10); |
| 4592 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4593 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4594 | } |
| 4595 | break; |
| 4596 | |
| 4597 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4598 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4599 | int64_t nMantissa; |
| 4600 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4601 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4602 | if(uErr) { |
| 4603 | return uErr; |
| 4604 | } |
| 4605 | return ExponentiateNN(nMantissa, |
| 4606 | pItem->val.expAndMantissa.nExponent, |
| 4607 | pnValue, |
| 4608 | Exponentitate2); |
| 4609 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4610 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4611 | } |
| 4612 | break; |
| 4613 | |
| 4614 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4615 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4616 | int64_t nMantissa; |
| 4617 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4618 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4619 | if(uErr) { |
| 4620 | return uErr; |
| 4621 | } |
| 4622 | return ExponentiateNN(nMantissa, |
| 4623 | pItem->val.expAndMantissa.nExponent, |
| 4624 | pnValue, |
| 4625 | Exponentitate2); |
| 4626 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4627 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4628 | } |
| 4629 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4630 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4631 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4632 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4633 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4634 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4635 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4636 | |
| 4637 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4638 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4639 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4640 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4641 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4642 | { |
| 4643 | QCBORItem Item; |
| 4644 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4645 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4646 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4647 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4648 | // The above conversion succeeded |
| 4649 | return; |
| 4650 | } |
| 4651 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4652 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4653 | // 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] | 4654 | return; |
| 4655 | } |
| 4656 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4657 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4660 | |
| 4661 | /* |
| 4662 | Public function, see header qcbor/qcbor_decode.h file |
| 4663 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4664 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4665 | int64_t nLabel, |
| 4666 | uint32_t uConvertTypes, |
| 4667 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4668 | { |
| 4669 | QCBORItem Item; |
| 4670 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4671 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4672 | nLabel, |
| 4673 | uConvertTypes, |
| 4674 | pnValue, |
| 4675 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4676 | |
| 4677 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4678 | // The above conversion succeeded |
| 4679 | return; |
| 4680 | } |
| 4681 | |
| 4682 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4683 | // The above conversion failed in a way that code below can't correct |
| 4684 | return; |
| 4685 | } |
| 4686 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4687 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4691 | /* |
| 4692 | Public function, see header qcbor/qcbor_decode.h file |
| 4693 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4694 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4695 | const char *szLabel, |
| 4696 | uint32_t uConvertTypes, |
| 4697 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4698 | { |
| 4699 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4700 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4701 | szLabel, |
| 4702 | uConvertTypes, |
| 4703 | pnValue, |
| 4704 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4705 | |
| 4706 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4707 | // The above conversion succeeded |
| 4708 | return; |
| 4709 | } |
| 4710 | |
| 4711 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4712 | // The above conversion failed in a way that code below can't correct |
| 4713 | return; |
| 4714 | } |
| 4715 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4716 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4720 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4721 | { |
| 4722 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4723 | case QCBOR_TYPE_DOUBLE: |
| 4724 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4725 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4726 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4727 | // Can't use llround here because it will not convert values |
| 4728 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4729 | // need to be converted so it is more complicated. |
| 4730 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4731 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4732 | if(isnan(pItem->val.dfnum)) { |
| 4733 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4734 | } else if(pItem->val.dfnum < 0) { |
| 4735 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4736 | } else { |
| 4737 | double dRounded = round(pItem->val.dfnum); |
| 4738 | // See discussion in DecodeDateEpoch() for |
| 4739 | // explanation of - 0x7ff |
| 4740 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4741 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4742 | } |
| 4743 | *puValue = (uint64_t)dRounded; |
| 4744 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4745 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4746 | if(isnan(pItem->val.fnum)) { |
| 4747 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4748 | } else if(pItem->val.fnum < 0) { |
| 4749 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4750 | } else { |
| 4751 | float fRounded = roundf(pItem->val.fnum); |
| 4752 | // See discussion in DecodeDateEpoch() for |
| 4753 | // explanation of - 0x7ff |
| 4754 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4755 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4756 | } |
| 4757 | *puValue = (uint64_t)fRounded; |
| 4758 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4759 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4760 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4761 | // round() and roundf() shouldn't result in exceptions here, but |
| 4762 | // catch them to be robust and thorough. Don't try to |
| 4763 | // distinguish between the various exceptions because it seems |
| 4764 | // they vary by CPU, compiler and OS. |
| 4765 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4766 | } |
| 4767 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4768 | } else { |
| 4769 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4770 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4771 | #else |
| 4772 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4773 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4774 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4775 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4776 | case QCBOR_TYPE_INT64: |
| 4777 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4778 | if(pItem->val.int64 >= 0) { |
| 4779 | *puValue = (uint64_t)pItem->val.int64; |
| 4780 | } else { |
| 4781 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4782 | } |
| 4783 | } else { |
| 4784 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4785 | } |
| 4786 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4787 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4788 | case QCBOR_TYPE_UINT64: |
| 4789 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4790 | *puValue = pItem->val.uint64; |
| 4791 | } else { |
| 4792 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4793 | } |
| 4794 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4795 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4796 | default: |
| 4797 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4798 | } |
| 4799 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4800 | return QCBOR_SUCCESS; |
| 4801 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4802 | |
| 4803 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4804 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4805 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4806 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4807 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4808 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4809 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4810 | return; |
| 4811 | } |
| 4812 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4813 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4814 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4815 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4816 | if(uError) { |
| 4817 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4818 | return; |
| 4819 | } |
| 4820 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4821 | if(pItem) { |
| 4822 | *pItem = Item; |
| 4823 | } |
| 4824 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4825 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4826 | } |
| 4827 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4828 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4829 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4830 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4831 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4832 | uint64_t *puValue, |
| 4833 | QCBORItem *pItem) |
| 4834 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4835 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4836 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4837 | return; |
| 4838 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4839 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4840 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4844 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4845 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4846 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4847 | uint64_t *puValue, |
| 4848 | QCBORItem *pItem) |
| 4849 | { |
| 4850 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4851 | return; |
| 4852 | } |
| 4853 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4854 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4855 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4856 | return; |
| 4857 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4858 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4859 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4860 | } |
| 4861 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4862 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4863 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4864 | static QCBORError |
| 4865 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4866 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4867 | switch(pItem->uDataType) { |
| 4868 | |
| 4869 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4870 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4871 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4872 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4873 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4874 | } |
| 4875 | break; |
| 4876 | |
| 4877 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4878 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4879 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4880 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4881 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4882 | } |
| 4883 | break; |
| 4884 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4885 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4886 | |
| 4887 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4888 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4889 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4890 | pItem->val.expAndMantissa.nExponent, |
| 4891 | puValue, |
| 4892 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4893 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4894 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4895 | } |
| 4896 | break; |
| 4897 | |
| 4898 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4899 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4900 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4901 | pItem->val.expAndMantissa.nExponent, |
| 4902 | puValue, |
| 4903 | Exponentitate2); |
| 4904 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4905 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4906 | } |
| 4907 | break; |
| 4908 | |
| 4909 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4910 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4911 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4912 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4913 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4914 | if(uErr != QCBOR_SUCCESS) { |
| 4915 | return uErr; |
| 4916 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4917 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4918 | pItem->val.expAndMantissa.nExponent, |
| 4919 | puValue, |
| 4920 | Exponentitate10); |
| 4921 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4922 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4923 | } |
| 4924 | break; |
| 4925 | |
| 4926 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4927 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4928 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4929 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4930 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4931 | } |
| 4932 | break; |
| 4933 | |
| 4934 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4935 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4936 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4937 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4938 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4939 | if(uErr != QCBOR_SUCCESS) { |
| 4940 | return uErr; |
| 4941 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4942 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4943 | pItem->val.expAndMantissa.nExponent, |
| 4944 | puValue, |
| 4945 | Exponentitate2); |
| 4946 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4947 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4948 | } |
| 4949 | break; |
| 4950 | |
| 4951 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4952 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4953 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4954 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4955 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4956 | } |
| 4957 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4958 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4959 | default: |
| 4960 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4961 | } |
| 4962 | } |
| 4963 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4964 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4965 | /* |
| 4966 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4967 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4968 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4969 | { |
| 4970 | QCBORItem Item; |
| 4971 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4972 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4973 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4974 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4975 | // The above conversion succeeded |
| 4976 | return; |
| 4977 | } |
| 4978 | |
| 4979 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4980 | // 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] | 4981 | return; |
| 4982 | } |
| 4983 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4984 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4985 | } |
| 4986 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4987 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4988 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4989 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4990 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4991 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4992 | int64_t nLabel, |
| 4993 | uint32_t uConvertTypes, |
| 4994 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4995 | { |
| 4996 | QCBORItem Item; |
| 4997 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4998 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 4999 | nLabel, |
| 5000 | uConvertTypes, |
| 5001 | puValue, |
| 5002 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5003 | |
| 5004 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5005 | // The above conversion succeeded |
| 5006 | return; |
| 5007 | } |
| 5008 | |
| 5009 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5010 | // The above conversion failed in a way that code below can't correct |
| 5011 | return; |
| 5012 | } |
| 5013 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5014 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | |
| 5018 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5019 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5020 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 5021 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5022 | const char *szLabel, |
| 5023 | uint32_t uConvertTypes, |
| 5024 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5025 | { |
| 5026 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5027 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 5028 | szLabel, |
| 5029 | uConvertTypes, |
| 5030 | puValue, |
| 5031 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5032 | |
| 5033 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5034 | // The above conversion succeeded |
| 5035 | return; |
| 5036 | } |
| 5037 | |
| 5038 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5039 | // The above conversion failed in a way that code below can't correct |
| 5040 | return; |
| 5041 | } |
| 5042 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5043 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5047 | |
| 5048 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5049 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 5050 | uint32_t uConvertTypes, |
| 5051 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5052 | { |
| 5053 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5054 | case QCBOR_TYPE_FLOAT: |
| 5055 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5056 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5057 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5058 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5059 | *pdValue = (double)pItem->val.fnum; |
| 5060 | } else { |
| 5061 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5062 | } |
| 5063 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5064 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5065 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5066 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5067 | break; |
| 5068 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5069 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5070 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5071 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5072 | *pdValue = pItem->val.dfnum; |
| 5073 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5074 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5075 | } |
| 5076 | } |
| 5077 | break; |
| 5078 | |
| 5079 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5080 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5081 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5082 | // A simple cast seems to do the job with no worry of exceptions. |
| 5083 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5084 | *pdValue = (double)pItem->val.int64; |
| 5085 | |
| 5086 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5087 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5088 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5089 | #else |
| 5090 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5091 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5092 | break; |
| 5093 | |
| 5094 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5095 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5096 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5097 | // A simple cast seems to do the job with no worry of exceptions. |
| 5098 | // There will be precision loss for some values. |
| 5099 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5100 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5101 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5102 | } |
| 5103 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5104 | #else |
| 5105 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5106 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5107 | |
| 5108 | default: |
| 5109 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5110 | } |
| 5111 | |
| 5112 | return QCBOR_SUCCESS; |
| 5113 | } |
| 5114 | |
| 5115 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5116 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5117 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5118 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5119 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5120 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5121 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5122 | return; |
| 5123 | } |
| 5124 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5125 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5126 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5127 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5128 | if(uError) { |
| 5129 | pMe->uLastError = (uint8_t)uError; |
| 5130 | return; |
| 5131 | } |
| 5132 | |
| 5133 | if(pItem) { |
| 5134 | *pItem = Item; |
| 5135 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5136 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5137 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5138 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5139 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5140 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5141 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 5142 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5143 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5144 | double *pdValue, |
| 5145 | QCBORItem *pItem) |
| 5146 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5147 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5148 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5149 | return; |
| 5150 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5151 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5152 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5153 | } |
| 5154 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5155 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5156 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 5157 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5158 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5159 | double *pdValue, |
| 5160 | QCBORItem *pItem) |
| 5161 | { |
| 5162 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5163 | return; |
| 5164 | } |
| 5165 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5166 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5167 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5168 | return; |
| 5169 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5170 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5171 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5175 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5176 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 5177 | { |
| 5178 | double dResult; |
| 5179 | |
| 5180 | dResult = 0.0; |
| 5181 | const uint8_t *pByte = BigNum.ptr; |
| 5182 | size_t uLen = BigNum.len; |
| 5183 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5184 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5185 | while(uLen--) { |
| 5186 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 5187 | } |
| 5188 | |
| 5189 | return dResult; |
| 5190 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5191 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5192 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5193 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5194 | static QCBORError |
| 5195 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5196 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5197 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5198 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5199 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 5200 | |
| 5201 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5202 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5203 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5204 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5205 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5206 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5207 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5208 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5209 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 5210 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5211 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5212 | } |
| 5213 | break; |
| 5214 | |
| 5215 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5216 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5217 | // Underflow gives 0, overflow gives infinity |
| 5218 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5219 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5220 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5221 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5222 | } |
| 5223 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5224 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5225 | |
| 5226 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5227 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5228 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 5229 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5230 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5231 | } |
| 5232 | break; |
| 5233 | |
| 5234 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5235 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5236 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5237 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5238 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5239 | } |
| 5240 | break; |
| 5241 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5242 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5243 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5244 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5245 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5246 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5247 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5248 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5249 | } |
| 5250 | break; |
| 5251 | |
| 5252 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5253 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5254 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5255 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5256 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5257 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5258 | } |
| 5259 | break; |
| 5260 | |
| 5261 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5262 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5263 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5264 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5265 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5266 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5267 | } |
| 5268 | break; |
| 5269 | |
| 5270 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5271 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5272 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5273 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5274 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5275 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5276 | } |
| 5277 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5278 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5279 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5280 | default: |
| 5281 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5282 | } |
| 5283 | |
| 5284 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5285 | |
| 5286 | #else |
| 5287 | (void)pItem; |
| 5288 | (void)uConvertTypes; |
| 5289 | (void)pdValue; |
| 5290 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5291 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5292 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
| 5295 | |
| 5296 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5297 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5298 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5299 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 5300 | uint32_t uConvertTypes, |
| 5301 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5302 | { |
| 5303 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5304 | QCBORItem Item; |
| 5305 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5306 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5307 | |
| 5308 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5309 | // The above conversion succeeded |
| 5310 | return; |
| 5311 | } |
| 5312 | |
| 5313 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5314 | // The above conversion failed in a way that code below can't correct |
| 5315 | return; |
| 5316 | } |
| 5317 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5318 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5319 | } |
| 5320 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5321 | |
| 5322 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5323 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5324 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5325 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5326 | int64_t nLabel, |
| 5327 | uint32_t uConvertTypes, |
| 5328 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5329 | { |
| 5330 | QCBORItem Item; |
| 5331 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5332 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5333 | |
| 5334 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5335 | // The above conversion succeeded |
| 5336 | return; |
| 5337 | } |
| 5338 | |
| 5339 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5340 | // The above conversion failed in a way that code below can't correct |
| 5341 | return; |
| 5342 | } |
| 5343 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5344 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5345 | } |
| 5346 | |
| 5347 | |
| 5348 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5349 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5350 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5351 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5352 | const char *szLabel, |
| 5353 | uint32_t uConvertTypes, |
| 5354 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5355 | { |
| 5356 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5357 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5358 | |
| 5359 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5360 | // The above conversion succeeded |
| 5361 | return; |
| 5362 | } |
| 5363 | |
| 5364 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5365 | // The above conversion failed in a way that code below can't correct |
| 5366 | return; |
| 5367 | } |
| 5368 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5369 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5370 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5371 | |
| 5372 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5373 | |
| 5374 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5375 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5376 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 5377 | { |
| 5378 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5379 | uInt = uInt << 8; |
| 5380 | }; |
| 5381 | |
| 5382 | UsefulOutBuf UOB; |
| 5383 | |
| 5384 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5385 | |
| 5386 | while(uInt) { |
| 5387 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5388 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5389 | uInt = uInt << 8; |
| 5390 | (void)xx; |
| 5391 | } |
| 5392 | |
| 5393 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5394 | } |
| 5395 | |
| 5396 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5397 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 5398 | TagSpecification TagSpec, |
| 5399 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5400 | { |
| 5401 | QCBORError uErr; |
| 5402 | // 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] | 5403 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5404 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5405 | if(uErr != QCBOR_SUCCESS) { |
| 5406 | goto Done; |
| 5407 | } |
| 5408 | |
| 5409 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 5410 | break; // Successful exit. Moving on to finish decoding. |
| 5411 | } |
| 5412 | |
| 5413 | // The item is an array, which means an undecoded |
| 5414 | // mantissa and exponent, so decode it. It will then |
| 5415 | // have a different type and exit the loop if. |
| 5416 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5417 | if(uErr != QCBOR_SUCCESS) { |
| 5418 | goto Done; |
| 5419 | } |
| 5420 | |
| 5421 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5422 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5423 | } |
| 5424 | Done: |
| 5425 | return uErr; |
| 5426 | } |
| 5427 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5428 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5429 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5430 | TagSpecification TagSpec, |
| 5431 | QCBORItem *pItem, |
| 5432 | int64_t *pnMantissa, |
| 5433 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5434 | { |
| 5435 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5436 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5437 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5438 | if(uErr != QCBOR_SUCCESS) { |
| 5439 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5440 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5441 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5442 | switch (pItem->uDataType) { |
| 5443 | |
| 5444 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5445 | case QCBOR_TYPE_BIGFLOAT: |
| 5446 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 5447 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5448 | break; |
| 5449 | |
| 5450 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5451 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5452 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5453 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5454 | break; |
| 5455 | |
| 5456 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5457 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5458 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5459 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5460 | break; |
| 5461 | |
| 5462 | default: |
| 5463 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5464 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5465 | |
| 5466 | Done: |
| 5467 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5468 | } |
| 5469 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5470 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5471 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5472 | TagSpecification TagSpec, |
| 5473 | QCBORItem *pItem, |
| 5474 | UsefulBuf BufferForMantissa, |
| 5475 | UsefulBufC *pMantissa, |
| 5476 | bool *pbIsNegative, |
| 5477 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5478 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5479 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5480 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5481 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5482 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5483 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | uint64_t uMantissa; |
| 5487 | |
| 5488 | switch (pItem->uDataType) { |
| 5489 | |
| 5490 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5491 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5492 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5493 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5494 | *pbIsNegative = false; |
| 5495 | } else { |
| 5496 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5497 | *pbIsNegative = true; |
| 5498 | } |
| 5499 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5500 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5501 | break; |
| 5502 | |
| 5503 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5504 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5505 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5506 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5507 | *pbIsNegative = false; |
| 5508 | break; |
| 5509 | |
| 5510 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5511 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5512 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5513 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5514 | *pbIsNegative = true; |
| 5515 | break; |
| 5516 | |
| 5517 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5518 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5519 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5520 | |
| 5521 | Done: |
| 5522 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5526 | /* |
| 5527 | Public function, see header qcbor/qcbor_decode.h file |
| 5528 | */ |
| 5529 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5530 | uint8_t uTagRequirement, |
| 5531 | int64_t *pnMantissa, |
| 5532 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5533 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5534 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5535 | return; |
| 5536 | } |
| 5537 | |
| 5538 | QCBORItem Item; |
| 5539 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5540 | if(uError) { |
| 5541 | pMe->uLastError = (uint8_t)uError; |
| 5542 | return; |
| 5543 | } |
| 5544 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5545 | const TagSpecification TagSpec = |
| 5546 | { |
| 5547 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5548 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5549 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5550 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5551 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5552 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5553 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5554 | } |
| 5555 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5556 | |
| 5557 | /* |
| 5558 | Public function, see header qcbor/qcbor_decode.h file |
| 5559 | */ |
| 5560 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5561 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5562 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5563 | int64_t *pnMantissa, |
| 5564 | int64_t *pnExponent) |
| 5565 | { |
| 5566 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5567 | return; |
| 5568 | } |
| 5569 | |
| 5570 | QCBORItem Item; |
| 5571 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5572 | |
| 5573 | const TagSpecification TagSpec = |
| 5574 | { |
| 5575 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5576 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5577 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5578 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5579 | }; |
| 5580 | |
| 5581 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5582 | } |
| 5583 | |
| 5584 | |
| 5585 | /* |
| 5586 | Public function, see header qcbor/qcbor_decode.h file |
| 5587 | */ |
| 5588 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5589 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5590 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5591 | int64_t *pnMantissa, |
| 5592 | int64_t *pnExponent) |
| 5593 | { |
| 5594 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5595 | return; |
| 5596 | } |
| 5597 | |
| 5598 | QCBORItem Item; |
| 5599 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5600 | |
| 5601 | const TagSpecification TagSpec = |
| 5602 | { |
| 5603 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5604 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5605 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5606 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5607 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5608 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5609 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5610 | } |
| 5611 | |
| 5612 | |
| 5613 | /* |
| 5614 | Public function, see header qcbor/qcbor_decode.h file |
| 5615 | */ |
| 5616 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5617 | uint8_t uTagRequirement, |
| 5618 | UsefulBuf MantissaBuffer, |
| 5619 | UsefulBufC *pMantissa, |
| 5620 | bool *pbMantissaIsNegative, |
| 5621 | int64_t *pnExponent) |
| 5622 | { |
| 5623 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5624 | return; |
| 5625 | } |
| 5626 | |
| 5627 | QCBORItem Item; |
| 5628 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5629 | if(uError) { |
| 5630 | pMe->uLastError = (uint8_t)uError; |
| 5631 | return; |
| 5632 | } |
| 5633 | |
| 5634 | const TagSpecification TagSpec = |
| 5635 | { |
| 5636 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5637 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5638 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5639 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5640 | }; |
| 5641 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5642 | ProcessMantissaAndExponentBig(pMe, |
| 5643 | TagSpec, |
| 5644 | &Item, |
| 5645 | MantissaBuffer, |
| 5646 | pMantissa, |
| 5647 | pbMantissaIsNegative, |
| 5648 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | |
| 5652 | /* |
| 5653 | Public function, see header qcbor/qcbor_decode.h file |
| 5654 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5655 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5656 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5657 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5658 | UsefulBuf BufferForMantissa, |
| 5659 | UsefulBufC *pMantissa, |
| 5660 | bool *pbIsNegative, |
| 5661 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5662 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5663 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5664 | return; |
| 5665 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5666 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5667 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5668 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5669 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5670 | return; |
| 5671 | } |
| 5672 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5673 | const TagSpecification TagSpec = |
| 5674 | { |
| 5675 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5676 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5677 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5678 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5679 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5680 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5681 | ProcessMantissaAndExponentBig(pMe, |
| 5682 | TagSpec, |
| 5683 | &Item, |
| 5684 | BufferForMantissa, |
| 5685 | pMantissa, |
| 5686 | pbIsNegative, |
| 5687 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5688 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5689 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5690 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5691 | /* |
| 5692 | Public function, see header qcbor/qcbor_decode.h file |
| 5693 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5694 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5695 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5696 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5697 | UsefulBuf BufferForMantissa, |
| 5698 | UsefulBufC *pMantissa, |
| 5699 | bool *pbIsNegative, |
| 5700 | int64_t *pnExponent) |
| 5701 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5702 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5703 | return; |
| 5704 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5705 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5706 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5707 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5708 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5709 | return; |
| 5710 | } |
| 5711 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5712 | const TagSpecification TagSpec = |
| 5713 | { |
| 5714 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5715 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5716 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5717 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5718 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5719 | |
| 5720 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5721 | } |
| 5722 | |
| 5723 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5724 | /* |
| 5725 | Public function, see header qcbor/qcbor_decode.h file |
| 5726 | */ |
| 5727 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5728 | uint8_t uTagRequirement, |
| 5729 | int64_t *pnMantissa, |
| 5730 | int64_t *pnExponent) |
| 5731 | { |
| 5732 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5733 | return; |
| 5734 | } |
| 5735 | |
| 5736 | QCBORItem Item; |
| 5737 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5738 | if(uError) { |
| 5739 | pMe->uLastError = (uint8_t)uError; |
| 5740 | return; |
| 5741 | } |
| 5742 | const TagSpecification TagSpec = |
| 5743 | { |
| 5744 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5745 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5746 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5747 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5748 | }; |
| 5749 | |
| 5750 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5751 | } |
| 5752 | |
| 5753 | |
| 5754 | /* |
| 5755 | Public function, see header qcbor/qcbor_decode.h file |
| 5756 | */ |
| 5757 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5758 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5759 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5760 | int64_t *pnMantissa, |
| 5761 | int64_t *pnExponent) |
| 5762 | { |
| 5763 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5764 | return; |
| 5765 | } |
| 5766 | |
| 5767 | QCBORItem Item; |
| 5768 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5769 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5770 | return; |
| 5771 | } |
| 5772 | |
| 5773 | const TagSpecification TagSpec = |
| 5774 | { |
| 5775 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5776 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5777 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5778 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5779 | }; |
| 5780 | |
| 5781 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5782 | } |
| 5783 | |
| 5784 | |
| 5785 | /* |
| 5786 | Public function, see header qcbor/qcbor_decode.h file |
| 5787 | */ |
| 5788 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5789 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5790 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5791 | int64_t *pnMantissa, |
| 5792 | int64_t *pnExponent) |
| 5793 | { |
| 5794 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5795 | return; |
| 5796 | } |
| 5797 | |
| 5798 | QCBORItem Item; |
| 5799 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5800 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5801 | return; |
| 5802 | } |
| 5803 | |
| 5804 | const TagSpecification TagSpec = |
| 5805 | { |
| 5806 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5807 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5808 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5809 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5810 | }; |
| 5811 | |
| 5812 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5813 | } |
| 5814 | |
| 5815 | |
| 5816 | /* |
| 5817 | Public function, see header qcbor/qcbor_decode.h file |
| 5818 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5819 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5820 | uint8_t uTagRequirement, |
| 5821 | UsefulBuf MantissaBuffer, |
| 5822 | UsefulBufC *pMantissa, |
| 5823 | bool *pbMantissaIsNegative, |
| 5824 | int64_t *pnExponent) |
| 5825 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5826 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5827 | return; |
| 5828 | } |
| 5829 | |
| 5830 | QCBORItem Item; |
| 5831 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5832 | if(uError) { |
| 5833 | pMe->uLastError = (uint8_t)uError; |
| 5834 | return; |
| 5835 | } |
| 5836 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5837 | const TagSpecification TagSpec = |
| 5838 | { |
| 5839 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5840 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5841 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5842 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5843 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5844 | |
| 5845 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5846 | } |
| 5847 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5848 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5849 | /* |
| 5850 | Public function, see header qcbor/qcbor_decode.h file |
| 5851 | */ |
| 5852 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5853 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5854 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5855 | UsefulBuf BufferForMantissa, |
| 5856 | UsefulBufC *pMantissa, |
| 5857 | bool *pbIsNegative, |
| 5858 | int64_t *pnExponent) |
| 5859 | { |
| 5860 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5861 | return; |
| 5862 | } |
| 5863 | |
| 5864 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5865 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5866 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5867 | return; |
| 5868 | } |
| 5869 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5870 | const TagSpecification TagSpec = |
| 5871 | { |
| 5872 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5873 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5874 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5875 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5876 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5877 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5878 | ProcessMantissaAndExponentBig(pMe, |
| 5879 | TagSpec, |
| 5880 | &Item, |
| 5881 | BufferForMantissa, |
| 5882 | pMantissa, |
| 5883 | pbIsNegative, |
| 5884 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5888 | /* |
| 5889 | Public function, see header qcbor/qcbor_decode.h file |
| 5890 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5891 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5892 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5893 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5894 | UsefulBuf BufferForMantissa, |
| 5895 | UsefulBufC *pMantissa, |
| 5896 | bool *pbIsNegative, |
| 5897 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5898 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5899 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5900 | return; |
| 5901 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5902 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5903 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5904 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5905 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5906 | return; |
| 5907 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5908 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5909 | const TagSpecification TagSpec = |
| 5910 | { |
| 5911 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5912 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5913 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5914 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5915 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5916 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5917 | ProcessMantissaAndExponentBig(pMe, |
| 5918 | TagSpec, |
| 5919 | &Item, |
| 5920 | BufferForMantissa, |
| 5921 | pMantissa, |
| 5922 | pbIsNegative, |
| 5923 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5924 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5925 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5926 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |