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 | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2021, Laurence Lundblade. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 4 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following |
| 13 | disclaimer in the documentation and/or other materials provided |
| 14 | with the distribution. |
| 15 | * Neither the name of The Linux Foundation nor the names of its |
| 16 | contributors, nor the name "Laurence Lundblade" may be used to |
| 17 | endorse or promote products derived from this software without |
| 18 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 31 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 32 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 33 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 34 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 35 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | 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 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 608 | * Decoding items is done in six layers, one calling the next one |
| 609 | * down. If a layer has no work to do for a particular item, it |
| 610 | * returns quickly. |
| 611 | * |
| 612 | * 1. 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 |
| 615 | * data. For the complex ones that an aggregate of data items, there |
| 616 | * is some further decoding and some limited recursion. |
| 617 | * |
| 618 | * 2. 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 breaks that terminate |
| 621 | * indefinite-length maps and arrays. |
| 622 | * |
| 623 | * 3. GetNext_MapEntry - This handles the combining of two items, the |
| 624 | * label and the data, that make up a map entry. It only does work on |
| 625 | * maps. It combines the label and data items into one labeled item. |
| 626 | * |
| 627 | * 4. GetNext_TaggedItem - This decodes type 6 tag numbers. It turns |
| 628 | * the tag numbers into bit flags associated with the data item. No |
| 629 | * actual decoding of the contents of the tag is performed here. |
| 630 | * |
| 631 | * 5. GetNext_FullItem - This assembles the sub-items that make up an |
| 632 | * indefinite-length string into one string item. It uses the string |
| 633 | * allocator to create contiguous space for the item. It processes all |
| 634 | * breaks that are part of indefinite-length strings. |
| 635 | * |
| 636 | * 6. GetNext_Item - This decodes the atomic data items in CBOR. Each |
| 637 | * atomic data item has a "major type", an integer "argument" and |
| 638 | * optionally some content. For text and byte strings, the content is |
| 639 | * the bytes that make up the string. These are the smallest data |
| 640 | * items that are considered to be well-formed. The content may also |
| 641 | * be other data items in the case of aggregate types. They are not |
| 642 | * handled in this layer. |
| 643 | * |
| 644 | * Roughly this takes 300 bytes of stack for vars. TODO: evaluate this |
| 645 | * more carefully and correctly. |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 646 | */ |
| 647 | |
| 648 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 649 | /** |
| 650 | * @brief Decode the CBOR head, the type and argument. |
| 651 | * |
| 652 | * @param[in] pUInBuf The input buffer to read from. |
| 653 | * @param[out] pnMajorType The decoded major type. |
| 654 | * @param[out] puArgument The decoded argument. |
| 655 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 656 | * |
| 657 | * @retval QCBOR_ERR_UNSUPPORTED |
| 658 | * @retval QCBOR_ERR_HIT_END |
| 659 | * |
| 660 | * This decodes the CBOR "head" that every CBOR data item has. See |
| 661 | * longer explaination of the head in documentation for |
| 662 | * QCBOREncode_EncodeHead(). |
| 663 | * |
| 664 | * This does the network->host byte order conversion. The conversion |
| 665 | * here also results in the conversion for floats in addition to that |
| 666 | * for lengths, tags and integer values. |
| 667 | * |
| 668 | * The int type is preferred to uint8_t for some variables as this |
| 669 | * avoids integer promotions, can reduce code size and makes static |
| 670 | * analyzers happier. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 671 | */ |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 672 | static inline QCBORError |
| 673 | DecodeHead(UsefulInputBuf *pUInBuf, |
| 674 | int *pnMajorType, |
| 675 | uint64_t *puArgument, |
| 676 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 677 | { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 678 | QCBORError uReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 679 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 680 | /* Get the initial byte that every CBOR data item has and break it |
| 681 | * down. */ |
| 682 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 683 | const int nTmpMajorType = nInitialByte >> 5; |
| 684 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 685 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 686 | /* Where the argument accumulates */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 687 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 688 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 689 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 690 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 691 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 692 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 693 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 694 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 695 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 696 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 697 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 698 | /* This shift and add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 699 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 700 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 701 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 702 | /* The reserved and thus-far unused additional info values */ |
| 703 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 704 | goto Done; |
| 705 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 706 | /* Less than 24, additional info is argument or 31, an |
| 707 | * indefinite length. No more bytes to get. |
| 708 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 709 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 710 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 711 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 712 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 713 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 714 | goto Done; |
| 715 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 716 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 717 | /* All successful if arrived here. */ |
| 718 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 719 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 720 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 721 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 722 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 723 | Done: |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 724 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 727 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 728 | /** |
| 729 | * @brief Decode integer types, major types 0 and 1. |
| 730 | * |
| 731 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 732 | * @param[in] uArgument The argument from the head. |
| 733 | * @param[out] pDecodedItem The filled in decoded item. |
| 734 | * |
| 735 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 736 | * |
| 737 | * Must only be called when major type is 0 or 1. |
| 738 | * |
| 739 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 740 | * all CPUs use it these days and the test vectors in the RFC are |
| 741 | * so. All integers in the CBOR structure are positive and the major |
| 742 | * type indicates positive or negative. CBOR can express positive |
| 743 | * integers up to 2^x - 1 where x is the number of bits and negative |
| 744 | * integers down to 2^x. Note that negative numbers can be one more |
| 745 | * away from zero than positive. Stdint, as far as I can tell, uses |
| 746 | * two's compliment to represent negative integers. |
| 747 | * |
| 748 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 749 | * used carefully here, and in particular why it isn't used in the |
| 750 | * public interface. Also see |
| 751 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 752 | * |
| 753 | * Int is used for values that need less than 16-bits and would be |
| 754 | * subject to integer promotion and result in complaining from static |
| 755 | * analyzers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 756 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 757 | static inline QCBORError |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 758 | DecodeInteger(int nMajorType, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 759 | { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 760 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 761 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 762 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 763 | if (uArgument <= INT64_MAX) { |
| 764 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 765 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 766 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 767 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 768 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 769 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 770 | } |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 771 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 772 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 773 | if(uArgument <= INT64_MAX) { |
| 774 | /* CBOR's representation of negative numbers lines up with |
| 775 | * the two-compliment representation. A negative integer has |
| 776 | * one more in range than a positive integer. INT64_MIN is |
| 777 | * equal to (-INT64_MAX) - 1. |
| 778 | */ |
| 779 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 780 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 781 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 782 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 783 | /* C can't represent a negative integer in this range so it |
| 784 | * is an error. |
| 785 | */ |
| 786 | uReturn = QCBOR_ERR_INT_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 789 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 790 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 793 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 794 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 795 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 796 | #error QCBOR_TYPE_FALSE macro value wrong |
| 797 | #endif |
| 798 | |
| 799 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 800 | #error QCBOR_TYPE_TRUE macro value wrong |
| 801 | #endif |
| 802 | |
| 803 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 804 | #error QCBOR_TYPE_NULL macro value wrong |
| 805 | #endif |
| 806 | |
| 807 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 808 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 809 | #endif |
| 810 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 811 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 812 | #error QCBOR_TYPE_BREAK macro value wrong |
| 813 | #endif |
| 814 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 815 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 816 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 817 | #endif |
| 818 | |
| 819 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 820 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 821 | #endif |
| 822 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 823 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 824 | /** |
| 825 | * @brief Decode type 7 -- true, false, floating-point, break... |
| 826 | * |
| 827 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 828 | * @param[in] uArgument The argument from the head. |
| 829 | * @param[out] pDecodedItem The filled in decoded item. |
| 830 | * |
| 831 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 832 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 833 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 834 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 835 | static inline QCBORError |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 836 | DecodeType7(int nAdditionalInfo, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 837 | { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 838 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 839 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 840 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 841 | * checks above make sure uAdditionalInfo values line up with |
| 842 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 843 | * > 0x1f so cast is safe. |
| 844 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 845 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 846 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 847 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 848 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 849 | * are caught before this is called. |
| 850 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 851 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 852 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 853 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 854 | /* Half-precision is returned as a double. The cast to |
| 855 | * uint16_t is safe because the encoded value was 16 bits. It |
| 856 | * was widened to 64 bits to be passed in here. |
| 857 | */ |
| 858 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 859 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 860 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 861 | uReturn = QCBOR_ERR_HALF_PRECISION_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 862 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 863 | break; |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 864 | case SINGLE_PREC_FLOAT: /* 26 */ |
| 865 | /* Single precision is normally returned as a double since |
| 866 | * double is widely supported, there is no loss of precision, |
| 867 | * it makes it easy for the caller in most cases and it can |
| 868 | * be converted back to single with no loss of precision |
| 869 | * |
| 870 | * The cast to uint32_t is safe because the encoded value was |
| 871 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 872 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 873 | { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 874 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 875 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 876 | /* In the normal case, use HW to convert float to |
| 877 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 878 | pDecodedItem->val.dfnum = (double)f; |
| 879 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 880 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 881 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 882 | pDecodedItem->val.fnum = f; |
| 883 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 884 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 885 | /* IEEE754_FloatToDouble() could be used here to return as |
| 886 | * a double, but it adds object code and most likely |
| 887 | * anyone disabling FLOAT HW use doesn't care about floats |
| 888 | * and wants to save object code. |
| 889 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 890 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 891 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 892 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 893 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 894 | case DOUBLE_PREC_FLOAT: /* 27 */ |
| 895 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 896 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 897 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 898 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 899 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 900 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 901 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 902 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 903 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 904 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 905 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 906 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 907 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 908 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 909 | * as e0 … f7 |
| 910 | */ |
| 911 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 912 | goto Done; |
| 913 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 914 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 915 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 916 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 917 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 918 | /* DecodeHead() will make uArgument equal to |
| 919 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 920 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 921 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 922 | */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 923 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 924 | break; |
| 925 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 926 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 927 | Done: |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 928 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 932 | /** |
| 933 | * @brief Decode text and byte strings |
| 934 | * |
| 935 | * @param[in] pAllocator The string allocator or NULL. |
| 936 | * @param[in] uStrLen The length of the string. |
| 937 | * @param[in] pUInBuf The surce from which to read the string's bytes. |
| 938 | * @param[out] pDecodedItem The filled in decoded item. |
| 939 | * |
| 940 | * @retval QCBOR_ERR_HIT_END |
| 941 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 942 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 943 | * |
| 944 | * The reads @c uStrlen bytes from @c pUInBuf and fills in @c |
| 945 | * pDecodedItem. If @c pAllocator is not NULL then memory for the |
| 946 | * string is allocated. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 947 | */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 948 | static inline QCBORError |
Laurence Lundblade | 63e68f7 | 2020-12-28 04:24:11 -0800 | [diff] [blame] | 949 | DecodeBytes(const QCBORInternalAllocator *pAllocator, |
| 950 | uint64_t uStrLen, |
| 951 | UsefulInputBuf *pUInBuf, |
| 952 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 953 | { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 954 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 955 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 956 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 957 | * CPUs. This check makes the casts to size_t below safe. |
| 958 | * |
| 959 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 960 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 961 | * one will care the limit on strings is 4 bytes shorter). |
| 962 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 963 | if(uStrLen > SIZE_MAX-4) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 964 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 965 | goto Done; |
| 966 | } |
| 967 | |
| 968 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 969 | if(UsefulBuf_IsNULLC(Bytes)) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 970 | /* Failed to get the bytes for this string item */ |
| 971 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 972 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 973 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 974 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 975 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 976 | /* Note that this is not where allocation to coallsece |
| 977 | * indefinite-length strings is done. This is for when the caller |
| 978 | * has requested all strings be allocated. Disabling indefinite |
| 979 | * length strings also disables this allocate-all option. |
| 980 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 981 | if(pAllocator) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 982 | /* request to use the string allocator to make a copy */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 983 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 984 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 985 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 986 | goto Done; |
| 987 | } |
| 988 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 989 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 990 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 991 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 992 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 993 | (void)pAllocator; |
| 994 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 995 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 996 | /* Normal case with no string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 997 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 998 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 999 | Done: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1000 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1003 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1004 | /** |
| 1005 | * @brief Map the CBOR major types for strings to the QCBOR types. |
| 1006 | * |
| 1007 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1008 | * @retturns QCBOR type number. |
| 1009 | * |
| 1010 | * This only works for the two string types. |
| 1011 | */ |
| 1012 | static inline uint8_t ConvertStringMajorTypes(int nCBORMajorType) |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1013 | { |
| 1014 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 1015 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 1016 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1017 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1018 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 1019 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 1020 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1021 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1022 | return (uint8_t)(nCBORMajorType + 4); |
| 1023 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1024 | |
| 1025 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1026 | /** |
| 1027 | * @brief Map the CBOR major types for arrays/maps to the QCBOR types. |
| 1028 | * |
| 1029 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1030 | * @retturns QCBOR type number. |
| 1031 | * |
| 1032 | * This only works for the two aggregate types. |
| 1033 | */ |
| 1034 | static inline uint8_t ConvertArrayOrMapType(int nCBORMajorType) |
| 1035 | { |
| 1036 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1037 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1038 | #endif |
| 1039 | |
| 1040 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1041 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1042 | #endif |
| 1043 | |
| 1044 | return (uint8_t)(nCBORMajorType); |
| 1045 | } |
| 1046 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1047 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1048 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 1049 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1050 | * |
| 1051 | * @param[in] pUInBuf Input buffer to read data item from. |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1052 | * @param[out] pDecodedItem The filled-in decoded item. |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1053 | * @param[in] pAllocator The allocator to use for strings or NULL. |
| 1054 | * |
| 1055 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1056 | * @retval QCBOR_ERR_HIT_END |
| 1057 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1058 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1059 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1060 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1061 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1062 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1063 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1064 | * This decodes the most primitive / atomic data item. It does |
| 1065 | * no combing of data items. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1066 | */ |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1067 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, |
| 1068 | QCBORItem *pDecodedItem, |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1069 | const QCBORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1070 | { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1071 | QCBORError uReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1072 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1073 | /* Get the major type and the argument. The argument could be |
| 1074 | * length of more bytes or the value depending on the major |
| 1075 | * type. nAdditionalInfo is an encoding of the length of the |
| 1076 | * uNumber and is needed to decode floats and doubles. |
| 1077 | */ |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 1078 | int nMajorType = 0; |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1079 | uint64_t uArgument = 0; |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 1080 | int nAdditionalInfo = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1081 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 1082 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1083 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1084 | uReturn = DecodeHead(pUInBuf, &nMajorType, &uArgument, &nAdditionalInfo); |
| 1085 | if(uReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1086 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1087 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1088 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1089 | /* At this point the major type and the argument are valid. We've |
| 1090 | * got the type and the argument that starts every CBOR data item. |
| 1091 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1092 | switch (nMajorType) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1093 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1094 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1095 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1096 | uReturn = QCBOR_ERR_BAD_INT; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1097 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1098 | uReturn = DecodeInteger(nMajorType, uArgument, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1099 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1100 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1101 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1102 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1103 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
| 1104 | pDecodedItem->uDataType = ConvertStringMajorTypes(nMajorType); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1105 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1106 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1107 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1108 | uReturn = DecodeBytes(pAllocator, uArgument, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1109 | } |
| 1110 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1111 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1112 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1113 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1114 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1115 | /* Indefinite-length string. */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1116 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1117 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1118 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1119 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1120 | break; |
| 1121 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1122 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1123 | /* Definite-length string. */ |
| 1124 | if(uArgument > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1125 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1126 | goto Done; |
| 1127 | } |
| 1128 | /* cast OK because of check above */ |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1129 | pDecodedItem->val.uCount = (uint16_t)uArgument; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1130 | } |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1131 | pDecodedItem->uDataType = ConvertArrayOrMapType(nMajorType); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1132 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1133 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1134 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1135 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1136 | uReturn = QCBOR_ERR_BAD_INT; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1137 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1138 | pDecodedItem->val.uTagV = uArgument; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1139 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1140 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1141 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1142 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1143 | case CBOR_MAJOR_TYPE_SIMPLE: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1144 | /* Major type 7: float, double, true, false, null... */ |
| 1145 | uReturn = DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1146 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1147 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1148 | default: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1149 | /* Never happens because DecodeHead() should never return > 7 */ |
| 1150 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1151 | break; |
| 1152 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1153 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1154 | Done: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1155 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1159 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 1160 | * @brief Process indefinite length strings (decode layer 5). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1161 | * |
| 1162 | * @param[in] pMe Decoder context |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1163 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1164 | * |
| 1165 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1166 | * @retval QCBOR_ERR_HIT_END |
| 1167 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1168 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1169 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1170 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1171 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1172 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1173 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1174 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1175 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1176 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1177 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1178 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1179 | * If it is, this loops getting the subsequent chunk data items that |
| 1180 | * make up the string. The string allocator is used to make a |
| 1181 | * contiguous buffer for the chunks. When this completes @c |
| 1182 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1183 | * |
| 1184 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1185 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1186 | static inline QCBORError |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1187 | GetNext_FullItem(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1188 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1189 | /* Aproximate stack usage |
| 1190 | * 64-bit 32-bit |
| 1191 | * local vars 32 16 |
| 1192 | * 2 UsefulBufs 32 16 |
| 1193 | * QCBORItem 56 52 |
| 1194 | * TOTAL 120 74 |
| 1195 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1196 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1197 | /* The string allocator is used here for two purposes: 1) |
| 1198 | * coalescing the chunks of an indefinite length string, 2) |
| 1199 | * allocating storage for every string returned. |
| 1200 | * |
| 1201 | * The first use is below in this function. Indefinite length |
| 1202 | * strings cannot be processed at all without a string allocator. |
| 1203 | * |
| 1204 | * The second used is in DecodeBytes() which is called by |
| 1205 | * GetNext_Item() below. This second use unneccessary for most use |
| 1206 | * and only happens when requested in the call to |
| 1207 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1208 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1209 | * |
| 1210 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1211 | * allocator altogether and thus both of these uses. It reduced the |
| 1212 | * decoder object code by about 400 bytes. |
| 1213 | */ |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1214 | const QCBORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1215 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1216 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1217 | const QCBORInternalAllocator *pAllocator = NULL; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1218 | |
| 1219 | if(pMe->StringAllocator.pfAllocator) { |
| 1220 | pAllocator = &(pMe->StringAllocator); |
| 1221 | if(pMe->bStringAllocateAll) { |
| 1222 | pAllocatorForGetNext = pAllocator; |
| 1223 | } |
| 1224 | } |
| 1225 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1226 | |
| 1227 | QCBORError uReturn; |
| 1228 | uReturn = GetNext_Item(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
| 1229 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1230 | goto Done; |
| 1231 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1232 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1233 | /* Only do indefinite length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1234 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1235 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1236 | goto Done; |
| 1237 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1238 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1239 | /* Is this a string with an indefinite length? */ |
| 1240 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1241 | goto Done; |
| 1242 | } |
| 1243 | |
| 1244 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 1245 | /* Can't do indefinite length strings without a string allocator */ |
| 1246 | if(pAllocator == NULL) { |
| 1247 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1248 | goto Done; |
| 1249 | } |
| 1250 | |
| 1251 | /* Loop getting chunks of the indefinite length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1252 | UsefulBufC FullString = NULLUsefulBufC; |
| 1253 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1254 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1255 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1256 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1257 | /* Pass a NULL string allocator to GetNext_Item() because the |
| 1258 | * individual string chunks in an indefinite length should not |
| 1259 | * be allocated. They are always copied in the the contiguous |
| 1260 | * buffer allocated here. |
| 1261 | */ |
| 1262 | uReturn = GetNext_Item(&(pMe->InBuf), &StringChunkItem, NULL); |
| 1263 | if(uReturn) { |
| 1264 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1265 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1266 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1267 | /* Is item is the marker for end of the indefinite length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1268 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1269 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1270 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1271 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1272 | break; |
| 1273 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1274 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1275 | /* All chunks must be of the same type, the type of the item |
| 1276 | * that introduces the indefinite length string. This also |
| 1277 | * catches errors where the chunk is not a string at all and an |
| 1278 | * indefinite length string inside an indefinite length string. |
| 1279 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1280 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1281 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1282 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1283 | break; |
| 1284 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1285 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1286 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1287 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1288 | * not NULL and a reallocation happens. |
| 1289 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1290 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1291 | UNCONST_POINTER(FullString.ptr), |
| 1292 | FullString.len + StringChunkItem.val.string.len); |
| 1293 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1294 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1295 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1296 | break; |
| 1297 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1298 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1299 | /* Copy new string chunk to the end of accumulated string */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1300 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1301 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1302 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1303 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1304 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1305 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1306 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1307 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1308 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1309 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1310 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1311 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1312 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1315 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1316 | /** |
| 1317 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1318 | * |
| 1319 | * @param[in] pMe The decode context. |
| 1320 | * @param[in] uUnMappedTag The tag number to map |
| 1321 | * @param[out] puMappedTagNumer The stored tag number. |
| 1322 | * |
| 1323 | * @return error code. |
| 1324 | * |
| 1325 | * The main point of mapping tag numbers is make QCBORItem |
| 1326 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1327 | * bytes. Without, it would take up 32 bytes. |
| 1328 | * |
| 1329 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1330 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1331 | * |
| 1332 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1333 | */ |
| 1334 | static inline QCBORError |
| 1335 | MapTagNumber(QCBORDecodeContext *pMe, uint64_t uUnMappedTag, uint16_t *puMappedTagNumer) |
| 1336 | { |
| 1337 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1338 | int uTagMapIndex; |
| 1339 | /* Is there room in the tag map, or is it in it already? */ |
| 1340 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1341 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1342 | break; |
| 1343 | } |
| 1344 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1345 | break; |
| 1346 | } |
| 1347 | } |
| 1348 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1349 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1350 | } |
| 1351 | |
| 1352 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1353 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1354 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1355 | |
| 1356 | } else { |
| 1357 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1358 | } |
| 1359 | |
| 1360 | return QCBOR_SUCCESS; |
| 1361 | } |
| 1362 | |
| 1363 | |
| 1364 | /** |
| 1365 | * @brief This converts a mapped tag number to the actual tag number. |
| 1366 | * |
| 1367 | * @param[in] pMe The decode context. |
| 1368 | * @param[in] uMappedTagNumber The stored tag number. |
| 1369 | * |
| 1370 | * @return The actual tag number is returned or |
| 1371 | * @ref CBOR_TAG_INVALID64 on error. |
| 1372 | * |
| 1373 | * This is the reverse of MapTagNumber() |
| 1374 | */ |
| 1375 | static uint64_t |
| 1376 | UnMapTagNumber(const QCBORDecodeContext *pMe, uint16_t uMappedTagNumber) |
| 1377 | { |
| 1378 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1379 | return uMappedTagNumber; |
| 1380 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1381 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1382 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1383 | /* This won't be negative because of code below in |
| 1384 | * MapTagNumber() |
| 1385 | */ |
| 1386 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1387 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1391 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1392 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 1393 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1394 | * |
| 1395 | * @param[in] pMe Decoder context |
| 1396 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1397 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1398 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1399 | * @retval QCBOR_ERR_HIT_END |
| 1400 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1401 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1402 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1403 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1404 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1405 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1406 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1407 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1408 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1409 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1410 | * |
| 1411 | * This loops getting atomic data items until one is not a tag |
| 1412 | * number. Usually this is largely pass-through because most |
| 1413 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1414 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1415 | static QCBORError |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1416 | GetNext_TaggedItem(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1417 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1418 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1419 | |
| 1420 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1421 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1422 | /* Be sure the memset does the right thing. */ |
| 1423 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1424 | #endif |
| 1425 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1426 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1427 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1428 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1429 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1430 | for(;;) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1431 | QCBORError uErr = GetNext_FullItem(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1432 | if(uErr != QCBOR_SUCCESS) { |
| 1433 | uReturn = uErr; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1434 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1435 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1436 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1437 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1438 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1439 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1440 | break; |
| 1441 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1442 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1443 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1444 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1445 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1446 | /* Continue on to get all tags wrapping this item even though |
| 1447 | * it is erroring out in the end. This allows decoding to |
| 1448 | * continue. This is a resource limit error, not a problem |
| 1449 | * with being well-formed CBOR. |
| 1450 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1451 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1452 | } |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1453 | /* Slide tags over one in the array to make room at index 0. |
| 1454 | * Must use memmove because the move source and destination |
| 1455 | * overlap. |
| 1456 | */ |
| 1457 | memmove(&auItemsTags[1], auItemsTags, sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1458 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1459 | /* Map the tag */ |
| 1460 | uint16_t uMappedTagNumer; |
| 1461 | uReturn = MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumer); |
| 1462 | /* Continue even on error so as to consume all tags wrapping |
| 1463 | * this data item so decoding can go on. If MapTagNumber() |
| 1464 | * errors once it will continue to error. |
| 1465 | */ |
| 1466 | auItemsTags[0] = uMappedTagNumer; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1467 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1468 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1469 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1470 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 1475 | * @brief Combine a map entry label and value into one item (decode layer 3). |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1476 | * |
| 1477 | * @param[in] pMe Decoder context |
| 1478 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1479 | * |
| 1480 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1481 | * @retval QCBOR_ERR_HIT_END |
| 1482 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1483 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1484 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1485 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1486 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1487 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1488 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1489 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1490 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1491 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1492 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1493 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1494 | * |
| 1495 | * If a the current nesting level is a map, then this |
| 1496 | * combines pairs of items into one data item with a label |
| 1497 | * and value. |
| 1498 | * |
| 1499 | * This is pass-through if the current nesting leve is |
| 1500 | * not a map. |
| 1501 | * |
| 1502 | * This also implements maps-as-array mode where a map |
| 1503 | * is treated like an array to allow caller to do their |
| 1504 | * own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1505 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1506 | static inline QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1507 | GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1508 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1509 | QCBORError uReturn = GetNext_TaggedItem(me, pDecodedItem); |
| 1510 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1511 | goto Done; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1512 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1513 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1514 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1515 | /* Break can't be a map entry */ |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1516 | goto Done; |
| 1517 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1518 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1519 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1520 | /* Normal decoding of maps -- combine label and value into one item. */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1521 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1522 | if(DecodeNesting_IsCurrentTypeMap(&(me->nesting))) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1523 | /* Save label in pDecodedItem and get the next which will |
| 1524 | * be the real data item. |
| 1525 | */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1526 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1527 | uReturn = GetNext_TaggedItem(me, pDecodedItem); |
| 1528 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1529 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1530 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1531 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1532 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1533 | |
| 1534 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1535 | /* strings are always good labels */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1536 | pDecodedItem->label.string = LabelItem.val.string; |
| 1537 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 1538 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1539 | /* It's not a string and we only want strings */ |
| 1540 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1541 | goto Done; |
| 1542 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1543 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1544 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1545 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1546 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1547 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1548 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1549 | pDecodedItem->label.string = LabelItem.val.string; |
| 1550 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1551 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1552 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1553 | /* label is not an int or a string. It is an arrray |
| 1554 | * or a float or such and this implementation doesn't handle that. |
| 1555 | * Also, tags on labels are ignored. |
| 1556 | */ |
| 1557 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1558 | goto Done; |
| 1559 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1560 | } |
| 1561 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1562 | /* Decoding of maps as arrays to let the caller decide what to do |
| 1563 | * about labels, particularly lables that are not integers or |
| 1564 | * strings. |
| 1565 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1566 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1567 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1568 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1569 | goto Done; |
| 1570 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1571 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1572 | /* Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2. |
| 1573 | * Cast is needed because of integer promotion. |
| 1574 | */ |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1575 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1576 | } |
| 1577 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1578 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1579 | Done: |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1580 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1584 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1585 | /** |
| 1586 | * @brief Peek and see if next data item is a break; |
| 1587 | * |
| 1588 | * @param[in] pUIB UsefulInputBuf to read from. |
| 1589 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1590 | * |
| 1591 | * @return Any decoding error. |
| 1592 | * |
| 1593 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1594 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1595 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1596 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1597 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1598 | { |
| 1599 | *pbNextIsBreak = false; |
| 1600 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1601 | QCBORItem Peek; |
| 1602 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
| 1603 | QCBORError uReturn = GetNext_Item(pUIB, &Peek, NULL); |
| 1604 | if(uReturn != QCBOR_SUCCESS) { |
| 1605 | return uReturn; |
| 1606 | } |
| 1607 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1608 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1609 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1610 | } else { |
| 1611 | *pbNextIsBreak = true; |
| 1612 | } |
| 1613 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1614 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1615 | return QCBOR_SUCCESS; |
| 1616 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1617 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1618 | |
| 1619 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1620 | /* |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1621 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1622 | * |
| 1623 | * @param[in] pMe The decode context. |
| 1624 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
| 1625 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1626 | * An item was just consumed, now figure out if it was the |
| 1627 | * end of an array/map map that can be closed out. That |
| 1628 | * may in turn close out the above array/map... |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1629 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1630 | static QCBORError NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1631 | { |
| 1632 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1633 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1634 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1635 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1636 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1637 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1638 | /* Nesting level is bstr-wrapped CBOR */ |
| 1639 | |
| 1640 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1641 | * so no further ascending can happen. |
| 1642 | */ |
| 1643 | break; |
| 1644 | |
| 1645 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1646 | /* Level is a definite-length array/map */ |
| 1647 | |
| 1648 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1649 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1650 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1651 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1652 | break; |
| 1653 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1654 | /* All items in a definite length array were consumed so it |
| 1655 | * is time to ascend one level. This happens below. |
| 1656 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1657 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1658 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1659 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1660 | /* Level is an indefinite-length array/map. */ |
| 1661 | |
| 1662 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1663 | bool bIsBreak = false; |
| 1664 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1665 | if(uReturn != QCBOR_SUCCESS) { |
| 1666 | goto Done; |
| 1667 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1668 | |
| 1669 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1670 | /* 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] | 1671 | break; |
| 1672 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1673 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1674 | /* It was a break in an indefinite length map / array so |
| 1675 | * it is time to ascend one level. |
| 1676 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1677 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1678 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1679 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1680 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1681 | |
| 1682 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1683 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1684 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1685 | * QCBORDecode_ExitBoundedMode(). |
| 1686 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1687 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1688 | /* Set the count to zero for definite length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1689 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1690 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1691 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1692 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1693 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1694 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1695 | break; |
| 1696 | } |
| 1697 | |
| 1698 | /* Finally, actually ascend one level. */ |
| 1699 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1700 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1701 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1702 | uReturn = QCBOR_SUCCESS; |
| 1703 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1704 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1705 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1706 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1707 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1708 | return uReturn; |
| 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 1713 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
| 1714 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1715 | This handles the traversal descending into and asecnding out of maps, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1716 | arrays and bstr-wrapped CBOR. It figures out the ends of definite and |
| 1717 | indefinte length maps and arrays by looking at the item count or |
| 1718 | finding CBOR breaks. It detects the ends of the top-level sequence |
| 1719 | and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1720 | |
| 1721 | @retval QCBOR_ERR_UNSUPPORTED X |
| 1722 | |
| 1723 | @retval QCBOR_ERR_HIT_END |
| 1724 | |
| 1725 | @retval QCBOR_ERR_INT_OVERFLOW X |
| 1726 | |
| 1727 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1728 | |
| 1729 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1730 | |
| 1731 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED X |
| 1732 | |
| 1733 | @retval QCBOR_ERR_BAD_TYPE_7 X |
| 1734 | |
| 1735 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1736 | |
| 1737 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1738 | |
| 1739 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1740 | |
| 1741 | @retval QCBOR_ERR_MAP_LABEL_TYPE X |
| 1742 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1743 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1744 | |
| 1745 | @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1746 | |
| 1747 | @retval QCBOR_ERR_BAD_BREAK |
| 1748 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1749 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1750 | static QCBORError |
| 1751 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1752 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1753 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1754 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1755 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | If out of bytes to consume, it is either the end of the top-level |
| 1758 | sequence of some bstr-wrapped CBOR that was entered. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1759 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1760 | In the case of bstr-wrapped CBOR, the length of the UsefulInputBuf |
| 1761 | was set to that of the bstr-wrapped CBOR. When the bstr-wrapped |
| 1762 | CBOR is exited, the length is set back to the top-level's length |
| 1763 | or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1764 | */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1765 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1766 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1767 | goto Done; |
| 1768 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1769 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1770 | /* |
| 1771 | 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] | 1772 | array. The check for a break ending indefinite length array is |
| 1773 | later in NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1774 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1775 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1776 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1777 | goto Done; |
| 1778 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1779 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1780 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1781 | uReturn = GetNext_MapEntry(me, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1782 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1783 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1784 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1785 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1786 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1787 | /* |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1788 | Breaks ending arrays/maps are processed later in the call to |
| 1789 | NestLevelAscender(). They should never show up here. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1790 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1791 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1792 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1793 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1794 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1795 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1796 | /* |
| 1797 | Record the nesting level for this data item before processing any |
| 1798 | of decrementing and descending. |
| 1799 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1800 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1801 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1802 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1803 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1804 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1805 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1806 | If the new item is a map or array, descend. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1807 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1808 | Empty indefinite length maps and arrays are descended into, but |
| 1809 | then ascended out of in the next chunk of code. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1810 | |
| 1811 | Maps and arrays do count as items in the map/array that |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1812 | encloses them so a decrement needs to be done for them too, but |
| 1813 | that is done only when all the items in them have been |
| 1814 | processed, not when they are opened with the exception of an |
| 1815 | empty map or array. |
| 1816 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1817 | QCBORError uDescendErr; |
| 1818 | uDescendErr = DecodeNesting_DescendMapOrArray(&(me->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1819 | pDecodedItem->uDataType, |
| 1820 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1821 | if(uDescendErr != QCBOR_SUCCESS) { |
| 1822 | /* This error is probably a traversal error and it |
| 1823 | overrides the non-traversal error. */ |
| 1824 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1825 | goto Done; |
| 1826 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1829 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1830 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1831 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1832 | /* |
| 1833 | The following cases are handled here: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1834 | - A non-aggregate item like an integer or string |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1835 | - An empty definite length map or array |
| 1836 | - 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] | 1837 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1838 | NestLevelAscender() does the work of decrementing the count for an |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1839 | definite length map/array and break detection for an indefinite |
| 1840 | length map/array. If the end of the map/array was reached, then |
| 1841 | it ascends nesting levels, possibly all the way to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1842 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1843 | QCBORError uAscendErr; |
| 1844 | uAscendErr = NestLevelAscender(me, true); |
| 1845 | if(uAscendErr != QCBOR_SUCCESS) { |
| 1846 | /* This error is probably a traversal error and it |
| 1847 | overrides the non-traversal error. */ |
| 1848 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1849 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1850 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1851 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1852 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1853 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1854 | /* |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1855 | Tell the caller what level is next. This tells them what |
| 1856 | maps/arrays were closed out and makes it possible for them to |
| 1857 | reconstruct the tree with just the information returned in |
| 1858 | a QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1859 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1860 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1861 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1862 | pDecodedItem->uNextNestLevel = 0; |
| 1863 | } else { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1864 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1865 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1866 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1867 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1868 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1871 | |
| 1872 | static inline void ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1873 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1874 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 1875 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 1876 | } |
| 1877 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1880 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1881 | |
Laurence Lundblade | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 1882 | /** |
| 1883 | * @brief Convert different epoch date formats in to the QCBOR epoch date format |
| 1884 | * |
| 1885 | * pDecodedItem[in,out] The data item to convert. |
| 1886 | * |
| 1887 | * @retval QCBOR_ERR_DATE_OVERFLOW |
| 1888 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED |
| 1889 | * @retval QCBOR_ERR_BAD_TAG_CONTENT |
| 1890 | * |
| 1891 | * The epoch date tag defined in QCBOR allows for floating-point |
| 1892 | * dates. It even allows a protocol to flop between date formats when |
| 1893 | * ever it wants. Floating-point dates aren't that useful as they are |
| 1894 | * only needed for dates beyond the age of the earth. |
| 1895 | * |
| 1896 | * This converts all the date formats into one format of an unsigned |
| 1897 | * integer plus a floating-point fraction. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1898 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1899 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1900 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1901 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1902 | |
| 1903 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1904 | |
| 1905 | switch (pDecodedItem->uDataType) { |
| 1906 | |
| 1907 | case QCBOR_TYPE_INT64: |
| 1908 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1909 | break; |
| 1910 | |
| 1911 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 1912 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 1913 | * always an overflow. |
| 1914 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1915 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1916 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1917 | break; |
| 1918 | |
| 1919 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1920 | case QCBOR_TYPE_FLOAT: |
| 1921 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1922 | { |
Laurence Lundblade | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 1923 | /* Convert working value to double if input was a float */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1924 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
Laurence Lundblade | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 1925 | pDecodedItem->val.dfnum : |
| 1926 | (double)pDecodedItem->val.fnum; |
| 1927 | |
| 1928 | /* The conversion from float to integer requires overflow |
| 1929 | * detection since floats can be much larger than integers. |
| 1930 | * This implementation errors out on these large float values |
| 1931 | * since they are beyond the age of the earth. |
| 1932 | * |
| 1933 | * These constants for the overflow check are computed by the |
| 1934 | * compiler. They are not computed at run time. |
| 1935 | * |
| 1936 | * The factor of 0x7ff is added/subtracted to avoid a |
| 1937 | * rounding error in the wrong direction when the compiler |
| 1938 | * computes these constants. There is rounding because an |
| 1939 | * 64-bit integer has 63 bits of precision where a double |
| 1940 | * only has 53 bits. Without the 0x7ff factor, the compiler |
| 1941 | * may round up and produce a double for the bounds check |
| 1942 | * that is larger than can be stored in a 64-bit integer. The |
| 1943 | * amoutn of 0x7ff is picked because it has 11 bits set. |
| 1944 | * |
| 1945 | * Without the 0x7ff there is a ~30 minute range of time |
| 1946 | * values 10 billion years in the past and in the future |
| 1947 | * where this code could go wrong. Some compilers correctly |
| 1948 | * generate a warning or error without the 0x7ff. |
| 1949 | */ |
| 1950 | const double dDateMax = (double)(INT64_MAX - 0x7ff); |
| 1951 | const double dDateMin = (double)(INT64_MIN + 0x7ff); |
| 1952 | |
| 1953 | if(isnan(d) || d > dDateMax || d < dDateMin) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1954 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1955 | goto Done; |
| 1956 | } |
Laurence Lundblade | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 1957 | |
| 1958 | /* The actual conversion */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1959 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1960 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 1961 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1962 | } |
Laurence Lundblade | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 1963 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1964 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1965 | uReturn = QCBOR_ERR_FLOAT_DATE_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1966 | goto Done; |
| 1967 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1968 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1969 | break; |
| 1970 | |
| 1971 | default: |
Laurence Lundblade | a333684 | 2021-01-03 12:38:36 -0800 | [diff] [blame] | 1972 | uReturn = QCBOR_ERR_BAD_TAG_CONTENT; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1973 | goto Done; |
| 1974 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1975 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1976 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1977 | |
| 1978 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1979 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | |
| 1983 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 1984 | /** |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 1985 | * @brief Decode decimal fractions and big floats. |
| 1986 | * |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 1987 | * @param[in] pMe The decode context. |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 1988 | * @param[in,out] pDecodedItem On input the array data item that |
| 1989 | * holds the mantissa and exponent. On |
| 1990 | * output the decoded mantissa and |
| 1991 | * exponent. |
| 1992 | * |
| 1993 | * @returns Decoding errors from getting primitive data items or |
| 1994 | * \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA. |
| 1995 | * |
| 1996 | * When called pDecodedItem must be the array that is tagged as a big |
| 1997 | * float or decimal fraction, the array that has the two members, the |
| 1998 | * exponent and mantissa. |
| 1999 | * |
| 2000 | * This will fetch and decode the exponent and mantissa and put the |
| 2001 | * result back into pDecodedItem. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2002 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2003 | static inline QCBORError |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2004 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2005 | { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2006 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2007 | |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2008 | /* --- Make sure it is an array; track nesting level of members --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2009 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2010 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2011 | goto Done; |
| 2012 | } |
| 2013 | |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2014 | /* A check for pDecodedItem->val.uCount == 2 would work for |
| 2015 | * definite length arrays, but not for indefnite. Instead remember |
| 2016 | * the nesting level the two integers must be at, which is one |
| 2017 | * deeper than that of the array. |
| 2018 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2019 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 2020 | |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2021 | /* --- Which is it, ecimal fraction or a bigfloat? --- */ |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2022 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(pMe, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2023 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 2024 | |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2025 | /* --- Get the exponent --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2026 | QCBORItem exponentItem; |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2027 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, &exponentItem); |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2028 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2029 | goto Done; |
| 2030 | } |
| 2031 | if(exponentItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2032 | /* Array is empty or a map/array encountered when expecting an int */ |
| 2033 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2034 | goto Done; |
| 2035 | } |
| 2036 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2037 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2038 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2039 | * also means that the only data arriving here of type |
| 2040 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2041 | * and thus an error that will get handled in the next else. |
| 2042 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2043 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2044 | } else { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2045 | /* Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2046 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2047 | goto Done; |
| 2048 | } |
| 2049 | |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2050 | /* --- Get the mantissa --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2051 | QCBORItem mantissaItem; |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2052 | uReturn = QCBORDecode_GetNextWithTags(pMe, &mantissaItem, NULL); |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2053 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2054 | goto Done; |
| 2055 | } |
| 2056 | if(mantissaItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2057 | /* Mantissa missing or map/array encountered when expecting number */ |
| 2058 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2059 | goto Done; |
| 2060 | } |
| 2061 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2062 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2063 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2064 | * also means that the only data arriving here of type |
| 2065 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2066 | * and thus an error that will get handled in an else below. |
| 2067 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2068 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2069 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2070 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2071 | /* Got a good big num mantissa */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2072 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2073 | /* Depends on numbering of QCBOR_TYPE_XXX */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2074 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 2075 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 2076 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2077 | } else { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2078 | /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2079 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2080 | goto Done; |
| 2081 | } |
| 2082 | |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2083 | /* --- Check that array only has the two numbers --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2084 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2085 | /* Extra items in the decimal fraction / big float */ |
| 2086 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2087 | goto Done; |
| 2088 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2089 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2090 | |
| 2091 | Done: |
Laurence Lundblade | e176d85 | 2021-01-03 17:50:30 -0800 | [diff] [blame] | 2092 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2093 | } |
| 2094 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 2095 | |
| 2096 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2097 | /** |
| 2098 | * @brief Decode the MIME type tag |
| 2099 | * |
| 2100 | * @param[in,out] pDecodedItem The item to decode. |
| 2101 | * |
| 2102 | * Handle the text and binary MIME type tags. Slightly too complicated |
| 2103 | * f or ProcessTaggedString() because the RFC 7049 MIME type was |
| 2104 | * incorreclty text-only. |
| 2105 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2106 | static inline QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2107 | { |
| 2108 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2109 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2110 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2111 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2112 | } else { |
| 2113 | return QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2114 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2115 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2116 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2117 | return QCBOR_SUCCESS; |
| 2118 | } |
| 2119 | |
| 2120 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2121 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2122 | * Table of CBOR tags whose content is either a text string or a byte |
| 2123 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2124 | * of uQCBORtype indicates the content should be a byte string rather |
| 2125 | * than a text string |
| 2126 | */ |
| 2127 | struct StringTagMapEntry { |
| 2128 | uint16_t uTagNumber; |
| 2129 | uint8_t uQCBORtype; |
| 2130 | }; |
| 2131 | |
| 2132 | #define IS_BYTE_STRING_BIT 0x80 |
| 2133 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2134 | |
| 2135 | static const struct StringTagMapEntry StringTagMap[] = { |
| 2136 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
| 2137 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2138 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2139 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2140 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
| 2141 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2142 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2143 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2144 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
| 2145 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2146 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2147 | }; |
| 2148 | |
| 2149 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2150 | /** |
| 2151 | * @brief Process standard CBOR tags whose content is a string |
| 2152 | * |
| 2153 | * @param[in] uTag The tag. |
| 2154 | * @param[in,out] pDecodedItem The data item. |
| 2155 | * |
| 2156 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2157 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
| 2158 | * \ref QCBOR_ERR_BAD_OPT_TAG if the content type was wrong for the tag. |
| 2159 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2160 | * Process the CBOR tags that whose content is a byte string or a text |
| 2161 | * string and for which the string is just passed on to the caller. |
| 2162 | * |
| 2163 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2164 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2165 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2166 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2167 | */ |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2168 | static inline QCBORError |
| 2169 | ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2170 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2171 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2172 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2173 | return QCBOR_ERR_UNSUPPORTED; |
| 2174 | } |
| 2175 | |
| 2176 | unsigned uIndex; |
| 2177 | for(uIndex = 0; StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2178 | if(StringTagMap[uIndex].uTagNumber == uTag) { |
| 2179 | break; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | const uint8_t uQCBORType = StringTagMap[uIndex].uQCBORtype; |
| 2184 | if(uQCBORType == QCBOR_TYPE_NONE) { |
| 2185 | /* repurpose this error to mean, not handled here */ |
| 2186 | return QCBOR_ERR_UNSUPPORTED; |
| 2187 | } |
| 2188 | |
| 2189 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2190 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2191 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2192 | } |
| 2193 | |
| 2194 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2195 | return QCBOR_ERR_BAD_OPT_TAG; |
| 2196 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2197 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2198 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2199 | return QCBOR_SUCCESS; |
| 2200 | } |
| 2201 | |
| 2202 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2203 | /** |
| 2204 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2205 | * |
| 2206 | * @param[in] pMe The decode context. |
| 2207 | * @param[out] pDecodedItem The decoded item. |
| 2208 | * |
| 2209 | * @return Decoding error code. |
| 2210 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2211 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2212 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2213 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2214 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2215 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2216 | static QCBORError |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2217 | QCBORDecode_GetNextTag(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2218 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2219 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2220 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2221 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2222 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2223 | goto Done; |
| 2224 | } |
| 2225 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2226 | /* When there are no tag numbers for the item, this exits first |
| 2227 | * thing and effectively does nothing. |
| 2228 | * |
| 2229 | * This loops over all the tag numbers accumulated for this item |
| 2230 | * trying to decode and interpret them. This stops at the end of |
| 2231 | * the list or at the first tag number that can't be interpreted by |
| 2232 | * this code. This is effectively a recursive processing of the |
| 2233 | * tags number list that handles nested tags. |
| 2234 | */ |
| 2235 | while(1) { |
| 2236 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2237 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2238 | */ |
| 2239 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2240 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2241 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2242 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2243 | break; |
| 2244 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2245 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2246 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2247 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2248 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2249 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2250 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2251 | uReturn = QCBORDecode_MantissaAndExponent(pMe, pDecodedItem); |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2252 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2253 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2254 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2255 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2256 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2257 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2258 | } else { |
| 2259 | /* See if it is a pass-through byte/text string tag; process if so */ |
| 2260 | uReturn = ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2261 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2262 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
| 2263 | /* It wasn't a pass-through byte/text string tag so it is |
| 2264 | * an unknown tag. This is the exit from the loop on the |
| 2265 | * first unknown tag. It is a successful exit. |
| 2266 | */ |
| 2267 | uReturn = QCBOR_SUCCESS; |
| 2268 | break; |
| 2269 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2270 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2271 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2272 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2273 | /* Error exit from the loop */ |
| 2274 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2275 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2276 | |
| 2277 | /* A tag was successfully processed, shift it out of the list of |
| 2278 | * tags returned. This is the loop increment. |
| 2279 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2280 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2284 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2288 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2289 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2290 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2291 | QCBORError |
| 2292 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2293 | { |
| 2294 | QCBORError uErr; |
| 2295 | uErr = QCBORDecode_GetNextTag(pMe, pDecodedItem); |
| 2296 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2297 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2298 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2299 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2300 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | |
| 2304 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2305 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2306 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2307 | QCBORError |
| 2308 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2309 | { |
| 2310 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2311 | const UsefulInputBuf Save = pMe->InBuf; |
| 2312 | |
| 2313 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2314 | |
| 2315 | pMe->nesting = SaveNesting; |
| 2316 | pMe->InBuf = Save; |
| 2317 | |
| 2318 | return uErr; |
| 2319 | } |
| 2320 | |
| 2321 | |
| 2322 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2323 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2324 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2325 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2326 | { |
| 2327 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2328 | return; |
| 2329 | } |
| 2330 | |
| 2331 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2336 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2337 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2338 | QCBORError |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2339 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2340 | QCBORItem *pDecodedItem, |
| 2341 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2342 | { |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2343 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2344 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2345 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2346 | if(uReturn != QCBOR_SUCCESS) { |
| 2347 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | if(pTags != NULL) { |
| 2351 | pTags->uNumUsed = 0; |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2352 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2353 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2354 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2355 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2356 | } |
| 2357 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2358 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2359 | } |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2360 | pTags->puTags[pTags->uNumUsed] = UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2361 | pTags->uNumUsed++; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2369 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2370 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2371 | */ |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2372 | bool QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2373 | const QCBORItem *pItem, |
| 2374 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2375 | { |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2376 | for(int uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2377 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2378 | break; |
| 2379 | } |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2380 | if(UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2381 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2382 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2383 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2384 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2385 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | |
| 2389 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2390 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2391 | */ |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2392 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *pMe) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2393 | { |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2394 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2395 | |
| 2396 | if(uReturn != QCBOR_SUCCESS) { |
| 2397 | goto Done; |
| 2398 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2399 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2400 | /* Error out if all the maps/arrays are not closed out */ |
| 2401 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2402 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2403 | goto Done; |
| 2404 | } |
| 2405 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2406 | /* Error out if not all the bytes are consumed */ |
| 2407 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2408 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2409 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2410 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2411 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2412 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2413 | /* Call the destructor for the string allocator if there is one. |
| 2414 | * Always called, even if there are errors; always have to clean up. |
| 2415 | */ |
| 2416 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2417 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2418 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2419 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2423 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2424 | * Public function, see header qcbor/qcbor_decode.h file |
| 2425 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2426 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2427 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2428 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2429 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2430 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2431 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2432 | return CBOR_TAG_INVALID64; |
| 2433 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2434 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2435 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2436 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 2437 | return UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2441 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2442 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame^] | 2443 | * Public function, see header qcbor/qcbor_decode.h file |
| 2444 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2445 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2446 | uint32_t uIndex) |
| 2447 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2448 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2449 | return CBOR_TAG_INVALID64; |
| 2450 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2451 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2452 | return CBOR_TAG_INVALID64; |
| 2453 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 2454 | return UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2455 | } |
| 2456 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2457 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2458 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2459 | |
| 2460 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2461 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2462 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2463 | /* =========================================================================== |
| 2464 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2465 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2466 | This implements a simple sting allocator for indefinite length |
| 2467 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2468 | implements the function type QCBORStringAllocate and allows easy |
| 2469 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2470 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2471 | This particular allocator is built-in for convenience. The caller |
| 2472 | can implement their own. All of this following code will get |
| 2473 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2474 | |
| 2475 | This is a very primitive memory allocator. It does not track |
| 2476 | individual allocations, only a high-water mark. A free or |
| 2477 | reallocation must be of the last chunk allocated. |
| 2478 | |
| 2479 | The size of the pool and offset to free memory are packed into the |
| 2480 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2481 | the decode context. Since the address of the pool may not be |
| 2482 | aligned, they have to be packed and unpacked as if they were |
| 2483 | serialized data of the wire or such. |
| 2484 | |
| 2485 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2486 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2487 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2488 | |
| 2489 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2490 | static inline int |
| 2491 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2492 | { |
| 2493 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2494 | UsefulInputBuf UIB; |
| 2495 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2496 | // Just assume the size here. It was checked during SetUp so |
| 2497 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2498 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2499 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2500 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2501 | return UsefulInputBuf_GetError(&UIB); |
| 2502 | } |
| 2503 | |
| 2504 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2505 | static inline int |
| 2506 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2507 | { |
| 2508 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2509 | // length check performed here is useful. |
| 2510 | UsefulOutBuf UOB; |
| 2511 | |
| 2512 | UsefulOutBuf_Init(&UOB, Pool); |
| 2513 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2514 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2515 | return UsefulOutBuf_GetError(&UOB); |
| 2516 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2517 | |
| 2518 | |
| 2519 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2520 | Internal function for an allocation, reallocation free and destuct. |
| 2521 | |
| 2522 | Having only one function rather than one each per mode saves space in |
| 2523 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2524 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2525 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2526 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2527 | static UsefulBuf |
| 2528 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2529 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2530 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2531 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2532 | uint32_t uPoolSize; |
| 2533 | uint32_t uFreeOffset; |
| 2534 | |
| 2535 | if(uNewSize > UINT32_MAX) { |
| 2536 | // This allocator is only good up to 4GB. This check should |
| 2537 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2538 | goto Done; |
| 2539 | } |
| 2540 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2541 | |
| 2542 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2543 | goto Done; |
| 2544 | } |
| 2545 | |
| 2546 | if(uNewSize) { |
| 2547 | if(pMem) { |
| 2548 | // REALLOCATION MODE |
| 2549 | // Calculate pointer to the end of the memory pool. It is |
| 2550 | // assumed that pPool + uPoolSize won't wrap around by |
| 2551 | // assuming the caller won't pass a pool buffer in that is |
| 2552 | // not in legitimate memory space. |
| 2553 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2554 | |
| 2555 | // Check that the pointer for reallocation is in the range of the |
| 2556 | // pool. This also makes sure that pointer math further down |
| 2557 | // doesn't wrap under or over. |
| 2558 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2559 | // Offset to start of chunk for reallocation. This won't |
| 2560 | // wrap under because of check that pMem >= pPool. Cast |
| 2561 | // is safe because the pool is always less than UINT32_MAX |
| 2562 | // because of check in QCBORDecode_SetMemPool(). |
| 2563 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2564 | |
| 2565 | // Check to see if the allocation will fit. uPoolSize - |
| 2566 | // uMemOffset will not wrap under because of check that |
| 2567 | // pMem is in the range of the uPoolSize by check above. |
| 2568 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2569 | ReturnValue.ptr = pMem; |
| 2570 | ReturnValue.len = uNewSize; |
| 2571 | |
| 2572 | // Addition won't wrap around over because uNewSize was |
| 2573 | // checked to be sure it is less than the pool size. |
| 2574 | uFreeOffset = uMemOffset + uNewSize32; |
| 2575 | } |
| 2576 | } |
| 2577 | } else { |
| 2578 | // ALLOCATION MODE |
| 2579 | // uPoolSize - uFreeOffset will not underflow because this |
| 2580 | // pool implementation makes sure uFreeOffset is always |
| 2581 | // smaller than uPoolSize through this check here and |
| 2582 | // reallocation case. |
| 2583 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2584 | ReturnValue.len = uNewSize; |
| 2585 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2586 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2587 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2588 | } |
| 2589 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2590 | if(pMem) { |
| 2591 | // FREE MODE |
| 2592 | // Cast is safe because of limit on pool size in |
| 2593 | // QCBORDecode_SetMemPool() |
| 2594 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2595 | } else { |
| 2596 | // DESTRUCT MODE |
| 2597 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2598 | } |
| 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 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2602 | MemPool_Pack(Pool, uFreeOffset); |
| 2603 | |
| 2604 | Done: |
| 2605 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2608 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2609 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2610 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2611 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2612 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2613 | UsefulBuf Pool, |
| 2614 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2615 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2616 | // The pool size and free mem offset are packed into the beginning |
| 2617 | // of the pool memory. This compile time check make sure the |
| 2618 | // constant in the header is correct. This check should optimize |
| 2619 | // down to nothing. |
| 2620 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2621 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2622 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2623 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2624 | // The pool size and free offset packed in to the beginning of pool |
| 2625 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2626 | // machines. |
| 2627 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2628 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2629 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2630 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2631 | // This checks that the pool buffer given is big enough. |
| 2632 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2633 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2634 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2635 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2636 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2637 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2638 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2639 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2640 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2641 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2642 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2643 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2644 | |
| 2645 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2646 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2647 | { |
| 2648 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2649 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2650 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2651 | |
| 2652 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2653 | Consume an entire map or array (and do next to |
| 2654 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2655 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2656 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2657 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2658 | const QCBORItem *pItemToConsume, |
| 2659 | uint_fast8_t *puNextNestLevel) |
| 2660 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2661 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2662 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2663 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2664 | // If it is a map or array, this will tell if it is empty. |
| 2665 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2666 | |
| 2667 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2668 | /* 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] | 2669 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2670 | /* This works for definite and indefinite length |
| 2671 | * maps and arrays by using the nesting level |
| 2672 | */ |
| 2673 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2674 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2675 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2676 | goto Done; |
| 2677 | } |
| 2678 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2679 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2680 | *puNextNestLevel = Item.uNextNestLevel; |
| 2681 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2682 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2683 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2684 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2685 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2686 | /* Just pass the nesting level through */ |
| 2687 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2688 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2689 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2690 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2691 | |
| 2692 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2693 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2697 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2698 | Works only for integer and string labels. Returns false |
| 2699 | for any other type. */ |
| 2700 | static inline bool |
| 2701 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2702 | { |
| 2703 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2704 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2705 | return true; |
| 2706 | } |
| 2707 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2708 | 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] | 2709 | return true; |
| 2710 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2711 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2712 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2713 | return true; |
| 2714 | } |
| 2715 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2716 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2717 | return true; |
| 2718 | } |
| 2719 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2720 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2721 | /* Other label types are never matched */ |
| 2722 | return false; |
| 2723 | } |
| 2724 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2725 | |
| 2726 | /* |
| 2727 | Returns true if Item1 and Item2 are the same type |
| 2728 | or if either are of QCBOR_TYPE_ANY. |
| 2729 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2730 | static inline bool |
| 2731 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2732 | { |
| 2733 | if(Item1.uDataType == Item2.uDataType) { |
| 2734 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2735 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2736 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2737 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2738 | return true; |
| 2739 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2740 | return false; |
| 2741 | } |
| 2742 | |
| 2743 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2744 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2745 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2746 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2747 | @param[in] pMe The decode context to search. |
| 2748 | @param[in,out] pItemArray The items to search for and the items found. |
| 2749 | @param[out] puOffset Byte offset of last item matched. |
| 2750 | @param[in] pCBContext Context for the not-found item call back. |
| 2751 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2752 | |
| 2753 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2754 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2755 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2756 | were found for one of the labels being |
| 2757 | search for. This duplicate detection is |
| 2758 | only performed for items in pItemArray, |
| 2759 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2760 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2761 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2762 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2763 | |
| 2764 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2765 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2766 | On input pItemArray contains a list of labels and data types |
| 2767 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2768 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2769 | On output the fully retrieved items are filled in with |
| 2770 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2771 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2772 | 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] | 2773 | |
| 2774 | 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] | 2775 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2776 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2777 | MapSearch(QCBORDecodeContext *pMe, |
| 2778 | QCBORItem *pItemArray, |
| 2779 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2780 | void *pCBContext, |
| 2781 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2782 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2783 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2784 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2785 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2786 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2787 | uReturn = pMe->uLastError; |
| 2788 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2789 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2790 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2791 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2792 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2793 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2794 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2795 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2796 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2797 | } |
| 2798 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2799 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2800 | // It is an empty bounded array or map |
| 2801 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2802 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2803 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2804 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2805 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2806 | // Nothing is ever found in an empty array or map. All items |
| 2807 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2808 | uReturn = QCBOR_SUCCESS; |
| 2809 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2810 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2811 | } |
| 2812 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2813 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2814 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2815 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2816 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2817 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2818 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2819 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2820 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2821 | Loop over all the items in the map or array. Each item |
| 2822 | could be a map or array, but label matching is only at |
| 2823 | the main level. This handles definite and indefinite |
| 2824 | length maps and arrays. The only reason this is ever |
| 2825 | called on arrays is to find their end position. |
| 2826 | |
| 2827 | This will always run over all items in order to do |
| 2828 | duplicate detection. |
| 2829 | |
| 2830 | This will exit with failure if it encounters an |
| 2831 | unrecoverable error, but continue on for recoverable |
| 2832 | errors. |
| 2833 | |
| 2834 | If a recoverable error occurs on a matched item, then |
| 2835 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2836 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2837 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2838 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2839 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2840 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2841 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2842 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2843 | /* Get the item */ |
| 2844 | QCBORItem Item; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2845 | QCBORError uResult = QCBORDecode_GetNextTag(pMe, &Item); |
| 2846 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2847 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2848 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2849 | goto Done; |
| 2850 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2851 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2852 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2853 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2854 | goto Done; |
| 2855 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2856 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2857 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2858 | bool bMatched = false; |
| 2859 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 2860 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2861 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2862 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2863 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2864 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2865 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2866 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2867 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2868 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2869 | goto Done; |
| 2870 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2871 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2872 | if(uResult != QCBOR_SUCCESS) { |
| 2873 | uReturn = uResult; |
| 2874 | goto Done; |
| 2875 | } |
| 2876 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2877 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2878 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2879 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2880 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2881 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2882 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2883 | bMatched = true; |
| 2884 | } |
| 2885 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2886 | |
| 2887 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2888 | if(!bMatched && pfCallback != NULL) { |
| 2889 | /* |
| 2890 | Call the callback on unmatched labels. |
| 2891 | (It is tempting to do duplicate detection here, but that would |
| 2892 | require dynamic memory allocation because the number of labels |
| 2893 | that might be encountered is unbounded.) |
| 2894 | */ |
| 2895 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2896 | if(uReturn != QCBOR_SUCCESS) { |
| 2897 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2898 | } |
| 2899 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2900 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2901 | /* |
| 2902 | Consume the item whether matched or not. This |
| 2903 | does the work of traversing maps and array and |
| 2904 | everything in them. In this loop only the |
| 2905 | items at the current nesting level are examined |
| 2906 | to match the labels. |
| 2907 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2908 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2909 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2910 | goto Done; |
| 2911 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2912 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2913 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2914 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2915 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2916 | |
| 2917 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2918 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2919 | // Check here makes sure that this won't accidentally be |
| 2920 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2921 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 2922 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 2923 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2924 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 2925 | goto Done; |
| 2926 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2927 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 2928 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2929 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2930 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2931 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 2932 | |
| 2933 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2934 | /* 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] | 2935 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2936 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2937 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 2938 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2939 | } |
| 2940 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2941 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2942 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2943 | } |
| 2944 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2945 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2946 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2947 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2948 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2949 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 2950 | int64_t nLabel, |
| 2951 | uint8_t uQcborType, |
| 2952 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2953 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2954 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2955 | return; |
| 2956 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2957 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2958 | QCBORItem OneItemSeach[2]; |
| 2959 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2960 | OneItemSeach[0].label.int64 = nLabel; |
| 2961 | OneItemSeach[0].uDataType = uQcborType; |
| 2962 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2963 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2964 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2965 | |
| 2966 | *pItem = OneItemSeach[0]; |
| 2967 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2968 | if(uReturn != QCBOR_SUCCESS) { |
| 2969 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2970 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2971 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2972 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2973 | } |
| 2974 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2975 | Done: |
| 2976 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2980 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2981 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2982 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2983 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 2984 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2985 | uint8_t uQcborType, |
| 2986 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2987 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2988 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2989 | return; |
| 2990 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2991 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2992 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2993 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2994 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2995 | OneItemSeach[0].uDataType = uQcborType; |
| 2996 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2997 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2998 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2999 | if(uReturn != QCBOR_SUCCESS) { |
| 3000 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3001 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3002 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3003 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3004 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3005 | } |
| 3006 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3007 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3008 | |
| 3009 | Done: |
| 3010 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3014 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3015 | static QCBORError |
| 3016 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3017 | { |
| 3018 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 3019 | if(uDataType == puTypeList[i]) { |
| 3020 | return QCBOR_SUCCESS; |
| 3021 | } |
| 3022 | } |
| 3023 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3024 | } |
| 3025 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3026 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3027 | /** |
| 3028 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3029 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3030 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3031 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3032 | @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] | 3033 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3034 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered |
| 3035 | tag value. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3036 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3037 | static QCBORError |
| 3038 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3039 | { |
| 3040 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3041 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3042 | /* There are tags that QCBOR couldn't process on this item and |
| 3043 | the caller has told us there should not be. */ |
| 3044 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3045 | } |
| 3046 | |
| 3047 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3048 | const int nItemType = pItem->uDataType; |
| 3049 | |
| 3050 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3051 | // Must match the tag and only the tag |
| 3052 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3053 | } |
| 3054 | |
| 3055 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3056 | if(uReturn == QCBOR_SUCCESS) { |
| 3057 | return QCBOR_SUCCESS; |
| 3058 | } |
| 3059 | |
| 3060 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3061 | /* Must match the content type and only the content type. |
| 3062 | There was no match just above so it is a fail. */ |
| 3063 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3064 | } |
| 3065 | |
| 3066 | /* If here it can match either the tag or the content |
| 3067 | and it hasn't matched the content, so the end |
| 3068 | result is whether it matches the tag. This is |
| 3069 | also the case that the CBOR standard discourages. */ |
| 3070 | |
| 3071 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3072 | } |
| 3073 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3074 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3075 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3076 | // This could be semi-private if need be |
| 3077 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3078 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3079 | int64_t nLabel, |
| 3080 | TagSpecification TagSpec, |
| 3081 | QCBORItem *pItem) |
| 3082 | { |
| 3083 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3084 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3085 | return; |
| 3086 | } |
| 3087 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3088 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3089 | } |
| 3090 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3091 | |
| 3092 | // This could be semi-private if need be |
| 3093 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3094 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3095 | const char *szLabel, |
| 3096 | TagSpecification TagSpec, |
| 3097 | QCBORItem *pItem) |
| 3098 | { |
| 3099 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3100 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3101 | return; |
| 3102 | } |
| 3103 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3104 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3107 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3108 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3109 | int64_t nLabel, |
| 3110 | TagSpecification TagSpec, |
| 3111 | UsefulBufC *pString) |
| 3112 | { |
| 3113 | QCBORItem Item; |
| 3114 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3115 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3116 | *pString = Item.val.string; |
| 3117 | } |
| 3118 | } |
| 3119 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3120 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3121 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3122 | const char * szLabel, |
| 3123 | TagSpecification TagSpec, |
| 3124 | UsefulBufC *pString) |
| 3125 | { |
| 3126 | QCBORItem Item; |
| 3127 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3128 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3129 | *pString = Item.val.string; |
| 3130 | } |
| 3131 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3132 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3133 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3134 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3135 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3136 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3137 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3138 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 3139 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3140 | } |
| 3141 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3142 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3143 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3144 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3145 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3146 | QCBORItem *pItemList, |
| 3147 | void *pCallbackCtx, |
| 3148 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3149 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3150 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 3151 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3155 | /** |
| 3156 | * @brief Search for a map/array by label and enter it |
| 3157 | * |
| 3158 | * @param[in] pMe The decode context. |
| 3159 | * @param[in] pSearch The map/array to search for. |
| 3160 | * |
| 3161 | * @c pSearch is expected to contain one item of type map or array |
| 3162 | * with the label specified. The current bounded map will be searched for |
| 3163 | * this and if found will be entered. |
| 3164 | * |
| 3165 | * If the label is not found, or the item found is not a map or array, |
| 3166 | * the error state is set. |
| 3167 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3168 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3169 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3170 | // The first item in pSearch is the one that is to be |
| 3171 | // entered. It should be the only one filled in. Any other |
| 3172 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3173 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3174 | return; |
| 3175 | } |
| 3176 | |
| 3177 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3178 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3179 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3180 | return; |
| 3181 | } |
| 3182 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3183 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3184 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3185 | return; |
| 3186 | } |
| 3187 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3188 | /* |
| 3189 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3190 | * next item for the pre-order traversal cursor to be the map/array |
| 3191 | * found by MapSearch(). The next few lines of code force the |
| 3192 | * cursor to that. |
| 3193 | * |
| 3194 | * There is no need to retain the old cursor because |
| 3195 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3196 | * beginning of the map/array being entered. |
| 3197 | * |
| 3198 | * The cursor is forced by: 1) setting the input buffer position to |
| 3199 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3200 | * counter to the total in the map/array, 3) setting the nesting |
| 3201 | * level. Setting the map/array counter to the total is not |
| 3202 | * strictly correct, but this is OK because this cursor only needs |
| 3203 | * to be used to get one item and MapSearch() has already found it |
| 3204 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3205 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3206 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3207 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3208 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3209 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3210 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3211 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3212 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3216 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3217 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3218 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3219 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3220 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3221 | QCBORItem OneItemSeach[2]; |
| 3222 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3223 | OneItemSeach[0].label.int64 = nLabel; |
| 3224 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3225 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3226 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3227 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3228 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3232 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3233 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3234 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3235 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3236 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3237 | QCBORItem OneItemSeach[2]; |
| 3238 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3239 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3240 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3241 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3242 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3243 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3246 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3247 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3248 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3249 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3250 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3251 | QCBORItem OneItemSeach[2]; |
| 3252 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3253 | OneItemSeach[0].label.int64 = nLabel; |
| 3254 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3255 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3256 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3257 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3260 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3261 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3262 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3263 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3264 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3265 | QCBORItem OneItemSeach[2]; |
| 3266 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3267 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3268 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3269 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3270 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3271 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3275 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3276 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3277 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3278 | QCBORError uErr; |
| 3279 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3280 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3281 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3282 | // Already in error state; do nothing. |
| 3283 | return; |
| 3284 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3285 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3286 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3287 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3288 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3289 | if(uErr != QCBOR_SUCCESS) { |
| 3290 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3291 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3292 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3293 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3294 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3297 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3298 | |
| 3299 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3300 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3301 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3302 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3303 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3304 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3305 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3306 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3307 | // Special case to increment nesting level for zero-length maps |
| 3308 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3309 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3310 | } |
| 3311 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3312 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3313 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3314 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3315 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3316 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3317 | if(pItem != NULL) { |
| 3318 | *pItem = Item; |
| 3319 | } |
| 3320 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3321 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3322 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3323 | } |
| 3324 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3325 | |
| 3326 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3327 | This is the common work for exiting a level that is a bounded map, |
| 3328 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3329 | |
| 3330 | One chunk of work is to set up the pre-order traversal so it is at |
| 3331 | the item just after the bounded map, array or bstr that is being |
| 3332 | exited. This is somewhat complex. |
| 3333 | |
| 3334 | The other work is to level-up the bounded mode to next higest bounded |
| 3335 | mode or the top level if there isn't one. |
| 3336 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3337 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3338 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3339 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3340 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3341 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3342 | /* |
| 3343 | First the pre-order-traversal byte offset is positioned to the |
| 3344 | item just after the bounded mode item that was just consumed. |
| 3345 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3346 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3347 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3348 | /* |
| 3349 | Next, set the current nesting level to one above the bounded level |
| 3350 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3351 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3352 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3353 | makes sure pCurrentBounded is valid. |
| 3354 | */ |
| 3355 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3356 | |
| 3357 | /* |
| 3358 | This does the complex work of leveling up the pre-order traversal |
| 3359 | when the end of a map or array or another bounded level is |
| 3360 | reached. It may do nothing, or ascend all the way to the top |
| 3361 | level. |
| 3362 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 3363 | uErr = NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3364 | if(uErr != QCBOR_SUCCESS) { |
| 3365 | goto Done; |
| 3366 | } |
| 3367 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3368 | /* |
| 3369 | This makes the next highest bounded level the current bounded |
| 3370 | level. If there is no next highest level, then no bounded mode is |
| 3371 | in effect. |
| 3372 | */ |
| 3373 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3374 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3375 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3376 | |
| 3377 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3378 | return uErr; |
| 3379 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3380 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3381 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3382 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3383 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3384 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3385 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3386 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3387 | return; |
| 3388 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3389 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3390 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3391 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3392 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3393 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3394 | goto Done; |
| 3395 | } |
| 3396 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3397 | /* |
| 3398 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3399 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3400 | from previous map search, then do a dummy search. |
| 3401 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3402 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3403 | QCBORItem Dummy; |
| 3404 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3405 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3406 | if(uErr != QCBOR_SUCCESS) { |
| 3407 | goto Done; |
| 3408 | } |
| 3409 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3410 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3411 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3412 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3413 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3414 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3418 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3419 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3420 | const QCBORItem *pItem, |
| 3421 | uint8_t uTagRequirement, |
| 3422 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3423 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3424 | if(pBstr) { |
| 3425 | *pBstr = NULLUsefulBufC; |
| 3426 | } |
| 3427 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3428 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3429 | // Already in error state; do nothing. |
| 3430 | return pMe->uLastError; |
| 3431 | } |
| 3432 | |
| 3433 | QCBORError uError = QCBOR_SUCCESS; |
| 3434 | |
| 3435 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3436 | uError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3437 | goto Done;; |
| 3438 | } |
| 3439 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3440 | const TagSpecification TagSpec = |
| 3441 | { |
| 3442 | uTagRequirement, |
| 3443 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3444 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3445 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3446 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3447 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3448 | if(uError != QCBOR_SUCCESS) { |
| 3449 | goto Done; |
| 3450 | } |
| 3451 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3452 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3453 | // Reverse the decrement done by GetNext() for the bstr so the |
| 3454 | // increment in NestLevelAscender() called by ExitBoundedLevel() |
| 3455 | // will work right. |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3456 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3457 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3458 | |
| 3459 | if(pBstr) { |
| 3460 | *pBstr = pItem->val.string; |
| 3461 | } |
| 3462 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3463 | // This saves the current length of the UsefulInputBuf and then |
| 3464 | // narrows the UsefulInputBuf to start and length of the wrapped |
| 3465 | // CBOR that is being entered. |
| 3466 | // |
| 3467 | // This makes sure the length is less than |
| 3468 | // QCBOR_MAX_DECODE_INPUT_SIZE which is slighly less than |
| 3469 | // UINT32_MAX. The value UINT32_MAX is used as a special indicator |
| 3470 | // value. The checks against QCBOR_MAX_DECODE_INPUT_SIZE also make |
| 3471 | // the casts safe. uEndOfBstr will always be less than |
| 3472 | // uPreviousLength because of the way UsefulInputBuf works so there |
| 3473 | // is no need to check it. There is also a range check in the |
| 3474 | // seek. |
| 3475 | // |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3476 | // Most of these calls are simple inline accessors so this doesn't |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3477 | // amount to much code. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3478 | // 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] | 3479 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3480 | if((uint32_t)uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3481 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3482 | goto Done; |
| 3483 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3484 | const size_t uEndOfBstr = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3485 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOfBstr - pItem->val.string.len); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3486 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3487 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3488 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3489 | (uint32_t)uPreviousLength, |
| 3490 | (uint32_t)uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3491 | Done: |
| 3492 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3496 | /* |
| 3497 | Public function, see header qcbor/qcbor_decode.h file |
| 3498 | */ |
| 3499 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3500 | uint8_t uTagRequirement, |
| 3501 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3502 | { |
| 3503 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3504 | // Already in error state; do nothing. |
| 3505 | return; |
| 3506 | } |
| 3507 | |
| 3508 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3509 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3510 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3511 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3512 | return; |
| 3513 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3514 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3515 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3516 | &Item, |
| 3517 | uTagRequirement, |
| 3518 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3522 | /* |
| 3523 | Public function, see header qcbor/qcbor_decode.h file |
| 3524 | */ |
| 3525 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3526 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3527 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3528 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3529 | { |
| 3530 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3531 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3532 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3533 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3534 | &Item, |
| 3535 | uTagRequirement, |
| 3536 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
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 | /* |
| 3541 | Public function, see header qcbor/qcbor_decode.h file |
| 3542 | */ |
| 3543 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3544 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3545 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3546 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3547 | { |
| 3548 | QCBORItem Item; |
| 3549 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3550 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3551 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3552 | &Item, |
| 3553 | uTagRequirement, |
| 3554 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3555 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3556 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3557 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3558 | /* |
| 3559 | Public function, see header qcbor/qcbor_decode.h file |
| 3560 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3561 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3562 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3563 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3564 | // Already in error state; do nothing. |
| 3565 | return; |
| 3566 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3567 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3568 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3569 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3570 | return; |
| 3571 | } |
| 3572 | |
| 3573 | /* |
| 3574 | Reset the length of the UsefulInputBuf to what it was before |
| 3575 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3576 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3577 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3578 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3579 | |
| 3580 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3581 | QCBORError uErr = ExitBoundedLevel(pMe, DecodeNesting_GetEndOfBstr(&(pMe->nesting))); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3582 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3585 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3586 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3587 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3588 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3589 | |
Laurence Lundblade | 11a064e | 2020-05-07 13:13:42 -0700 | [diff] [blame] | 3590 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3591 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3592 | static QCBORError |
| 3593 | InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3594 | { |
| 3595 | switch(pItem->uDataType) { |
| 3596 | case QCBOR_TYPE_TRUE: |
| 3597 | *pBool = true; |
| 3598 | return QCBOR_SUCCESS; |
| 3599 | break; |
| 3600 | |
| 3601 | case QCBOR_TYPE_FALSE: |
| 3602 | *pBool = false; |
| 3603 | return QCBOR_SUCCESS; |
| 3604 | break; |
| 3605 | |
| 3606 | default: |
| 3607 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3608 | break; |
| 3609 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3610 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3611 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3612 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3613 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3614 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3615 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3616 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3617 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3618 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3619 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3620 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3621 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3622 | return; |
| 3623 | } |
| 3624 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3625 | QCBORError nError; |
| 3626 | QCBORItem Item; |
| 3627 | |
| 3628 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3629 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3630 | pMe->uLastError = (uint8_t)nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3631 | return; |
| 3632 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3633 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3634 | } |
| 3635 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3636 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3637 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3638 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3639 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3640 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3641 | { |
| 3642 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3643 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3644 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3645 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3648 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3649 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3650 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3651 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3652 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3653 | { |
| 3654 | QCBORItem Item; |
| 3655 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3656 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3657 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | |
| 3661 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3662 | |
| 3663 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3664 | QCBORItem *pItem, |
| 3665 | uint8_t uTagRequirement, |
| 3666 | int64_t *pnTime) |
| 3667 | { |
| 3668 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3669 | // Already in error state, do nothing |
| 3670 | return; |
| 3671 | } |
| 3672 | |
| 3673 | QCBORError uErr; |
| 3674 | |
| 3675 | const TagSpecification TagSpec = |
| 3676 | { |
| 3677 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3678 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3679 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3680 | }; |
| 3681 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3682 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3683 | if(uErr != QCBOR_SUCCESS) { |
| 3684 | goto Done; |
| 3685 | } |
| 3686 | |
| 3687 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3688 | uErr = DecodeDateEpoch(pItem); |
| 3689 | if(uErr != QCBOR_SUCCESS) { |
| 3690 | goto Done; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3694 | // Save the tags in the last item's tags in the decode context |
| 3695 | // for QCBORDecode_GetNthTagOfLast() |
| 3696 | CopyTags(pMe, pItem); |
| 3697 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3698 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3699 | |
| 3700 | Done: |
| 3701 | pMe->uLastError = (uint8_t)uErr; |
| 3702 | } |
| 3703 | |
| 3704 | |
| 3705 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3706 | uint8_t uTagRequirement, |
| 3707 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3708 | { |
| 3709 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3710 | // Already in error state, do nothing |
| 3711 | return; |
| 3712 | } |
| 3713 | |
| 3714 | QCBORItem Item; |
| 3715 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3716 | |
| 3717 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3718 | } |
| 3719 | |
| 3720 | |
| 3721 | void |
| 3722 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3723 | int64_t nLabel, |
| 3724 | uint8_t uTagRequirement, |
| 3725 | int64_t *pnTime) |
| 3726 | { |
| 3727 | QCBORItem Item; |
| 3728 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3729 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3730 | } |
| 3731 | |
| 3732 | |
| 3733 | void |
| 3734 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3735 | const char *szLabel, |
| 3736 | uint8_t uTagRequirement, |
| 3737 | int64_t *pnTime) |
| 3738 | { |
| 3739 | QCBORItem Item; |
| 3740 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3741 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3742 | } |
| 3743 | |
| 3744 | |
| 3745 | |
| 3746 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3747 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3748 | TagSpecification TagSpec, |
| 3749 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3750 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3751 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3752 | // Already in error state, do nothing |
| 3753 | return; |
| 3754 | } |
| 3755 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3756 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3757 | QCBORItem Item; |
| 3758 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3759 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3760 | if(uError != QCBOR_SUCCESS) { |
| 3761 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3762 | return; |
| 3763 | } |
| 3764 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3765 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3766 | |
| 3767 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3768 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 3769 | } else { |
| 3770 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3771 | } |
| 3772 | } |
| 3773 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3774 | |
| 3775 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3776 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3777 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3778 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3779 | UsefulBufC *pValue, |
| 3780 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3781 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3782 | const TagSpecification TagSpec = |
| 3783 | { |
| 3784 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3785 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3786 | {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] | 3787 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3788 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3789 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3790 | if(uErr != QCBOR_SUCCESS) { |
| 3791 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3792 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3793 | |
| 3794 | *pValue = pItem->val.string; |
| 3795 | |
| 3796 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3797 | *pbIsNegative = false; |
| 3798 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3799 | *pbIsNegative = true; |
| 3800 | } |
| 3801 | |
| 3802 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3806 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3807 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3808 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3809 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3810 | uint8_t uTagRequirement, |
| 3811 | UsefulBufC *pValue, |
| 3812 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3813 | { |
| 3814 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3815 | // Already in error state, do nothing |
| 3816 | return; |
| 3817 | } |
| 3818 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3819 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3820 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3821 | if(uError != QCBOR_SUCCESS) { |
| 3822 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3823 | return; |
| 3824 | } |
| 3825 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3826 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3829 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3830 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3831 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3832 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3833 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3834 | int64_t nLabel, |
| 3835 | uint8_t uTagRequirement, |
| 3836 | UsefulBufC *pValue, |
| 3837 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3838 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3839 | QCBORItem Item; |
| 3840 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3841 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3842 | return; |
| 3843 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3844 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3845 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3846 | } |
| 3847 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3848 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3849 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3850 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3851 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3852 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3853 | const char *szLabel, |
| 3854 | uint8_t uTagRequirement, |
| 3855 | UsefulBufC *pValue, |
| 3856 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3857 | { |
| 3858 | QCBORItem Item; |
| 3859 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3860 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3861 | return; |
| 3862 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3863 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3864 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | |
| 3868 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3869 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3870 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3871 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3872 | const QCBORItem *pItem, |
| 3873 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3874 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3875 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3876 | const TagSpecification TagSpecText = |
| 3877 | { |
| 3878 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3879 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3880 | {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] | 3881 | }; |
| 3882 | const TagSpecification TagSpecBinary = |
| 3883 | { |
| 3884 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3885 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3886 | {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] | 3887 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3888 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3889 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3890 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3891 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3892 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3893 | if(pbIsTag257 != NULL) { |
| 3894 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3895 | } |
| 3896 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3897 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3898 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3899 | if(pbIsTag257 != NULL) { |
| 3900 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3901 | } |
| 3902 | uReturn = QCBOR_SUCCESS; |
| 3903 | |
| 3904 | } else { |
| 3905 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3906 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3907 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3908 | return uReturn; |
| 3909 | } |
| 3910 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3911 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 3912 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3913 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3914 | |
| 3915 | |
| 3916 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3917 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3918 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3919 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3920 | |
| 3921 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3922 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3923 | static QCBORError |
| 3924 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3925 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3926 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3927 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3928 | if(uResult != 0) { |
| 3929 | /* This loop will run a maximum of 19 times because |
| 3930 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 3931 | * exit with the overflow error |
| 3932 | */ |
| 3933 | for(; nExponent > 0; nExponent--) { |
| 3934 | if(uResult > UINT64_MAX / 10) { |
| 3935 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 3936 | } |
| 3937 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3938 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3939 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3940 | for(; nExponent < 0; nExponent++) { |
| 3941 | uResult = uResult / 10; |
| 3942 | if(uResult == 0) { |
| 3943 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3944 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3945 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3946 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3947 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3948 | |
| 3949 | *puResult = uResult; |
| 3950 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3951 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3955 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3956 | static QCBORError |
| 3957 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3958 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3959 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3960 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3961 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3962 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3963 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3964 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3965 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3966 | */ |
| 3967 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3968 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3969 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3970 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3971 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3972 | nExponent--; |
| 3973 | } |
| 3974 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3975 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3976 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3977 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3978 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3979 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3980 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3981 | } |
| 3982 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3983 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3984 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3985 | return QCBOR_SUCCESS; |
| 3986 | } |
| 3987 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3988 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3989 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3990 | Compute value with signed mantissa and signed result. Works with |
| 3991 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3992 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3993 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 3994 | int64_t nExponent, |
| 3995 | int64_t *pnResult, |
| 3996 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3997 | { |
| 3998 | uint64_t uResult; |
| 3999 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4000 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4001 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4002 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 4003 | |
| 4004 | // Do the exponentiation of the positive mantissa |
| 4005 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4006 | if(uReturn) { |
| 4007 | return uReturn; |
| 4008 | } |
| 4009 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4010 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4011 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4012 | of INT64_MIN. This assumes two's compliment representation where |
| 4013 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 4014 | Trying to write -INT64_MIN doesn't work to get this because the |
| 4015 | compiler tries to work with an int64_t which can't represent |
| 4016 | -INT64_MIN. |
| 4017 | */ |
| 4018 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 4019 | |
| 4020 | // Error out if too large |
| 4021 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4022 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4023 | } |
| 4024 | |
| 4025 | // Casts are safe because of checks above |
| 4026 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 4027 | |
| 4028 | return QCBOR_SUCCESS; |
| 4029 | } |
| 4030 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4031 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4032 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4033 | Compute value with signed mantissa and unsigned result. Works with |
| 4034 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4035 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4036 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 4037 | int64_t nExponent, |
| 4038 | uint64_t *puResult, |
| 4039 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4040 | { |
| 4041 | if(nMantissa < 0) { |
| 4042 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4043 | } |
| 4044 | |
| 4045 | // Cast to unsigned is OK because of check for negative |
| 4046 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 4047 | // Exponentiation is straight forward |
| 4048 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4049 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4050 | |
| 4051 | |
| 4052 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4053 | Compute value with signed mantissa and unsigned result. Works with |
| 4054 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4055 | */ |
| 4056 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 4057 | int64_t nExponent, |
| 4058 | uint64_t *puResult, |
| 4059 | fExponentiator pfExp) |
| 4060 | { |
| 4061 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4062 | } |
| 4063 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4064 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4065 | |
| 4066 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4067 | |
| 4068 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4069 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4070 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4071 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4072 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4073 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4074 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4075 | const uint8_t *pByte = BigNum.ptr; |
| 4076 | size_t uLen = BigNum.len; |
| 4077 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4078 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4079 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4080 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4081 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4082 | } |
| 4083 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4084 | *pResult = uResult; |
| 4085 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4086 | } |
| 4087 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4088 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4089 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4090 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4091 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4092 | } |
| 4093 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4094 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4095 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4096 | { |
| 4097 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4098 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 4099 | if(uError) { |
| 4100 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4101 | } |
| 4102 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 4103 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4104 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4108 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4109 | { |
| 4110 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4111 | /* The negative integer furthest from zero for a C int64_t is |
| 4112 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4113 | negative number in CBOR is computed as -n - 1 where n is the |
| 4114 | encoded integer, where n is what is in the variable BigNum. When |
| 4115 | converting BigNum to a uint64_t, the maximum value is thus |
| 4116 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 4117 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4118 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4119 | -n - 1 <= INT64_MIN. |
| 4120 | -n - 1 <= -INT64_MAX - 1 |
| 4121 | n <= INT64_MAX. |
| 4122 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4123 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4124 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4125 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4126 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4127 | |
| 4128 | /// Now apply -n - 1. The cast is safe because |
| 4129 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4130 | // is the largest positive integer that an int64_t can |
| 4131 | // represent. */ |
| 4132 | *pnResult = -(int64_t)uResult - 1; |
| 4133 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4134 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4135 | } |
| 4136 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4137 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4138 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4139 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4140 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4141 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4142 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4143 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4144 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4145 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4146 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4147 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4148 | |
| 4149 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4150 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4151 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4152 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4153 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4154 | static QCBORError |
| 4155 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4156 | { |
| 4157 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4158 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4159 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4160 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4161 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4162 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 4163 | http://www.cplusplus.com/reference/cmath/llround/ |
| 4164 | */ |
| 4165 | // Not interested in FE_INEXACT |
| 4166 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4167 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4168 | *pnValue = llround(pItem->val.dfnum); |
| 4169 | } else { |
| 4170 | *pnValue = lroundf(pItem->val.fnum); |
| 4171 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4172 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4173 | // llround() shouldn't result in divide by zero, but catch |
| 4174 | // it here in case it unexpectedly does. Don't try to |
| 4175 | // distinguish between the various exceptions because it seems |
| 4176 | // they vary by CPU, compiler and OS. |
| 4177 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 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 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4182 | #else |
| 4183 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4184 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4185 | break; |
| 4186 | |
| 4187 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4188 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4189 | *pnValue = pItem->val.int64; |
| 4190 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4191 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4192 | } |
| 4193 | break; |
| 4194 | |
| 4195 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4196 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4197 | if(pItem->val.uint64 < INT64_MAX) { |
| 4198 | *pnValue = pItem->val.int64; |
| 4199 | } else { |
| 4200 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4201 | } |
| 4202 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4203 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4204 | } |
| 4205 | break; |
| 4206 | |
| 4207 | default: |
| 4208 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4209 | } |
| 4210 | return QCBOR_SUCCESS; |
| 4211 | } |
| 4212 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4213 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4214 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4215 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4216 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4217 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4218 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4219 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4220 | return; |
| 4221 | } |
| 4222 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4223 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4224 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4225 | if(uError) { |
| 4226 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4227 | return; |
| 4228 | } |
| 4229 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4230 | if(pItem) { |
| 4231 | *pItem = Item; |
| 4232 | } |
| 4233 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4234 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4235 | } |
| 4236 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4237 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4238 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4239 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4240 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4241 | int64_t *pnValue, |
| 4242 | QCBORItem *pItem) |
| 4243 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4244 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4245 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4246 | return; |
| 4247 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4248 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4249 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4250 | } |
| 4251 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4252 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4253 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4254 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4255 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4256 | int64_t *pnValue, |
| 4257 | QCBORItem *pItem) |
| 4258 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4259 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4260 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4261 | return; |
| 4262 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4263 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4264 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4268 | /* |
| 4269 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4270 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4271 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4272 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4273 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4274 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4275 | |
| 4276 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4277 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4278 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4279 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4280 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4281 | static QCBORError |
| 4282 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4283 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4284 | switch(pItem->uDataType) { |
| 4285 | |
| 4286 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4287 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4288 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4289 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4290 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4291 | } |
| 4292 | break; |
| 4293 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4294 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4295 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4296 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -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 | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4299 | } |
| 4300 | break; |
| 4301 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4302 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4303 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4304 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4305 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4306 | pItem->val.expAndMantissa.nExponent, |
| 4307 | pnValue, |
| 4308 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4309 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4310 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4311 | } |
| 4312 | break; |
| 4313 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4314 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4315 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4316 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4317 | pItem->val.expAndMantissa.nExponent, |
| 4318 | pnValue, |
| 4319 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4320 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4321 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4322 | } |
| 4323 | break; |
| 4324 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4325 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4326 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4327 | int64_t nMantissa; |
| 4328 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4329 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4330 | if(uErr) { |
| 4331 | return uErr; |
| 4332 | } |
| 4333 | return ExponentiateNN(nMantissa, |
| 4334 | pItem->val.expAndMantissa.nExponent, |
| 4335 | pnValue, |
| 4336 | Exponentitate10); |
| 4337 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4338 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4339 | } |
| 4340 | break; |
| 4341 | |
| 4342 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4343 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4344 | int64_t nMantissa; |
| 4345 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4346 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4347 | if(uErr) { |
| 4348 | return uErr; |
| 4349 | } |
| 4350 | return ExponentiateNN(nMantissa, |
| 4351 | pItem->val.expAndMantissa.nExponent, |
| 4352 | pnValue, |
| 4353 | Exponentitate10); |
| 4354 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4355 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4356 | } |
| 4357 | break; |
| 4358 | |
| 4359 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4360 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4361 | int64_t nMantissa; |
| 4362 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4363 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4364 | if(uErr) { |
| 4365 | return uErr; |
| 4366 | } |
| 4367 | return ExponentiateNN(nMantissa, |
| 4368 | pItem->val.expAndMantissa.nExponent, |
| 4369 | pnValue, |
| 4370 | Exponentitate2); |
| 4371 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4372 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4373 | } |
| 4374 | break; |
| 4375 | |
| 4376 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4377 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4378 | int64_t nMantissa; |
| 4379 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4380 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4381 | if(uErr) { |
| 4382 | return uErr; |
| 4383 | } |
| 4384 | return ExponentiateNN(nMantissa, |
| 4385 | pItem->val.expAndMantissa.nExponent, |
| 4386 | pnValue, |
| 4387 | Exponentitate2); |
| 4388 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4389 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4390 | } |
| 4391 | break; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4392 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4393 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4394 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4395 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4396 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4397 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4398 | |
| 4399 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4400 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4401 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4402 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4403 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4404 | { |
| 4405 | QCBORItem Item; |
| 4406 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4407 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4408 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4409 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4410 | // The above conversion succeeded |
| 4411 | return; |
| 4412 | } |
| 4413 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4414 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4415 | // 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] | 4416 | return; |
| 4417 | } |
| 4418 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4419 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4422 | |
| 4423 | /* |
| 4424 | Public function, see header qcbor/qcbor_decode.h file |
| 4425 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4426 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4427 | int64_t nLabel, |
| 4428 | uint32_t uConvertTypes, |
| 4429 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4430 | { |
| 4431 | QCBORItem Item; |
| 4432 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4433 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4434 | nLabel, |
| 4435 | uConvertTypes, |
| 4436 | pnValue, |
| 4437 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4438 | |
| 4439 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4440 | // The above conversion succeeded |
| 4441 | return; |
| 4442 | } |
| 4443 | |
| 4444 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4445 | // The above conversion failed in a way that code below can't correct |
| 4446 | return; |
| 4447 | } |
| 4448 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4449 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4453 | /* |
| 4454 | Public function, see header qcbor/qcbor_decode.h file |
| 4455 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4456 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4457 | const char *szLabel, |
| 4458 | uint32_t uConvertTypes, |
| 4459 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4460 | { |
| 4461 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4462 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4463 | szLabel, |
| 4464 | uConvertTypes, |
| 4465 | pnValue, |
| 4466 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4467 | |
| 4468 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4469 | // The above conversion succeeded |
| 4470 | return; |
| 4471 | } |
| 4472 | |
| 4473 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4474 | // The above conversion failed in a way that code below can't correct |
| 4475 | return; |
| 4476 | } |
| 4477 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4478 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4482 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4483 | { |
| 4484 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4485 | case QCBOR_TYPE_DOUBLE: |
| 4486 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4487 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4488 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4489 | // Can't use llround here because it will not convert values |
| 4490 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4491 | // need to be converted so it is more complicated. |
| 4492 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4493 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4494 | if(isnan(pItem->val.dfnum)) { |
| 4495 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4496 | } else if(pItem->val.dfnum < 0) { |
| 4497 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4498 | } else { |
| 4499 | double dRounded = round(pItem->val.dfnum); |
| 4500 | // See discussion in DecodeDateEpoch() for |
| 4501 | // explanation of - 0x7ff |
| 4502 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4503 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4504 | } |
| 4505 | *puValue = (uint64_t)dRounded; |
| 4506 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4507 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4508 | if(isnan(pItem->val.fnum)) { |
| 4509 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4510 | } else if(pItem->val.fnum < 0) { |
| 4511 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4512 | } else { |
| 4513 | float fRounded = roundf(pItem->val.fnum); |
| 4514 | // See discussion in DecodeDateEpoch() for |
| 4515 | // explanation of - 0x7ff |
| 4516 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4517 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4518 | } |
| 4519 | *puValue = (uint64_t)fRounded; |
| 4520 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4521 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4522 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4523 | // round() and roundf() shouldn't result in exceptions here, but |
| 4524 | // catch them to be robust and thorough. Don't try to |
| 4525 | // distinguish between the various exceptions because it seems |
| 4526 | // they vary by CPU, compiler and OS. |
| 4527 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4528 | } |
| 4529 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4530 | } else { |
| 4531 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4532 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4533 | #else |
| 4534 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4535 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4536 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4537 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4538 | case QCBOR_TYPE_INT64: |
| 4539 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4540 | if(pItem->val.int64 >= 0) { |
| 4541 | *puValue = (uint64_t)pItem->val.int64; |
| 4542 | } else { |
| 4543 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4544 | } |
| 4545 | } else { |
| 4546 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4547 | } |
| 4548 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4549 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4550 | case QCBOR_TYPE_UINT64: |
| 4551 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4552 | *puValue = pItem->val.uint64; |
| 4553 | } else { |
| 4554 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4555 | } |
| 4556 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4557 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4558 | default: |
| 4559 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4560 | } |
| 4561 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4562 | return QCBOR_SUCCESS; |
| 4563 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4564 | |
| 4565 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4566 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4567 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4568 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4569 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4570 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4571 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4572 | return; |
| 4573 | } |
| 4574 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4575 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4576 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4577 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4578 | if(uError) { |
| 4579 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4580 | return; |
| 4581 | } |
| 4582 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4583 | if(pItem) { |
| 4584 | *pItem = Item; |
| 4585 | } |
| 4586 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4587 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4588 | } |
| 4589 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4590 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4591 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4592 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4593 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4594 | uint64_t *puValue, |
| 4595 | QCBORItem *pItem) |
| 4596 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4597 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4598 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4599 | return; |
| 4600 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4601 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4602 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4606 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4607 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4608 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4609 | uint64_t *puValue, |
| 4610 | QCBORItem *pItem) |
| 4611 | { |
| 4612 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4613 | return; |
| 4614 | } |
| 4615 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4616 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4617 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4618 | return; |
| 4619 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4620 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4621 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4622 | } |
| 4623 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4624 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4625 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4626 | static QCBORError |
| 4627 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4628 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4629 | switch(pItem->uDataType) { |
| 4630 | |
| 4631 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4632 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4633 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4634 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4635 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4636 | } |
| 4637 | break; |
| 4638 | |
| 4639 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4640 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4641 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4642 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4643 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4644 | } |
| 4645 | break; |
| 4646 | |
| 4647 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4648 | |
| 4649 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4650 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4651 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4652 | pItem->val.expAndMantissa.nExponent, |
| 4653 | puValue, |
| 4654 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4655 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4656 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4657 | } |
| 4658 | break; |
| 4659 | |
| 4660 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4661 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4662 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4663 | pItem->val.expAndMantissa.nExponent, |
| 4664 | puValue, |
| 4665 | Exponentitate2); |
| 4666 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4667 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4668 | } |
| 4669 | break; |
| 4670 | |
| 4671 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4672 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4673 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4674 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4675 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4676 | if(uErr != QCBOR_SUCCESS) { |
| 4677 | return uErr; |
| 4678 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4679 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4680 | pItem->val.expAndMantissa.nExponent, |
| 4681 | puValue, |
| 4682 | Exponentitate10); |
| 4683 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4684 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4685 | } |
| 4686 | break; |
| 4687 | |
| 4688 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4689 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4690 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4691 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4692 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4693 | } |
| 4694 | break; |
| 4695 | |
| 4696 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4697 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4698 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4699 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4700 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4701 | if(uErr != QCBOR_SUCCESS) { |
| 4702 | return uErr; |
| 4703 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4704 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4705 | pItem->val.expAndMantissa.nExponent, |
| 4706 | puValue, |
| 4707 | Exponentitate2); |
| 4708 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4709 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4710 | } |
| 4711 | break; |
| 4712 | |
| 4713 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4714 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4715 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4716 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4717 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4718 | } |
| 4719 | break; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4720 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4721 | default: |
| 4722 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4723 | } |
| 4724 | } |
| 4725 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4726 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4727 | /* |
| 4728 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4729 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4730 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4731 | { |
| 4732 | QCBORItem Item; |
| 4733 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4734 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4735 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4736 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4737 | // The above conversion succeeded |
| 4738 | return; |
| 4739 | } |
| 4740 | |
| 4741 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4742 | // 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] | 4743 | return; |
| 4744 | } |
| 4745 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4746 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4747 | } |
| 4748 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4749 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4750 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4751 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4752 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4753 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4754 | int64_t nLabel, |
| 4755 | uint32_t uConvertTypes, |
| 4756 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4757 | { |
| 4758 | QCBORItem Item; |
| 4759 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4760 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 4761 | nLabel, |
| 4762 | uConvertTypes, |
| 4763 | puValue, |
| 4764 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4765 | |
| 4766 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4767 | // The above conversion succeeded |
| 4768 | return; |
| 4769 | } |
| 4770 | |
| 4771 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4772 | // The above conversion failed in a way that code below can't correct |
| 4773 | return; |
| 4774 | } |
| 4775 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4776 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | |
| 4780 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4781 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4782 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4783 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4784 | const char *szLabel, |
| 4785 | uint32_t uConvertTypes, |
| 4786 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4787 | { |
| 4788 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4789 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 4790 | szLabel, |
| 4791 | uConvertTypes, |
| 4792 | puValue, |
| 4793 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4794 | |
| 4795 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4796 | // The above conversion succeeded |
| 4797 | return; |
| 4798 | } |
| 4799 | |
| 4800 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4801 | // The above conversion failed in a way that code below can't correct |
| 4802 | return; |
| 4803 | } |
| 4804 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4805 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4809 | |
| 4810 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4811 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4812 | uint32_t uConvertTypes, |
| 4813 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4814 | { |
| 4815 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4816 | case QCBOR_TYPE_FLOAT: |
| 4817 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4818 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4819 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4820 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4821 | *pdValue = (double)pItem->val.fnum; |
| 4822 | } else { |
| 4823 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4824 | } |
| 4825 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4826 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4827 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4828 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4829 | break; |
| 4830 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4831 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4832 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4833 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4834 | *pdValue = pItem->val.dfnum; |
| 4835 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4836 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4837 | } |
| 4838 | } |
| 4839 | break; |
| 4840 | |
| 4841 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4842 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4843 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4844 | // A simple cast seems to do the job with no worry of exceptions. |
| 4845 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4846 | *pdValue = (double)pItem->val.int64; |
| 4847 | |
| 4848 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4849 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4850 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4851 | #else |
| 4852 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4853 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4854 | break; |
| 4855 | |
| 4856 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4857 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4858 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4859 | // A simple cast seems to do the job with no worry of exceptions. |
| 4860 | // There will be precision loss for some values. |
| 4861 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4862 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4863 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4864 | } |
| 4865 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4866 | #else |
| 4867 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4868 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4869 | |
| 4870 | default: |
| 4871 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4872 | } |
| 4873 | |
| 4874 | return QCBOR_SUCCESS; |
| 4875 | } |
| 4876 | |
| 4877 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4878 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4879 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4880 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4881 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4882 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4883 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4884 | return; |
| 4885 | } |
| 4886 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4887 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4888 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4889 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4890 | if(uError) { |
| 4891 | pMe->uLastError = (uint8_t)uError; |
| 4892 | return; |
| 4893 | } |
| 4894 | |
| 4895 | if(pItem) { |
| 4896 | *pItem = Item; |
| 4897 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4898 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4899 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4900 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4901 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4902 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4903 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4904 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4905 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4906 | double *pdValue, |
| 4907 | QCBORItem *pItem) |
| 4908 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4909 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4910 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4911 | return; |
| 4912 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4913 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4914 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4917 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4918 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4919 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4920 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4921 | double *pdValue, |
| 4922 | QCBORItem *pItem) |
| 4923 | { |
| 4924 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4925 | return; |
| 4926 | } |
| 4927 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4928 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4929 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4930 | return; |
| 4931 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4932 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4933 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4937 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4938 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 4939 | { |
| 4940 | double dResult; |
| 4941 | |
| 4942 | dResult = 0.0; |
| 4943 | const uint8_t *pByte = BigNum.ptr; |
| 4944 | size_t uLen = BigNum.len; |
| 4945 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 4946 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4947 | while(uLen--) { |
| 4948 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 4949 | } |
| 4950 | |
| 4951 | return dResult; |
| 4952 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4953 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4954 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4955 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4956 | static QCBORError |
| 4957 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4958 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4959 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4960 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4961 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 4962 | |
| 4963 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4964 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4965 | |
| 4966 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4967 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4968 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4969 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4970 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4971 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 4972 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4973 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4974 | } |
| 4975 | break; |
| 4976 | |
| 4977 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4978 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4979 | // Underflow gives 0, overflow gives infinity |
| 4980 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4981 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4982 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4983 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4984 | } |
| 4985 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4986 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4987 | |
| 4988 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4989 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4990 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 4991 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4992 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4993 | } |
| 4994 | break; |
| 4995 | |
| 4996 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4997 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4998 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4999 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5000 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5001 | } |
| 5002 | break; |
| 5003 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5004 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5005 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5006 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5007 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5008 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5009 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5010 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5011 | } |
| 5012 | break; |
| 5013 | |
| 5014 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5015 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5016 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5017 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5018 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5019 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5020 | } |
| 5021 | break; |
| 5022 | |
| 5023 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5024 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5025 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5026 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5027 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5028 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5029 | } |
| 5030 | break; |
| 5031 | |
| 5032 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5033 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5034 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5035 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5036 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5037 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5038 | } |
| 5039 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5040 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5041 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5042 | default: |
| 5043 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5044 | } |
| 5045 | |
| 5046 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5047 | |
| 5048 | #else |
| 5049 | (void)pItem; |
| 5050 | (void)uConvertTypes; |
| 5051 | (void)pdValue; |
| 5052 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5053 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5054 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | |
| 5058 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5059 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5060 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5061 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 5062 | uint32_t uConvertTypes, |
| 5063 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5064 | { |
| 5065 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5066 | QCBORItem Item; |
| 5067 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5068 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5069 | |
| 5070 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5071 | // The above conversion succeeded |
| 5072 | return; |
| 5073 | } |
| 5074 | |
| 5075 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5076 | // The above conversion failed in a way that code below can't correct |
| 5077 | return; |
| 5078 | } |
| 5079 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5080 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5081 | } |
| 5082 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5083 | |
| 5084 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5085 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5086 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5087 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5088 | int64_t nLabel, |
| 5089 | uint32_t uConvertTypes, |
| 5090 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5091 | { |
| 5092 | QCBORItem Item; |
| 5093 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5094 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5095 | |
| 5096 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5097 | // The above conversion succeeded |
| 5098 | return; |
| 5099 | } |
| 5100 | |
| 5101 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5102 | // The above conversion failed in a way that code below can't correct |
| 5103 | return; |
| 5104 | } |
| 5105 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5106 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | |
| 5110 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5111 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5112 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5113 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5114 | const char *szLabel, |
| 5115 | uint32_t uConvertTypes, |
| 5116 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5117 | { |
| 5118 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5119 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5120 | |
| 5121 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5122 | // The above conversion succeeded |
| 5123 | return; |
| 5124 | } |
| 5125 | |
| 5126 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5127 | // The above conversion failed in a way that code below can't correct |
| 5128 | return; |
| 5129 | } |
| 5130 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5131 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5132 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5133 | |
| 5134 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5135 | |
| 5136 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5137 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5138 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 5139 | { |
| 5140 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5141 | uInt = uInt << 8; |
| 5142 | }; |
| 5143 | |
| 5144 | UsefulOutBuf UOB; |
| 5145 | |
| 5146 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5147 | |
| 5148 | while(uInt) { |
| 5149 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5150 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5151 | uInt = uInt << 8; |
| 5152 | (void)xx; |
| 5153 | } |
| 5154 | |
| 5155 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5156 | } |
| 5157 | |
| 5158 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5159 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 5160 | TagSpecification TagSpec, |
| 5161 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5162 | { |
| 5163 | QCBORError uErr; |
| 5164 | // 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] | 5165 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5166 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5167 | if(uErr != QCBOR_SUCCESS) { |
| 5168 | goto Done; |
| 5169 | } |
| 5170 | |
| 5171 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 5172 | break; // Successful exit. Moving on to finish decoding. |
| 5173 | } |
| 5174 | |
| 5175 | // The item is an array, which means an undecoded |
| 5176 | // mantissa and exponent, so decode it. It will then |
| 5177 | // have a different type and exit the loop if. |
| 5178 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5179 | if(uErr != QCBOR_SUCCESS) { |
| 5180 | goto Done; |
| 5181 | } |
| 5182 | |
| 5183 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5184 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5185 | } |
| 5186 | Done: |
| 5187 | return uErr; |
| 5188 | } |
| 5189 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5190 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5191 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5192 | TagSpecification TagSpec, |
| 5193 | QCBORItem *pItem, |
| 5194 | int64_t *pnMantissa, |
| 5195 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5196 | { |
| 5197 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5198 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5199 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5200 | if(uErr != QCBOR_SUCCESS) { |
| 5201 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5202 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5203 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5204 | switch (pItem->uDataType) { |
| 5205 | |
| 5206 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5207 | case QCBOR_TYPE_BIGFLOAT: |
| 5208 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 5209 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5210 | break; |
| 5211 | |
| 5212 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5213 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5214 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5215 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5216 | break; |
| 5217 | |
| 5218 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5219 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5220 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5221 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5222 | break; |
| 5223 | |
| 5224 | default: |
| 5225 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5226 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5227 | |
| 5228 | Done: |
| 5229 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5230 | } |
| 5231 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5232 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5233 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5234 | TagSpecification TagSpec, |
| 5235 | QCBORItem *pItem, |
| 5236 | UsefulBuf BufferForMantissa, |
| 5237 | UsefulBufC *pMantissa, |
| 5238 | bool *pbIsNegative, |
| 5239 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5240 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5241 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5242 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5243 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5244 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5245 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | uint64_t uMantissa; |
| 5249 | |
| 5250 | switch (pItem->uDataType) { |
| 5251 | |
| 5252 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5253 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5254 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5255 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5256 | *pbIsNegative = false; |
| 5257 | } else { |
| 5258 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5259 | *pbIsNegative = true; |
| 5260 | } |
| 5261 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5262 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5263 | break; |
| 5264 | |
| 5265 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5266 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5267 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5268 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5269 | *pbIsNegative = false; |
| 5270 | break; |
| 5271 | |
| 5272 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5273 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5274 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5275 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5276 | *pbIsNegative = true; |
| 5277 | break; |
| 5278 | |
| 5279 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5280 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5281 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5282 | |
| 5283 | Done: |
| 5284 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5288 | /* |
| 5289 | Public function, see header qcbor/qcbor_decode.h file |
| 5290 | */ |
| 5291 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5292 | uint8_t uTagRequirement, |
| 5293 | int64_t *pnMantissa, |
| 5294 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5295 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5296 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5297 | return; |
| 5298 | } |
| 5299 | |
| 5300 | QCBORItem Item; |
| 5301 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5302 | if(uError) { |
| 5303 | pMe->uLastError = (uint8_t)uError; |
| 5304 | return; |
| 5305 | } |
| 5306 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5307 | const TagSpecification TagSpec = |
| 5308 | { |
| 5309 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5310 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5311 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5312 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5313 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5314 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5315 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5316 | } |
| 5317 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5318 | |
| 5319 | /* |
| 5320 | Public function, see header qcbor/qcbor_decode.h file |
| 5321 | */ |
| 5322 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5323 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5324 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5325 | int64_t *pnMantissa, |
| 5326 | int64_t *pnExponent) |
| 5327 | { |
| 5328 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5329 | return; |
| 5330 | } |
| 5331 | |
| 5332 | QCBORItem Item; |
| 5333 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5334 | |
| 5335 | const TagSpecification TagSpec = |
| 5336 | { |
| 5337 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5338 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5339 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5340 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5341 | }; |
| 5342 | |
| 5343 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5344 | } |
| 5345 | |
| 5346 | |
| 5347 | /* |
| 5348 | Public function, see header qcbor/qcbor_decode.h file |
| 5349 | */ |
| 5350 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5351 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5352 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5353 | int64_t *pnMantissa, |
| 5354 | int64_t *pnExponent) |
| 5355 | { |
| 5356 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5357 | return; |
| 5358 | } |
| 5359 | |
| 5360 | QCBORItem Item; |
| 5361 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5362 | |
| 5363 | const TagSpecification TagSpec = |
| 5364 | { |
| 5365 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5366 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5367 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5368 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5369 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5370 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5371 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5372 | } |
| 5373 | |
| 5374 | |
| 5375 | /* |
| 5376 | Public function, see header qcbor/qcbor_decode.h file |
| 5377 | */ |
| 5378 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5379 | uint8_t uTagRequirement, |
| 5380 | UsefulBuf MantissaBuffer, |
| 5381 | UsefulBufC *pMantissa, |
| 5382 | bool *pbMantissaIsNegative, |
| 5383 | int64_t *pnExponent) |
| 5384 | { |
| 5385 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5386 | return; |
| 5387 | } |
| 5388 | |
| 5389 | QCBORItem Item; |
| 5390 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5391 | if(uError) { |
| 5392 | pMe->uLastError = (uint8_t)uError; |
| 5393 | return; |
| 5394 | } |
| 5395 | |
| 5396 | const TagSpecification TagSpec = |
| 5397 | { |
| 5398 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5399 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5400 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5401 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5402 | }; |
| 5403 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5404 | ProcessMantissaAndExponentBig(pMe, |
| 5405 | TagSpec, |
| 5406 | &Item, |
| 5407 | MantissaBuffer, |
| 5408 | pMantissa, |
| 5409 | pbMantissaIsNegative, |
| 5410 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | |
| 5414 | /* |
| 5415 | Public function, see header qcbor/qcbor_decode.h file |
| 5416 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5417 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5418 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5419 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5420 | UsefulBuf BufferForMantissa, |
| 5421 | UsefulBufC *pMantissa, |
| 5422 | bool *pbIsNegative, |
| 5423 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5424 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5425 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5426 | return; |
| 5427 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5428 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5429 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5430 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5431 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5432 | return; |
| 5433 | } |
| 5434 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5435 | const TagSpecification TagSpec = |
| 5436 | { |
| 5437 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5438 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5439 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5440 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5441 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5442 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5443 | ProcessMantissaAndExponentBig(pMe, |
| 5444 | TagSpec, |
| 5445 | &Item, |
| 5446 | BufferForMantissa, |
| 5447 | pMantissa, |
| 5448 | pbIsNegative, |
| 5449 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5450 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5451 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5452 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5453 | /* |
| 5454 | Public function, see header qcbor/qcbor_decode.h file |
| 5455 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5456 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5457 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5458 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5459 | UsefulBuf BufferForMantissa, |
| 5460 | UsefulBufC *pMantissa, |
| 5461 | bool *pbIsNegative, |
| 5462 | int64_t *pnExponent) |
| 5463 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5464 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5465 | return; |
| 5466 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5467 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5468 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5469 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5470 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5471 | return; |
| 5472 | } |
| 5473 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5474 | const TagSpecification TagSpec = |
| 5475 | { |
| 5476 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5477 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5478 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5479 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5480 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5481 | |
| 5482 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5483 | } |
| 5484 | |
| 5485 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5486 | /* |
| 5487 | Public function, see header qcbor/qcbor_decode.h file |
| 5488 | */ |
| 5489 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5490 | uint8_t uTagRequirement, |
| 5491 | int64_t *pnMantissa, |
| 5492 | int64_t *pnExponent) |
| 5493 | { |
| 5494 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5495 | return; |
| 5496 | } |
| 5497 | |
| 5498 | QCBORItem Item; |
| 5499 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5500 | if(uError) { |
| 5501 | pMe->uLastError = (uint8_t)uError; |
| 5502 | return; |
| 5503 | } |
| 5504 | const TagSpecification TagSpec = |
| 5505 | { |
| 5506 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5507 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5508 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5509 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5510 | }; |
| 5511 | |
| 5512 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5513 | } |
| 5514 | |
| 5515 | |
| 5516 | /* |
| 5517 | Public function, see header qcbor/qcbor_decode.h file |
| 5518 | */ |
| 5519 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5520 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5521 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5522 | int64_t *pnMantissa, |
| 5523 | int64_t *pnExponent) |
| 5524 | { |
| 5525 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5526 | return; |
| 5527 | } |
| 5528 | |
| 5529 | QCBORItem Item; |
| 5530 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5531 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5532 | return; |
| 5533 | } |
| 5534 | |
| 5535 | const TagSpecification TagSpec = |
| 5536 | { |
| 5537 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5538 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5539 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5540 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5541 | }; |
| 5542 | |
| 5543 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5544 | } |
| 5545 | |
| 5546 | |
| 5547 | /* |
| 5548 | Public function, see header qcbor/qcbor_decode.h file |
| 5549 | */ |
| 5550 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5551 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5552 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5553 | int64_t *pnMantissa, |
| 5554 | int64_t *pnExponent) |
| 5555 | { |
| 5556 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5557 | return; |
| 5558 | } |
| 5559 | |
| 5560 | QCBORItem Item; |
| 5561 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5562 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5563 | return; |
| 5564 | } |
| 5565 | |
| 5566 | const TagSpecification TagSpec = |
| 5567 | { |
| 5568 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5569 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5570 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5571 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5572 | }; |
| 5573 | |
| 5574 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5575 | } |
| 5576 | |
| 5577 | |
| 5578 | /* |
| 5579 | Public function, see header qcbor/qcbor_decode.h file |
| 5580 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5581 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5582 | uint8_t uTagRequirement, |
| 5583 | UsefulBuf MantissaBuffer, |
| 5584 | UsefulBufC *pMantissa, |
| 5585 | bool *pbMantissaIsNegative, |
| 5586 | int64_t *pnExponent) |
| 5587 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5588 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5589 | return; |
| 5590 | } |
| 5591 | |
| 5592 | QCBORItem Item; |
| 5593 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5594 | if(uError) { |
| 5595 | pMe->uLastError = (uint8_t)uError; |
| 5596 | return; |
| 5597 | } |
| 5598 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5599 | const TagSpecification TagSpec = |
| 5600 | { |
| 5601 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5602 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5603 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5604 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5605 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5606 | |
| 5607 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5610 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5611 | /* |
| 5612 | Public function, see header qcbor/qcbor_decode.h file |
| 5613 | */ |
| 5614 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5615 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5616 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5617 | UsefulBuf BufferForMantissa, |
| 5618 | UsefulBufC *pMantissa, |
| 5619 | bool *pbIsNegative, |
| 5620 | int64_t *pnExponent) |
| 5621 | { |
| 5622 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5623 | return; |
| 5624 | } |
| 5625 | |
| 5626 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5627 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5628 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5629 | return; |
| 5630 | } |
| 5631 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5632 | const TagSpecification TagSpec = |
| 5633 | { |
| 5634 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5635 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5636 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5637 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5638 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5639 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5640 | ProcessMantissaAndExponentBig(pMe, |
| 5641 | TagSpec, |
| 5642 | &Item, |
| 5643 | BufferForMantissa, |
| 5644 | pMantissa, |
| 5645 | pbIsNegative, |
| 5646 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5650 | /* |
| 5651 | Public function, see header qcbor/qcbor_decode.h file |
| 5652 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5653 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5654 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5655 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5656 | UsefulBuf BufferForMantissa, |
| 5657 | UsefulBufC *pMantissa, |
| 5658 | bool *pbIsNegative, |
| 5659 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5660 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5661 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5662 | return; |
| 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 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5666 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5667 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5668 | return; |
| 5669 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5670 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5671 | const TagSpecification TagSpec = |
| 5672 | { |
| 5673 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5674 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5675 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5676 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5677 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5678 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5679 | ProcessMantissaAndExponentBig(pMe, |
| 5680 | TagSpec, |
| 5681 | &Item, |
| 5682 | BufferForMantissa, |
| 5683 | pMantissa, |
| 5684 | pbIsNegative, |
| 5685 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5686 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5687 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5688 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |