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 | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2020, 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 | 6474c98 | 2020-12-26 22:14:34 -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 | 53b945a | 2020-12-27 02:05:01 -0800 | [diff] [blame] | 39 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 40 | #include <math.h> /* For isnan(), llround(), llroudf(), round(), roundf(), |
| 41 | * pow(), exp2() |
| 42 | */ |
| 43 | #include <fenv.h> /* feclearexcept(), fetestexcept() */ |
Laurence Lundblade | 53b945a | 2020-12-27 02:05:01 -0800 | [diff] [blame] | 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 | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 48 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 49 | /* |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 50 | * This casts away the const-ness of a pointer, usually so it can be |
| 51 | * freed or realloced. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 52 | */ |
| 53 | #define UNCONST_POINTER(ptr) ((void *)(ptr)) |
| 54 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 55 | #define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type)) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 56 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 57 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 58 | |
| 59 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 60 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 61 | QCBORItem_IsMapOrArray(const QCBORItem *pMe) |
| 62 | { |
| 63 | const uint8_t uDataType = pMe->uDataType; |
| 64 | return uDataType == QCBOR_TYPE_MAP || |
| 65 | uDataType == QCBOR_TYPE_ARRAY || |
| 66 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
| 67 | } |
| 68 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 69 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 70 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 71 | { |
| 72 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | if(pMe->val.uCount != 0) { |
| 77 | return false; |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 82 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 83 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 84 | { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 85 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 86 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if(pMe->val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 94 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 95 | (void)pMe; |
| 96 | return false; |
| 97 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 101 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 102 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 103 | ===========================================================================*/ |
| 104 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 105 | /* |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 106 | * See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
| 107 | * the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 108 | */ |
| 109 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 110 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 111 | static inline uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 112 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 113 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 114 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 115 | /* Limit in DecodeNesting_Descend against more than |
| 116 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 117 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 118 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 121 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 122 | static inline uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 123 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 124 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 125 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 126 | /* Limit in DecodeNesting_Descend against more than |
| 127 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 128 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 129 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 132 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 133 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 134 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 135 | { |
| 136 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 137 | } |
| 138 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 139 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 140 | static inline bool |
| 141 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 142 | { |
| 143 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 144 | return true; |
| 145 | } else { |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 151 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 152 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 153 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 154 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 155 | return true; |
| 156 | } else { |
| 157 | return false; |
| 158 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 161 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 162 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 163 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 164 | { |
| 165 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 166 | /* Not a map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 167 | return false; |
| 168 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 169 | |
| 170 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 171 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 172 | /* Is indefinite */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } |
Laurence Lundblade | 53b945a | 2020-12-27 02:05:01 -0800 | [diff] [blame] | 175 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 176 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 177 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 178 | /* All checks passed; is a definte length map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 182 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 183 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 184 | { |
| 185 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 186 | /* is a byte string */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 192 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 193 | static inline bool DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 194 | { |
| 195 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 196 | return true; |
| 197 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 198 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 204 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 205 | static inline void DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 206 | { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 207 | /* Should be only called on maps and arrays */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 208 | /* |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 209 | * DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 210 | * larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 211 | * uin32_t so the cast is safe. |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 212 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 213 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 214 | |
| 215 | if(bIsEmpty) { |
| 216 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 217 | } |
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 void DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 222 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 223 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 226 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 227 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 228 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 229 | { |
| 230 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 231 | /* No bounded map or array set up */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 235 | /* Not a map or array; end of those is by byte count */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 236 | return false; |
| 237 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 238 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 239 | /* In a traveral at a level deeper than the bounded level */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 242 | /* Works for both definite and indefinite length maps/arrays */ |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 243 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0 && |
| 244 | pNesting->pCurrentBounded->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 245 | /* Count is not zero, still unconsumed item */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 246 | return false; |
| 247 | } |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 248 | /* All checks passed, got to the end of an array or map*/ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 249 | return true; |
| 250 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 251 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 252 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 253 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 254 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 255 | { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 256 | /* Must only be called on map / array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 257 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 258 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 259 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 264 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 265 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 266 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 267 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 268 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 269 | return true; |
| 270 | } else { |
| 271 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 272 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 275 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 276 | static inline bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 277 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 278 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 279 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 282 | |
| 283 | if(pNesting->pCurrentBounded->uLevelType != uType) { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 290 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 291 | static inline void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 292 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 293 | { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 294 | /* Only call on a defnite length array / map */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 295 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 296 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 297 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 298 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 299 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 300 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 301 | { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 302 | /* Only call on a defnite length array / map */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 303 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 304 | } |
| 305 | |
| 306 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 307 | static inline void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 308 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 309 | { |
| 310 | pNesting->pCurrent--; |
| 311 | } |
| 312 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 313 | |
| 314 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 315 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 316 | { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 317 | /* Error out if nesting is too deep */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 318 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 319 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 322 | /* The actual descend */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 323 | pNesting->pCurrent++; |
| 324 | |
| 325 | pNesting->pCurrent->uLevelType = uType; |
| 326 | |
| 327 | return QCBOR_SUCCESS; |
| 328 | } |
| 329 | |
| 330 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 331 | static inline QCBORError |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 332 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, |
| 333 | bool bIsEmpty, |
| 334 | size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 335 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 336 | /* |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 337 | * Should only be called on map/array. |
| 338 | * |
| 339 | * Have descended into this before this is called. The job here is |
| 340 | * just to mark it in bounded mode. |
| 341 | * |
| 342 | * Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
Laurence Lundblade | 53b945a | 2020-12-27 02:05:01 -0800 | [diff] [blame] | 343 | * uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET. |
| 344 | * |
| 345 | * 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] | 346 | */ |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 347 | if((uint32_t)uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 348 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 351 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 352 | |
| 353 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 354 | |
| 355 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 358 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 359 | static inline QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 360 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 361 | uint8_t uQCBORType, |
| 362 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 363 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 364 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 365 | |
| 366 | if(uCount == 0) { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 367 | /* Nothing to do for empty definite lenth arrays. They are just are |
| 368 | * effectively the same as an item that is not a map or array. |
| 369 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 370 | goto Done; |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 371 | /* Empty indefinite length maps and arrays are handled elsewhere */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 374 | /* Error out if arrays is too long to handle */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 375 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 376 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 377 | uError = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 378 | goto Done; |
| 379 | } |
| 380 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 381 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 382 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 383 | goto Done; |
| 384 | } |
| 385 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 386 | /* Fill in the new map/array level. Check above makes casts OK. */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 387 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 388 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 389 | |
| 390 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 391 | |
| 392 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 393 | return uError;; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | static inline void |
| 398 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 399 | { |
| 400 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | static inline void |
| 405 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 406 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 407 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 408 | pNesting->pCurrentBounded--; |
| 409 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 410 | break; |
| 411 | } |
| 412 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 415 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 416 | static inline void |
| 417 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 418 | { |
| 419 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 420 | } |
| 421 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 422 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 423 | static inline QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 424 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 425 | uint32_t uEndOffset, |
| 426 | uint32_t uEndOfBstr) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 427 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 428 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 429 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 430 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 431 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 432 | goto Done; |
| 433 | } |
| 434 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 435 | /* Fill in the new byte string level */ |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 436 | pNesting->pCurrent->u.bs.uPreviousEndOffset = uEndOffset; |
| 437 | pNesting->pCurrent->u.bs.uEndOfBstr = uEndOfBstr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 438 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 439 | /* Bstr wrapped levels are always bounded */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 440 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 441 | |
| 442 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 443 | return uError;; |
| 444 | } |
| 445 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 446 | |
| 447 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 448 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 449 | { |
| 450 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 453 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 454 | static inline void |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 455 | DecodeNesting_ResetMapOrArrayCount(QCBORDecodeNesting *pNesting) |
| 456 | { |
| 457 | pNesting->pCurrentBounded->u.ma.uCountCursor = pNesting->pCurrentBounded->u.ma.uCountTotal; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | static inline void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 462 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 463 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 464 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 465 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 466 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 470 | static inline void |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 471 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, |
| 472 | QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 473 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 474 | *pSave = *pNesting; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 475 | pNesting->pCurrent = pNesting->pCurrentBounded; |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 476 | DecodeNesting_ResetMapOrArrayCount(pNesting); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 479 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 480 | static inline void |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 481 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, |
| 482 | const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 483 | { |
| 484 | *pNesting = *pSave; |
| 485 | } |
| 486 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 487 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 488 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 489 | DecodeNesting_GetEndOfBstr(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 490 | { |
| 491 | return pMe->pCurrentBounded->u.bs.uEndOfBstr; |
| 492 | } |
| 493 | |
| 494 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 495 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 496 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 497 | { |
| 498 | return pMe->pCurrentBounded->u.bs.uPreviousEndOffset; |
| 499 | } |
| 500 | |
| 501 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 502 | |
| 503 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 504 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 505 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 506 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 507 | |
| 508 | The following four functions are pretty wrappers for invocation of |
| 509 | the string allocator supplied by the caller. |
| 510 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 511 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 512 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 513 | static inline void |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 514 | StringAllocator_Free(const QCBORInternalAllocator *pMe, void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 515 | { |
| 516 | (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0); |
| 517 | } |
| 518 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 519 | // StringAllocator_Reallocate called with pMem NULL is |
| 520 | // equal to StringAllocator_Allocate() |
| 521 | static inline UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 522 | StringAllocator_Reallocate(const QCBORInternalAllocator *pMe, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 523 | void *pMem, |
| 524 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 525 | { |
| 526 | return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize); |
| 527 | } |
| 528 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 529 | static inline UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 530 | StringAllocator_Allocate(const QCBORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 531 | { |
| 532 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 533 | } |
| 534 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 535 | static inline void |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 536 | StringAllocator_Destruct(const QCBORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 537 | { |
| 538 | if(pMe->pfAllocator) { |
| 539 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 540 | } |
| 541 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 542 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 543 | |
| 544 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 545 | |
| 546 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 547 | /*=========================================================================== |
| 548 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 549 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 550 | See qcbor/qcbor_decode.h for definition of the object |
| 551 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 552 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 553 | /* |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 554 | * Public function, see header file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 555 | */ |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 556 | void QCBORDecode_Init(QCBORDecodeContext *pMe, |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 557 | UsefulBufC EncodedCBOR, |
| 558 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 559 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 560 | memset(pMe, 0, sizeof(QCBORDecodeContext)); |
| 561 | UsefulInputBuf_Init(&(pMe->InBuf), EncodedCBOR); |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 562 | /* Don't bother with error check on decode mode. If a bad value is |
| 563 | * passed it will just act as if the default normal mode of 0 was set. |
| 564 | */ |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 565 | pMe->uDecodeMode = (uint8_t)nDecodeMode; |
| 566 | DecodeNesting_Init(&(pMe->nesting)); |
| 567 | |
| 568 | /* Inialize me->auMappedTags to CBOR_TAG_INVALID16. See |
| 569 | * GetNext_TaggedItem() and MapTagNumber(). */ |
| 570 | memset(pMe->auMappedTags, 0xff, sizeof(pMe->auMappedTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 574 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 575 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 576 | /* |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 577 | * Public function, see header file |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 578 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 579 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 580 | QCBORStringAllocate pfAllocateFunction, |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 581 | void *pAllocateContext, |
| 582 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 583 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 584 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 585 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 586 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 587 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 588 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 589 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 590 | |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 591 | |
| 592 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 593 | /* |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 594 | * Deprecated public function, see header file |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 595 | */ |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 596 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 597 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 598 | { |
Laurence Lundblade | 6474c98 | 2020-12-26 22:14:34 -0800 | [diff] [blame] | 599 | /* This does nothing now. It is retained for backwards compatibility */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 600 | (void)pMe; |
| 601 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 602 | } |
| 603 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 604 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 605 | |
| 606 | |
| 607 | /* |
| 608 | Decoding items is done in 5 layered functions, one calling the |
| 609 | next one down. If a layer has no work to do for a particular item |
| 610 | it returns quickly. |
| 611 | |
| 612 | - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes |
| 613 | tagged data items, turning them into the local C representation. |
| 614 | For the most simple it is just associating a QCBOR_TYPE with the data. For |
| 615 | the complex ones that an aggregate of data items, there is some further |
| 616 | decoding and a little bit of recursion. |
| 617 | |
| 618 | - QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
| 619 | ends of maps and arrays. It tracks descending into and ascending |
| 620 | out of maps/arrays. It processes all breaks that terminate |
| 621 | indefinite length maps and arrays. |
| 622 | |
| 623 | - GetNext_MapEntry -- This handles the combining of two |
| 624 | items, the label and the data, that make up a map entry. |
| 625 | It only does work on maps. It combines the label and data |
| 626 | items into one labeled item. |
| 627 | |
| 628 | - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the |
| 629 | tags into bit flags associated with the data item. No actual decoding |
| 630 | of the contents of the tagged item is performed here. |
| 631 | |
| 632 | - GetNext_FullItem -- This assembles the sub-items that make up |
| 633 | an indefinte length string into one string item. It uses the |
| 634 | string allocater to create contiguous space for the item. It |
| 635 | processes all breaks that are part of indefinite length strings. |
| 636 | |
| 637 | - GetNext_Item -- This decodes the atomic data items in CBOR. Each |
| 638 | atomic data item has a "major type", an integer "argument" and optionally |
| 639 | some content. For text and byte strings, the content is the bytes |
| 640 | that make up the string. These are the smallest data items that are |
| 641 | considered to be well-formed. The content may also be other data items in |
| 642 | the case of aggregate types. They are not handled in this layer. |
| 643 | |
| 644 | Roughly this takes 300 bytes of stack for vars. Need to |
| 645 | evaluate this more carefully and correctly. |
| 646 | |
| 647 | */ |
| 648 | |
| 649 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 650 | /** |
| 651 | * @brief Decode the CBOR head, the type and argument. |
| 652 | * |
| 653 | * @param[in] pUInBuf The input buffer to read from. |
| 654 | * @param[out] pnMajorType The decoded major type. |
| 655 | * @param[out] puArgument The decoded argument. |
| 656 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 657 | * |
| 658 | * @retval QCBOR_ERR_UNSUPPORTED |
| 659 | * @retval QCBOR_ERR_HIT_END |
| 660 | * |
| 661 | * This decodes the CBOR "head" that every CBOR data item has. See |
| 662 | * longer explaination of the head in documentation for |
| 663 | * QCBOREncode_EncodeHead(). |
| 664 | * |
| 665 | * This does the network->host byte order conversion. The conversion |
| 666 | * here also results in the conversion for floats in addition to that |
| 667 | * for lengths, tags and integer values. |
| 668 | * |
| 669 | * The int type is preferred to uint8_t for some variables as this |
| 670 | * avoids integer promotions, can reduce code size and makes static |
| 671 | * analyzers happier. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 672 | */ |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 673 | static inline QCBORError |
| 674 | DecodeHead(UsefulInputBuf *pUInBuf, |
| 675 | int *pnMajorType, |
| 676 | uint64_t *puArgument, |
| 677 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 678 | { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 679 | QCBORError uReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 680 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 681 | /* Get the initial byte that every CBOR data item has and break it |
| 682 | * down. */ |
| 683 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 684 | const int nTmpMajorType = nInitialByte >> 5; |
| 685 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 686 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 687 | /* Where the argument accumulates */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 688 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 689 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 690 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 691 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 692 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 693 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 694 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 695 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 696 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 697 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 698 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 699 | /* This shift and add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 700 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 701 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 702 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 703 | /* The reserved and thus-far unused additional info values */ |
| 704 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 705 | goto Done; |
| 706 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 707 | /* Less than 24, additional info is argument or 31, an |
| 708 | * indefinite length. No more bytes to get. |
| 709 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 710 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 711 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 712 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 713 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 714 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 715 | goto Done; |
| 716 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 717 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 718 | /* All successful if arrived here. */ |
| 719 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 720 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 721 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 722 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 723 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 724 | Done: |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 725 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 728 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 729 | /** |
| 730 | * @brief Decode integer types, major types 0 and 1. |
| 731 | * |
| 732 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 733 | * @param[in] uArgument The argument from the head. |
| 734 | * @param[out] pDecodedItem The filled in decoded item. |
| 735 | * |
| 736 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 737 | * |
| 738 | * Must only be called when major type is 0 or 1. |
| 739 | * |
| 740 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 741 | * all CPUs use it these days and the test vectors in the RFC are |
| 742 | * so. All integers in the CBOR structure are positive and the major |
| 743 | * type indicates positive or negative. CBOR can express positive |
| 744 | * integers up to 2^x - 1 where x is the number of bits and negative |
| 745 | * integers down to 2^x. Note that negative numbers can be one more |
| 746 | * away from zero than positive. Stdint, as far as I can tell, uses |
| 747 | * two's compliment to represent negative integers. |
| 748 | * |
| 749 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 750 | * used carefully here, and in particular why it isn't used in the |
| 751 | * public interface. Also see |
| 752 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 753 | * |
| 754 | * Int is used for values that need less than 16-bits and would be |
| 755 | * subject to integer promotion and result in complaining from static |
| 756 | * analyzers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 757 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 758 | static inline QCBORError |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 759 | DecodeInteger(int nMajorType, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 760 | { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 761 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 762 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 763 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 764 | if (uArgument <= INT64_MAX) { |
| 765 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 766 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 767 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 768 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 769 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 770 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 771 | } |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 772 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 773 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 774 | if(uArgument <= INT64_MAX) { |
| 775 | /* CBOR's representation of negative numbers lines up with |
| 776 | * the two-compliment representation. A negative integer has |
| 777 | * one more in range than a positive integer. INT64_MIN is |
| 778 | * equal to (-INT64_MAX) - 1. |
| 779 | */ |
| 780 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 781 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 782 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 783 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 784 | /* C can't represent a negative integer in this range so it |
| 785 | * is an error. |
| 786 | */ |
| 787 | uReturn = QCBOR_ERR_INT_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 788 | } |
| 789 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 790 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 791 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 794 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 795 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 796 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 797 | #error QCBOR_TYPE_FALSE macro value wrong |
| 798 | #endif |
| 799 | |
| 800 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 801 | #error QCBOR_TYPE_TRUE macro value wrong |
| 802 | #endif |
| 803 | |
| 804 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 805 | #error QCBOR_TYPE_NULL macro value wrong |
| 806 | #endif |
| 807 | |
| 808 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 809 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 810 | #endif |
| 811 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 812 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 813 | #error QCBOR_TYPE_BREAK macro value wrong |
| 814 | #endif |
| 815 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 816 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 817 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 818 | #endif |
| 819 | |
| 820 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 821 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 822 | #endif |
| 823 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 824 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 825 | /** |
| 826 | * @brief Decode type 7 -- true, false, floating-point, break... |
| 827 | * |
| 828 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 829 | * @param[in] uArgument The argument from the head. |
| 830 | * @param[out] pDecodedItem The filled in decoded item. |
| 831 | * |
| 832 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 833 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 834 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 835 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 836 | static inline QCBORError |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 837 | DecodeType7(int nAdditionalInfo, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 838 | { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 839 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 840 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 841 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 842 | * checks above make sure uAdditionalInfo values line up with |
| 843 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 844 | * > 0x1f so cast is safe. |
| 845 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 846 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 847 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 848 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 849 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 850 | * are caught before this is called. |
| 851 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 852 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 853 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 854 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 855 | /* Half-precision is returned as a double. The cast to |
| 856 | * uint16_t is safe because the encoded value was 16 bits. It |
| 857 | * was widened to 64 bits to be passed in here. |
| 858 | */ |
| 859 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 860 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 861 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 862 | uReturn = QCBOR_ERR_HALF_PRECISION_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 863 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 864 | break; |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 865 | case SINGLE_PREC_FLOAT: /* 26 */ |
| 866 | /* Single precision is normally returned as a double since |
| 867 | * double is widely supported, there is no loss of precision, |
| 868 | * it makes it easy for the caller in most cases and it can |
| 869 | * be converted back to single with no loss of precision |
| 870 | * |
| 871 | * The cast to uint32_t is safe because the encoded value was |
| 872 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 873 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 874 | { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 875 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 876 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 877 | /* In the normal case, use HW to convert float to |
| 878 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 879 | pDecodedItem->val.dfnum = (double)f; |
| 880 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 881 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 882 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 883 | pDecodedItem->val.fnum = f; |
| 884 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 885 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 886 | /* IEEE754_FloatToDouble() could be used here to return as |
| 887 | * a double, but it adds object code and most likely |
| 888 | * anyone disabling FLOAT HW use doesn't care about floats |
| 889 | * and wants to save object code. |
| 890 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 891 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 892 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 893 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 894 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 895 | case DOUBLE_PREC_FLOAT: /* 27 */ |
| 896 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 897 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 898 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 899 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 900 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 901 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 902 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 903 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 904 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 905 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 906 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 907 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 908 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 909 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 910 | * as e0 … f7 |
| 911 | */ |
| 912 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 913 | goto Done; |
| 914 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 915 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 916 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 917 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 918 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 919 | /* DecodeHead() will make uArgument equal to |
| 920 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 921 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 922 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 923 | */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 924 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 925 | break; |
| 926 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 927 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 928 | Done: |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 929 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 933 | /** |
| 934 | * @brief Decode text and byte strings |
| 935 | * |
| 936 | * @param[in] pAllocator The string allocator or NULL. |
| 937 | * @param[in] uStrLen The length of the string. |
| 938 | * @param[in] pUInBuf The surce from which to read the string's bytes. |
| 939 | * @param[out] pDecodedItem The filled in decoded item. |
| 940 | * |
| 941 | * @retval QCBOR_ERR_HIT_END |
| 942 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 943 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 944 | * |
| 945 | * The reads @c uStrlen bytes from @c pUInBuf and fills in @c |
| 946 | * pDecodedItem. If @c pAllocator is not NULL then memory for the |
| 947 | * string is allocated. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 948 | */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 949 | static inline QCBORError |
Laurence Lundblade | 63e68f7 | 2020-12-28 04:24:11 -0800 | [diff] [blame] | 950 | DecodeBytes(const QCBORInternalAllocator *pAllocator, |
| 951 | uint64_t uStrLen, |
| 952 | UsefulInputBuf *pUInBuf, |
| 953 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 954 | { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 955 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 956 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 957 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 958 | * CPUs. This check makes the casts to size_t below safe. |
| 959 | * |
| 960 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 961 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 962 | * one will care the limit on strings is 4 bytes shorter). |
| 963 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 964 | if(uStrLen > SIZE_MAX-4) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 965 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 966 | goto Done; |
| 967 | } |
| 968 | |
| 969 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 970 | if(UsefulBuf_IsNULLC(Bytes)) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 971 | /* Failed to get the bytes for this string item */ |
| 972 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 973 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 974 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 975 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 976 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 977 | /* Note that this is not where allocation to coallsece |
| 978 | * indefinite-length strings is done. This is for when the caller |
| 979 | * has requested all strings be allocated. Disabling indefinite |
| 980 | * length strings also disables this allocate-all option. |
| 981 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 982 | if(pAllocator) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 983 | /* request to use the string allocator to make a copy */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 984 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 985 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 986 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 987 | goto Done; |
| 988 | } |
| 989 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 990 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 991 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 992 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 993 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 994 | (void)pAllocator; |
| 995 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 996 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 997 | /* Normal case with no string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 998 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 999 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1000 | Done: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1001 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1004 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1005 | /** |
| 1006 | * @brief Map the CBOR major types for strings to the QCBOR types. |
| 1007 | * |
| 1008 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1009 | * @retturns QCBOR type number. |
| 1010 | * |
| 1011 | * This only works for the two string types. |
| 1012 | */ |
| 1013 | static inline uint8_t ConvertStringMajorTypes(int nCBORMajorType) |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1014 | { |
| 1015 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 1016 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 1017 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1018 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1019 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 1020 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 1021 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1022 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1023 | return (uint8_t)(nCBORMajorType + 4); |
| 1024 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1025 | |
| 1026 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1027 | /** |
| 1028 | * @brief Map the CBOR major types for arrays/maps to the QCBOR types. |
| 1029 | * |
| 1030 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1031 | * @retturns QCBOR type number. |
| 1032 | * |
| 1033 | * This only works for the two aggregate types. |
| 1034 | */ |
| 1035 | static inline uint8_t ConvertArrayOrMapType(int nCBORMajorType) |
| 1036 | { |
| 1037 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1038 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1039 | #endif |
| 1040 | |
| 1041 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1042 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1043 | #endif |
| 1044 | |
| 1045 | return (uint8_t)(nCBORMajorType); |
| 1046 | } |
| 1047 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1048 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1049 | /** |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1050 | * @brief Decode a single primitive data item. |
| 1051 | * |
| 1052 | * @param[in] pUInBuf Input buffer to read data item from. |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1053 | * @param[out] pDecodedItem The filled-in decoded item. |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1054 | * @param[in] pAllocator The allocator to use for strings or NULL. |
| 1055 | * |
| 1056 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1057 | * @retval QCBOR_ERR_HIT_END |
| 1058 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1059 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1060 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1061 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1062 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1063 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1064 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1065 | * This decodes the most primitive / atomic data item. It does |
| 1066 | * no combing of data items. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1067 | */ |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1068 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, |
| 1069 | QCBORItem *pDecodedItem, |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1070 | const QCBORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1071 | { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1072 | QCBORError uReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1073 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1074 | /* Get the major type and the argument. The argument could be |
| 1075 | * length of more bytes or the value depending on the major |
| 1076 | * type. nAdditionalInfo is an encoding of the length of the |
| 1077 | * uNumber and is needed to decode floats and doubles. |
| 1078 | */ |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 1079 | int nMajorType = 0; |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1080 | uint64_t uArgument = 0; |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 1081 | int nAdditionalInfo = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1082 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 1083 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1084 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1085 | uReturn = DecodeHead(pUInBuf, &nMajorType, &uArgument, &nAdditionalInfo); |
| 1086 | if(uReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1087 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1088 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1089 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1090 | /* At this point the major type and the argument are valid. We've |
| 1091 | * got the type and the argument that starts every CBOR data item. |
| 1092 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1093 | switch (nMajorType) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1094 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1095 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1096 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1097 | uReturn = QCBOR_ERR_BAD_INT; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1098 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1099 | uReturn = DecodeInteger(nMajorType, uArgument, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1100 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1101 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1102 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1103 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1104 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
| 1105 | pDecodedItem->uDataType = ConvertStringMajorTypes(nMajorType); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1106 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1107 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1108 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1109 | uReturn = DecodeBytes(pAllocator, uArgument, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1110 | } |
| 1111 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1112 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1113 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1114 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1115 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1116 | /* Indefinite-length string. */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1117 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1118 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1119 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1120 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1121 | break; |
| 1122 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1123 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1124 | /* Definite-length string. */ |
| 1125 | if(uArgument > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1126 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1127 | goto Done; |
| 1128 | } |
| 1129 | /* cast OK because of check above */ |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1130 | pDecodedItem->val.uCount = (uint16_t)uArgument; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1131 | } |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1132 | pDecodedItem->uDataType = ConvertArrayOrMapType(nMajorType); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1133 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1134 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1135 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1136 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1137 | uReturn = QCBOR_ERR_BAD_INT; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1138 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1139 | pDecodedItem->val.uTagV = uArgument; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1140 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1141 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1142 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1143 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1144 | case CBOR_MAJOR_TYPE_SIMPLE: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1145 | /* Major type 7: float, double, true, false, null... */ |
| 1146 | uReturn = DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1147 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1148 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1149 | default: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1150 | /* Never happens because DecodeHead() should never return > 7 */ |
| 1151 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1152 | break; |
| 1153 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1154 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1155 | Done: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1156 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1160 | /** |
| 1161 | * @brief Process indefinite length strings |
| 1162 | * |
| 1163 | * @param[in] pMe Decoder context |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1164 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1165 | * |
| 1166 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1167 | * @retval QCBOR_ERR_HIT_END |
| 1168 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1169 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1170 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1171 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1172 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1173 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1174 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1175 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1176 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1177 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1178 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1179 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1180 | * If it is, this loops getting the subsequent chunk data items that |
| 1181 | * make up the string. The string allocator is used to make a |
| 1182 | * contiguous buffer for the chunks. When this completes @c |
| 1183 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1184 | * |
| 1185 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1186 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1187 | static inline QCBORError |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1188 | GetNext_FullItem(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1189 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1190 | /* Aproximate stack usage |
| 1191 | * 64-bit 32-bit |
| 1192 | * local vars 32 16 |
| 1193 | * 2 UsefulBufs 32 16 |
| 1194 | * QCBORItem 56 52 |
| 1195 | * TOTAL 120 74 |
| 1196 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1197 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1198 | /* The string allocator is used here for two purposes: 1) |
| 1199 | * coalescing the chunks of an indefinite length string, 2) |
| 1200 | * allocating storage for every string returned. |
| 1201 | * |
| 1202 | * The first use is below in this function. Indefinite length |
| 1203 | * strings cannot be processed at all without a string allocator. |
| 1204 | * |
| 1205 | * The second used is in DecodeBytes() which is called by |
| 1206 | * GetNext_Item() below. This second use unneccessary for most use |
| 1207 | * and only happens when requested in the call to |
| 1208 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1209 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1210 | * |
| 1211 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1212 | * allocator altogether and thus both of these uses. It reduced the |
| 1213 | * decoder object code by about 400 bytes. |
| 1214 | */ |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1215 | const QCBORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1216 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1217 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1218 | const QCBORInternalAllocator *pAllocator = NULL; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1219 | |
| 1220 | if(pMe->StringAllocator.pfAllocator) { |
| 1221 | pAllocator = &(pMe->StringAllocator); |
| 1222 | if(pMe->bStringAllocateAll) { |
| 1223 | pAllocatorForGetNext = pAllocator; |
| 1224 | } |
| 1225 | } |
| 1226 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1227 | |
| 1228 | QCBORError uReturn; |
| 1229 | uReturn = GetNext_Item(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
| 1230 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1231 | goto Done; |
| 1232 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1233 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1234 | /* Only do indefinite length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1235 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1236 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1237 | goto Done; |
| 1238 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1239 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1240 | /* Is this a string with an indefinite length? */ |
| 1241 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1242 | goto Done; |
| 1243 | } |
| 1244 | |
| 1245 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 1246 | /* Can't do indefinite length strings without a string allocator */ |
| 1247 | if(pAllocator == NULL) { |
| 1248 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1249 | goto Done; |
| 1250 | } |
| 1251 | |
| 1252 | /* Loop getting chunks of the indefinite length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1253 | UsefulBufC FullString = NULLUsefulBufC; |
| 1254 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1255 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1256 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1257 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1258 | /* Pass a NULL string allocator to GetNext_Item() because the |
| 1259 | * individual string chunks in an indefinite length should not |
| 1260 | * be allocated. They are always copied in the the contiguous |
| 1261 | * buffer allocated here. |
| 1262 | */ |
| 1263 | uReturn = GetNext_Item(&(pMe->InBuf), &StringChunkItem, NULL); |
| 1264 | if(uReturn) { |
| 1265 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1266 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1267 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1268 | /* Is item is the marker for end of the indefinite length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1269 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1270 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1271 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1272 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1273 | break; |
| 1274 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1275 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1276 | /* All chunks must be of the same type, the type of the item |
| 1277 | * that introduces the indefinite length string. This also |
| 1278 | * catches errors where the chunk is not a string at all and an |
| 1279 | * indefinite length string inside an indefinite length string. |
| 1280 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1281 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1282 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1283 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1284 | break; |
| 1285 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1286 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1287 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1288 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1289 | * not NULL and a reallocation happens. |
| 1290 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1291 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1292 | UNCONST_POINTER(FullString.ptr), |
| 1293 | FullString.len + StringChunkItem.val.string.len); |
| 1294 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1295 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1296 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1297 | break; |
| 1298 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1299 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1300 | /* Copy new string chunk to the end of accumulated string */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1301 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1302 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1303 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1304 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1305 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1306 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1307 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1308 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1309 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1310 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1311 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1312 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1313 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1316 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1317 | /** |
| 1318 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1319 | * |
| 1320 | * @param[in] pMe The decode context. |
| 1321 | * @param[in] uUnMappedTag The tag number to map |
| 1322 | * @param[out] puMappedTagNumer The stored tag number. |
| 1323 | * |
| 1324 | * @return error code. |
| 1325 | * |
| 1326 | * The main point of mapping tag numbers is make QCBORItem |
| 1327 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1328 | * bytes. Without, it would take up 32 bytes. |
| 1329 | * |
| 1330 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1331 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1332 | * |
| 1333 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1334 | */ |
| 1335 | static inline QCBORError |
| 1336 | MapTagNumber(QCBORDecodeContext *pMe, uint64_t uUnMappedTag, uint16_t *puMappedTagNumer) |
| 1337 | { |
| 1338 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1339 | int uTagMapIndex; |
| 1340 | /* Is there room in the tag map, or is it in it already? */ |
| 1341 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1342 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1343 | break; |
| 1344 | } |
| 1345 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1346 | break; |
| 1347 | } |
| 1348 | } |
| 1349 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1350 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1351 | } |
| 1352 | |
| 1353 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1354 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1355 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1356 | |
| 1357 | } else { |
| 1358 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1359 | } |
| 1360 | |
| 1361 | return QCBOR_SUCCESS; |
| 1362 | } |
| 1363 | |
| 1364 | |
| 1365 | /** |
| 1366 | * @brief This converts a mapped tag number to the actual tag number. |
| 1367 | * |
| 1368 | * @param[in] pMe The decode context. |
| 1369 | * @param[in] uMappedTagNumber The stored tag number. |
| 1370 | * |
| 1371 | * @return The actual tag number is returned or |
| 1372 | * @ref CBOR_TAG_INVALID64 on error. |
| 1373 | * |
| 1374 | * This is the reverse of MapTagNumber() |
| 1375 | */ |
| 1376 | static uint64_t |
| 1377 | UnMapTagNumber(const QCBORDecodeContext *pMe, uint16_t uMappedTagNumber) |
| 1378 | { |
| 1379 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1380 | return uMappedTagNumber; |
| 1381 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1382 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1383 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1384 | /* This won't be negative because of code below in |
| 1385 | * MapTagNumber() |
| 1386 | */ |
| 1387 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1388 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1392 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1393 | /** |
| 1394 | * @brief Aggregate all tags wrapping a data item. |
| 1395 | * |
| 1396 | * @param[in] pMe Decoder context |
| 1397 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1398 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1399 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1400 | * @retval QCBOR_ERR_HIT_END |
| 1401 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1402 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1403 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1404 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1405 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1406 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1407 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1408 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1409 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1410 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1411 | * |
| 1412 | * This loops getting atomic data items until one is not a tag |
| 1413 | * number. Usually this is largely pass-through because most |
| 1414 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1415 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1416 | static QCBORError |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1417 | GetNext_TaggedItem(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1418 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1419 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1420 | |
| 1421 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1422 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1423 | /* Be sure the memset does the right thing. */ |
| 1424 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1425 | #endif |
| 1426 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1427 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1428 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1429 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1430 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1431 | for(;;) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1432 | QCBORError uErr = GetNext_FullItem(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1433 | if(uErr != QCBOR_SUCCESS) { |
| 1434 | uReturn = uErr; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1435 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1436 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1437 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1438 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1439 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1440 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1441 | break; |
| 1442 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1443 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1444 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1445 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1446 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1447 | /* Continue on to get all tags wrapping this item even though |
| 1448 | * it is erroring out in the end. This allows decoding to |
| 1449 | * continue. This is a resource limit error, not a problem |
| 1450 | * with being well-formed CBOR. |
| 1451 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1452 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1453 | } |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1454 | /* Slide tags over one in the array to make room at index 0. |
| 1455 | * Must use memmove because the move source and destination |
| 1456 | * overlap. |
| 1457 | */ |
| 1458 | memmove(&auItemsTags[1], auItemsTags, sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1459 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1460 | /* Map the tag */ |
| 1461 | uint16_t uMappedTagNumer; |
| 1462 | uReturn = MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumer); |
| 1463 | /* Continue even on error so as to consume all tags wrapping |
| 1464 | * this data item so decoding can go on. If MapTagNumber() |
| 1465 | * errors once it will continue to error. |
| 1466 | */ |
| 1467 | auItemsTags[0] = uMappedTagNumer; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1468 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1469 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1470 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1471 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | /* |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1476 | * @brief Combine a map entry label and value into one item. |
| 1477 | * |
| 1478 | * @param[in] pMe Decoder context |
| 1479 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1480 | * |
| 1481 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1482 | * @retval QCBOR_ERR_HIT_END |
| 1483 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1484 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1485 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1486 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1487 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1488 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1489 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1490 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1491 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1492 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1493 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1494 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1495 | * |
| 1496 | * If a the current nesting level is a map, then this |
| 1497 | * combines pairs of items into one data item with a label |
| 1498 | * and value. |
| 1499 | * |
| 1500 | * This is pass-through if the current nesting leve is |
| 1501 | * not a map. |
| 1502 | * |
| 1503 | * This also implements maps-as-array mode where a map |
| 1504 | * is treated like an array to allow caller to do their |
| 1505 | * own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1506 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1507 | static inline QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1508 | GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1509 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1510 | QCBORError uReturn = GetNext_TaggedItem(me, pDecodedItem); |
| 1511 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1512 | goto Done; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1513 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1514 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1515 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1516 | /* Break can't be a map entry */ |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1517 | goto Done; |
| 1518 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1519 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1520 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1521 | /* Normal decoding of maps -- combine label and value into one item. */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1522 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1523 | if(DecodeNesting_IsCurrentTypeMap(&(me->nesting))) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1524 | /* Save label in pDecodedItem and get the next which will |
| 1525 | * be the real data item. |
| 1526 | */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1527 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1528 | uReturn = GetNext_TaggedItem(me, pDecodedItem); |
| 1529 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1530 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1531 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1532 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1533 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1534 | |
| 1535 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1536 | /* strings are always good labels */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1537 | pDecodedItem->label.string = LabelItem.val.string; |
| 1538 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 1539 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1540 | /* It's not a string and we only want strings */ |
| 1541 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1542 | goto Done; |
| 1543 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1544 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1545 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1546 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1547 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1548 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1549 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1550 | pDecodedItem->label.string = LabelItem.val.string; |
| 1551 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1552 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1553 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1554 | /* label is not an int or a string. It is an arrray |
| 1555 | * or a float or such and this implementation doesn't handle that. |
| 1556 | * Also, tags on labels are ignored. |
| 1557 | */ |
| 1558 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1559 | goto Done; |
| 1560 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1561 | } |
| 1562 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1563 | /* Decoding of maps as arrays to let the caller decide what to do |
| 1564 | * about labels, particularly lables that are not integers or |
| 1565 | * strings. |
| 1566 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1567 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1568 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1569 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1570 | goto Done; |
| 1571 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1572 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1573 | /* Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2. |
| 1574 | * Cast is needed because of integer promotion. |
| 1575 | */ |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1576 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1579 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1580 | Done: |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1581 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1585 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1586 | /** |
| 1587 | * @brief Peek and see if next data item is a break; |
| 1588 | * |
| 1589 | * @param[in] pUIB UsefulInputBuf to read from. |
| 1590 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1591 | * |
| 1592 | * @return Any decoding error. |
| 1593 | * |
| 1594 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1595 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1596 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1597 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1598 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1599 | { |
| 1600 | *pbNextIsBreak = false; |
| 1601 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1602 | QCBORItem Peek; |
| 1603 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
| 1604 | QCBORError uReturn = GetNext_Item(pUIB, &Peek, NULL); |
| 1605 | if(uReturn != QCBOR_SUCCESS) { |
| 1606 | return uReturn; |
| 1607 | } |
| 1608 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1609 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1610 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1611 | } else { |
| 1612 | *pbNextIsBreak = true; |
| 1613 | } |
| 1614 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1615 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1616 | return QCBOR_SUCCESS; |
| 1617 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1618 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1619 | |
| 1620 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1621 | /* |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1622 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1623 | * |
| 1624 | * @param[in] pMe The decode context. |
| 1625 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
| 1626 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1627 | * An item was just consumed, now figure out if it was the |
| 1628 | * end of an array/map map that can be closed out. That |
| 1629 | * may in turn close out the above array/map... |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1630 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1631 | static QCBORError NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1632 | { |
| 1633 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1634 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1635 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1636 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1637 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1638 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1639 | /* Nesting level is bstr-wrapped CBOR */ |
| 1640 | |
| 1641 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1642 | * so no further ascending can happen. |
| 1643 | */ |
| 1644 | break; |
| 1645 | |
| 1646 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1647 | /* Level is a definite-length array/map */ |
| 1648 | |
| 1649 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1650 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1651 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1652 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1653 | break; |
| 1654 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1655 | /* All items in a definite length array were consumed so it |
| 1656 | * is time to ascend one level. This happens below. |
| 1657 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1658 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1659 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1660 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1661 | /* Level is an indefinite-length array/map. */ |
| 1662 | |
| 1663 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1664 | bool bIsBreak = false; |
| 1665 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1666 | if(uReturn != QCBOR_SUCCESS) { |
| 1667 | goto Done; |
| 1668 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1669 | |
| 1670 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1671 | /* 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] | 1672 | break; |
| 1673 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1674 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1675 | /* It was a break in an indefinite length map / array so |
| 1676 | * it is time to ascend one level. |
| 1677 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1678 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1679 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1680 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1681 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1682 | |
| 1683 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1684 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1685 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1686 | * QCBORDecode_ExitBoundedMode(). |
| 1687 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1688 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1689 | /* Set the count to zero for definite length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1690 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1691 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1692 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1693 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1694 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1695 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1696 | break; |
| 1697 | } |
| 1698 | |
| 1699 | /* Finally, actually ascend one level. */ |
| 1700 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1701 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1702 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1703 | uReturn = QCBOR_SUCCESS; |
| 1704 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1705 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1706 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1707 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1708 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1709 | return uReturn; |
| 1710 | } |
| 1711 | |
| 1712 | |
| 1713 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1714 | This handles the traversal descending into and asecnding out of maps, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1715 | arrays and bstr-wrapped CBOR. It figures out the ends of definite and |
| 1716 | indefinte length maps and arrays by looking at the item count or |
| 1717 | finding CBOR breaks. It detects the ends of the top-level sequence |
| 1718 | and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1719 | |
| 1720 | @retval QCBOR_ERR_UNSUPPORTED X |
| 1721 | |
| 1722 | @retval QCBOR_ERR_HIT_END |
| 1723 | |
| 1724 | @retval QCBOR_ERR_INT_OVERFLOW X |
| 1725 | |
| 1726 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1727 | |
| 1728 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1729 | |
| 1730 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED X |
| 1731 | |
| 1732 | @retval QCBOR_ERR_BAD_TYPE_7 X |
| 1733 | |
| 1734 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1735 | |
| 1736 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1737 | |
| 1738 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1739 | |
| 1740 | @retval QCBOR_ERR_MAP_LABEL_TYPE X |
| 1741 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1742 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1743 | |
| 1744 | @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1745 | |
| 1746 | @retval QCBOR_ERR_BAD_BREAK |
| 1747 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1748 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1749 | static QCBORError |
| 1750 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1751 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1752 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1753 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1754 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1755 | /* |
| 1756 | If out of bytes to consume, it is either the end of the top-level |
| 1757 | sequence of some bstr-wrapped CBOR that was entered. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1758 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1759 | In the case of bstr-wrapped CBOR, the length of the UsefulInputBuf |
| 1760 | was set to that of the bstr-wrapped CBOR. When the bstr-wrapped |
| 1761 | CBOR is exited, the length is set back to the top-level's length |
| 1762 | or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1763 | */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1764 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1765 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1766 | goto Done; |
| 1767 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1768 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1769 | /* |
| 1770 | Check to see if at the end of a bounded definite length map or |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1771 | array. The check for a break ending indefinite length array is |
| 1772 | later in NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1773 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1774 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1775 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1776 | goto Done; |
| 1777 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1778 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1779 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1780 | uReturn = GetNext_MapEntry(me, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1781 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1782 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1783 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1784 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1785 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1786 | /* |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1787 | Breaks ending arrays/maps are processed later in the call to |
| 1788 | NestLevelAscender(). They should never show up here. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1789 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1790 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1791 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1792 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1793 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1794 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1795 | /* |
| 1796 | Record the nesting level for this data item before processing any |
| 1797 | of decrementing and descending. |
| 1798 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1799 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1800 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1801 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1802 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1803 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1804 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1805 | If the new item is a map or array, descend. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1806 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1807 | Empty indefinite length maps and arrays are descended into, but |
| 1808 | then ascended out of in the next chunk of code. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1809 | |
| 1810 | Maps and arrays do count as items in the map/array that |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1811 | encloses them so a decrement needs to be done for them too, but |
| 1812 | 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. |
| 1815 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1816 | QCBORError uDescendErr; |
| 1817 | uDescendErr = DecodeNesting_DescendMapOrArray(&(me->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) { |
| 1821 | /* This error is probably a traversal error and it |
| 1822 | overrides the non-traversal error. */ |
| 1823 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1824 | goto Done; |
| 1825 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1828 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1829 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1830 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1831 | /* |
| 1832 | The following cases are handled here: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1833 | - A non-aggregate item like an integer or string |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1834 | - An empty definite length map or array |
| 1835 | - An indefinite length map or array that might be empty or might not. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1836 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1837 | NestLevelAscender() does the work of decrementing the count for an |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1838 | definite length map/array and break detection for an indefinite |
| 1839 | length map/array. If the end of the map/array was reached, then |
| 1840 | it ascends nesting levels, possibly all the way to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1841 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1842 | QCBORError uAscendErr; |
| 1843 | uAscendErr = NestLevelAscender(me, true); |
| 1844 | if(uAscendErr != QCBOR_SUCCESS) { |
| 1845 | /* This error is probably a traversal error and it |
| 1846 | overrides the non-traversal error. */ |
| 1847 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1848 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1849 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1850 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1851 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1852 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1853 | /* |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1854 | Tell the caller what level is next. This tells them what |
| 1855 | maps/arrays were closed out and makes it possible for them to |
| 1856 | reconstruct the tree with just the information returned in |
| 1857 | a QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1858 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1859 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1860 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1861 | pDecodedItem->uNextNestLevel = 0; |
| 1862 | } else { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1863 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1864 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1865 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1866 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1867 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1870 | |
| 1871 | static inline void ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1872 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 1873 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 1874 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 1875 | } |
| 1876 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1879 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1880 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1881 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1882 | The epoch formatted date. Turns lots of different forms of encoding |
| 1883 | date into uniform one |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1884 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1885 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1886 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1887 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1888 | |
| 1889 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1890 | |
| 1891 | switch (pDecodedItem->uDataType) { |
| 1892 | |
| 1893 | case QCBOR_TYPE_INT64: |
| 1894 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1895 | break; |
| 1896 | |
| 1897 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1898 | // This only happens for CBOR type 0 > INT64_MAX so it is |
| 1899 | // always an overflow. |
| 1900 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1901 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1902 | break; |
| 1903 | |
| 1904 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1905 | case QCBOR_TYPE_FLOAT: |
| 1906 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1907 | { |
| 1908 | // This comparison needs to be done as a float before |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1909 | // conversion to an int64_t to be able to detect doubles that |
| 1910 | // are too large to fit into an int64_t. A double has 52 |
| 1911 | // bits of preceision. An int64_t has 63. Casting INT64_MAX |
| 1912 | // to a double actually causes a round up which is bad and |
| 1913 | // wrong for the comparison because it will allow conversion |
| 1914 | // of doubles that can't fit into a uint64_t. To remedy this |
| 1915 | // INT64_MAX - 0x7ff is used as the cutoff point because if |
| 1916 | // that value rounds up in conversion to double it will still |
| 1917 | // be less than INT64_MAX. 0x7ff is picked because it has 11 |
| 1918 | // bits set. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1919 | // |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1920 | // INT64_MAX seconds is on the order of 10 billion years, and |
| 1921 | // the earth is less than 5 billion years old, so for most |
| 1922 | // uses this conversion error won't occur even though doubles |
| 1923 | // can go much larger. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1924 | // |
| 1925 | // Without the 0x7ff there is a ~30 minute range of time |
| 1926 | // values 10 billion years in the past and in the future |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1927 | // where this code would go wrong. Some compilers |
| 1928 | // will generate warnings or errors without the 0x7ff |
| 1929 | // because of the precision issue. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1930 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
| 1931 | pDecodedItem->val.dfnum : |
| 1932 | (double)pDecodedItem->val.fnum; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1933 | if(isnan(d) || |
| 1934 | d > (double)(INT64_MAX - 0x7ff) || |
| 1935 | d < (double)(INT64_MIN + 0x7ff)) { |
| 1936 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1937 | goto Done; |
| 1938 | } |
| 1939 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1940 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 1941 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1942 | } |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1943 | #else |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1944 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1945 | uReturn = QCBOR_ERR_FLOAT_DATE_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1946 | goto Done; |
| 1947 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1948 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1949 | break; |
| 1950 | |
| 1951 | default: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1952 | uReturn = QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1953 | goto Done; |
| 1954 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1955 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1956 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1957 | |
| 1958 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1959 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | |
| 1963 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1964 | /* |
| 1965 | Decode decimal fractions and big floats. |
| 1966 | |
| 1967 | When called pDecodedItem must be the array that is tagged as a big |
| 1968 | float or decimal fraction, the array that has the two members, the |
| 1969 | exponent and mantissa. |
| 1970 | |
| 1971 | This will fetch and decode the exponent and mantissa and put the |
| 1972 | result back into pDecodedItem. |
| 1973 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 1974 | static inline QCBORError |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1975 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
| 1976 | { |
| 1977 | QCBORError nReturn; |
| 1978 | |
| 1979 | // --- Make sure it is an array; track nesting level of members --- |
| 1980 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 1981 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1982 | goto Done; |
| 1983 | } |
| 1984 | |
| 1985 | // A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1986 | // definite length arrays, but not for indefnite. Instead remember |
| 1987 | // the nesting level the two integers must be at, which is one |
| 1988 | // deeper than that of the array. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1989 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 1990 | |
| 1991 | // --- Is it a decimal fraction or a bigfloat? --- |
| 1992 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
| 1993 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 1994 | |
| 1995 | // --- Get the exponent --- |
| 1996 | QCBORItem exponentItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1997 | nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1998 | if(nReturn != QCBOR_SUCCESS) { |
| 1999 | goto Done; |
| 2000 | } |
| 2001 | if(exponentItem.uNestingLevel != nNestLevel) { |
| 2002 | // Array is empty or a map/array encountered when expecting an int |
| 2003 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 2004 | goto Done; |
| 2005 | } |
| 2006 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
| 2007 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 2008 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 2009 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 2010 | // will be too large for this to handle and thus an error that will |
| 2011 | // get handled in the next else. |
| 2012 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2013 | } else { |
| 2014 | // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 2015 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 2016 | goto Done; |
| 2017 | } |
| 2018 | |
| 2019 | // --- Get the mantissa --- |
| 2020 | QCBORItem mantissaItem; |
| 2021 | nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL); |
| 2022 | if(nReturn != QCBOR_SUCCESS) { |
| 2023 | goto Done; |
| 2024 | } |
| 2025 | if(mantissaItem.uNestingLevel != nNestLevel) { |
| 2026 | // Mantissa missing or map/array encountered when expecting number |
| 2027 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 2028 | goto Done; |
| 2029 | } |
| 2030 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
| 2031 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 2032 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 2033 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 2034 | // will be too large for this to handle and thus an error that |
| 2035 | // will get handled in an else below. |
| 2036 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2037 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2038 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2039 | // Got a good big num mantissa |
| 2040 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
| 2041 | // Depends on numbering of QCBOR_TYPE_XXX |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2042 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 2043 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 2044 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2045 | } else { |
| 2046 | // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 2047 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 2048 | goto Done; |
| 2049 | } |
| 2050 | |
| 2051 | // --- Check that array only has the two numbers --- |
| 2052 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 2053 | // Extra items in the decimal fraction / big float |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2054 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 2055 | goto Done; |
| 2056 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2057 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2058 | |
| 2059 | Done: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2060 | return nReturn; |
| 2061 | } |
| 2062 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 2063 | |
| 2064 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2065 | static inline QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2066 | { |
| 2067 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2068 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2069 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2070 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2071 | } else { |
| 2072 | return QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2073 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2074 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2075 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2076 | return QCBOR_SUCCESS; |
| 2077 | } |
| 2078 | |
| 2079 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2080 | /* |
| 2081 | * Table of CBOR tags whose content is either a text string or a byte |
| 2082 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2083 | * of uQCBORtype indicates the content should be a byte string rather |
| 2084 | * than a text string |
| 2085 | */ |
| 2086 | struct StringTagMapEntry { |
| 2087 | uint16_t uTagNumber; |
| 2088 | uint8_t uQCBORtype; |
| 2089 | }; |
| 2090 | |
| 2091 | #define IS_BYTE_STRING_BIT 0x80 |
| 2092 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2093 | |
| 2094 | static const struct StringTagMapEntry StringTagMap[] = { |
| 2095 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
| 2096 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2097 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2098 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2099 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
| 2100 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2101 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2102 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2103 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
| 2104 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2105 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2106 | }; |
| 2107 | |
| 2108 | |
| 2109 | /* |
| 2110 | * Process the CBOR tags that whose content is a byte string or a text |
| 2111 | * string and for which the string is just passed on to the caller. |
| 2112 | * |
| 2113 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2114 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2115 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2116 | * possible. |
| 2117 | * |
| 2118 | * This returns QCBOR_SUCCESS if the tag was procssed, |
| 2119 | * QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
| 2120 | * QCBOR_ERR_BAD_OPT_TAG if the content type was wrong for the tag. |
| 2121 | */ |
| 2122 | static inline |
| 2123 | QCBORError ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2124 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2125 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2126 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2127 | return QCBOR_ERR_UNSUPPORTED; |
| 2128 | } |
| 2129 | |
| 2130 | unsigned uIndex; |
| 2131 | for(uIndex = 0; StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2132 | if(StringTagMap[uIndex].uTagNumber == uTag) { |
| 2133 | break; |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | const uint8_t uQCBORType = StringTagMap[uIndex].uQCBORtype; |
| 2138 | if(uQCBORType == QCBOR_TYPE_NONE) { |
| 2139 | /* repurpose this error to mean, not handled here */ |
| 2140 | return QCBOR_ERR_UNSUPPORTED; |
| 2141 | } |
| 2142 | |
| 2143 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2144 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2145 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2146 | } |
| 2147 | |
| 2148 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2149 | return QCBOR_ERR_BAD_OPT_TAG; |
| 2150 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2151 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2152 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2153 | return QCBOR_SUCCESS; |
| 2154 | } |
| 2155 | |
| 2156 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2157 | /* |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2158 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2159 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2160 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2161 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2162 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2163 | static QCBORError |
| 2164 | QCBORDecode_GetNextTag(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2165 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2166 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2167 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2168 | uReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem); |
| 2169 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2170 | goto Done; |
| 2171 | } |
| 2172 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2173 | /* When there are no tag numbers for the item, this exits first |
| 2174 | * thing and effectively does nothing. |
| 2175 | * |
| 2176 | * This loops over all the tag numbers accumulated for this item |
| 2177 | * trying to decode and interpret them. This stops at the end of |
| 2178 | * the list or at the first tag number that can't be interpreted by |
| 2179 | * this code. This is effectively a recursive processing of the |
| 2180 | * tags number list that handles nested tags. |
| 2181 | */ |
| 2182 | while(1) { |
| 2183 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2184 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2185 | */ |
| 2186 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2187 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2188 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2189 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2190 | break; |
| 2191 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2192 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2193 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2194 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2195 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2196 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2197 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2198 | uReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem); |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2199 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2200 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2201 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2202 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2203 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2204 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2205 | } else { |
| 2206 | /* See if it is a pass-through byte/text string tag; process if so */ |
| 2207 | uReturn = ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2208 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2209 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
| 2210 | /* It wasn't a pass-through byte/text string tag so it is |
| 2211 | * an unknown tag. This is the exit from the loop on the |
| 2212 | * first unknown tag. It is a successful exit. |
| 2213 | */ |
| 2214 | uReturn = QCBOR_SUCCESS; |
| 2215 | break; |
| 2216 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2217 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2218 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2219 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2220 | /* Error exit from the loop */ |
| 2221 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2222 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2223 | |
| 2224 | /* A tag was successfully processed, shift it out of the list of |
| 2225 | * tags returned. This is the loop increment. |
| 2226 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2227 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2231 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2235 | /* |
| 2236 | Public function, see header qcbor/qcbor_decode.h file |
| 2237 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2238 | QCBORError |
| 2239 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2240 | { |
| 2241 | QCBORError uErr; |
| 2242 | uErr = QCBORDecode_GetNextTag(pMe, pDecodedItem); |
| 2243 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2244 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2245 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2246 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2247 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2252 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2253 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2254 | QCBORError |
| 2255 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2256 | { |
| 2257 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2258 | const UsefulInputBuf Save = pMe->InBuf; |
| 2259 | |
| 2260 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2261 | |
| 2262 | pMe->nesting = SaveNesting; |
| 2263 | pMe->InBuf = Save; |
| 2264 | |
| 2265 | return uErr; |
| 2266 | } |
| 2267 | |
| 2268 | |
| 2269 | /* |
| 2270 | Public function, see header qcbor/qcbor_decode.h file |
| 2271 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2272 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2273 | { |
| 2274 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2275 | return; |
| 2276 | } |
| 2277 | |
| 2278 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2279 | } |
| 2280 | |
| 2281 | |
| 2282 | /* |
| 2283 | Public function, see header qcbor/qcbor_decode.h file |
| 2284 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2285 | QCBORError |
| 2286 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, |
| 2287 | QCBORItem *pDecodedItem, |
| 2288 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2289 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2290 | QCBORError nReturn; |
| 2291 | |
| 2292 | nReturn = QCBORDecode_GetNext(me, pDecodedItem); |
| 2293 | if(nReturn != QCBOR_SUCCESS) { |
| 2294 | return nReturn; |
| 2295 | } |
| 2296 | |
| 2297 | if(pTags != NULL) { |
| 2298 | pTags->uNumUsed = 0; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2299 | // Reverse the order because pTags is reverse of |
| 2300 | // QCBORItem.uTags. |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2301 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2302 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2303 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2304 | } |
| 2305 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2306 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2307 | } |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 2308 | pTags->puTags[pTags->uNumUsed] = UnMapTagNumber(me,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2309 | pTags->uNumUsed++; |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2317 | |
| 2318 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2319 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2320 | */ |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2321 | bool QCBORDecode_IsTagged(QCBORDecodeContext *me, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2322 | const QCBORItem *pItem, |
| 2323 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2324 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2325 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2326 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2327 | break; |
| 2328 | } |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 2329 | if(UnMapTagNumber(me, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2330 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2331 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2332 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2333 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2334 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | |
| 2338 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2339 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2340 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2341 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2342 | { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2343 | QCBORError uReturn = me->uLastError; |
| 2344 | |
| 2345 | if(uReturn != QCBOR_SUCCESS) { |
| 2346 | goto Done; |
| 2347 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2348 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2349 | // Error out if all the maps/arrays are not closed out |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2350 | if(!DecodeNesting_IsCurrentAtTop(&(me->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2351 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2352 | goto Done; |
| 2353 | } |
| 2354 | |
| 2355 | // Error out if not all the bytes are consumed |
| 2356 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2357 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2358 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2359 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2360 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2361 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2362 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2363 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2364 | StringAllocator_Destruct(&(me->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2365 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2366 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2367 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2371 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2372 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2373 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2374 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2375 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2376 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2377 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2378 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2379 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2380 | return CBOR_TAG_INVALID64; |
| 2381 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2382 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2383 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2384 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 2385 | return UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2389 | /* |
| 2390 | Public function, see header qcbor/qcbor_decode.h file |
| 2391 | */ |
| 2392 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2393 | uint32_t uIndex) |
| 2394 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2395 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2396 | return CBOR_TAG_INVALID64; |
| 2397 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2398 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2399 | return CBOR_TAG_INVALID64; |
| 2400 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame^] | 2401 | return UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2402 | } |
| 2403 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2404 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2405 | /* |
| 2406 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2407 | Decoder errors handled in this file |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2408 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2409 | - Hit end of input before it was expected while decoding type and |
| 2410 | number QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2411 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2412 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2413 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2414 | - Hit end of input while decoding a text or byte string |
| 2415 | QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2416 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2417 | - Encountered conflicting tags -- e.g., an item is tagged both a date |
| 2418 | string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2419 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2420 | - Encontered an array or mapp that has too many items |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2421 | QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2422 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2423 | - Encountered array/map nesting that is too deep |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2424 | QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2425 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2426 | - An epoch date > INT64_MAX or < INT64_MIN was encountered |
| 2427 | QCBOR_ERR_DATE_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2428 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2429 | - The type of a map label is not a string or int |
| 2430 | QCBOR_ERR_MAP_LABEL_TYPE |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2431 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2432 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2433 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2434 | */ |
| 2435 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2436 | |
| 2437 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2438 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2439 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2440 | /* =========================================================================== |
| 2441 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2442 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2443 | This implements a simple sting allocator for indefinite length |
| 2444 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2445 | implements the function type QCBORStringAllocate and allows easy |
| 2446 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2447 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2448 | This particular allocator is built-in for convenience. The caller |
| 2449 | can implement their own. All of this following code will get |
| 2450 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2451 | |
| 2452 | This is a very primitive memory allocator. It does not track |
| 2453 | individual allocations, only a high-water mark. A free or |
| 2454 | reallocation must be of the last chunk allocated. |
| 2455 | |
| 2456 | The size of the pool and offset to free memory are packed into the |
| 2457 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2458 | the decode context. Since the address of the pool may not be |
| 2459 | aligned, they have to be packed and unpacked as if they were |
| 2460 | serialized data of the wire or such. |
| 2461 | |
| 2462 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2463 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2464 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2465 | |
| 2466 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2467 | static inline int |
| 2468 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2469 | { |
| 2470 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2471 | UsefulInputBuf UIB; |
| 2472 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2473 | // Just assume the size here. It was checked during SetUp so |
| 2474 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2475 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2476 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2477 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2478 | return UsefulInputBuf_GetError(&UIB); |
| 2479 | } |
| 2480 | |
| 2481 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2482 | static inline int |
| 2483 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2484 | { |
| 2485 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2486 | // length check performed here is useful. |
| 2487 | UsefulOutBuf UOB; |
| 2488 | |
| 2489 | UsefulOutBuf_Init(&UOB, Pool); |
| 2490 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2491 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2492 | return UsefulOutBuf_GetError(&UOB); |
| 2493 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2494 | |
| 2495 | |
| 2496 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2497 | Internal function for an allocation, reallocation free and destuct. |
| 2498 | |
| 2499 | Having only one function rather than one each per mode saves space in |
| 2500 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2501 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2502 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2503 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2504 | static UsefulBuf |
| 2505 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2506 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2507 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2508 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2509 | uint32_t uPoolSize; |
| 2510 | uint32_t uFreeOffset; |
| 2511 | |
| 2512 | if(uNewSize > UINT32_MAX) { |
| 2513 | // This allocator is only good up to 4GB. This check should |
| 2514 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2515 | goto Done; |
| 2516 | } |
| 2517 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2518 | |
| 2519 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2520 | goto Done; |
| 2521 | } |
| 2522 | |
| 2523 | if(uNewSize) { |
| 2524 | if(pMem) { |
| 2525 | // REALLOCATION MODE |
| 2526 | // Calculate pointer to the end of the memory pool. It is |
| 2527 | // assumed that pPool + uPoolSize won't wrap around by |
| 2528 | // assuming the caller won't pass a pool buffer in that is |
| 2529 | // not in legitimate memory space. |
| 2530 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2531 | |
| 2532 | // Check that the pointer for reallocation is in the range of the |
| 2533 | // pool. This also makes sure that pointer math further down |
| 2534 | // doesn't wrap under or over. |
| 2535 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2536 | // Offset to start of chunk for reallocation. This won't |
| 2537 | // wrap under because of check that pMem >= pPool. Cast |
| 2538 | // is safe because the pool is always less than UINT32_MAX |
| 2539 | // because of check in QCBORDecode_SetMemPool(). |
| 2540 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2541 | |
| 2542 | // Check to see if the allocation will fit. uPoolSize - |
| 2543 | // uMemOffset will not wrap under because of check that |
| 2544 | // pMem is in the range of the uPoolSize by check above. |
| 2545 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2546 | ReturnValue.ptr = pMem; |
| 2547 | ReturnValue.len = uNewSize; |
| 2548 | |
| 2549 | // Addition won't wrap around over because uNewSize was |
| 2550 | // checked to be sure it is less than the pool size. |
| 2551 | uFreeOffset = uMemOffset + uNewSize32; |
| 2552 | } |
| 2553 | } |
| 2554 | } else { |
| 2555 | // ALLOCATION MODE |
| 2556 | // uPoolSize - uFreeOffset will not underflow because this |
| 2557 | // pool implementation makes sure uFreeOffset is always |
| 2558 | // smaller than uPoolSize through this check here and |
| 2559 | // reallocation case. |
| 2560 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2561 | ReturnValue.len = uNewSize; |
| 2562 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2563 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2564 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2565 | } |
| 2566 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2567 | if(pMem) { |
| 2568 | // FREE MODE |
| 2569 | // Cast is safe because of limit on pool size in |
| 2570 | // QCBORDecode_SetMemPool() |
| 2571 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2572 | } else { |
| 2573 | // DESTRUCT MODE |
| 2574 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2575 | } |
| 2576 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2577 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2578 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2579 | MemPool_Pack(Pool, uFreeOffset); |
| 2580 | |
| 2581 | Done: |
| 2582 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2583 | } |
| 2584 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2585 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2586 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2587 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2588 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2589 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2590 | UsefulBuf Pool, |
| 2591 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2592 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2593 | // The pool size and free mem offset are packed into the beginning |
| 2594 | // of the pool memory. This compile time check make sure the |
| 2595 | // constant in the header is correct. This check should optimize |
| 2596 | // down to nothing. |
| 2597 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2598 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2599 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2600 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2601 | // The pool size and free offset packed in to the beginning of pool |
| 2602 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2603 | // machines. |
| 2604 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2605 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2606 | } |
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 | // This checks that the pool buffer given is big enough. |
| 2609 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2610 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2611 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2612 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2613 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2614 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2615 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2616 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2617 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2618 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2619 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2620 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2621 | |
| 2622 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2623 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2624 | { |
| 2625 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2626 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2627 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2628 | |
| 2629 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2630 | Consume an entire map or array (and do next to |
| 2631 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2632 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2633 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2634 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2635 | const QCBORItem *pItemToConsume, |
| 2636 | uint_fast8_t *puNextNestLevel) |
| 2637 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2638 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2639 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2640 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2641 | // If it is a map or array, this will tell if it is empty. |
| 2642 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2643 | |
| 2644 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2645 | /* 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] | 2646 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2647 | /* This works for definite and indefinite length |
| 2648 | * maps and arrays by using the nesting level |
| 2649 | */ |
| 2650 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2651 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2652 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2653 | goto Done; |
| 2654 | } |
| 2655 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2656 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2657 | *puNextNestLevel = Item.uNextNestLevel; |
| 2658 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2659 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2660 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2661 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2662 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2663 | /* Just pass the nesting level through */ |
| 2664 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2665 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2666 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2667 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2668 | |
| 2669 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2670 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2674 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2675 | Works only for integer and string labels. Returns false |
| 2676 | for any other type. */ |
| 2677 | static inline bool |
| 2678 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2679 | { |
| 2680 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2681 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2682 | return true; |
| 2683 | } |
| 2684 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2685 | 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] | 2686 | return true; |
| 2687 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2688 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2689 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2690 | return true; |
| 2691 | } |
| 2692 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2693 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2694 | return true; |
| 2695 | } |
| 2696 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2697 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2698 | /* Other label types are never matched */ |
| 2699 | return false; |
| 2700 | } |
| 2701 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2702 | |
| 2703 | /* |
| 2704 | Returns true if Item1 and Item2 are the same type |
| 2705 | or if either are of QCBOR_TYPE_ANY. |
| 2706 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2707 | static inline bool |
| 2708 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2709 | { |
| 2710 | if(Item1.uDataType == Item2.uDataType) { |
| 2711 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2712 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2713 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2714 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2715 | return true; |
| 2716 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2717 | return false; |
| 2718 | } |
| 2719 | |
| 2720 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2721 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2722 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2723 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2724 | @param[in] pMe The decode context to search. |
| 2725 | @param[in,out] pItemArray The items to search for and the items found. |
| 2726 | @param[out] puOffset Byte offset of last item matched. |
| 2727 | @param[in] pCBContext Context for the not-found item call back. |
| 2728 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2729 | |
| 2730 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2731 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2732 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2733 | were found for one of the labels being |
| 2734 | search for. This duplicate detection is |
| 2735 | only performed for items in pItemArray, |
| 2736 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2737 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2738 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2739 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2740 | |
| 2741 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2742 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2743 | On input pItemArray contains a list of labels and data types |
| 2744 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2745 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2746 | On output the fully retrieved items are filled in with |
| 2747 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2748 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2749 | 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] | 2750 | |
| 2751 | 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] | 2752 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2753 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2754 | MapSearch(QCBORDecodeContext *pMe, |
| 2755 | QCBORItem *pItemArray, |
| 2756 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2757 | void *pCBContext, |
| 2758 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2759 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2760 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2761 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2762 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2763 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2764 | uReturn = pMe->uLastError; |
| 2765 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2766 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2767 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2768 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2769 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2770 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2771 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2772 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2773 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2776 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2777 | // It is an empty bounded array or map |
| 2778 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2779 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2780 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2781 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2782 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2783 | // Nothing is ever found in an empty array or map. All items |
| 2784 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2785 | uReturn = QCBOR_SUCCESS; |
| 2786 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2787 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2788 | } |
| 2789 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2790 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2791 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2792 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2793 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2794 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2795 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2796 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2797 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2798 | Loop over all the items in the map or array. Each item |
| 2799 | could be a map or array, but label matching is only at |
| 2800 | the main level. This handles definite and indefinite |
| 2801 | length maps and arrays. The only reason this is ever |
| 2802 | called on arrays is to find their end position. |
| 2803 | |
| 2804 | This will always run over all items in order to do |
| 2805 | duplicate detection. |
| 2806 | |
| 2807 | This will exit with failure if it encounters an |
| 2808 | unrecoverable error, but continue on for recoverable |
| 2809 | errors. |
| 2810 | |
| 2811 | If a recoverable error occurs on a matched item, then |
| 2812 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2813 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2814 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2815 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2816 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2817 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2818 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2819 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2820 | /* Get the item */ |
| 2821 | QCBORItem Item; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2822 | QCBORError uResult = QCBORDecode_GetNextTag(pMe, &Item); |
| 2823 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2824 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2825 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2826 | goto Done; |
| 2827 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2828 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2829 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2830 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2831 | goto Done; |
| 2832 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2833 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2834 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2835 | bool bMatched = false; |
| 2836 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 2837 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2838 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2839 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2840 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2841 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2842 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2843 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2844 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2845 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2846 | goto Done; |
| 2847 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2848 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2849 | if(uResult != QCBOR_SUCCESS) { |
| 2850 | uReturn = uResult; |
| 2851 | goto Done; |
| 2852 | } |
| 2853 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2854 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2855 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2856 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2857 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2858 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2859 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2860 | bMatched = true; |
| 2861 | } |
| 2862 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2863 | |
| 2864 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2865 | if(!bMatched && pfCallback != NULL) { |
| 2866 | /* |
| 2867 | Call the callback on unmatched labels. |
| 2868 | (It is tempting to do duplicate detection here, but that would |
| 2869 | require dynamic memory allocation because the number of labels |
| 2870 | that might be encountered is unbounded.) |
| 2871 | */ |
| 2872 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2873 | if(uReturn != QCBOR_SUCCESS) { |
| 2874 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2875 | } |
| 2876 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2877 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2878 | /* |
| 2879 | Consume the item whether matched or not. This |
| 2880 | does the work of traversing maps and array and |
| 2881 | everything in them. In this loop only the |
| 2882 | items at the current nesting level are examined |
| 2883 | to match the labels. |
| 2884 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2885 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2886 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2887 | goto Done; |
| 2888 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2889 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2890 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2891 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2892 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2893 | |
| 2894 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2895 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2896 | // Check here makes sure that this won't accidentally be |
| 2897 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2898 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 2899 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 2900 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2901 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 2902 | goto Done; |
| 2903 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2904 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 2905 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2906 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2907 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2908 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 2909 | |
| 2910 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2911 | /* 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] | 2912 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2913 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2914 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 2915 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2916 | } |
| 2917 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2918 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2919 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2920 | } |
| 2921 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2922 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2923 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2924 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2925 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2926 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 2927 | int64_t nLabel, |
| 2928 | uint8_t uQcborType, |
| 2929 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2930 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2931 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2932 | return; |
| 2933 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2934 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2935 | QCBORItem OneItemSeach[2]; |
| 2936 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2937 | OneItemSeach[0].label.int64 = nLabel; |
| 2938 | OneItemSeach[0].uDataType = uQcborType; |
| 2939 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2940 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2941 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2942 | |
| 2943 | *pItem = OneItemSeach[0]; |
| 2944 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2945 | if(uReturn != QCBOR_SUCCESS) { |
| 2946 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2947 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2948 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2949 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2950 | } |
| 2951 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2952 | Done: |
| 2953 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2957 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2958 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2959 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2960 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 2961 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2962 | uint8_t uQcborType, |
| 2963 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2964 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2965 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2966 | return; |
| 2967 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2968 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2969 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2970 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2971 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2972 | OneItemSeach[0].uDataType = uQcborType; |
| 2973 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2974 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2975 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2976 | if(uReturn != QCBOR_SUCCESS) { |
| 2977 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2978 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2979 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2980 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2981 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2982 | } |
| 2983 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2984 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2985 | |
| 2986 | Done: |
| 2987 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2991 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2992 | static QCBORError |
| 2993 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2994 | { |
| 2995 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 2996 | if(uDataType == puTypeList[i]) { |
| 2997 | return QCBOR_SUCCESS; |
| 2998 | } |
| 2999 | } |
| 3000 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3001 | } |
| 3002 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3003 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3004 | /** |
| 3005 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3006 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3007 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3008 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3009 | @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] | 3010 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3011 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered |
| 3012 | tag value. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3013 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3014 | static QCBORError |
| 3015 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3016 | { |
| 3017 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3018 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3019 | /* There are tags that QCBOR couldn't process on this item and |
| 3020 | the caller has told us there should not be. */ |
| 3021 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3022 | } |
| 3023 | |
| 3024 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3025 | const int nItemType = pItem->uDataType; |
| 3026 | |
| 3027 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3028 | // Must match the tag and only the tag |
| 3029 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3030 | } |
| 3031 | |
| 3032 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3033 | if(uReturn == QCBOR_SUCCESS) { |
| 3034 | return QCBOR_SUCCESS; |
| 3035 | } |
| 3036 | |
| 3037 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3038 | /* Must match the content type and only the content type. |
| 3039 | There was no match just above so it is a fail. */ |
| 3040 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3041 | } |
| 3042 | |
| 3043 | /* If here it can match either the tag or the content |
| 3044 | and it hasn't matched the content, so the end |
| 3045 | result is whether it matches the tag. This is |
| 3046 | also the case that the CBOR standard discourages. */ |
| 3047 | |
| 3048 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3049 | } |
| 3050 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3051 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3052 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3053 | // This could be semi-private if need be |
| 3054 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3055 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3056 | int64_t nLabel, |
| 3057 | TagSpecification TagSpec, |
| 3058 | QCBORItem *pItem) |
| 3059 | { |
| 3060 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3061 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3062 | return; |
| 3063 | } |
| 3064 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3065 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3068 | |
| 3069 | // This could be semi-private if need be |
| 3070 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3071 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3072 | const char *szLabel, |
| 3073 | TagSpecification TagSpec, |
| 3074 | QCBORItem *pItem) |
| 3075 | { |
| 3076 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3077 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3078 | return; |
| 3079 | } |
| 3080 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3081 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3084 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3085 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3086 | int64_t nLabel, |
| 3087 | TagSpecification TagSpec, |
| 3088 | UsefulBufC *pString) |
| 3089 | { |
| 3090 | QCBORItem Item; |
| 3091 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3092 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3093 | *pString = Item.val.string; |
| 3094 | } |
| 3095 | } |
| 3096 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3097 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3098 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3099 | const char * szLabel, |
| 3100 | TagSpecification TagSpec, |
| 3101 | UsefulBufC *pString) |
| 3102 | { |
| 3103 | QCBORItem Item; |
| 3104 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3105 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3106 | *pString = Item.val.string; |
| 3107 | } |
| 3108 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3109 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3110 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3111 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3112 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3113 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3114 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3115 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 3116 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3117 | } |
| 3118 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3119 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3120 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3121 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3122 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3123 | QCBORItem *pItemList, |
| 3124 | void *pCallbackCtx, |
| 3125 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3126 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3127 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 3128 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3132 | /** |
| 3133 | * @brief Search for a map/array by label and enter it |
| 3134 | * |
| 3135 | * @param[in] pMe The decode context. |
| 3136 | * @param[in] pSearch The map/array to search for. |
| 3137 | * |
| 3138 | * @c pSearch is expected to contain one item of type map or array |
| 3139 | * with the label specified. The current bounded map will be searched for |
| 3140 | * this and if found will be entered. |
| 3141 | * |
| 3142 | * If the label is not found, or the item found is not a map or array, |
| 3143 | * the error state is set. |
| 3144 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3145 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3146 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3147 | // The first item in pSearch is the one that is to be |
| 3148 | // entered. It should be the only one filled in. Any other |
| 3149 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3150 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3151 | return; |
| 3152 | } |
| 3153 | |
| 3154 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3155 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3156 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3157 | return; |
| 3158 | } |
| 3159 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3160 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3161 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3162 | return; |
| 3163 | } |
| 3164 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3165 | /* |
| 3166 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3167 | * next item for the pre-order traversal cursor to be the map/array |
| 3168 | * found by MapSearch(). The next few lines of code force the |
| 3169 | * cursor to that. |
| 3170 | * |
| 3171 | * There is no need to retain the old cursor because |
| 3172 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3173 | * beginning of the map/array being entered. |
| 3174 | * |
| 3175 | * The cursor is forced by: 1) setting the input buffer position to |
| 3176 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3177 | * counter to the total in the map/array, 3) setting the nesting |
| 3178 | * level. Setting the map/array counter to the total is not |
| 3179 | * strictly correct, but this is OK because this cursor only needs |
| 3180 | * to be used to get one item and MapSearch() has already found it |
| 3181 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3182 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3183 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3184 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3185 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3186 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3187 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3188 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3189 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3193 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3194 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3195 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3196 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3197 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3198 | QCBORItem OneItemSeach[2]; |
| 3199 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3200 | OneItemSeach[0].label.int64 = nLabel; |
| 3201 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3202 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3203 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3204 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3205 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3209 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3210 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3211 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3212 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3213 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3214 | QCBORItem OneItemSeach[2]; |
| 3215 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3216 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3217 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3218 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3219 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3220 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3223 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3224 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3225 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3226 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3227 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3228 | QCBORItem OneItemSeach[2]; |
| 3229 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3230 | OneItemSeach[0].label.int64 = nLabel; |
| 3231 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3232 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3233 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3234 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3235 | } |
| 3236 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3237 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3238 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3239 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3240 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3241 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3242 | QCBORItem OneItemSeach[2]; |
| 3243 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3244 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3245 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3246 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3247 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3248 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3252 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3253 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3254 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3255 | QCBORError uErr; |
| 3256 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3257 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3258 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3259 | // Already in error state; do nothing. |
| 3260 | return; |
| 3261 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3262 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3263 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3264 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3265 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3266 | if(uErr != QCBOR_SUCCESS) { |
| 3267 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3268 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3269 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3270 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3271 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3274 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3275 | |
| 3276 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3277 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3278 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3279 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3280 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3281 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3282 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3283 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3284 | // Special case to increment nesting level for zero-length maps |
| 3285 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3286 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3287 | } |
| 3288 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3289 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3290 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3291 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3292 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3293 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3294 | if(pItem != NULL) { |
| 3295 | *pItem = Item; |
| 3296 | } |
| 3297 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3298 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3299 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3302 | |
| 3303 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3304 | This is the common work for exiting a level that is a bounded map, |
| 3305 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3306 | |
| 3307 | One chunk of work is to set up the pre-order traversal so it is at |
| 3308 | the item just after the bounded map, array or bstr that is being |
| 3309 | exited. This is somewhat complex. |
| 3310 | |
| 3311 | The other work is to level-up the bounded mode to next higest bounded |
| 3312 | mode or the top level if there isn't one. |
| 3313 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3314 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3315 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3316 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3317 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3318 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3319 | /* |
| 3320 | First the pre-order-traversal byte offset is positioned to the |
| 3321 | item just after the bounded mode item that was just consumed. |
| 3322 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3323 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3324 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3325 | /* |
| 3326 | Next, set the current nesting level to one above the bounded level |
| 3327 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3328 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3329 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3330 | makes sure pCurrentBounded is valid. |
| 3331 | */ |
| 3332 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3333 | |
| 3334 | /* |
| 3335 | This does the complex work of leveling up the pre-order traversal |
| 3336 | when the end of a map or array or another bounded level is |
| 3337 | reached. It may do nothing, or ascend all the way to the top |
| 3338 | level. |
| 3339 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 3340 | uErr = NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3341 | if(uErr != QCBOR_SUCCESS) { |
| 3342 | goto Done; |
| 3343 | } |
| 3344 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3345 | /* |
| 3346 | This makes the next highest bounded level the current bounded |
| 3347 | level. If there is no next highest level, then no bounded mode is |
| 3348 | in effect. |
| 3349 | */ |
| 3350 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3351 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3352 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3353 | |
| 3354 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3355 | return uErr; |
| 3356 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3357 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3358 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3359 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3360 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3361 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3362 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3363 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3364 | return; |
| 3365 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3366 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3367 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3368 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3369 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3370 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3371 | goto Done; |
| 3372 | } |
| 3373 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3374 | /* |
| 3375 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3376 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3377 | from previous map search, then do a dummy search. |
| 3378 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3379 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3380 | QCBORItem Dummy; |
| 3381 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3382 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3383 | if(uErr != QCBOR_SUCCESS) { |
| 3384 | goto Done; |
| 3385 | } |
| 3386 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3387 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3388 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3389 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3390 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3391 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3395 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3396 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3397 | const QCBORItem *pItem, |
| 3398 | uint8_t uTagRequirement, |
| 3399 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3400 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3401 | if(pBstr) { |
| 3402 | *pBstr = NULLUsefulBufC; |
| 3403 | } |
| 3404 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3405 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3406 | // Already in error state; do nothing. |
| 3407 | return pMe->uLastError; |
| 3408 | } |
| 3409 | |
| 3410 | QCBORError uError = QCBOR_SUCCESS; |
| 3411 | |
| 3412 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3413 | uError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3414 | goto Done;; |
| 3415 | } |
| 3416 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3417 | const TagSpecification TagSpec = |
| 3418 | { |
| 3419 | uTagRequirement, |
| 3420 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3421 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3422 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3423 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3424 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3425 | if(uError != QCBOR_SUCCESS) { |
| 3426 | goto Done; |
| 3427 | } |
| 3428 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3429 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3430 | // Reverse the decrement done by GetNext() for the bstr so the |
| 3431 | // increment in NestLevelAscender() called by ExitBoundedLevel() |
| 3432 | // will work right. |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3433 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3434 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3435 | |
| 3436 | if(pBstr) { |
| 3437 | *pBstr = pItem->val.string; |
| 3438 | } |
| 3439 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3440 | // This saves the current length of the UsefulInputBuf and then |
| 3441 | // narrows the UsefulInputBuf to start and length of the wrapped |
| 3442 | // CBOR that is being entered. |
| 3443 | // |
| 3444 | // This makes sure the length is less than |
| 3445 | // QCBOR_MAX_DECODE_INPUT_SIZE which is slighly less than |
| 3446 | // UINT32_MAX. The value UINT32_MAX is used as a special indicator |
| 3447 | // value. The checks against QCBOR_MAX_DECODE_INPUT_SIZE also make |
| 3448 | // the casts safe. uEndOfBstr will always be less than |
| 3449 | // uPreviousLength because of the way UsefulInputBuf works so there |
| 3450 | // is no need to check it. There is also a range check in the |
| 3451 | // seek. |
| 3452 | // |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3453 | // Most of these calls are simple inline accessors so this doesn't |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3454 | // amount to much code. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3455 | // Cast of uPreviousLength to uint32_t for cases where SIZE_MAX < UINT32_MAX. |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3456 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3457 | if((uint32_t)uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3458 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3459 | goto Done; |
| 3460 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3461 | const size_t uEndOfBstr = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3462 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOfBstr - pItem->val.string.len); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3463 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3464 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3465 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3466 | (uint32_t)uPreviousLength, |
| 3467 | (uint32_t)uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3468 | Done: |
| 3469 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3473 | /* |
| 3474 | Public function, see header qcbor/qcbor_decode.h file |
| 3475 | */ |
| 3476 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3477 | uint8_t uTagRequirement, |
| 3478 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3479 | { |
| 3480 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3481 | // Already in error state; do nothing. |
| 3482 | return; |
| 3483 | } |
| 3484 | |
| 3485 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3486 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3487 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3488 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3489 | return; |
| 3490 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3491 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3492 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3493 | &Item, |
| 3494 | uTagRequirement, |
| 3495 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3499 | /* |
| 3500 | Public function, see header qcbor/qcbor_decode.h file |
| 3501 | */ |
| 3502 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3503 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3504 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3505 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3506 | { |
| 3507 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3508 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3509 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3510 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3511 | &Item, |
| 3512 | uTagRequirement, |
| 3513 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3516 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3517 | /* |
| 3518 | Public function, see header qcbor/qcbor_decode.h file |
| 3519 | */ |
| 3520 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3521 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3522 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3523 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3524 | { |
| 3525 | QCBORItem Item; |
| 3526 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3527 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3528 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3529 | &Item, |
| 3530 | uTagRequirement, |
| 3531 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3532 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3533 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3534 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3535 | /* |
| 3536 | Public function, see header qcbor/qcbor_decode.h file |
| 3537 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3538 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3539 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3540 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3541 | // Already in error state; do nothing. |
| 3542 | return; |
| 3543 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3544 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3545 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3546 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3547 | return; |
| 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | Reset the length of the UsefulInputBuf to what it was before |
| 3552 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3553 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3554 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3555 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3556 | |
| 3557 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3558 | QCBORError uErr = ExitBoundedLevel(pMe, DecodeNesting_GetEndOfBstr(&(pMe->nesting))); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3559 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3560 | } |
| 3561 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3562 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3563 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3564 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3565 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3566 | |
Laurence Lundblade | 11a064e | 2020-05-07 13:13:42 -0700 | [diff] [blame] | 3567 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3568 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3569 | static QCBORError |
| 3570 | InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3571 | { |
| 3572 | switch(pItem->uDataType) { |
| 3573 | case QCBOR_TYPE_TRUE: |
| 3574 | *pBool = true; |
| 3575 | return QCBOR_SUCCESS; |
| 3576 | break; |
| 3577 | |
| 3578 | case QCBOR_TYPE_FALSE: |
| 3579 | *pBool = false; |
| 3580 | return QCBOR_SUCCESS; |
| 3581 | break; |
| 3582 | |
| 3583 | default: |
| 3584 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3585 | break; |
| 3586 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3587 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3588 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3589 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3590 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3591 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3592 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3593 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3594 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3595 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3596 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3597 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3598 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3599 | return; |
| 3600 | } |
| 3601 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3602 | QCBORError nError; |
| 3603 | QCBORItem Item; |
| 3604 | |
| 3605 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3606 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3607 | pMe->uLastError = (uint8_t)nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3608 | return; |
| 3609 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3610 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3611 | } |
| 3612 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3613 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3614 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3615 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3616 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3617 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3618 | { |
| 3619 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3620 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3621 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3622 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3625 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3626 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3627 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3628 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3629 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3630 | { |
| 3631 | QCBORItem Item; |
| 3632 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3633 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3634 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | |
| 3638 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3639 | |
| 3640 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3641 | QCBORItem *pItem, |
| 3642 | uint8_t uTagRequirement, |
| 3643 | int64_t *pnTime) |
| 3644 | { |
| 3645 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3646 | // Already in error state, do nothing |
| 3647 | return; |
| 3648 | } |
| 3649 | |
| 3650 | QCBORError uErr; |
| 3651 | |
| 3652 | const TagSpecification TagSpec = |
| 3653 | { |
| 3654 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3655 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3656 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3657 | }; |
| 3658 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3659 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3660 | if(uErr != QCBOR_SUCCESS) { |
| 3661 | goto Done; |
| 3662 | } |
| 3663 | |
| 3664 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3665 | uErr = DecodeDateEpoch(pItem); |
| 3666 | if(uErr != QCBOR_SUCCESS) { |
| 3667 | goto Done; |
| 3668 | } |
| 3669 | } |
| 3670 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3671 | // Save the tags in the last item's tags in the decode context |
| 3672 | // for QCBORDecode_GetNthTagOfLast() |
| 3673 | CopyTags(pMe, pItem); |
| 3674 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3675 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3676 | |
| 3677 | Done: |
| 3678 | pMe->uLastError = (uint8_t)uErr; |
| 3679 | } |
| 3680 | |
| 3681 | |
| 3682 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3683 | uint8_t uTagRequirement, |
| 3684 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3685 | { |
| 3686 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3687 | // Already in error state, do nothing |
| 3688 | return; |
| 3689 | } |
| 3690 | |
| 3691 | QCBORItem Item; |
| 3692 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3693 | |
| 3694 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3695 | } |
| 3696 | |
| 3697 | |
| 3698 | void |
| 3699 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3700 | int64_t nLabel, |
| 3701 | uint8_t uTagRequirement, |
| 3702 | int64_t *pnTime) |
| 3703 | { |
| 3704 | QCBORItem Item; |
| 3705 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3706 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3707 | } |
| 3708 | |
| 3709 | |
| 3710 | void |
| 3711 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3712 | const char *szLabel, |
| 3713 | uint8_t uTagRequirement, |
| 3714 | int64_t *pnTime) |
| 3715 | { |
| 3716 | QCBORItem Item; |
| 3717 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3718 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3719 | } |
| 3720 | |
| 3721 | |
| 3722 | |
| 3723 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3724 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3725 | TagSpecification TagSpec, |
| 3726 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3727 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3728 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3729 | // Already in error state, do nothing |
| 3730 | return; |
| 3731 | } |
| 3732 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3733 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3734 | QCBORItem Item; |
| 3735 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3736 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3737 | if(uError != QCBOR_SUCCESS) { |
| 3738 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3739 | return; |
| 3740 | } |
| 3741 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3742 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3743 | |
| 3744 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3745 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 3746 | } else { |
| 3747 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3748 | } |
| 3749 | } |
| 3750 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3751 | |
| 3752 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3753 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3754 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3755 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3756 | UsefulBufC *pValue, |
| 3757 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3758 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3759 | const TagSpecification TagSpec = |
| 3760 | { |
| 3761 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3762 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3763 | {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] | 3764 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3765 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3766 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3767 | if(uErr != QCBOR_SUCCESS) { |
| 3768 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3769 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3770 | |
| 3771 | *pValue = pItem->val.string; |
| 3772 | |
| 3773 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3774 | *pbIsNegative = false; |
| 3775 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3776 | *pbIsNegative = true; |
| 3777 | } |
| 3778 | |
| 3779 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3783 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3784 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3785 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3786 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3787 | uint8_t uTagRequirement, |
| 3788 | UsefulBufC *pValue, |
| 3789 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3790 | { |
| 3791 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3792 | // Already in error state, do nothing |
| 3793 | return; |
| 3794 | } |
| 3795 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3796 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3797 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3798 | if(uError != QCBOR_SUCCESS) { |
| 3799 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3800 | return; |
| 3801 | } |
| 3802 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3803 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3806 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3807 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3808 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3809 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3810 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3811 | int64_t nLabel, |
| 3812 | uint8_t uTagRequirement, |
| 3813 | UsefulBufC *pValue, |
| 3814 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3815 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3816 | QCBORItem Item; |
| 3817 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3818 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3819 | return; |
| 3820 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3821 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3822 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3823 | } |
| 3824 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3825 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3826 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3827 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3828 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3829 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3830 | const char *szLabel, |
| 3831 | uint8_t uTagRequirement, |
| 3832 | UsefulBufC *pValue, |
| 3833 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3834 | { |
| 3835 | QCBORItem Item; |
| 3836 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3837 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3838 | return; |
| 3839 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3840 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3841 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | |
| 3845 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3846 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3847 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3848 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3849 | const QCBORItem *pItem, |
| 3850 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3851 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3852 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3853 | const TagSpecification TagSpecText = |
| 3854 | { |
| 3855 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3856 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3857 | {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] | 3858 | }; |
| 3859 | const TagSpecification TagSpecBinary = |
| 3860 | { |
| 3861 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3862 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3863 | {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] | 3864 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3865 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3866 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3867 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3868 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3869 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3870 | if(pbIsTag257 != NULL) { |
| 3871 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3872 | } |
| 3873 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3874 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3875 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3876 | if(pbIsTag257 != NULL) { |
| 3877 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3878 | } |
| 3879 | uReturn = QCBOR_SUCCESS; |
| 3880 | |
| 3881 | } else { |
| 3882 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3883 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3884 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3885 | return uReturn; |
| 3886 | } |
| 3887 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3888 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 3889 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3890 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3891 | |
| 3892 | |
| 3893 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3894 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3895 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3896 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3897 | |
| 3898 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3899 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3900 | static QCBORError |
| 3901 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3902 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3903 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3904 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3905 | if(uResult != 0) { |
| 3906 | /* This loop will run a maximum of 19 times because |
| 3907 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 3908 | * exit with the overflow error |
| 3909 | */ |
| 3910 | for(; nExponent > 0; nExponent--) { |
| 3911 | if(uResult > UINT64_MAX / 10) { |
| 3912 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 3913 | } |
| 3914 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3915 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3916 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3917 | for(; nExponent < 0; nExponent++) { |
| 3918 | uResult = uResult / 10; |
| 3919 | if(uResult == 0) { |
| 3920 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3921 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3922 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3923 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3924 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3925 | |
| 3926 | *puResult = uResult; |
| 3927 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3928 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3932 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3933 | static QCBORError |
| 3934 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3935 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3936 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3937 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3938 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3939 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3940 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3941 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3942 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3943 | */ |
| 3944 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3945 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3946 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3947 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3948 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3949 | nExponent--; |
| 3950 | } |
| 3951 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3952 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3953 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3954 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3955 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3956 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3957 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3958 | } |
| 3959 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3960 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3961 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3962 | return QCBOR_SUCCESS; |
| 3963 | } |
| 3964 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3965 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3966 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3967 | Compute value with signed mantissa and signed result. Works with |
| 3968 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3969 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3970 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 3971 | int64_t nExponent, |
| 3972 | int64_t *pnResult, |
| 3973 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3974 | { |
| 3975 | uint64_t uResult; |
| 3976 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3977 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3978 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3979 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 3980 | |
| 3981 | // Do the exponentiation of the positive mantissa |
| 3982 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 3983 | if(uReturn) { |
| 3984 | return uReturn; |
| 3985 | } |
| 3986 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3987 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 3988 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 3989 | of INT64_MIN. This assumes two's compliment representation where |
| 3990 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 3991 | Trying to write -INT64_MIN doesn't work to get this because the |
| 3992 | compiler tries to work with an int64_t which can't represent |
| 3993 | -INT64_MIN. |
| 3994 | */ |
| 3995 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 3996 | |
| 3997 | // Error out if too large |
| 3998 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3999 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4000 | } |
| 4001 | |
| 4002 | // Casts are safe because of checks above |
| 4003 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 4004 | |
| 4005 | return QCBOR_SUCCESS; |
| 4006 | } |
| 4007 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4008 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4009 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4010 | Compute value with signed mantissa and unsigned result. Works with |
| 4011 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4012 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4013 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 4014 | int64_t nExponent, |
| 4015 | uint64_t *puResult, |
| 4016 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4017 | { |
| 4018 | if(nMantissa < 0) { |
| 4019 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4020 | } |
| 4021 | |
| 4022 | // Cast to unsigned is OK because of check for negative |
| 4023 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 4024 | // Exponentiation is straight forward |
| 4025 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4026 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4027 | |
| 4028 | |
| 4029 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4030 | Compute value with signed mantissa and unsigned result. Works with |
| 4031 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4032 | */ |
| 4033 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 4034 | int64_t nExponent, |
| 4035 | uint64_t *puResult, |
| 4036 | fExponentiator pfExp) |
| 4037 | { |
| 4038 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4039 | } |
| 4040 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4041 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4042 | |
| 4043 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4044 | |
| 4045 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4046 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4047 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4048 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4049 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4050 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4051 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4052 | const uint8_t *pByte = BigNum.ptr; |
| 4053 | size_t uLen = BigNum.len; |
| 4054 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4055 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4056 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4057 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4058 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4059 | } |
| 4060 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4061 | *pResult = uResult; |
| 4062 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4065 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4066 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4067 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4068 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4069 | } |
| 4070 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4071 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4072 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4073 | { |
| 4074 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4075 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 4076 | if(uError) { |
| 4077 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4078 | } |
| 4079 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 4080 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4081 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4085 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4086 | { |
| 4087 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4088 | /* The negative integer furthest from zero for a C int64_t is |
| 4089 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4090 | negative number in CBOR is computed as -n - 1 where n is the |
| 4091 | encoded integer, where n is what is in the variable BigNum. When |
| 4092 | converting BigNum to a uint64_t, the maximum value is thus |
| 4093 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 4094 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4095 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4096 | -n - 1 <= INT64_MIN. |
| 4097 | -n - 1 <= -INT64_MAX - 1 |
| 4098 | n <= INT64_MAX. |
| 4099 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4100 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4101 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4102 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4103 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4104 | |
| 4105 | /// Now apply -n - 1. The cast is safe because |
| 4106 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4107 | // is the largest positive integer that an int64_t can |
| 4108 | // represent. */ |
| 4109 | *pnResult = -(int64_t)uResult - 1; |
| 4110 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4111 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4112 | } |
| 4113 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4114 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4115 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4116 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4117 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4118 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4119 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4120 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4121 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4122 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4123 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4124 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4125 | |
| 4126 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4127 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4128 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4129 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4130 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4131 | static QCBORError |
| 4132 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4133 | { |
| 4134 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4135 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4136 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4137 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4138 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4139 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 4140 | http://www.cplusplus.com/reference/cmath/llround/ |
| 4141 | */ |
| 4142 | // Not interested in FE_INEXACT |
| 4143 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4144 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4145 | *pnValue = llround(pItem->val.dfnum); |
| 4146 | } else { |
| 4147 | *pnValue = lroundf(pItem->val.fnum); |
| 4148 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4149 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4150 | // llround() shouldn't result in divide by zero, but catch |
| 4151 | // it here in case it unexpectedly does. Don't try to |
| 4152 | // distinguish between the various exceptions because it seems |
| 4153 | // they vary by CPU, compiler and OS. |
| 4154 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4155 | } |
| 4156 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4157 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4158 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4159 | #else |
| 4160 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4161 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4162 | break; |
| 4163 | |
| 4164 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4165 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4166 | *pnValue = pItem->val.int64; |
| 4167 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4168 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4169 | } |
| 4170 | break; |
| 4171 | |
| 4172 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4173 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4174 | if(pItem->val.uint64 < INT64_MAX) { |
| 4175 | *pnValue = pItem->val.int64; |
| 4176 | } else { |
| 4177 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4178 | } |
| 4179 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4180 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4181 | } |
| 4182 | break; |
| 4183 | |
| 4184 | default: |
| 4185 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4186 | } |
| 4187 | return QCBOR_SUCCESS; |
| 4188 | } |
| 4189 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4190 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4191 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4192 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4193 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4194 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4195 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4196 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4197 | return; |
| 4198 | } |
| 4199 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4200 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4201 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4202 | if(uError) { |
| 4203 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4204 | return; |
| 4205 | } |
| 4206 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4207 | if(pItem) { |
| 4208 | *pItem = Item; |
| 4209 | } |
| 4210 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4211 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4212 | } |
| 4213 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4214 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4215 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4216 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4217 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4218 | int64_t *pnValue, |
| 4219 | QCBORItem *pItem) |
| 4220 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4221 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4222 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4223 | return; |
| 4224 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4225 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4226 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4227 | } |
| 4228 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4229 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4230 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4231 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4232 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4233 | int64_t *pnValue, |
| 4234 | QCBORItem *pItem) |
| 4235 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4236 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4237 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4238 | return; |
| 4239 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4240 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4241 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4245 | /* |
| 4246 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4247 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4248 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4249 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4250 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4251 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4252 | |
| 4253 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4254 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4255 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4256 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4257 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4258 | static QCBORError |
| 4259 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4260 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4261 | switch(pItem->uDataType) { |
| 4262 | |
| 4263 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4264 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4265 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4266 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4267 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4268 | } |
| 4269 | break; |
| 4270 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4271 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4272 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4273 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4274 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4275 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4276 | } |
| 4277 | break; |
| 4278 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4279 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4280 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4281 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4282 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4283 | pItem->val.expAndMantissa.nExponent, |
| 4284 | pnValue, |
| 4285 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4286 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4287 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4288 | } |
| 4289 | break; |
| 4290 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4291 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4292 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4293 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4294 | pItem->val.expAndMantissa.nExponent, |
| 4295 | pnValue, |
| 4296 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4297 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4298 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4299 | } |
| 4300 | break; |
| 4301 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4302 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4303 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4304 | int64_t nMantissa; |
| 4305 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4306 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4307 | if(uErr) { |
| 4308 | return uErr; |
| 4309 | } |
| 4310 | return ExponentiateNN(nMantissa, |
| 4311 | pItem->val.expAndMantissa.nExponent, |
| 4312 | pnValue, |
| 4313 | Exponentitate10); |
| 4314 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4315 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4316 | } |
| 4317 | break; |
| 4318 | |
| 4319 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4320 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4321 | int64_t nMantissa; |
| 4322 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4323 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4324 | if(uErr) { |
| 4325 | return uErr; |
| 4326 | } |
| 4327 | return ExponentiateNN(nMantissa, |
| 4328 | pItem->val.expAndMantissa.nExponent, |
| 4329 | pnValue, |
| 4330 | Exponentitate10); |
| 4331 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4332 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4333 | } |
| 4334 | break; |
| 4335 | |
| 4336 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4337 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4338 | int64_t nMantissa; |
| 4339 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4340 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4341 | if(uErr) { |
| 4342 | return uErr; |
| 4343 | } |
| 4344 | return ExponentiateNN(nMantissa, |
| 4345 | pItem->val.expAndMantissa.nExponent, |
| 4346 | pnValue, |
| 4347 | Exponentitate2); |
| 4348 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4349 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4350 | } |
| 4351 | break; |
| 4352 | |
| 4353 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4354 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4355 | int64_t nMantissa; |
| 4356 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4357 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4358 | if(uErr) { |
| 4359 | return uErr; |
| 4360 | } |
| 4361 | return ExponentiateNN(nMantissa, |
| 4362 | pItem->val.expAndMantissa.nExponent, |
| 4363 | pnValue, |
| 4364 | Exponentitate2); |
| 4365 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4366 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4367 | } |
| 4368 | break; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4369 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4370 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4371 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4372 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4373 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4374 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4375 | |
| 4376 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4377 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4378 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4379 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4380 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4381 | { |
| 4382 | QCBORItem Item; |
| 4383 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4384 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4385 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4386 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4387 | // The above conversion succeeded |
| 4388 | return; |
| 4389 | } |
| 4390 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4391 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4392 | // 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] | 4393 | return; |
| 4394 | } |
| 4395 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4396 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4397 | } |
| 4398 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4399 | |
| 4400 | /* |
| 4401 | Public function, see header qcbor/qcbor_decode.h file |
| 4402 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4403 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4404 | int64_t nLabel, |
| 4405 | uint32_t uConvertTypes, |
| 4406 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4407 | { |
| 4408 | QCBORItem Item; |
| 4409 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4410 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4411 | nLabel, |
| 4412 | uConvertTypes, |
| 4413 | pnValue, |
| 4414 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4415 | |
| 4416 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4417 | // The above conversion succeeded |
| 4418 | return; |
| 4419 | } |
| 4420 | |
| 4421 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4422 | // The above conversion failed in a way that code below can't correct |
| 4423 | return; |
| 4424 | } |
| 4425 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4426 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4430 | /* |
| 4431 | Public function, see header qcbor/qcbor_decode.h file |
| 4432 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4433 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4434 | const char *szLabel, |
| 4435 | uint32_t uConvertTypes, |
| 4436 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4437 | { |
| 4438 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4439 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4440 | szLabel, |
| 4441 | uConvertTypes, |
| 4442 | pnValue, |
| 4443 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4444 | |
| 4445 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4446 | // The above conversion succeeded |
| 4447 | return; |
| 4448 | } |
| 4449 | |
| 4450 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4451 | // The above conversion failed in a way that code below can't correct |
| 4452 | return; |
| 4453 | } |
| 4454 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4455 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4459 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4460 | { |
| 4461 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4462 | case QCBOR_TYPE_DOUBLE: |
| 4463 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4464 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4465 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4466 | // Can't use llround here because it will not convert values |
| 4467 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4468 | // need to be converted so it is more complicated. |
| 4469 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4470 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4471 | if(isnan(pItem->val.dfnum)) { |
| 4472 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4473 | } else if(pItem->val.dfnum < 0) { |
| 4474 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4475 | } else { |
| 4476 | double dRounded = round(pItem->val.dfnum); |
| 4477 | // See discussion in DecodeDateEpoch() for |
| 4478 | // explanation of - 0x7ff |
| 4479 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4480 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4481 | } |
| 4482 | *puValue = (uint64_t)dRounded; |
| 4483 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4484 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4485 | if(isnan(pItem->val.fnum)) { |
| 4486 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4487 | } else if(pItem->val.fnum < 0) { |
| 4488 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4489 | } else { |
| 4490 | float fRounded = roundf(pItem->val.fnum); |
| 4491 | // See discussion in DecodeDateEpoch() for |
| 4492 | // explanation of - 0x7ff |
| 4493 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4494 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4495 | } |
| 4496 | *puValue = (uint64_t)fRounded; |
| 4497 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4498 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4499 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4500 | // round() and roundf() shouldn't result in exceptions here, but |
| 4501 | // catch them to be robust and thorough. Don't try to |
| 4502 | // distinguish between the various exceptions because it seems |
| 4503 | // they vary by CPU, compiler and OS. |
| 4504 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4505 | } |
| 4506 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4507 | } else { |
| 4508 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4509 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4510 | #else |
| 4511 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4512 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4513 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4514 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4515 | case QCBOR_TYPE_INT64: |
| 4516 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4517 | if(pItem->val.int64 >= 0) { |
| 4518 | *puValue = (uint64_t)pItem->val.int64; |
| 4519 | } else { |
| 4520 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4521 | } |
| 4522 | } else { |
| 4523 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4524 | } |
| 4525 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4526 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4527 | case QCBOR_TYPE_UINT64: |
| 4528 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4529 | *puValue = pItem->val.uint64; |
| 4530 | } else { |
| 4531 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4532 | } |
| 4533 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4534 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4535 | default: |
| 4536 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4537 | } |
| 4538 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4539 | return QCBOR_SUCCESS; |
| 4540 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4541 | |
| 4542 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4543 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4544 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4545 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4546 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4547 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4548 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4549 | return; |
| 4550 | } |
| 4551 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4552 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4553 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4554 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4555 | if(uError) { |
| 4556 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4557 | return; |
| 4558 | } |
| 4559 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4560 | if(pItem) { |
| 4561 | *pItem = Item; |
| 4562 | } |
| 4563 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4564 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4565 | } |
| 4566 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4567 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4568 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4569 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4570 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4571 | uint64_t *puValue, |
| 4572 | QCBORItem *pItem) |
| 4573 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4574 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4575 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4576 | return; |
| 4577 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4578 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4579 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4583 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4584 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4585 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4586 | uint64_t *puValue, |
| 4587 | QCBORItem *pItem) |
| 4588 | { |
| 4589 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4590 | return; |
| 4591 | } |
| 4592 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4593 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4594 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4595 | return; |
| 4596 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4597 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4598 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4599 | } |
| 4600 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4601 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4602 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4603 | static QCBORError |
| 4604 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4605 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4606 | switch(pItem->uDataType) { |
| 4607 | |
| 4608 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4609 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4610 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4611 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4612 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4613 | } |
| 4614 | break; |
| 4615 | |
| 4616 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4617 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4618 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4619 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4620 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4621 | } |
| 4622 | break; |
| 4623 | |
| 4624 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4625 | |
| 4626 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4627 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4628 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4629 | pItem->val.expAndMantissa.nExponent, |
| 4630 | puValue, |
| 4631 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4632 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4633 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4634 | } |
| 4635 | break; |
| 4636 | |
| 4637 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4638 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4639 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4640 | pItem->val.expAndMantissa.nExponent, |
| 4641 | puValue, |
| 4642 | Exponentitate2); |
| 4643 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4644 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4645 | } |
| 4646 | break; |
| 4647 | |
| 4648 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4649 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4650 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4651 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4652 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4653 | if(uErr != QCBOR_SUCCESS) { |
| 4654 | return uErr; |
| 4655 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4656 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4657 | pItem->val.expAndMantissa.nExponent, |
| 4658 | puValue, |
| 4659 | Exponentitate10); |
| 4660 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4661 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4662 | } |
| 4663 | break; |
| 4664 | |
| 4665 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4666 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4667 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4668 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4669 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4670 | } |
| 4671 | break; |
| 4672 | |
| 4673 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4674 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4675 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4676 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4677 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4678 | if(uErr != QCBOR_SUCCESS) { |
| 4679 | return uErr; |
| 4680 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4681 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4682 | pItem->val.expAndMantissa.nExponent, |
| 4683 | puValue, |
| 4684 | Exponentitate2); |
| 4685 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4686 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4687 | } |
| 4688 | break; |
| 4689 | |
| 4690 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4691 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4692 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4693 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4694 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4695 | } |
| 4696 | break; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4697 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4698 | default: |
| 4699 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4700 | } |
| 4701 | } |
| 4702 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4703 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4704 | /* |
| 4705 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4706 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4707 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4708 | { |
| 4709 | QCBORItem Item; |
| 4710 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4711 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4712 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4713 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4714 | // The above conversion succeeded |
| 4715 | return; |
| 4716 | } |
| 4717 | |
| 4718 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4719 | // 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] | 4720 | return; |
| 4721 | } |
| 4722 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4723 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4724 | } |
| 4725 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4726 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4727 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4728 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4729 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4730 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4731 | int64_t nLabel, |
| 4732 | uint32_t uConvertTypes, |
| 4733 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4734 | { |
| 4735 | QCBORItem Item; |
| 4736 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4737 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 4738 | nLabel, |
| 4739 | uConvertTypes, |
| 4740 | puValue, |
| 4741 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4742 | |
| 4743 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4744 | // The above conversion succeeded |
| 4745 | return; |
| 4746 | } |
| 4747 | |
| 4748 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4749 | // The above conversion failed in a way that code below can't correct |
| 4750 | return; |
| 4751 | } |
| 4752 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4753 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | |
| 4757 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4758 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4759 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4760 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4761 | const char *szLabel, |
| 4762 | uint32_t uConvertTypes, |
| 4763 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4764 | { |
| 4765 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4766 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 4767 | szLabel, |
| 4768 | uConvertTypes, |
| 4769 | puValue, |
| 4770 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4771 | |
| 4772 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4773 | // The above conversion succeeded |
| 4774 | return; |
| 4775 | } |
| 4776 | |
| 4777 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4778 | // The above conversion failed in a way that code below can't correct |
| 4779 | return; |
| 4780 | } |
| 4781 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4782 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4786 | |
| 4787 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4788 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4789 | uint32_t uConvertTypes, |
| 4790 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4791 | { |
| 4792 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4793 | case QCBOR_TYPE_FLOAT: |
| 4794 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4795 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4796 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4797 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4798 | *pdValue = (double)pItem->val.fnum; |
| 4799 | } else { |
| 4800 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4801 | } |
| 4802 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4803 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4804 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4805 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4806 | break; |
| 4807 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4808 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4809 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4810 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4811 | *pdValue = pItem->val.dfnum; |
| 4812 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4813 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4814 | } |
| 4815 | } |
| 4816 | break; |
| 4817 | |
| 4818 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4819 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4820 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4821 | // A simple cast seems to do the job with no worry of exceptions. |
| 4822 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4823 | *pdValue = (double)pItem->val.int64; |
| 4824 | |
| 4825 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4826 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4827 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4828 | #else |
| 4829 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4830 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4831 | break; |
| 4832 | |
| 4833 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4834 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4835 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4836 | // A simple cast seems to do the job with no worry of exceptions. |
| 4837 | // There will be precision loss for some values. |
| 4838 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4839 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4840 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4841 | } |
| 4842 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4843 | #else |
| 4844 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4845 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4846 | |
| 4847 | default: |
| 4848 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4849 | } |
| 4850 | |
| 4851 | return QCBOR_SUCCESS; |
| 4852 | } |
| 4853 | |
| 4854 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4855 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4856 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4857 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4858 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4859 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4860 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4861 | return; |
| 4862 | } |
| 4863 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4864 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4865 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4866 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4867 | if(uError) { |
| 4868 | pMe->uLastError = (uint8_t)uError; |
| 4869 | return; |
| 4870 | } |
| 4871 | |
| 4872 | if(pItem) { |
| 4873 | *pItem = Item; |
| 4874 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4875 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4876 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4877 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4878 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4879 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4880 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4881 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4882 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4883 | double *pdValue, |
| 4884 | QCBORItem *pItem) |
| 4885 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4886 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4887 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4888 | return; |
| 4889 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4890 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4891 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4892 | } |
| 4893 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4894 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4895 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4896 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4897 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4898 | double *pdValue, |
| 4899 | QCBORItem *pItem) |
| 4900 | { |
| 4901 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4902 | return; |
| 4903 | } |
| 4904 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4905 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4906 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4907 | return; |
| 4908 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4909 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4910 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4914 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4915 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 4916 | { |
| 4917 | double dResult; |
| 4918 | |
| 4919 | dResult = 0.0; |
| 4920 | const uint8_t *pByte = BigNum.ptr; |
| 4921 | size_t uLen = BigNum.len; |
| 4922 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 4923 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4924 | while(uLen--) { |
| 4925 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 4926 | } |
| 4927 | |
| 4928 | return dResult; |
| 4929 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4930 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4931 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4932 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4933 | static QCBORError |
| 4934 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4935 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4936 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4937 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4938 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 4939 | |
| 4940 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4941 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4942 | |
| 4943 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4944 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4945 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4946 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4947 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4948 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 4949 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4950 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4951 | } |
| 4952 | break; |
| 4953 | |
| 4954 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4955 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4956 | // Underflow gives 0, overflow gives infinity |
| 4957 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4958 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4959 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4960 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4961 | } |
| 4962 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4963 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4964 | |
| 4965 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4966 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4967 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 4968 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4969 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4970 | } |
| 4971 | break; |
| 4972 | |
| 4973 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4974 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4975 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4976 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4977 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4978 | } |
| 4979 | break; |
| 4980 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4981 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4982 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4983 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4984 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4985 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4986 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4987 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4988 | } |
| 4989 | break; |
| 4990 | |
| 4991 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4992 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4993 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4994 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4995 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4996 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4997 | } |
| 4998 | break; |
| 4999 | |
| 5000 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5001 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5002 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5003 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5004 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5005 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5006 | } |
| 5007 | break; |
| 5008 | |
| 5009 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5010 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5011 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5012 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5013 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5014 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5015 | } |
| 5016 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5017 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5018 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5019 | default: |
| 5020 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5021 | } |
| 5022 | |
| 5023 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5024 | |
| 5025 | #else |
| 5026 | (void)pItem; |
| 5027 | (void)uConvertTypes; |
| 5028 | (void)pdValue; |
| 5029 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5030 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5031 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | |
| 5035 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5036 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5037 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5038 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 5039 | uint32_t uConvertTypes, |
| 5040 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5041 | { |
| 5042 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5043 | QCBORItem Item; |
| 5044 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5045 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5046 | |
| 5047 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5048 | // The above conversion succeeded |
| 5049 | return; |
| 5050 | } |
| 5051 | |
| 5052 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5053 | // The above conversion failed in a way that code below can't correct |
| 5054 | return; |
| 5055 | } |
| 5056 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5057 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5058 | } |
| 5059 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5060 | |
| 5061 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5062 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5063 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5064 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5065 | int64_t nLabel, |
| 5066 | uint32_t uConvertTypes, |
| 5067 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5068 | { |
| 5069 | QCBORItem Item; |
| 5070 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5071 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5072 | |
| 5073 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5074 | // The above conversion succeeded |
| 5075 | return; |
| 5076 | } |
| 5077 | |
| 5078 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5079 | // The above conversion failed in a way that code below can't correct |
| 5080 | return; |
| 5081 | } |
| 5082 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5083 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | |
| 5087 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5088 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5089 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5090 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5091 | const char *szLabel, |
| 5092 | uint32_t uConvertTypes, |
| 5093 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5094 | { |
| 5095 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5096 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5097 | |
| 5098 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5099 | // The above conversion succeeded |
| 5100 | return; |
| 5101 | } |
| 5102 | |
| 5103 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5104 | // The above conversion failed in a way that code below can't correct |
| 5105 | return; |
| 5106 | } |
| 5107 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5108 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5109 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5110 | |
| 5111 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5112 | |
| 5113 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5114 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5115 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 5116 | { |
| 5117 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5118 | uInt = uInt << 8; |
| 5119 | }; |
| 5120 | |
| 5121 | UsefulOutBuf UOB; |
| 5122 | |
| 5123 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5124 | |
| 5125 | while(uInt) { |
| 5126 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5127 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5128 | uInt = uInt << 8; |
| 5129 | (void)xx; |
| 5130 | } |
| 5131 | |
| 5132 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5133 | } |
| 5134 | |
| 5135 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5136 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 5137 | TagSpecification TagSpec, |
| 5138 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5139 | { |
| 5140 | QCBORError uErr; |
| 5141 | // 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] | 5142 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5143 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5144 | if(uErr != QCBOR_SUCCESS) { |
| 5145 | goto Done; |
| 5146 | } |
| 5147 | |
| 5148 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 5149 | break; // Successful exit. Moving on to finish decoding. |
| 5150 | } |
| 5151 | |
| 5152 | // The item is an array, which means an undecoded |
| 5153 | // mantissa and exponent, so decode it. It will then |
| 5154 | // have a different type and exit the loop if. |
| 5155 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5156 | if(uErr != QCBOR_SUCCESS) { |
| 5157 | goto Done; |
| 5158 | } |
| 5159 | |
| 5160 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5161 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5162 | } |
| 5163 | Done: |
| 5164 | return uErr; |
| 5165 | } |
| 5166 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5167 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5168 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5169 | TagSpecification TagSpec, |
| 5170 | QCBORItem *pItem, |
| 5171 | int64_t *pnMantissa, |
| 5172 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5173 | { |
| 5174 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5175 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5176 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5177 | if(uErr != QCBOR_SUCCESS) { |
| 5178 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5179 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5180 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5181 | switch (pItem->uDataType) { |
| 5182 | |
| 5183 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5184 | case QCBOR_TYPE_BIGFLOAT: |
| 5185 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 5186 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5187 | break; |
| 5188 | |
| 5189 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5190 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5191 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5192 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5193 | break; |
| 5194 | |
| 5195 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5196 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5197 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5198 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5199 | break; |
| 5200 | |
| 5201 | default: |
| 5202 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5203 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5204 | |
| 5205 | Done: |
| 5206 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5207 | } |
| 5208 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5209 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5210 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5211 | TagSpecification TagSpec, |
| 5212 | QCBORItem *pItem, |
| 5213 | UsefulBuf BufferForMantissa, |
| 5214 | UsefulBufC *pMantissa, |
| 5215 | bool *pbIsNegative, |
| 5216 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5217 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5218 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5219 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5220 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5221 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5222 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5223 | } |
| 5224 | |
| 5225 | uint64_t uMantissa; |
| 5226 | |
| 5227 | switch (pItem->uDataType) { |
| 5228 | |
| 5229 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5230 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5231 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5232 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5233 | *pbIsNegative = false; |
| 5234 | } else { |
| 5235 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5236 | *pbIsNegative = true; |
| 5237 | } |
| 5238 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5239 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5240 | break; |
| 5241 | |
| 5242 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5243 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5244 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5245 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5246 | *pbIsNegative = false; |
| 5247 | break; |
| 5248 | |
| 5249 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5250 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5251 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5252 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5253 | *pbIsNegative = true; |
| 5254 | break; |
| 5255 | |
| 5256 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5257 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5258 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5259 | |
| 5260 | Done: |
| 5261 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5265 | /* |
| 5266 | Public function, see header qcbor/qcbor_decode.h file |
| 5267 | */ |
| 5268 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5269 | uint8_t uTagRequirement, |
| 5270 | int64_t *pnMantissa, |
| 5271 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5272 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5273 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5274 | return; |
| 5275 | } |
| 5276 | |
| 5277 | QCBORItem Item; |
| 5278 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5279 | if(uError) { |
| 5280 | pMe->uLastError = (uint8_t)uError; |
| 5281 | return; |
| 5282 | } |
| 5283 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5284 | const TagSpecification TagSpec = |
| 5285 | { |
| 5286 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5287 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5288 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5289 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5290 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5291 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5292 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5295 | |
| 5296 | /* |
| 5297 | Public function, see header qcbor/qcbor_decode.h file |
| 5298 | */ |
| 5299 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5300 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5301 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5302 | int64_t *pnMantissa, |
| 5303 | int64_t *pnExponent) |
| 5304 | { |
| 5305 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5306 | return; |
| 5307 | } |
| 5308 | |
| 5309 | QCBORItem Item; |
| 5310 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5311 | |
| 5312 | const TagSpecification TagSpec = |
| 5313 | { |
| 5314 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5315 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5316 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5317 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5318 | }; |
| 5319 | |
| 5320 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5321 | } |
| 5322 | |
| 5323 | |
| 5324 | /* |
| 5325 | Public function, see header qcbor/qcbor_decode.h file |
| 5326 | */ |
| 5327 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5328 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5329 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5330 | int64_t *pnMantissa, |
| 5331 | int64_t *pnExponent) |
| 5332 | { |
| 5333 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5334 | return; |
| 5335 | } |
| 5336 | |
| 5337 | QCBORItem Item; |
| 5338 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5339 | |
| 5340 | const TagSpecification TagSpec = |
| 5341 | { |
| 5342 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5343 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5344 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5345 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5346 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5347 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5348 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5349 | } |
| 5350 | |
| 5351 | |
| 5352 | /* |
| 5353 | Public function, see header qcbor/qcbor_decode.h file |
| 5354 | */ |
| 5355 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5356 | uint8_t uTagRequirement, |
| 5357 | UsefulBuf MantissaBuffer, |
| 5358 | UsefulBufC *pMantissa, |
| 5359 | bool *pbMantissaIsNegative, |
| 5360 | int64_t *pnExponent) |
| 5361 | { |
| 5362 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5363 | return; |
| 5364 | } |
| 5365 | |
| 5366 | QCBORItem Item; |
| 5367 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5368 | if(uError) { |
| 5369 | pMe->uLastError = (uint8_t)uError; |
| 5370 | return; |
| 5371 | } |
| 5372 | |
| 5373 | const TagSpecification TagSpec = |
| 5374 | { |
| 5375 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5376 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5377 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5378 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5379 | }; |
| 5380 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5381 | ProcessMantissaAndExponentBig(pMe, |
| 5382 | TagSpec, |
| 5383 | &Item, |
| 5384 | MantissaBuffer, |
| 5385 | pMantissa, |
| 5386 | pbMantissaIsNegative, |
| 5387 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | |
| 5391 | /* |
| 5392 | Public function, see header qcbor/qcbor_decode.h file |
| 5393 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5394 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5395 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5396 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5397 | UsefulBuf BufferForMantissa, |
| 5398 | UsefulBufC *pMantissa, |
| 5399 | bool *pbIsNegative, |
| 5400 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5401 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5402 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5403 | return; |
| 5404 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5405 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5406 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5407 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5408 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5409 | return; |
| 5410 | } |
| 5411 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5412 | const TagSpecification TagSpec = |
| 5413 | { |
| 5414 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5415 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5416 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5417 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5418 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5419 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5420 | ProcessMantissaAndExponentBig(pMe, |
| 5421 | TagSpec, |
| 5422 | &Item, |
| 5423 | BufferForMantissa, |
| 5424 | pMantissa, |
| 5425 | pbIsNegative, |
| 5426 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5427 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5428 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5429 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5430 | /* |
| 5431 | Public function, see header qcbor/qcbor_decode.h file |
| 5432 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5433 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5434 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5435 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5436 | UsefulBuf BufferForMantissa, |
| 5437 | UsefulBufC *pMantissa, |
| 5438 | bool *pbIsNegative, |
| 5439 | int64_t *pnExponent) |
| 5440 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5441 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5442 | return; |
| 5443 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5444 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5445 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5446 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5447 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5448 | return; |
| 5449 | } |
| 5450 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5451 | const TagSpecification TagSpec = |
| 5452 | { |
| 5453 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5454 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5455 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5456 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5457 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5458 | |
| 5459 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5460 | } |
| 5461 | |
| 5462 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5463 | /* |
| 5464 | Public function, see header qcbor/qcbor_decode.h file |
| 5465 | */ |
| 5466 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5467 | uint8_t uTagRequirement, |
| 5468 | int64_t *pnMantissa, |
| 5469 | int64_t *pnExponent) |
| 5470 | { |
| 5471 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5472 | return; |
| 5473 | } |
| 5474 | |
| 5475 | QCBORItem Item; |
| 5476 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5477 | if(uError) { |
| 5478 | pMe->uLastError = (uint8_t)uError; |
| 5479 | return; |
| 5480 | } |
| 5481 | const TagSpecification TagSpec = |
| 5482 | { |
| 5483 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5484 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5485 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5486 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5487 | }; |
| 5488 | |
| 5489 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5490 | } |
| 5491 | |
| 5492 | |
| 5493 | /* |
| 5494 | Public function, see header qcbor/qcbor_decode.h file |
| 5495 | */ |
| 5496 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5497 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5498 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5499 | int64_t *pnMantissa, |
| 5500 | int64_t *pnExponent) |
| 5501 | { |
| 5502 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5503 | return; |
| 5504 | } |
| 5505 | |
| 5506 | QCBORItem Item; |
| 5507 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5508 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5509 | return; |
| 5510 | } |
| 5511 | |
| 5512 | const TagSpecification TagSpec = |
| 5513 | { |
| 5514 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5515 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5516 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5517 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5518 | }; |
| 5519 | |
| 5520 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5521 | } |
| 5522 | |
| 5523 | |
| 5524 | /* |
| 5525 | Public function, see header qcbor/qcbor_decode.h file |
| 5526 | */ |
| 5527 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5528 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5529 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5530 | int64_t *pnMantissa, |
| 5531 | int64_t *pnExponent) |
| 5532 | { |
| 5533 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5534 | return; |
| 5535 | } |
| 5536 | |
| 5537 | QCBORItem Item; |
| 5538 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5539 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5540 | return; |
| 5541 | } |
| 5542 | |
| 5543 | const TagSpecification TagSpec = |
| 5544 | { |
| 5545 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5546 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5547 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5548 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5549 | }; |
| 5550 | |
| 5551 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5552 | } |
| 5553 | |
| 5554 | |
| 5555 | /* |
| 5556 | Public function, see header qcbor/qcbor_decode.h file |
| 5557 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5558 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5559 | uint8_t uTagRequirement, |
| 5560 | UsefulBuf MantissaBuffer, |
| 5561 | UsefulBufC *pMantissa, |
| 5562 | bool *pbMantissaIsNegative, |
| 5563 | int64_t *pnExponent) |
| 5564 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5565 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5566 | return; |
| 5567 | } |
| 5568 | |
| 5569 | QCBORItem Item; |
| 5570 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5571 | if(uError) { |
| 5572 | pMe->uLastError = (uint8_t)uError; |
| 5573 | return; |
| 5574 | } |
| 5575 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5576 | const TagSpecification TagSpec = |
| 5577 | { |
| 5578 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5579 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5580 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5581 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5582 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5583 | |
| 5584 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5585 | } |
| 5586 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5587 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5588 | /* |
| 5589 | Public function, see header qcbor/qcbor_decode.h file |
| 5590 | */ |
| 5591 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5592 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5593 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5594 | UsefulBuf BufferForMantissa, |
| 5595 | UsefulBufC *pMantissa, |
| 5596 | bool *pbIsNegative, |
| 5597 | int64_t *pnExponent) |
| 5598 | { |
| 5599 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5600 | return; |
| 5601 | } |
| 5602 | |
| 5603 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5604 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5605 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5606 | return; |
| 5607 | } |
| 5608 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5609 | const TagSpecification TagSpec = |
| 5610 | { |
| 5611 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5612 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5613 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5614 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5615 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5616 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5617 | ProcessMantissaAndExponentBig(pMe, |
| 5618 | TagSpec, |
| 5619 | &Item, |
| 5620 | BufferForMantissa, |
| 5621 | pMantissa, |
| 5622 | pbIsNegative, |
| 5623 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5627 | /* |
| 5628 | Public function, see header qcbor/qcbor_decode.h file |
| 5629 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5630 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5631 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5632 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5633 | UsefulBuf BufferForMantissa, |
| 5634 | UsefulBufC *pMantissa, |
| 5635 | bool *pbIsNegative, |
| 5636 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5637 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5638 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5639 | return; |
| 5640 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5641 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5642 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5643 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5644 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5645 | return; |
| 5646 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5647 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5648 | const TagSpecification TagSpec = |
| 5649 | { |
| 5650 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5651 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5652 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5653 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5654 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5655 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5656 | ProcessMantissaAndExponentBig(pMe, |
| 5657 | TagSpec, |
| 5658 | &Item, |
| 5659 | BufferForMantissa, |
| 5660 | pMantissa, |
| 5661 | pbIsNegative, |
| 5662 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5663 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5664 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5665 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |