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 | * |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 612 | * 1. QCBORDecode_GetNextTagContent - The top layer processes tagged |
| 613 | * data items, turning them into the local C representation. For the |
| 614 | * most simple it is just associating a QCBOR_TYPE with the data. For |
| 615 | * the complex ones that an aggregate of data items, there is some |
| 616 | * further decoding and some limited recursion. |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 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 | * |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 623 | * 3. QCBORDecode_GetNextMapEntry - This handles the combining of two |
| 624 | * items, the label and the data, that make up a map entry. It only |
| 625 | * does work on maps. It combines the label and data items into one |
| 626 | * labeled item. |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 627 | * |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 628 | * 4. QCBORDecode_GetNextTagNumber - This decodes type 6 tag |
| 629 | * numbers. It turns the tag numbers into bit flags associated with |
| 630 | * the data item. No actual decoding of the contents of the tag is |
| 631 | * performed here. |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 632 | * |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 633 | * 5. QCBORDecode_GetNextFullString - This assembles the sub-items |
| 634 | * that make up an indefinite-length string into one string item. It |
| 635 | * uses the string allocator to create contiguous space for the |
| 636 | * item. It processes all breaks that are part of indefinite-length |
| 637 | * strings. |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 638 | * |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 639 | * 6. DecodeAtomicDataItem - This decodes the atomic data items in |
| 640 | * CBOR. Each atomic data item has a "major type", an integer |
| 641 | * "argument" and optionally some content. For text and byte strings, |
| 642 | * the content is the bytes that make up the string. These are the |
| 643 | * smallest data items that are considered to be well-formed. The |
| 644 | * content may also be other data items in the case of aggregate |
| 645 | * types. They are not handled in this layer. |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 646 | * |
| 647 | * Roughly this takes 300 bytes of stack for vars. TODO: evaluate this |
| 648 | * more carefully and correctly. |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 649 | */ |
| 650 | |
| 651 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 652 | /** |
| 653 | * @brief Decode the CBOR head, the type and argument. |
| 654 | * |
| 655 | * @param[in] pUInBuf The input buffer to read from. |
| 656 | * @param[out] pnMajorType The decoded major type. |
| 657 | * @param[out] puArgument The decoded argument. |
| 658 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 659 | * |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 660 | * @retval QCBOR_ERR_UNSUPPORTED |
| 661 | * @retval QCBOR_ERR_HIT_END |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 662 | * |
| 663 | * This decodes the CBOR "head" that every CBOR data item has. See |
| 664 | * longer explaination of the head in documentation for |
| 665 | * QCBOREncode_EncodeHead(). |
| 666 | * |
| 667 | * This does the network->host byte order conversion. The conversion |
| 668 | * here also results in the conversion for floats in addition to that |
| 669 | * for lengths, tags and integer values. |
| 670 | * |
| 671 | * The int type is preferred to uint8_t for some variables as this |
| 672 | * avoids integer promotions, can reduce code size and makes static |
| 673 | * analyzers happier. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 674 | */ |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 675 | static inline QCBORError |
| 676 | DecodeHead(UsefulInputBuf *pUInBuf, |
| 677 | int *pnMajorType, |
| 678 | uint64_t *puArgument, |
| 679 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 680 | { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 681 | QCBORError uReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 682 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 683 | /* Get the initial byte that every CBOR data item has and break it |
| 684 | * down. */ |
| 685 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 686 | const int nTmpMajorType = nInitialByte >> 5; |
| 687 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 688 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 689 | /* Where the argument accumulates */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 690 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 691 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 692 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 693 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 694 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 695 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 696 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 697 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 698 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 699 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 700 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 701 | /* This shift and add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 702 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 703 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 704 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 705 | /* The reserved and thus-far unused additional info values */ |
| 706 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 707 | goto Done; |
| 708 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 709 | /* Less than 24, additional info is argument or 31, an |
| 710 | * indefinite length. No more bytes to get. |
| 711 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 712 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 713 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 714 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 715 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 716 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 717 | goto Done; |
| 718 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 719 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 720 | /* All successful if arrived here. */ |
| 721 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 722 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 723 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 724 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 725 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 726 | Done: |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 727 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 730 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 731 | /** |
| 732 | * @brief Decode integer types, major types 0 and 1. |
| 733 | * |
| 734 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 735 | * @param[in] uArgument The argument from the head. |
| 736 | * @param[out] pDecodedItem The filled in decoded item. |
| 737 | * |
| 738 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 739 | * |
| 740 | * Must only be called when major type is 0 or 1. |
| 741 | * |
| 742 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 743 | * all CPUs use it these days and the test vectors in the RFC are |
| 744 | * so. All integers in the CBOR structure are positive and the major |
| 745 | * type indicates positive or negative. CBOR can express positive |
| 746 | * integers up to 2^x - 1 where x is the number of bits and negative |
| 747 | * integers down to 2^x. Note that negative numbers can be one more |
| 748 | * away from zero than positive. Stdint, as far as I can tell, uses |
| 749 | * two's compliment to represent negative integers. |
| 750 | * |
| 751 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 752 | * used carefully here, and in particular why it isn't used in the |
| 753 | * public interface. Also see |
| 754 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 755 | * |
| 756 | * Int is used for values that need less than 16-bits and would be |
| 757 | * subject to integer promotion and result in complaining from static |
| 758 | * analyzers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 759 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 760 | static inline QCBORError |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 761 | DecodeInteger(int nMajorType, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 762 | { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 763 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 764 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 765 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 766 | if (uArgument <= INT64_MAX) { |
| 767 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 768 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 769 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 770 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 771 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 772 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 773 | } |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 774 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 775 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 776 | if(uArgument <= INT64_MAX) { |
| 777 | /* CBOR's representation of negative numbers lines up with |
| 778 | * the two-compliment representation. A negative integer has |
| 779 | * one more in range than a positive integer. INT64_MIN is |
| 780 | * equal to (-INT64_MAX) - 1. |
| 781 | */ |
| 782 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 783 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 784 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 785 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 786 | /* C can't represent a negative integer in this range so it |
| 787 | * is an error. |
| 788 | */ |
| 789 | uReturn = QCBOR_ERR_INT_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 792 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 793 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 796 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 797 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 798 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 799 | #error QCBOR_TYPE_FALSE macro value wrong |
| 800 | #endif |
| 801 | |
| 802 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 803 | #error QCBOR_TYPE_TRUE macro value wrong |
| 804 | #endif |
| 805 | |
| 806 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 807 | #error QCBOR_TYPE_NULL macro value wrong |
| 808 | #endif |
| 809 | |
| 810 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 811 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 812 | #endif |
| 813 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 814 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 815 | #error QCBOR_TYPE_BREAK macro value wrong |
| 816 | #endif |
| 817 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 818 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 819 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 820 | #endif |
| 821 | |
| 822 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 823 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 824 | #endif |
| 825 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 826 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 827 | /** |
| 828 | * @brief Decode type 7 -- true, false, floating-point, break... |
| 829 | * |
| 830 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 831 | * @param[in] uArgument The argument from the head. |
| 832 | * @param[out] pDecodedItem The filled in decoded item. |
| 833 | * |
| 834 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 835 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 836 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 837 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 838 | static inline QCBORError |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 839 | DecodeType7(int nAdditionalInfo, uint64_t uArgument, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 840 | { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 841 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 842 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 843 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 844 | * checks above make sure uAdditionalInfo values line up with |
| 845 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 846 | * > 0x1f so cast is safe. |
| 847 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 848 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 849 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 850 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 851 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 852 | * are caught before this is called. |
| 853 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 854 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 855 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 856 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 857 | /* Half-precision is returned as a double. The cast to |
| 858 | * uint16_t is safe because the encoded value was 16 bits. It |
| 859 | * was widened to 64 bits to be passed in here. |
| 860 | */ |
| 861 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 862 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 863 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 864 | uReturn = QCBOR_ERR_HALF_PRECISION_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 865 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 866 | break; |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 867 | case SINGLE_PREC_FLOAT: /* 26 */ |
| 868 | /* Single precision is normally returned as a double since |
| 869 | * double is widely supported, there is no loss of precision, |
| 870 | * it makes it easy for the caller in most cases and it can |
| 871 | * be converted back to single with no loss of precision |
| 872 | * |
| 873 | * The cast to uint32_t is safe because the encoded value was |
| 874 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 875 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 876 | { |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 877 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 878 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 879 | /* In the normal case, use HW to convert float to |
| 880 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 881 | pDecodedItem->val.dfnum = (double)f; |
| 882 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 883 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 884 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 885 | pDecodedItem->val.fnum = f; |
| 886 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 887 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 888 | /* IEEE754_FloatToDouble() could be used here to return as |
| 889 | * a double, but it adds object code and most likely |
| 890 | * anyone disabling FLOAT HW use doesn't care about floats |
| 891 | * and wants to save object code. |
| 892 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 893 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 894 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 895 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 896 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 897 | case DOUBLE_PREC_FLOAT: /* 27 */ |
| 898 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 899 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 900 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 901 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 902 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 903 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 904 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 905 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 906 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 907 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 908 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 909 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 910 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 911 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 912 | * as e0 … f7 |
| 913 | */ |
| 914 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 915 | goto Done; |
| 916 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 917 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 918 | |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 919 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 920 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 921 | /* DecodeHead() will make uArgument equal to |
| 922 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 923 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 924 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 925 | */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 926 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 927 | break; |
| 928 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 929 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 930 | Done: |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 931 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 935 | /** |
| 936 | * @brief Decode text and byte strings |
| 937 | * |
| 938 | * @param[in] pAllocator The string allocator or NULL. |
| 939 | * @param[in] uStrLen The length of the string. |
| 940 | * @param[in] pUInBuf The surce from which to read the string's bytes. |
| 941 | * @param[out] pDecodedItem The filled in decoded item. |
| 942 | * |
| 943 | * @retval QCBOR_ERR_HIT_END |
| 944 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 945 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 946 | * |
| 947 | * The reads @c uStrlen bytes from @c pUInBuf and fills in @c |
| 948 | * pDecodedItem. If @c pAllocator is not NULL then memory for the |
| 949 | * string is allocated. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 950 | */ |
Laurence Lundblade | 3829909 | 2020-12-28 04:13:50 -0800 | [diff] [blame] | 951 | static inline QCBORError |
Laurence Lundblade | 63e68f7 | 2020-12-28 04:24:11 -0800 | [diff] [blame] | 952 | DecodeBytes(const QCBORInternalAllocator *pAllocator, |
| 953 | uint64_t uStrLen, |
| 954 | UsefulInputBuf *pUInBuf, |
| 955 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 956 | { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 957 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 958 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 959 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 960 | * CPUs. This check makes the casts to size_t below safe. |
| 961 | * |
| 962 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 963 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 964 | * one will care the limit on strings is 4 bytes shorter). |
| 965 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 966 | if(uStrLen > SIZE_MAX-4) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 967 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 968 | goto Done; |
| 969 | } |
| 970 | |
| 971 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 972 | if(UsefulBuf_IsNULLC(Bytes)) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 973 | /* Failed to get the bytes for this string item */ |
| 974 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 975 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 976 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 977 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 978 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 979 | /* Note that this is not where allocation to coallsece |
| 980 | * indefinite-length strings is done. This is for when the caller |
| 981 | * has requested all strings be allocated. Disabling indefinite |
| 982 | * length strings also disables this allocate-all option. |
| 983 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 984 | if(pAllocator) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 985 | /* request to use the string allocator to make a copy */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 986 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 987 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 988 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 989 | goto Done; |
| 990 | } |
| 991 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 992 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 993 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 994 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 995 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 996 | (void)pAllocator; |
| 997 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 998 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 999 | /* Normal case with no string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1000 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1001 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1002 | Done: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1003 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1006 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1007 | /** |
| 1008 | * @brief Map the CBOR major types for strings to the QCBOR types. |
| 1009 | * |
| 1010 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1011 | * @retturns QCBOR type number. |
| 1012 | * |
| 1013 | * This only works for the two string types. |
| 1014 | */ |
| 1015 | static inline uint8_t ConvertStringMajorTypes(int nCBORMajorType) |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1016 | { |
| 1017 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 1018 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 1019 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1020 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1021 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 1022 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 1023 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1024 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1025 | return (uint8_t)(nCBORMajorType + 4); |
| 1026 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1027 | |
| 1028 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1029 | /** |
| 1030 | * @brief Map the CBOR major types for arrays/maps to the QCBOR types. |
| 1031 | * |
| 1032 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1033 | * @retturns QCBOR type number. |
| 1034 | * |
| 1035 | * This only works for the two aggregate types. |
| 1036 | */ |
| 1037 | static inline uint8_t ConvertArrayOrMapType(int nCBORMajorType) |
| 1038 | { |
| 1039 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1040 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1041 | #endif |
| 1042 | |
| 1043 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1044 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1045 | #endif |
| 1046 | |
| 1047 | return (uint8_t)(nCBORMajorType); |
| 1048 | } |
| 1049 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1050 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1051 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 1052 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1053 | * |
| 1054 | * @param[in] pUInBuf Input buffer to read data item from. |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1055 | * @param[out] pDecodedItem The filled-in decoded item. |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1056 | * @param[in] pAllocator The allocator to use for strings or NULL. |
| 1057 | * |
| 1058 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1059 | * @retval QCBOR_ERR_HIT_END |
| 1060 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1061 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1062 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1063 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1064 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1065 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1066 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1067 | * This decodes the most primitive / atomic data item. It does |
| 1068 | * no combing of data items. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1069 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1070 | static QCBORError |
| 1071 | DecodeAtomicDataItem(UsefulInputBuf *pUInBuf, |
| 1072 | QCBORItem *pDecodedItem, |
| 1073 | const QCBORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1074 | { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1075 | QCBORError uReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1076 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1077 | /* Get the major type and the argument. The argument could be |
| 1078 | * length of more bytes or the value depending on the major |
| 1079 | * type. nAdditionalInfo is an encoding of the length of the |
| 1080 | * uNumber and is needed to decode floats and doubles. |
| 1081 | */ |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 1082 | int nMajorType = 0; |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1083 | uint64_t uArgument = 0; |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 1084 | int nAdditionalInfo = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1085 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 1086 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1087 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1088 | uReturn = DecodeHead(pUInBuf, &nMajorType, &uArgument, &nAdditionalInfo); |
| 1089 | if(uReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1090 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1091 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1092 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1093 | /* At this point the major type and the argument are valid. We've |
| 1094 | * got the type and the argument that starts every CBOR data item. |
| 1095 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1096 | switch (nMajorType) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1097 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1098 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1099 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1100 | uReturn = QCBOR_ERR_BAD_INT; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1101 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1102 | uReturn = DecodeInteger(nMajorType, uArgument, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1103 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1104 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1105 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1106 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1107 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
| 1108 | pDecodedItem->uDataType = ConvertStringMajorTypes(nMajorType); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1109 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1110 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1111 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1112 | uReturn = DecodeBytes(pAllocator, uArgument, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1113 | } |
| 1114 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1115 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1116 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1117 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1118 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1119 | /* Indefinite-length string. */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1120 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1121 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1122 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1123 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1124 | break; |
| 1125 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1126 | } else { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1127 | /* Definite-length string. */ |
| 1128 | if(uArgument > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1129 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1130 | goto Done; |
| 1131 | } |
| 1132 | /* cast OK because of check above */ |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1133 | pDecodedItem->val.uCount = (uint16_t)uArgument; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1134 | } |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1135 | pDecodedItem->uDataType = ConvertArrayOrMapType(nMajorType); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1136 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1137 | |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1138 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1139 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1140 | uReturn = QCBOR_ERR_BAD_INT; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1141 | } else { |
Laurence Lundblade | 1c92972 | 2020-12-27 02:44:57 -0800 | [diff] [blame] | 1142 | pDecodedItem->val.uTagV = uArgument; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1143 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1144 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1145 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1146 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1147 | case CBOR_MAJOR_TYPE_SIMPLE: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1148 | /* Major type 7: float, double, true, false, null... */ |
| 1149 | uReturn = DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1150 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1151 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1152 | default: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1153 | /* Never happens because DecodeHead() should never return > 7 */ |
| 1154 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1155 | break; |
| 1156 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1157 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1158 | Done: |
Laurence Lundblade | 554ebd5 | 2020-12-29 03:26:06 -0800 | [diff] [blame] | 1159 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1163 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 1164 | * @brief Process indefinite length strings (decode layer 5). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1165 | * |
| 1166 | * @param[in] pMe Decoder context |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1167 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1168 | * |
| 1169 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1170 | * @retval QCBOR_ERR_HIT_END |
| 1171 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1172 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1173 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1174 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1175 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1176 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1177 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1178 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1179 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1180 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1181 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1182 | * |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1183 | * If it is, this loops getting the subsequent chunk data items that |
| 1184 | * make up the string. The string allocator is used to make a |
| 1185 | * contiguous buffer for the chunks. When this completes @c |
| 1186 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1187 | * |
| 1188 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1189 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1190 | static inline QCBORError |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1191 | QCBORDecode_GetNextFullString(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1192 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1193 | /* Aproximate stack usage |
| 1194 | * 64-bit 32-bit |
| 1195 | * local vars 32 16 |
| 1196 | * 2 UsefulBufs 32 16 |
| 1197 | * QCBORItem 56 52 |
| 1198 | * TOTAL 120 74 |
| 1199 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1200 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1201 | /* The string allocator is used here for two purposes: 1) |
| 1202 | * coalescing the chunks of an indefinite length string, 2) |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1203 | * allocating storage for every string returned when requested. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1204 | * |
| 1205 | * The first use is below in this function. Indefinite length |
| 1206 | * strings cannot be processed at all without a string allocator. |
| 1207 | * |
| 1208 | * The second used is in DecodeBytes() which is called by |
| 1209 | * GetNext_Item() below. This second use unneccessary for most use |
| 1210 | * and only happens when requested in the call to |
| 1211 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1212 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1213 | * |
| 1214 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1215 | * allocator altogether and thus both of these uses. It reduced the |
| 1216 | * decoder object code by about 400 bytes. |
| 1217 | */ |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1218 | const QCBORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1219 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1220 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1221 | const QCBORInternalAllocator *pAllocator = NULL; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1222 | |
| 1223 | if(pMe->StringAllocator.pfAllocator) { |
| 1224 | pAllocator = &(pMe->StringAllocator); |
| 1225 | if(pMe->bStringAllocateAll) { |
| 1226 | pAllocatorForGetNext = pAllocator; |
| 1227 | } |
| 1228 | } |
| 1229 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1230 | |
| 1231 | QCBORError uReturn; |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1232 | uReturn = DecodeAtomicDataItem(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1233 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1234 | goto Done; |
| 1235 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1236 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1237 | /* Only do indefinite length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1238 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1239 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1240 | goto Done; |
| 1241 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1242 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1243 | /* Is this a string with an indefinite length? */ |
| 1244 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1245 | goto Done; |
| 1246 | } |
| 1247 | |
| 1248 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 1249 | /* Can't do indefinite length strings without a string allocator */ |
| 1250 | if(pAllocator == NULL) { |
| 1251 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1252 | goto Done; |
| 1253 | } |
| 1254 | |
| 1255 | /* Loop getting chunks of the indefinite length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1256 | UsefulBufC FullString = NULLUsefulBufC; |
| 1257 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1258 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1259 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1260 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1261 | /* Pass a NULL string allocator to GetNext_Item() because the |
| 1262 | * individual string chunks in an indefinite length should not |
| 1263 | * be allocated. They are always copied in the the contiguous |
| 1264 | * buffer allocated here. |
| 1265 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1266 | uReturn = DecodeAtomicDataItem(&(pMe->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1267 | if(uReturn) { |
| 1268 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1269 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1270 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1271 | /* Is item is the marker for end of the indefinite length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1272 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1273 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1274 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1275 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1276 | break; |
| 1277 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1278 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1279 | /* All chunks must be of the same type, the type of the item |
| 1280 | * that introduces the indefinite length string. This also |
| 1281 | * catches errors where the chunk is not a string at all and an |
| 1282 | * indefinite length string inside an indefinite length string. |
| 1283 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1284 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1285 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1286 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1287 | break; |
| 1288 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1289 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1290 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1291 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1292 | * not NULL and a reallocation happens. |
| 1293 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1294 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1295 | UNCONST_POINTER(FullString.ptr), |
| 1296 | FullString.len + StringChunkItem.val.string.len); |
| 1297 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1298 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1299 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1300 | break; |
| 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 | /* Copy new string chunk to the end of accumulated string */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1304 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1305 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1306 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1307 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1308 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1309 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1310 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1311 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1312 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1313 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1314 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1315 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1316 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1319 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1320 | /** |
| 1321 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1322 | * |
| 1323 | * @param[in] pMe The decode context. |
| 1324 | * @param[in] uUnMappedTag The tag number to map |
| 1325 | * @param[out] puMappedTagNumer The stored tag number. |
| 1326 | * |
| 1327 | * @return error code. |
| 1328 | * |
| 1329 | * The main point of mapping tag numbers is make QCBORItem |
| 1330 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1331 | * bytes. Without, it would take up 32 bytes. |
| 1332 | * |
| 1333 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1334 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1335 | * |
| 1336 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1337 | */ |
| 1338 | static inline QCBORError |
| 1339 | MapTagNumber(QCBORDecodeContext *pMe, uint64_t uUnMappedTag, uint16_t *puMappedTagNumer) |
| 1340 | { |
| 1341 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1342 | unsigned uTagMapIndex; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1343 | /* Is there room in the tag map, or is it in it already? */ |
| 1344 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1345 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1346 | break; |
| 1347 | } |
| 1348 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1349 | break; |
| 1350 | } |
| 1351 | } |
| 1352 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1353 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1354 | } |
| 1355 | |
| 1356 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1357 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1358 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1359 | |
| 1360 | } else { |
| 1361 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1362 | } |
| 1363 | |
| 1364 | return QCBOR_SUCCESS; |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | /** |
| 1369 | * @brief This converts a mapped tag number to the actual tag number. |
| 1370 | * |
| 1371 | * @param[in] pMe The decode context. |
| 1372 | * @param[in] uMappedTagNumber The stored tag number. |
| 1373 | * |
| 1374 | * @return The actual tag number is returned or |
| 1375 | * @ref CBOR_TAG_INVALID64 on error. |
| 1376 | * |
| 1377 | * This is the reverse of MapTagNumber() |
| 1378 | */ |
| 1379 | static uint64_t |
| 1380 | UnMapTagNumber(const QCBORDecodeContext *pMe, uint16_t uMappedTagNumber) |
| 1381 | { |
| 1382 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1383 | return uMappedTagNumber; |
| 1384 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1385 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1386 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1387 | /* This won't be negative because of code below in |
| 1388 | * MapTagNumber() |
| 1389 | */ |
| 1390 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1391 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1395 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1396 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 1397 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1398 | * |
| 1399 | * @param[in] pMe Decoder context |
| 1400 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1401 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1402 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1403 | * @retval QCBOR_ERR_HIT_END |
| 1404 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1405 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1406 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1407 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1408 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1409 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1410 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1411 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1412 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1413 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1414 | * |
| 1415 | * This loops getting atomic data items until one is not a tag |
| 1416 | * number. Usually this is largely pass-through because most |
| 1417 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1418 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1419 | static QCBORError |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1420 | QCBORDecode_GetNextTagNumber(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1421 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1422 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1423 | |
| 1424 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1425 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1426 | /* Be sure the memset does the right thing. */ |
| 1427 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1428 | #endif |
| 1429 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1430 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1431 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1432 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1433 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1434 | for(;;) { |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1435 | QCBORError uErr = QCBORDecode_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1436 | if(uErr != QCBOR_SUCCESS) { |
| 1437 | uReturn = uErr; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1438 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1439 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1440 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1441 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1442 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1443 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1444 | break; |
| 1445 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1446 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1447 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1448 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1449 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1450 | /* Continue on to get all tags wrapping this item even though |
| 1451 | * it is erroring out in the end. This allows decoding to |
| 1452 | * continue. This is a resource limit error, not a problem |
| 1453 | * with being well-formed CBOR. |
| 1454 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1455 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1456 | } |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1457 | /* Slide tags over one in the array to make room at index 0. |
| 1458 | * Must use memmove because the move source and destination |
| 1459 | * overlap. |
| 1460 | */ |
| 1461 | memmove(&auItemsTags[1], auItemsTags, sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1462 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1463 | /* Map the tag */ |
| 1464 | uint16_t uMappedTagNumer; |
| 1465 | uReturn = MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumer); |
| 1466 | /* Continue even on error so as to consume all tags wrapping |
| 1467 | * this data item so decoding can go on. If MapTagNumber() |
| 1468 | * errors once it will continue to error. |
| 1469 | */ |
| 1470 | auItemsTags[0] = uMappedTagNumer; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1471 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1472 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1473 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1474 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1478 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 1479 | * @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] | 1480 | * |
| 1481 | * @param[in] pMe Decoder context |
| 1482 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1483 | * |
| 1484 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1485 | * @retval QCBOR_ERR_HIT_END |
| 1486 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1487 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1488 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1489 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1490 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1491 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1492 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1493 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1494 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1495 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1496 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1497 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1498 | * |
| 1499 | * If a the current nesting level is a map, then this |
| 1500 | * combines pairs of items into one data item with a label |
| 1501 | * and value. |
| 1502 | * |
| 1503 | * This is pass-through if the current nesting leve is |
| 1504 | * not a map. |
| 1505 | * |
| 1506 | * This also implements maps-as-array mode where a map |
| 1507 | * is treated like an array to allow caller to do their |
| 1508 | * own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1509 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1510 | static inline QCBORError |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1511 | QCBORDecode_GetNextMapEntry(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1512 | { |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1513 | QCBORError uReturn = QCBORDecode_GetNextTagNumber(pMe, pDecodedItem); |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1514 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1515 | goto Done; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1516 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1517 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1518 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1519 | /* Break can't be a map entry */ |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1520 | goto Done; |
| 1521 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1522 | |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1523 | if(pMe->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1524 | /* Normal decoding of maps -- combine label and value into one item. */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1525 | |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1526 | if(DecodeNesting_IsCurrentTypeMap(&(pMe->nesting))) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1527 | /* Save label in pDecodedItem and get the next which will |
| 1528 | * be the real data item. |
| 1529 | */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1530 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1531 | uReturn = QCBORDecode_GetNextTagNumber(pMe, pDecodedItem); |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1532 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1533 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1534 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1535 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1536 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1537 | |
| 1538 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1539 | /* strings are always good labels */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1540 | pDecodedItem->label.string = LabelItem.val.string; |
| 1541 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1542 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == pMe->uDecodeMode) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1543 | /* It's not a string and we only want strings */ |
| 1544 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1545 | goto Done; |
| 1546 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1547 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1548 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1549 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1550 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1551 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1552 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1553 | pDecodedItem->label.string = LabelItem.val.string; |
| 1554 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1555 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1556 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1557 | /* label is not an int or a string. It is an arrray |
| 1558 | * or a float or such and this implementation doesn't handle that. |
| 1559 | * Also, tags on labels are ignored. |
| 1560 | */ |
| 1561 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1562 | goto Done; |
| 1563 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1564 | } |
| 1565 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1566 | /* Decoding of maps as arrays to let the caller decide what to do |
| 1567 | * about labels, particularly lables that are not integers or |
| 1568 | * strings. |
| 1569 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1570 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1571 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1572 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1573 | goto Done; |
| 1574 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1575 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1576 | /* Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2. |
| 1577 | * Cast is needed because of integer promotion. |
| 1578 | */ |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1579 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1580 | } |
| 1581 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1582 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1583 | Done: |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1584 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1588 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1589 | /** |
| 1590 | * @brief Peek and see if next data item is a break; |
| 1591 | * |
| 1592 | * @param[in] pUIB UsefulInputBuf to read from. |
| 1593 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1594 | * |
| 1595 | * @return Any decoding error. |
| 1596 | * |
| 1597 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1598 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1599 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1600 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1601 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1602 | { |
| 1603 | *pbNextIsBreak = false; |
| 1604 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1605 | QCBORItem Peek; |
| 1606 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1607 | QCBORError uReturn = DecodeAtomicDataItem(pUIB, &Peek, NULL); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1608 | if(uReturn != QCBOR_SUCCESS) { |
| 1609 | return uReturn; |
| 1610 | } |
| 1611 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1612 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1613 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1614 | } else { |
| 1615 | *pbNextIsBreak = true; |
| 1616 | } |
| 1617 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1618 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1619 | return QCBOR_SUCCESS; |
| 1620 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1621 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1622 | |
| 1623 | |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1624 | /** |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1625 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1626 | * |
| 1627 | * @param[in] pMe The decode context. |
| 1628 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
| 1629 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1630 | * An item was just consumed, now figure out if it was the |
| 1631 | * end of an array/map map that can be closed out. That |
| 1632 | * may in turn close out the above array/map... |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1633 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1634 | static QCBORError |
| 1635 | QCBORDecode_NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1636 | { |
| 1637 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1638 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1639 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1640 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1641 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1642 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1643 | /* Nesting level is bstr-wrapped CBOR */ |
| 1644 | |
| 1645 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1646 | * so no further ascending can happen. |
| 1647 | */ |
| 1648 | break; |
| 1649 | |
| 1650 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1651 | /* Level is a definite-length array/map */ |
| 1652 | |
| 1653 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1654 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1655 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1656 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1657 | break; |
| 1658 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1659 | /* All items in a definite length array were consumed so it |
| 1660 | * is time to ascend one level. This happens below. |
| 1661 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1662 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1663 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1664 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1665 | /* Level is an indefinite-length array/map. */ |
| 1666 | |
| 1667 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1668 | bool bIsBreak = false; |
| 1669 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1670 | if(uReturn != QCBOR_SUCCESS) { |
| 1671 | goto Done; |
| 1672 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1673 | |
| 1674 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1675 | /* 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] | 1676 | break; |
| 1677 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1678 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1679 | /* It was a break in an indefinite length map / array so |
| 1680 | * it is time to ascend one level. |
| 1681 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1682 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1683 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1684 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1685 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1686 | |
| 1687 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1688 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1689 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1690 | * QCBORDecode_ExitBoundedMode(). |
| 1691 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1692 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1693 | /* Set the count to zero for definite length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1694 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1695 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1696 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1697 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1698 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1699 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1700 | break; |
| 1701 | } |
| 1702 | |
| 1703 | /* Finally, actually ascend one level. */ |
| 1704 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1705 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1706 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1707 | uReturn = QCBOR_SUCCESS; |
| 1708 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1709 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1710 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1711 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1712 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1713 | return uReturn; |
| 1714 | } |
| 1715 | |
| 1716 | |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1717 | /** |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 1718 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1719 | * |
| 1720 | * @param[in] pMe Decoder context |
| 1721 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1722 | * |
| 1723 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1724 | * @retval QCBOR_ERR_HIT_END |
| 1725 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1726 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1727 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1728 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1729 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1730 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1731 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1732 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1733 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1734 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1735 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1736 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1737 | * @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1738 | * @retval QCBOR_ERR_BAD_BREAK |
| 1739 | * @retval QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
| 1740 | * |
| 1741 | * This handles the traversal descending into and asecnding out of |
| 1742 | * maps, arrays and bstr-wrapped CBOR. It figures out the ends of |
| 1743 | * definite and indefinte-length maps and arrays by looking at the |
| 1744 | * item count or finding CBOR breaks. It detects the ends of the |
| 1745 | * top-level sequence and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1746 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1747 | static QCBORError |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1748 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1749 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1750 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1751 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1752 | |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1753 | /* If out of bytes to consume, it is either the end of the |
| 1754 | * top-level sequence of some bstr-wrapped CBOR that was entered. |
| 1755 | * |
| 1756 | * In the case of bstr-wrapped CBOR, the length of the |
| 1757 | * UsefulInputBuf was set to that of the bstr-wrapped CBOR. When |
| 1758 | * the bstr-wrapped CBOR is exited, the length is set back to the |
| 1759 | * top-level's length or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1760 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1761 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1762 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1763 | goto Done; |
| 1764 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1765 | |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1766 | /* Check to see if at the end of a bounded definite length map or |
| 1767 | * array. The check for a break ending indefinite length array is |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1768 | * later in QCBORDecode_NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1769 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1770 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1771 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1772 | goto Done; |
| 1773 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1774 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1775 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1776 | uReturn = QCBORDecode_GetNextMapEntry(pMe, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1777 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1778 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1779 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1780 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1781 | |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1782 | /* Breaks ending arrays/maps are processed later in the call to |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1783 | * QCBORDecode_NestLevelAscender(). They should never show up here. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1784 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1785 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1786 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1787 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1788 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1789 | |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1790 | /* Record the nesting level for this data item before processing |
| 1791 | * any of decrementing and descending. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1792 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1793 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1794 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1795 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1796 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1797 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1798 | /* If the new item is a map or array, descend. |
| 1799 | * |
| 1800 | * Empty indefinite length maps and arrays are descended into, |
| 1801 | * but then ascended out of in the next chunk of code. |
| 1802 | * |
| 1803 | * Maps and arrays do count as items in the map/array that |
| 1804 | * encloses them so a decrement needs to be done for them too, |
| 1805 | * but that is done only when all the items in them have been |
| 1806 | * processed, not when they are opened with the exception of an |
| 1807 | * empty map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1808 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1809 | QCBORError uDescendErr; |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1810 | uDescendErr = DecodeNesting_DescendMapOrArray(&(pMe->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1811 | pDecodedItem->uDataType, |
| 1812 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1813 | if(uDescendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1814 | /* This error is probably a traversal error and it overrides |
| 1815 | * the non-traversal error. |
| 1816 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1817 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1818 | goto Done; |
| 1819 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1822 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1823 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1824 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1825 | /* The following cases are handled here: |
| 1826 | * - A non-aggregate item like an integer or string |
| 1827 | * - An empty definite length map or array |
| 1828 | * - An indefinite length map or array that might be empty or might not. |
| 1829 | * |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1830 | * QCBORDecode_NestLevelAscender() does the work of decrementing the count |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1831 | * for an definite length map/array and break detection for an |
| 1832 | * indefinite length map/array. If the end of the map/array was |
| 1833 | * reached, then it ascends nesting levels, possibly all the way |
| 1834 | * to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1835 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1836 | QCBORError uAscendErr; |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1837 | uAscendErr = QCBORDecode_NestLevelAscender(pMe, true); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1838 | if(uAscendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1839 | /* This error is probably a traversal error and it overrides |
| 1840 | * the non-traversal error. |
| 1841 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1842 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1843 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1844 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1845 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1846 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1847 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1848 | /* Tell the caller what level is next. This tells them what |
| 1849 | * maps/arrays were closed out and makes it possible for them to |
| 1850 | * reconstruct the tree with just the information returned in a |
| 1851 | * QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1852 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1853 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1854 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1855 | pDecodedItem->uNextNestLevel = 0; |
| 1856 | } else { |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 1857 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1858 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1859 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1860 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1861 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1864 | |
Laurence Lundblade | 00c54bf | 2021-01-03 23:17:38 -0800 | [diff] [blame] | 1865 | /** |
| 1866 | * @brief Shift 0th tag out of the tag list. |
| 1867 | * |
| 1868 | * pDecodedItem[in,out] The data item to convert. |
| 1869 | * |
| 1870 | * The 0th tag is discarded. \ref CBOR_TAG_INVALID16 is |
| 1871 | * shifted into empty slot at the end of the tag list. |
| 1872 | */ |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1873 | static inline void ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1874 | { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 1875 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 1876 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 1877 | } |
| 1878 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -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; |
| 2114 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2115 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2116 | return QCBOR_SUCCESS; |
| 2117 | } |
| 2118 | |
| 2119 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2120 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2121 | * Table of CBOR tags whose content is either a text string or a byte |
| 2122 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2123 | * of uQCBORtype indicates the content should be a byte string rather |
| 2124 | * than a text string |
| 2125 | */ |
| 2126 | struct StringTagMapEntry { |
| 2127 | uint16_t uTagNumber; |
| 2128 | uint8_t uQCBORtype; |
| 2129 | }; |
| 2130 | |
| 2131 | #define IS_BYTE_STRING_BIT 0x80 |
| 2132 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2133 | |
| 2134 | static const struct StringTagMapEntry StringTagMap[] = { |
| 2135 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
| 2136 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2137 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2138 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2139 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
| 2140 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2141 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2142 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2143 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
| 2144 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2145 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2146 | }; |
| 2147 | |
| 2148 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2149 | /** |
| 2150 | * @brief Process standard CBOR tags whose content is a string |
| 2151 | * |
| 2152 | * @param[in] uTag The tag. |
| 2153 | * @param[in,out] pDecodedItem The data item. |
| 2154 | * |
| 2155 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2156 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
| 2157 | * \ref QCBOR_ERR_BAD_OPT_TAG if the content type was wrong for the tag. |
| 2158 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2159 | * Process the CBOR tags that whose content is a byte string or a text |
| 2160 | * string and for which the string is just passed on to the caller. |
| 2161 | * |
| 2162 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2163 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2164 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2165 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2166 | */ |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2167 | static inline QCBORError |
| 2168 | ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2169 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2170 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2171 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2172 | return QCBOR_ERR_UNSUPPORTED; |
| 2173 | } |
| 2174 | |
| 2175 | unsigned uIndex; |
| 2176 | for(uIndex = 0; StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2177 | if(StringTagMap[uIndex].uTagNumber == uTag) { |
| 2178 | break; |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | const uint8_t uQCBORType = StringTagMap[uIndex].uQCBORtype; |
| 2183 | if(uQCBORType == QCBOR_TYPE_NONE) { |
| 2184 | /* repurpose this error to mean, not handled here */ |
| 2185 | return QCBOR_ERR_UNSUPPORTED; |
| 2186 | } |
| 2187 | |
| 2188 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2189 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2190 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2191 | } |
| 2192 | |
| 2193 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2194 | return QCBOR_ERR_BAD_OPT_TAG; |
| 2195 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2196 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2197 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2198 | return QCBOR_SUCCESS; |
| 2199 | } |
| 2200 | |
| 2201 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2202 | /** |
| 2203 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2204 | * |
| 2205 | * @param[in] pMe The decode context. |
| 2206 | * @param[out] pDecodedItem The decoded item. |
| 2207 | * |
| 2208 | * @return Decoding error code. |
| 2209 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2210 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2211 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2212 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2213 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2214 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2215 | static QCBORError |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 2216 | QCBORDecode_GetNextTagContent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2217 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2218 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2219 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2220 | uReturn = QCBORDecode_GetNextMapOrArray(pMe, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2221 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2222 | goto Done; |
| 2223 | } |
| 2224 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2225 | /* When there are no tag numbers for the item, this exits first |
| 2226 | * thing and effectively does nothing. |
| 2227 | * |
| 2228 | * This loops over all the tag numbers accumulated for this item |
| 2229 | * trying to decode and interpret them. This stops at the end of |
| 2230 | * the list or at the first tag number that can't be interpreted by |
| 2231 | * this code. This is effectively a recursive processing of the |
| 2232 | * tags number list that handles nested tags. |
| 2233 | */ |
| 2234 | while(1) { |
| 2235 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2236 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2237 | */ |
| 2238 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2239 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2240 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2241 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2242 | break; |
| 2243 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2244 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2245 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2246 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2247 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2248 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2249 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2250 | uReturn = QCBORDecode_MantissaAndExponent(pMe, pDecodedItem); |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2251 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2252 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2253 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2254 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2255 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2256 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2257 | } else { |
| 2258 | /* See if it is a pass-through byte/text string tag; process if so */ |
| 2259 | uReturn = ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2260 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2261 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
| 2262 | /* It wasn't a pass-through byte/text string tag so it is |
| 2263 | * an unknown tag. This is the exit from the loop on the |
| 2264 | * first unknown tag. It is a successful exit. |
| 2265 | */ |
| 2266 | uReturn = QCBOR_SUCCESS; |
| 2267 | break; |
| 2268 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2269 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2270 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2271 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2272 | /* Error exit from the loop */ |
| 2273 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2274 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2275 | |
| 2276 | /* A tag was successfully processed, shift it out of the list of |
| 2277 | * tags returned. This is the loop increment. |
| 2278 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2279 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2283 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2287 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2288 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2289 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2290 | QCBORError |
| 2291 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2292 | { |
| 2293 | QCBORError uErr; |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 2294 | uErr = QCBORDecode_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2295 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2296 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2297 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2298 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2299 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | |
| 2303 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2304 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2305 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2306 | QCBORError |
| 2307 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2308 | { |
| 2309 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2310 | const UsefulInputBuf Save = pMe->InBuf; |
| 2311 | |
| 2312 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2313 | |
| 2314 | pMe->nesting = SaveNesting; |
| 2315 | pMe->InBuf = Save; |
| 2316 | |
| 2317 | return uErr; |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2322 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2323 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2324 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2325 | { |
| 2326 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2327 | return; |
| 2328 | } |
| 2329 | |
| 2330 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2331 | } |
| 2332 | |
| 2333 | |
| 2334 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2335 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2336 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2337 | QCBORError |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2338 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2339 | QCBORItem *pDecodedItem, |
| 2340 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2341 | { |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2342 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2343 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2344 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2345 | if(uReturn != QCBOR_SUCCESS) { |
| 2346 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | if(pTags != NULL) { |
| 2350 | pTags->uNumUsed = 0; |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2351 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2352 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2353 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2354 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2355 | } |
| 2356 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2357 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2358 | } |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2359 | pTags->puTags[pTags->uNumUsed] = UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2360 | pTags->uNumUsed++; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2368 | /* |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 2369 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2370 | */ |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2371 | bool QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2372 | const QCBORItem *pItem, |
| 2373 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2374 | { |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 2375 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2376 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2377 | break; |
| 2378 | } |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2379 | if(UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2380 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2381 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2382 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2383 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2384 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | |
| 2388 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2389 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2390 | */ |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2391 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *pMe) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2392 | { |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2393 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2394 | |
| 2395 | if(uReturn != QCBOR_SUCCESS) { |
| 2396 | goto Done; |
| 2397 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2398 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2399 | /* Error out if all the maps/arrays are not closed out */ |
| 2400 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2401 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2402 | goto Done; |
| 2403 | } |
| 2404 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2405 | /* Error out if not all the bytes are consumed */ |
| 2406 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2407 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2408 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2409 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2410 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2411 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2412 | /* Call the destructor for the string allocator if there is one. |
| 2413 | * Always called, even if there are errors; always have to clean up. |
| 2414 | */ |
| 2415 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2416 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2417 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2418 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2422 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2423 | * Public function, see header qcbor/qcbor_decode.h file |
| 2424 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2425 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2426 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2427 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2428 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2429 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2430 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2431 | return CBOR_TAG_INVALID64; |
| 2432 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2433 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2434 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2435 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 2436 | return UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2440 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2441 | /* |
Laurence Lundblade | 123224b | 2021-01-03 20:58:06 -0800 | [diff] [blame] | 2442 | * Public function, see header qcbor/qcbor_decode.h file |
| 2443 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2444 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2445 | uint32_t uIndex) |
| 2446 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2447 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2448 | return CBOR_TAG_INVALID64; |
| 2449 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2450 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2451 | return CBOR_TAG_INVALID64; |
| 2452 | } else { |
Laurence Lundblade | 25403ed | 2021-01-02 16:28:07 -0800 | [diff] [blame] | 2453 | return UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2454 | } |
| 2455 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2456 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2457 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2458 | |
| 2459 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2460 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2461 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2462 | /* =========================================================================== |
| 2463 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2464 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2465 | This implements a simple sting allocator for indefinite length |
| 2466 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2467 | implements the function type QCBORStringAllocate and allows easy |
| 2468 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2469 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2470 | This particular allocator is built-in for convenience. The caller |
| 2471 | can implement their own. All of this following code will get |
| 2472 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2473 | |
| 2474 | This is a very primitive memory allocator. It does not track |
| 2475 | individual allocations, only a high-water mark. A free or |
| 2476 | reallocation must be of the last chunk allocated. |
| 2477 | |
| 2478 | The size of the pool and offset to free memory are packed into the |
| 2479 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2480 | the decode context. Since the address of the pool may not be |
| 2481 | aligned, they have to be packed and unpacked as if they were |
| 2482 | serialized data of the wire or such. |
| 2483 | |
| 2484 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2485 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2486 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2487 | |
| 2488 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2489 | static inline int |
| 2490 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2491 | { |
| 2492 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2493 | UsefulInputBuf UIB; |
| 2494 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2495 | // Just assume the size here. It was checked during SetUp so |
| 2496 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2497 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2498 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2499 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2500 | return UsefulInputBuf_GetError(&UIB); |
| 2501 | } |
| 2502 | |
| 2503 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2504 | static inline int |
| 2505 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2506 | { |
| 2507 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2508 | // length check performed here is useful. |
| 2509 | UsefulOutBuf UOB; |
| 2510 | |
| 2511 | UsefulOutBuf_Init(&UOB, Pool); |
| 2512 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2513 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2514 | return UsefulOutBuf_GetError(&UOB); |
| 2515 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2516 | |
| 2517 | |
| 2518 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2519 | Internal function for an allocation, reallocation free and destuct. |
| 2520 | |
| 2521 | Having only one function rather than one each per mode saves space in |
| 2522 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2523 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2524 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2525 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2526 | static UsefulBuf |
| 2527 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2528 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2529 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2530 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2531 | uint32_t uPoolSize; |
| 2532 | uint32_t uFreeOffset; |
| 2533 | |
| 2534 | if(uNewSize > UINT32_MAX) { |
| 2535 | // This allocator is only good up to 4GB. This check should |
| 2536 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2537 | goto Done; |
| 2538 | } |
| 2539 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2540 | |
| 2541 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2542 | goto Done; |
| 2543 | } |
| 2544 | |
| 2545 | if(uNewSize) { |
| 2546 | if(pMem) { |
| 2547 | // REALLOCATION MODE |
| 2548 | // Calculate pointer to the end of the memory pool. It is |
| 2549 | // assumed that pPool + uPoolSize won't wrap around by |
| 2550 | // assuming the caller won't pass a pool buffer in that is |
| 2551 | // not in legitimate memory space. |
| 2552 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2553 | |
| 2554 | // Check that the pointer for reallocation is in the range of the |
| 2555 | // pool. This also makes sure that pointer math further down |
| 2556 | // doesn't wrap under or over. |
| 2557 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2558 | // Offset to start of chunk for reallocation. This won't |
| 2559 | // wrap under because of check that pMem >= pPool. Cast |
| 2560 | // is safe because the pool is always less than UINT32_MAX |
| 2561 | // because of check in QCBORDecode_SetMemPool(). |
| 2562 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2563 | |
| 2564 | // Check to see if the allocation will fit. uPoolSize - |
| 2565 | // uMemOffset will not wrap under because of check that |
| 2566 | // pMem is in the range of the uPoolSize by check above. |
| 2567 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2568 | ReturnValue.ptr = pMem; |
| 2569 | ReturnValue.len = uNewSize; |
| 2570 | |
| 2571 | // Addition won't wrap around over because uNewSize was |
| 2572 | // checked to be sure it is less than the pool size. |
| 2573 | uFreeOffset = uMemOffset + uNewSize32; |
| 2574 | } |
| 2575 | } |
| 2576 | } else { |
| 2577 | // ALLOCATION MODE |
| 2578 | // uPoolSize - uFreeOffset will not underflow because this |
| 2579 | // pool implementation makes sure uFreeOffset is always |
| 2580 | // smaller than uPoolSize through this check here and |
| 2581 | // reallocation case. |
| 2582 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2583 | ReturnValue.len = uNewSize; |
| 2584 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2585 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2586 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2587 | } |
| 2588 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2589 | if(pMem) { |
| 2590 | // FREE MODE |
| 2591 | // Cast is safe because of limit on pool size in |
| 2592 | // QCBORDecode_SetMemPool() |
| 2593 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2594 | } else { |
| 2595 | // DESTRUCT MODE |
| 2596 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2597 | } |
| 2598 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2599 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2600 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2601 | MemPool_Pack(Pool, uFreeOffset); |
| 2602 | |
| 2603 | Done: |
| 2604 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2605 | } |
| 2606 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2607 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2608 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2609 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2610 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2611 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2612 | UsefulBuf Pool, |
| 2613 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2614 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2615 | // The pool size and free mem offset are packed into the beginning |
| 2616 | // of the pool memory. This compile time check make sure the |
| 2617 | // constant in the header is correct. This check should optimize |
| 2618 | // down to nothing. |
| 2619 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2620 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2621 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2622 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2623 | // The pool size and free offset packed in to the beginning of pool |
| 2624 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2625 | // machines. |
| 2626 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2627 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2628 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2629 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2630 | // This checks that the pool buffer given is big enough. |
| 2631 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2632 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2633 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2634 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2635 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2636 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2637 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2638 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2639 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2640 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2641 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2642 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2643 | |
| 2644 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2645 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2646 | { |
| 2647 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2648 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2649 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2650 | |
| 2651 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2652 | Consume an entire map or array (and do next to |
| 2653 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2654 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2655 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2656 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2657 | const QCBORItem *pItemToConsume, |
| 2658 | uint_fast8_t *puNextNestLevel) |
| 2659 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2660 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2661 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2662 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2663 | // If it is a map or array, this will tell if it is empty. |
| 2664 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2665 | |
| 2666 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2667 | /* 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] | 2668 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2669 | /* This works for definite and indefinite length |
| 2670 | * maps and arrays by using the nesting level |
| 2671 | */ |
| 2672 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2673 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2674 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2675 | goto Done; |
| 2676 | } |
| 2677 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2678 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2679 | *puNextNestLevel = Item.uNextNestLevel; |
| 2680 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2681 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2682 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2683 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2684 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2685 | /* Just pass the nesting level through */ |
| 2686 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2687 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2688 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2689 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2690 | |
| 2691 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2692 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2696 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2697 | Works only for integer and string labels. Returns false |
| 2698 | for any other type. */ |
| 2699 | static inline bool |
| 2700 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2701 | { |
| 2702 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2703 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2704 | return true; |
| 2705 | } |
| 2706 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2707 | 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] | 2708 | return true; |
| 2709 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2710 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2711 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2712 | return true; |
| 2713 | } |
| 2714 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2715 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2716 | return true; |
| 2717 | } |
| 2718 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2719 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2720 | /* Other label types are never matched */ |
| 2721 | return false; |
| 2722 | } |
| 2723 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2724 | |
| 2725 | /* |
| 2726 | Returns true if Item1 and Item2 are the same type |
| 2727 | or if either are of QCBOR_TYPE_ANY. |
| 2728 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2729 | static inline bool |
| 2730 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2731 | { |
| 2732 | if(Item1.uDataType == Item2.uDataType) { |
| 2733 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2734 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2735 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2736 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2737 | return true; |
| 2738 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2743 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2744 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2745 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2746 | @param[in] pMe The decode context to search. |
| 2747 | @param[in,out] pItemArray The items to search for and the items found. |
| 2748 | @param[out] puOffset Byte offset of last item matched. |
| 2749 | @param[in] pCBContext Context for the not-found item call back. |
| 2750 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2751 | |
| 2752 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2753 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2754 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2755 | were found for one of the labels being |
| 2756 | search for. This duplicate detection is |
| 2757 | only performed for items in pItemArray, |
| 2758 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2759 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2760 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2761 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2762 | |
| 2763 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2764 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2765 | On input pItemArray contains a list of labels and data types |
| 2766 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2767 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2768 | On output the fully retrieved items are filled in with |
| 2769 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2770 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2771 | 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] | 2772 | |
| 2773 | 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] | 2774 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2775 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2776 | MapSearch(QCBORDecodeContext *pMe, |
| 2777 | QCBORItem *pItemArray, |
| 2778 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2779 | void *pCBContext, |
| 2780 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2781 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2782 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2783 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2784 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2785 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2786 | uReturn = pMe->uLastError; |
| 2787 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2788 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2789 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2790 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2791 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2792 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2793 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2794 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2795 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2796 | } |
| 2797 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2798 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2799 | // It is an empty bounded array or map |
| 2800 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2801 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2802 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2803 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2804 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2805 | // Nothing is ever found in an empty array or map. All items |
| 2806 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2807 | uReturn = QCBOR_SUCCESS; |
| 2808 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2809 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2810 | } |
| 2811 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2812 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2813 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2814 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2815 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2816 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2817 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2818 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2819 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2820 | Loop over all the items in the map or array. Each item |
| 2821 | could be a map or array, but label matching is only at |
| 2822 | the main level. This handles definite and indefinite |
| 2823 | length maps and arrays. The only reason this is ever |
| 2824 | called on arrays is to find their end position. |
| 2825 | |
| 2826 | This will always run over all items in order to do |
| 2827 | duplicate detection. |
| 2828 | |
| 2829 | This will exit with failure if it encounters an |
| 2830 | unrecoverable error, but continue on for recoverable |
| 2831 | errors. |
| 2832 | |
| 2833 | If a recoverable error occurs on a matched item, then |
| 2834 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2835 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2836 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2837 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2838 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2839 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2840 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2841 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2842 | /* Get the item */ |
| 2843 | QCBORItem Item; |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 2844 | QCBORError uResult = QCBORDecode_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2845 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2846 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2847 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2848 | goto Done; |
| 2849 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2850 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2851 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2852 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2853 | goto Done; |
| 2854 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2855 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2856 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2857 | bool bMatched = false; |
| 2858 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 2859 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2860 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2861 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2862 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2863 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2864 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2865 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2866 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2867 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2868 | goto Done; |
| 2869 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2870 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2871 | if(uResult != QCBOR_SUCCESS) { |
| 2872 | uReturn = uResult; |
| 2873 | goto Done; |
| 2874 | } |
| 2875 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2876 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2877 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2878 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2879 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2880 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2881 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2882 | bMatched = true; |
| 2883 | } |
| 2884 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2885 | |
| 2886 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2887 | if(!bMatched && pfCallback != NULL) { |
| 2888 | /* |
| 2889 | Call the callback on unmatched labels. |
| 2890 | (It is tempting to do duplicate detection here, but that would |
| 2891 | require dynamic memory allocation because the number of labels |
| 2892 | that might be encountered is unbounded.) |
| 2893 | */ |
| 2894 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2895 | if(uReturn != QCBOR_SUCCESS) { |
| 2896 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2897 | } |
| 2898 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2899 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2900 | /* |
| 2901 | Consume the item whether matched or not. This |
| 2902 | does the work of traversing maps and array and |
| 2903 | everything in them. In this loop only the |
| 2904 | items at the current nesting level are examined |
| 2905 | to match the labels. |
| 2906 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2907 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2908 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2909 | goto Done; |
| 2910 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2911 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2912 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2913 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2914 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2915 | |
| 2916 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2917 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2918 | // Check here makes sure that this won't accidentally be |
| 2919 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2920 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 2921 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 2922 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2923 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 2924 | goto Done; |
| 2925 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2926 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 2927 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2928 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2929 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2930 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 2931 | |
| 2932 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2933 | /* 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] | 2934 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2935 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2936 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 2937 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2938 | } |
| 2939 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2940 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2941 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2944 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2945 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2946 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2947 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2948 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 2949 | int64_t nLabel, |
| 2950 | uint8_t uQcborType, |
| 2951 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2952 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2953 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2954 | return; |
| 2955 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2956 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2957 | QCBORItem OneItemSeach[2]; |
| 2958 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2959 | OneItemSeach[0].label.int64 = nLabel; |
| 2960 | OneItemSeach[0].uDataType = uQcborType; |
| 2961 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2962 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2963 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2964 | |
| 2965 | *pItem = OneItemSeach[0]; |
| 2966 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2967 | if(uReturn != QCBOR_SUCCESS) { |
| 2968 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2969 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2970 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2971 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2972 | } |
| 2973 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2974 | Done: |
| 2975 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2979 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2980 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2981 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2982 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 2983 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2984 | uint8_t uQcborType, |
| 2985 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2986 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2987 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2988 | return; |
| 2989 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2990 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2991 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2992 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2993 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2994 | OneItemSeach[0].uDataType = uQcborType; |
| 2995 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2996 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2997 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2998 | if(uReturn != QCBOR_SUCCESS) { |
| 2999 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3000 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3001 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3002 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3003 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3004 | } |
| 3005 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3006 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3007 | |
| 3008 | Done: |
| 3009 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3013 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3014 | static QCBORError |
| 3015 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3016 | { |
| 3017 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 3018 | if(uDataType == puTypeList[i]) { |
| 3019 | return QCBOR_SUCCESS; |
| 3020 | } |
| 3021 | } |
| 3022 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3023 | } |
| 3024 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3025 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3026 | /** |
| 3027 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3028 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3029 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3030 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3031 | @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] | 3032 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3033 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered |
| 3034 | tag value. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3035 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3036 | static QCBORError |
| 3037 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3038 | { |
| 3039 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3040 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3041 | /* There are tags that QCBOR couldn't process on this item and |
| 3042 | the caller has told us there should not be. */ |
| 3043 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3044 | } |
| 3045 | |
| 3046 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3047 | const int nItemType = pItem->uDataType; |
| 3048 | |
| 3049 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3050 | // Must match the tag and only the tag |
| 3051 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3052 | } |
| 3053 | |
| 3054 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3055 | if(uReturn == QCBOR_SUCCESS) { |
| 3056 | return QCBOR_SUCCESS; |
| 3057 | } |
| 3058 | |
| 3059 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3060 | /* Must match the content type and only the content type. |
| 3061 | There was no match just above so it is a fail. */ |
| 3062 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3063 | } |
| 3064 | |
| 3065 | /* If here it can match either the tag or the content |
| 3066 | and it hasn't matched the content, so the end |
| 3067 | result is whether it matches the tag. This is |
| 3068 | also the case that the CBOR standard discourages. */ |
| 3069 | |
| 3070 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 3071 | } |
| 3072 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3073 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3074 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3075 | // This could be semi-private if need be |
| 3076 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3077 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3078 | int64_t nLabel, |
| 3079 | TagSpecification TagSpec, |
| 3080 | QCBORItem *pItem) |
| 3081 | { |
| 3082 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3083 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3084 | return; |
| 3085 | } |
| 3086 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3087 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3088 | } |
| 3089 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3090 | |
| 3091 | // This could be semi-private if need be |
| 3092 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3093 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3094 | const char *szLabel, |
| 3095 | TagSpecification TagSpec, |
| 3096 | QCBORItem *pItem) |
| 3097 | { |
| 3098 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3099 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3100 | return; |
| 3101 | } |
| 3102 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3103 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3106 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3107 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3108 | int64_t nLabel, |
| 3109 | TagSpecification TagSpec, |
| 3110 | UsefulBufC *pString) |
| 3111 | { |
| 3112 | QCBORItem Item; |
| 3113 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3114 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3115 | *pString = Item.val.string; |
| 3116 | } |
| 3117 | } |
| 3118 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3119 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3120 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3121 | const char * szLabel, |
| 3122 | TagSpecification TagSpec, |
| 3123 | UsefulBufC *pString) |
| 3124 | { |
| 3125 | QCBORItem Item; |
| 3126 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3127 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3128 | *pString = Item.val.string; |
| 3129 | } |
| 3130 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3131 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3132 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3133 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3134 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3135 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3136 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3137 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 3138 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3139 | } |
| 3140 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3141 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3142 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3143 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3144 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3145 | QCBORItem *pItemList, |
| 3146 | void *pCallbackCtx, |
| 3147 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3148 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3149 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 3150 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3154 | /** |
| 3155 | * @brief Search for a map/array by label and enter it |
| 3156 | * |
| 3157 | * @param[in] pMe The decode context. |
| 3158 | * @param[in] pSearch The map/array to search for. |
| 3159 | * |
| 3160 | * @c pSearch is expected to contain one item of type map or array |
| 3161 | * with the label specified. The current bounded map will be searched for |
| 3162 | * this and if found will be entered. |
| 3163 | * |
| 3164 | * If the label is not found, or the item found is not a map or array, |
| 3165 | * the error state is set. |
| 3166 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3167 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3168 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3169 | // The first item in pSearch is the one that is to be |
| 3170 | // entered. It should be the only one filled in. Any other |
| 3171 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3172 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3173 | return; |
| 3174 | } |
| 3175 | |
| 3176 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3177 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3178 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3179 | return; |
| 3180 | } |
| 3181 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3182 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3183 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3184 | return; |
| 3185 | } |
| 3186 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3187 | /* |
| 3188 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3189 | * next item for the pre-order traversal cursor to be the map/array |
| 3190 | * found by MapSearch(). The next few lines of code force the |
| 3191 | * cursor to that. |
| 3192 | * |
| 3193 | * There is no need to retain the old cursor because |
| 3194 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3195 | * beginning of the map/array being entered. |
| 3196 | * |
| 3197 | * The cursor is forced by: 1) setting the input buffer position to |
| 3198 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3199 | * counter to the total in the map/array, 3) setting the nesting |
| 3200 | * level. Setting the map/array counter to the total is not |
| 3201 | * strictly correct, but this is OK because this cursor only needs |
| 3202 | * to be used to get one item and MapSearch() has already found it |
| 3203 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3204 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3205 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3206 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3207 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3208 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3209 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3210 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3211 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3215 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3216 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3217 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3218 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3219 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3220 | QCBORItem OneItemSeach[2]; |
| 3221 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3222 | OneItemSeach[0].label.int64 = nLabel; |
| 3223 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3224 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3225 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3226 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3227 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3231 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3232 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3233 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3234 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3235 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3236 | QCBORItem OneItemSeach[2]; |
| 3237 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3238 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3239 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3240 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3241 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3242 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3245 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3246 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3247 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3248 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3249 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3250 | QCBORItem OneItemSeach[2]; |
| 3251 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3252 | OneItemSeach[0].label.int64 = nLabel; |
| 3253 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3254 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3255 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3256 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3257 | } |
| 3258 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3259 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3260 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3261 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3262 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3263 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3264 | QCBORItem OneItemSeach[2]; |
| 3265 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3266 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3267 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3268 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3269 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3270 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3274 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3275 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3276 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3277 | QCBORError uErr; |
| 3278 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3279 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3280 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3281 | // Already in error state; do nothing. |
| 3282 | return; |
| 3283 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3284 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3285 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3286 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3287 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3288 | if(uErr != QCBOR_SUCCESS) { |
| 3289 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3290 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3291 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3292 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3293 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3296 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3297 | |
| 3298 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3299 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3300 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3301 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3302 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3303 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3304 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3305 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3306 | // Special case to increment nesting level for zero-length maps |
| 3307 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3308 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3309 | } |
| 3310 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3311 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3312 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3313 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3314 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3315 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3316 | if(pItem != NULL) { |
| 3317 | *pItem = Item; |
| 3318 | } |
| 3319 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3320 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3321 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3324 | |
| 3325 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3326 | This is the common work for exiting a level that is a bounded map, |
| 3327 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3328 | |
| 3329 | One chunk of work is to set up the pre-order traversal so it is at |
| 3330 | the item just after the bounded map, array or bstr that is being |
| 3331 | exited. This is somewhat complex. |
| 3332 | |
| 3333 | The other work is to level-up the bounded mode to next higest bounded |
| 3334 | mode or the top level if there isn't one. |
| 3335 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3336 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3337 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3338 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3339 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3340 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3341 | /* |
| 3342 | First the pre-order-traversal byte offset is positioned to the |
| 3343 | item just after the bounded mode item that was just consumed. |
| 3344 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3345 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3346 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3347 | /* |
| 3348 | Next, set the current nesting level to one above the bounded level |
| 3349 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3350 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3351 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3352 | makes sure pCurrentBounded is valid. |
| 3353 | */ |
| 3354 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3355 | |
| 3356 | /* |
| 3357 | This does the complex work of leveling up the pre-order traversal |
| 3358 | when the end of a map or array or another bounded level is |
| 3359 | reached. It may do nothing, or ascend all the way to the top |
| 3360 | level. |
| 3361 | */ |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 3362 | uErr = QCBORDecode_NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3363 | if(uErr != QCBOR_SUCCESS) { |
| 3364 | goto Done; |
| 3365 | } |
| 3366 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3367 | /* |
| 3368 | This makes the next highest bounded level the current bounded |
| 3369 | level. If there is no next highest level, then no bounded mode is |
| 3370 | in effect. |
| 3371 | */ |
| 3372 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3373 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3374 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3375 | |
| 3376 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3377 | return uErr; |
| 3378 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3379 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3380 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3381 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3382 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3383 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3384 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3385 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3386 | return; |
| 3387 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3388 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3389 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3390 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3391 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3392 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3393 | goto Done; |
| 3394 | } |
| 3395 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3396 | /* |
| 3397 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3398 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3399 | from previous map search, then do a dummy search. |
| 3400 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3401 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3402 | QCBORItem Dummy; |
| 3403 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3404 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3405 | if(uErr != QCBOR_SUCCESS) { |
| 3406 | goto Done; |
| 3407 | } |
| 3408 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3409 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3410 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3411 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3412 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3413 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3417 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3418 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3419 | const QCBORItem *pItem, |
| 3420 | uint8_t uTagRequirement, |
| 3421 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3422 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3423 | if(pBstr) { |
| 3424 | *pBstr = NULLUsefulBufC; |
| 3425 | } |
| 3426 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3427 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3428 | // Already in error state; do nothing. |
| 3429 | return pMe->uLastError; |
| 3430 | } |
| 3431 | |
| 3432 | QCBORError uError = QCBOR_SUCCESS; |
| 3433 | |
| 3434 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3435 | uError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3436 | goto Done;; |
| 3437 | } |
| 3438 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3439 | const TagSpecification TagSpec = |
| 3440 | { |
| 3441 | uTagRequirement, |
| 3442 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3443 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3444 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3445 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3446 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3447 | if(uError != QCBOR_SUCCESS) { |
| 3448 | goto Done; |
| 3449 | } |
| 3450 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3451 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3452 | // Reverse the decrement done by GetNext() for the bstr so the |
Laurence Lundblade | 7c12e25 | 2021-01-04 15:44:18 -0800 | [diff] [blame] | 3453 | // increment in QCBORDecode_NestLevelAscender() called by ExitBoundedLevel() |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3454 | // will work right. |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3455 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3456 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3457 | |
| 3458 | if(pBstr) { |
| 3459 | *pBstr = pItem->val.string; |
| 3460 | } |
| 3461 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3462 | // This saves the current length of the UsefulInputBuf and then |
| 3463 | // narrows the UsefulInputBuf to start and length of the wrapped |
| 3464 | // CBOR that is being entered. |
| 3465 | // |
| 3466 | // This makes sure the length is less than |
| 3467 | // QCBOR_MAX_DECODE_INPUT_SIZE which is slighly less than |
| 3468 | // UINT32_MAX. The value UINT32_MAX is used as a special indicator |
| 3469 | // value. The checks against QCBOR_MAX_DECODE_INPUT_SIZE also make |
| 3470 | // the casts safe. uEndOfBstr will always be less than |
| 3471 | // uPreviousLength because of the way UsefulInputBuf works so there |
| 3472 | // is no need to check it. There is also a range check in the |
| 3473 | // seek. |
| 3474 | // |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3475 | // Most of these calls are simple inline accessors so this doesn't |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3476 | // amount to much code. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3477 | // 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] | 3478 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3479 | if((uint32_t)uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3480 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3481 | goto Done; |
| 3482 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3483 | const size_t uEndOfBstr = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3484 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOfBstr - pItem->val.string.len); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3485 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3486 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3487 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3488 | (uint32_t)uPreviousLength, |
| 3489 | (uint32_t)uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3490 | Done: |
| 3491 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3495 | /* |
| 3496 | Public function, see header qcbor/qcbor_decode.h file |
| 3497 | */ |
| 3498 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3499 | uint8_t uTagRequirement, |
| 3500 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3501 | { |
| 3502 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3503 | // Already in error state; do nothing. |
| 3504 | return; |
| 3505 | } |
| 3506 | |
| 3507 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3508 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3509 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3510 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3511 | return; |
| 3512 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3513 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3514 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3515 | &Item, |
| 3516 | uTagRequirement, |
| 3517 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3521 | /* |
| 3522 | Public function, see header qcbor/qcbor_decode.h file |
| 3523 | */ |
| 3524 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3525 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3526 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3527 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3528 | { |
| 3529 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3530 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3531 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3532 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3533 | &Item, |
| 3534 | uTagRequirement, |
| 3535 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3536 | } |
| 3537 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3538 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3539 | /* |
| 3540 | Public function, see header qcbor/qcbor_decode.h file |
| 3541 | */ |
| 3542 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3543 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3544 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3545 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3546 | { |
| 3547 | QCBORItem Item; |
| 3548 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3549 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3550 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3551 | &Item, |
| 3552 | uTagRequirement, |
| 3553 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3554 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3555 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3556 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3557 | /* |
| 3558 | Public function, see header qcbor/qcbor_decode.h file |
| 3559 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3560 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3561 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3562 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3563 | // Already in error state; do nothing. |
| 3564 | return; |
| 3565 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3566 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3567 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3568 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3569 | return; |
| 3570 | } |
| 3571 | |
| 3572 | /* |
| 3573 | Reset the length of the UsefulInputBuf to what it was before |
| 3574 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3575 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3576 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3577 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3578 | |
| 3579 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3580 | QCBORError uErr = ExitBoundedLevel(pMe, DecodeNesting_GetEndOfBstr(&(pMe->nesting))); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3581 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3582 | } |
| 3583 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3584 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3585 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -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 | 11a064e | 2020-05-07 13:13:42 -0700 | [diff] [blame] | 3589 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3590 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3591 | static QCBORError |
| 3592 | InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3593 | { |
| 3594 | switch(pItem->uDataType) { |
| 3595 | case QCBOR_TYPE_TRUE: |
| 3596 | *pBool = true; |
| 3597 | return QCBOR_SUCCESS; |
| 3598 | break; |
| 3599 | |
| 3600 | case QCBOR_TYPE_FALSE: |
| 3601 | *pBool = false; |
| 3602 | return QCBOR_SUCCESS; |
| 3603 | break; |
| 3604 | |
| 3605 | default: |
| 3606 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3607 | break; |
| 3608 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3609 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3610 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3611 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3612 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3613 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3614 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3615 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3616 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3617 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3618 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3619 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3620 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3621 | return; |
| 3622 | } |
| 3623 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3624 | QCBORError nError; |
| 3625 | QCBORItem Item; |
| 3626 | |
| 3627 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3628 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3629 | pMe->uLastError = (uint8_t)nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3630 | return; |
| 3631 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3632 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3633 | } |
| 3634 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3635 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3636 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3637 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3638 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3639 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3640 | { |
| 3641 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3642 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3643 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3644 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3647 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3648 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3649 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3650 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3651 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3652 | { |
| 3653 | QCBORItem Item; |
| 3654 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3655 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3656 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | |
| 3660 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3661 | |
| 3662 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3663 | QCBORItem *pItem, |
| 3664 | uint8_t uTagRequirement, |
| 3665 | int64_t *pnTime) |
| 3666 | { |
| 3667 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3668 | // Already in error state, do nothing |
| 3669 | return; |
| 3670 | } |
| 3671 | |
| 3672 | QCBORError uErr; |
| 3673 | |
| 3674 | const TagSpecification TagSpec = |
| 3675 | { |
| 3676 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3677 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3678 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3679 | }; |
| 3680 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3681 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3682 | if(uErr != QCBOR_SUCCESS) { |
| 3683 | goto Done; |
| 3684 | } |
| 3685 | |
| 3686 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3687 | uErr = DecodeDateEpoch(pItem); |
| 3688 | if(uErr != QCBOR_SUCCESS) { |
| 3689 | goto Done; |
| 3690 | } |
| 3691 | } |
| 3692 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3693 | // Save the tags in the last item's tags in the decode context |
| 3694 | // for QCBORDecode_GetNthTagOfLast() |
| 3695 | CopyTags(pMe, pItem); |
| 3696 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3697 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3698 | |
| 3699 | Done: |
| 3700 | pMe->uLastError = (uint8_t)uErr; |
| 3701 | } |
| 3702 | |
| 3703 | |
| 3704 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3705 | uint8_t uTagRequirement, |
| 3706 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3707 | { |
| 3708 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3709 | // Already in error state, do nothing |
| 3710 | return; |
| 3711 | } |
| 3712 | |
| 3713 | QCBORItem Item; |
| 3714 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3715 | |
| 3716 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3717 | } |
| 3718 | |
| 3719 | |
| 3720 | void |
| 3721 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3722 | int64_t nLabel, |
| 3723 | uint8_t uTagRequirement, |
| 3724 | int64_t *pnTime) |
| 3725 | { |
| 3726 | QCBORItem Item; |
| 3727 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3728 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3729 | } |
| 3730 | |
| 3731 | |
| 3732 | void |
| 3733 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3734 | const char *szLabel, |
| 3735 | uint8_t uTagRequirement, |
| 3736 | int64_t *pnTime) |
| 3737 | { |
| 3738 | QCBORItem Item; |
| 3739 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3740 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3741 | } |
| 3742 | |
| 3743 | |
| 3744 | |
| 3745 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3746 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3747 | TagSpecification TagSpec, |
| 3748 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3749 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3750 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3751 | // Already in error state, do nothing |
| 3752 | return; |
| 3753 | } |
| 3754 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3755 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3756 | QCBORItem Item; |
| 3757 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3758 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3759 | if(uError != QCBOR_SUCCESS) { |
| 3760 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3761 | return; |
| 3762 | } |
| 3763 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3764 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3765 | |
| 3766 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3767 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 3768 | } else { |
| 3769 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3770 | } |
| 3771 | } |
| 3772 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3773 | |
| 3774 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3775 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3776 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3777 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3778 | UsefulBufC *pValue, |
| 3779 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3780 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3781 | const TagSpecification TagSpec = |
| 3782 | { |
| 3783 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3784 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3785 | {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] | 3786 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3787 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3788 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3789 | if(uErr != QCBOR_SUCCESS) { |
| 3790 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3791 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3792 | |
| 3793 | *pValue = pItem->val.string; |
| 3794 | |
| 3795 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3796 | *pbIsNegative = false; |
| 3797 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3798 | *pbIsNegative = true; |
| 3799 | } |
| 3800 | |
| 3801 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3805 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3806 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3807 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3808 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3809 | uint8_t uTagRequirement, |
| 3810 | UsefulBufC *pValue, |
| 3811 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3812 | { |
| 3813 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3814 | // Already in error state, do nothing |
| 3815 | return; |
| 3816 | } |
| 3817 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3818 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3819 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3820 | if(uError != QCBOR_SUCCESS) { |
| 3821 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3822 | return; |
| 3823 | } |
| 3824 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3825 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3826 | } |
| 3827 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3828 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3829 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3830 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3831 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3832 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3833 | int64_t nLabel, |
| 3834 | uint8_t uTagRequirement, |
| 3835 | UsefulBufC *pValue, |
| 3836 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3837 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3838 | QCBORItem Item; |
| 3839 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3840 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3841 | return; |
| 3842 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3843 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3844 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3847 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3848 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3849 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3850 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3851 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3852 | const char *szLabel, |
| 3853 | uint8_t uTagRequirement, |
| 3854 | UsefulBufC *pValue, |
| 3855 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3856 | { |
| 3857 | QCBORItem Item; |
| 3858 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3859 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3860 | return; |
| 3861 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3862 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3863 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | |
| 3867 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3868 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3869 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3870 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3871 | const QCBORItem *pItem, |
| 3872 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3873 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3874 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3875 | const TagSpecification TagSpecText = |
| 3876 | { |
| 3877 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3878 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3879 | {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] | 3880 | }; |
| 3881 | const TagSpecification TagSpecBinary = |
| 3882 | { |
| 3883 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3884 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3885 | {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] | 3886 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3887 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3888 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3889 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3890 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3891 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3892 | if(pbIsTag257 != NULL) { |
| 3893 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3894 | } |
| 3895 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3896 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3897 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3898 | if(pbIsTag257 != NULL) { |
| 3899 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3900 | } |
| 3901 | uReturn = QCBOR_SUCCESS; |
| 3902 | |
| 3903 | } else { |
| 3904 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3905 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3906 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3907 | return uReturn; |
| 3908 | } |
| 3909 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3910 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 3911 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3912 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3913 | |
| 3914 | |
| 3915 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3916 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3917 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3918 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3919 | |
| 3920 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3921 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3922 | static QCBORError |
| 3923 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3924 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3925 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3926 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3927 | if(uResult != 0) { |
| 3928 | /* This loop will run a maximum of 19 times because |
| 3929 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 3930 | * exit with the overflow error |
| 3931 | */ |
| 3932 | for(; nExponent > 0; nExponent--) { |
| 3933 | if(uResult > UINT64_MAX / 10) { |
| 3934 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 3935 | } |
| 3936 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3937 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3938 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3939 | for(; nExponent < 0; nExponent++) { |
| 3940 | uResult = uResult / 10; |
| 3941 | if(uResult == 0) { |
| 3942 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3943 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3944 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3945 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3946 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3947 | |
| 3948 | *puResult = uResult; |
| 3949 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3950 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3954 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3955 | static QCBORError |
| 3956 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3957 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3958 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3959 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3960 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3961 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3962 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3963 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3964 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3965 | */ |
| 3966 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3967 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3968 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3969 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3970 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3971 | nExponent--; |
| 3972 | } |
| 3973 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3974 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3975 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3976 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3977 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3978 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3979 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3980 | } |
| 3981 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3982 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3983 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3984 | return QCBOR_SUCCESS; |
| 3985 | } |
| 3986 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3987 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3988 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3989 | Compute value with signed mantissa and signed result. Works with |
| 3990 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3991 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3992 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 3993 | int64_t nExponent, |
| 3994 | int64_t *pnResult, |
| 3995 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3996 | { |
| 3997 | uint64_t uResult; |
| 3998 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3999 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4000 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4001 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 4002 | |
| 4003 | // Do the exponentiation of the positive mantissa |
| 4004 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4005 | if(uReturn) { |
| 4006 | return uReturn; |
| 4007 | } |
| 4008 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4009 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 4010 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4011 | of INT64_MIN. This assumes two's compliment representation where |
| 4012 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 4013 | Trying to write -INT64_MIN doesn't work to get this because the |
| 4014 | compiler tries to work with an int64_t which can't represent |
| 4015 | -INT64_MIN. |
| 4016 | */ |
| 4017 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 4018 | |
| 4019 | // Error out if too large |
| 4020 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4021 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4022 | } |
| 4023 | |
| 4024 | // Casts are safe because of checks above |
| 4025 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 4026 | |
| 4027 | return QCBOR_SUCCESS; |
| 4028 | } |
| 4029 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4030 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4031 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4032 | Compute value with signed mantissa and unsigned result. Works with |
| 4033 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4034 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4035 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 4036 | int64_t nExponent, |
| 4037 | uint64_t *puResult, |
| 4038 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4039 | { |
| 4040 | if(nMantissa < 0) { |
| 4041 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4042 | } |
| 4043 | |
| 4044 | // Cast to unsigned is OK because of check for negative |
| 4045 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 4046 | // Exponentiation is straight forward |
| 4047 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4048 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4049 | |
| 4050 | |
| 4051 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4052 | Compute value with signed mantissa and unsigned result. Works with |
| 4053 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4054 | */ |
| 4055 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 4056 | int64_t nExponent, |
| 4057 | uint64_t *puResult, |
| 4058 | fExponentiator pfExp) |
| 4059 | { |
| 4060 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4061 | } |
| 4062 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4063 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4064 | |
| 4065 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4066 | |
| 4067 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4068 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4069 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4070 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4071 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4072 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4073 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4074 | const uint8_t *pByte = BigNum.ptr; |
| 4075 | size_t uLen = BigNum.len; |
| 4076 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4077 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4078 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4079 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4080 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4083 | *pResult = uResult; |
| 4084 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4085 | } |
| 4086 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4087 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4088 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4089 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4090 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4091 | } |
| 4092 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4093 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4094 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4095 | { |
| 4096 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4097 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 4098 | if(uError) { |
| 4099 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4100 | } |
| 4101 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 4102 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4103 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4107 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4108 | { |
| 4109 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4110 | /* The negative integer furthest from zero for a C int64_t is |
| 4111 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4112 | negative number in CBOR is computed as -n - 1 where n is the |
| 4113 | encoded integer, where n is what is in the variable BigNum. When |
| 4114 | converting BigNum to a uint64_t, the maximum value is thus |
| 4115 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 4116 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4117 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4118 | -n - 1 <= INT64_MIN. |
| 4119 | -n - 1 <= -INT64_MAX - 1 |
| 4120 | n <= INT64_MAX. |
| 4121 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4122 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4123 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4124 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4125 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4126 | |
| 4127 | /// Now apply -n - 1. The cast is safe because |
| 4128 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4129 | // is the largest positive integer that an int64_t can |
| 4130 | // represent. */ |
| 4131 | *pnResult = -(int64_t)uResult - 1; |
| 4132 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4133 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4134 | } |
| 4135 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 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 | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4139 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4140 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4141 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4142 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4143 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4144 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4145 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4146 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4147 | |
| 4148 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4149 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4150 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4151 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4152 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4153 | static QCBORError |
| 4154 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4155 | { |
| 4156 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4157 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4158 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4159 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4160 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4161 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 4162 | http://www.cplusplus.com/reference/cmath/llround/ |
| 4163 | */ |
| 4164 | // Not interested in FE_INEXACT |
| 4165 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4166 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4167 | *pnValue = llround(pItem->val.dfnum); |
| 4168 | } else { |
| 4169 | *pnValue = lroundf(pItem->val.fnum); |
| 4170 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4171 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4172 | // llround() shouldn't result in divide by zero, but catch |
| 4173 | // it here in case it unexpectedly does. Don't try to |
| 4174 | // distinguish between the various exceptions because it seems |
| 4175 | // they vary by CPU, compiler and OS. |
| 4176 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4177 | } |
| 4178 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4179 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4180 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4181 | #else |
| 4182 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4183 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4184 | break; |
| 4185 | |
| 4186 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4187 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4188 | *pnValue = pItem->val.int64; |
| 4189 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4190 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4191 | } |
| 4192 | break; |
| 4193 | |
| 4194 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4195 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4196 | if(pItem->val.uint64 < INT64_MAX) { |
| 4197 | *pnValue = pItem->val.int64; |
| 4198 | } else { |
| 4199 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4200 | } |
| 4201 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4202 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4203 | } |
| 4204 | break; |
| 4205 | |
| 4206 | default: |
| 4207 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4208 | } |
| 4209 | return QCBOR_SUCCESS; |
| 4210 | } |
| 4211 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4212 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4213 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4214 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4215 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4216 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4217 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4218 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4219 | return; |
| 4220 | } |
| 4221 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4222 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4223 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4224 | if(uError) { |
| 4225 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4226 | return; |
| 4227 | } |
| 4228 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4229 | if(pItem) { |
| 4230 | *pItem = Item; |
| 4231 | } |
| 4232 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4233 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4234 | } |
| 4235 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4236 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4237 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4238 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4239 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4240 | int64_t *pnValue, |
| 4241 | QCBORItem *pItem) |
| 4242 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4243 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4244 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4245 | return; |
| 4246 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4247 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4248 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4251 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4252 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4253 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4254 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4255 | int64_t *pnValue, |
| 4256 | QCBORItem *pItem) |
| 4257 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4258 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4259 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4260 | return; |
| 4261 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4262 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4263 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4267 | /* |
| 4268 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4269 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4270 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4271 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4272 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4273 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4274 | |
| 4275 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4276 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4277 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4278 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4279 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4280 | static QCBORError |
| 4281 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4282 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4283 | switch(pItem->uDataType) { |
| 4284 | |
| 4285 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4286 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4287 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4288 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4289 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4290 | } |
| 4291 | break; |
| 4292 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4293 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4294 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4295 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4296 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4297 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4298 | } |
| 4299 | break; |
| 4300 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4301 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4302 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4303 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4304 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4305 | pItem->val.expAndMantissa.nExponent, |
| 4306 | pnValue, |
| 4307 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4308 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4309 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4310 | } |
| 4311 | break; |
| 4312 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4313 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4314 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4315 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4316 | pItem->val.expAndMantissa.nExponent, |
| 4317 | pnValue, |
| 4318 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4319 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4320 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4321 | } |
| 4322 | break; |
| 4323 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4324 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4325 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4326 | int64_t nMantissa; |
| 4327 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4328 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4329 | if(uErr) { |
| 4330 | return uErr; |
| 4331 | } |
| 4332 | return ExponentiateNN(nMantissa, |
| 4333 | pItem->val.expAndMantissa.nExponent, |
| 4334 | pnValue, |
| 4335 | Exponentitate10); |
| 4336 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4337 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4338 | } |
| 4339 | break; |
| 4340 | |
| 4341 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4342 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4343 | int64_t nMantissa; |
| 4344 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4345 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4346 | if(uErr) { |
| 4347 | return uErr; |
| 4348 | } |
| 4349 | return ExponentiateNN(nMantissa, |
| 4350 | pItem->val.expAndMantissa.nExponent, |
| 4351 | pnValue, |
| 4352 | Exponentitate10); |
| 4353 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4354 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4355 | } |
| 4356 | break; |
| 4357 | |
| 4358 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4359 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4360 | int64_t nMantissa; |
| 4361 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4362 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4363 | if(uErr) { |
| 4364 | return uErr; |
| 4365 | } |
| 4366 | return ExponentiateNN(nMantissa, |
| 4367 | pItem->val.expAndMantissa.nExponent, |
| 4368 | pnValue, |
| 4369 | Exponentitate2); |
| 4370 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4371 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4372 | } |
| 4373 | break; |
| 4374 | |
| 4375 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4376 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4377 | int64_t nMantissa; |
| 4378 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4379 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4380 | if(uErr) { |
| 4381 | return uErr; |
| 4382 | } |
| 4383 | return ExponentiateNN(nMantissa, |
| 4384 | pItem->val.expAndMantissa.nExponent, |
| 4385 | pnValue, |
| 4386 | Exponentitate2); |
| 4387 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4388 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4389 | } |
| 4390 | break; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4391 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4392 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4393 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4394 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4395 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4396 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4397 | |
| 4398 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4399 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4400 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4401 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4402 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4403 | { |
| 4404 | QCBORItem Item; |
| 4405 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4406 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4407 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4408 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4409 | // The above conversion succeeded |
| 4410 | return; |
| 4411 | } |
| 4412 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4413 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4414 | // 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] | 4415 | return; |
| 4416 | } |
| 4417 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4418 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4419 | } |
| 4420 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4421 | |
| 4422 | /* |
| 4423 | Public function, see header qcbor/qcbor_decode.h file |
| 4424 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4425 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4426 | int64_t nLabel, |
| 4427 | uint32_t uConvertTypes, |
| 4428 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4429 | { |
| 4430 | QCBORItem Item; |
| 4431 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4432 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4433 | nLabel, |
| 4434 | uConvertTypes, |
| 4435 | pnValue, |
| 4436 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4437 | |
| 4438 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4439 | // The above conversion succeeded |
| 4440 | return; |
| 4441 | } |
| 4442 | |
| 4443 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4444 | // The above conversion failed in a way that code below can't correct |
| 4445 | return; |
| 4446 | } |
| 4447 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4448 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4452 | /* |
| 4453 | Public function, see header qcbor/qcbor_decode.h file |
| 4454 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4455 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4456 | const char *szLabel, |
| 4457 | uint32_t uConvertTypes, |
| 4458 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4459 | { |
| 4460 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4461 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4462 | szLabel, |
| 4463 | uConvertTypes, |
| 4464 | pnValue, |
| 4465 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4466 | |
| 4467 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4468 | // The above conversion succeeded |
| 4469 | return; |
| 4470 | } |
| 4471 | |
| 4472 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4473 | // The above conversion failed in a way that code below can't correct |
| 4474 | return; |
| 4475 | } |
| 4476 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4477 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4481 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4482 | { |
| 4483 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4484 | case QCBOR_TYPE_DOUBLE: |
| 4485 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4486 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4487 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4488 | // Can't use llround here because it will not convert values |
| 4489 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4490 | // need to be converted so it is more complicated. |
| 4491 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4492 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4493 | if(isnan(pItem->val.dfnum)) { |
| 4494 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4495 | } else if(pItem->val.dfnum < 0) { |
| 4496 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4497 | } else { |
| 4498 | double dRounded = round(pItem->val.dfnum); |
| 4499 | // See discussion in DecodeDateEpoch() for |
| 4500 | // explanation of - 0x7ff |
| 4501 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4502 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4503 | } |
| 4504 | *puValue = (uint64_t)dRounded; |
| 4505 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4506 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4507 | if(isnan(pItem->val.fnum)) { |
| 4508 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4509 | } else if(pItem->val.fnum < 0) { |
| 4510 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4511 | } else { |
| 4512 | float fRounded = roundf(pItem->val.fnum); |
| 4513 | // See discussion in DecodeDateEpoch() for |
| 4514 | // explanation of - 0x7ff |
| 4515 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4516 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4517 | } |
| 4518 | *puValue = (uint64_t)fRounded; |
| 4519 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4520 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4521 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4522 | // round() and roundf() shouldn't result in exceptions here, but |
| 4523 | // catch them to be robust and thorough. Don't try to |
| 4524 | // distinguish between the various exceptions because it seems |
| 4525 | // they vary by CPU, compiler and OS. |
| 4526 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4527 | } |
| 4528 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4529 | } else { |
| 4530 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4531 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4532 | #else |
| 4533 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4534 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4535 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4536 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4537 | case QCBOR_TYPE_INT64: |
| 4538 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4539 | if(pItem->val.int64 >= 0) { |
| 4540 | *puValue = (uint64_t)pItem->val.int64; |
| 4541 | } else { |
| 4542 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4543 | } |
| 4544 | } else { |
| 4545 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4546 | } |
| 4547 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4548 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4549 | case QCBOR_TYPE_UINT64: |
| 4550 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4551 | *puValue = pItem->val.uint64; |
| 4552 | } else { |
| 4553 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4554 | } |
| 4555 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4556 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4557 | default: |
| 4558 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4559 | } |
| 4560 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4561 | return QCBOR_SUCCESS; |
| 4562 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4563 | |
| 4564 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4565 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4566 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4567 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4568 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4569 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4570 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4571 | return; |
| 4572 | } |
| 4573 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4574 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4575 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4576 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4577 | if(uError) { |
| 4578 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4579 | return; |
| 4580 | } |
| 4581 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4582 | if(pItem) { |
| 4583 | *pItem = Item; |
| 4584 | } |
| 4585 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4586 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4587 | } |
| 4588 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4589 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4590 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4591 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4592 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4593 | uint64_t *puValue, |
| 4594 | QCBORItem *pItem) |
| 4595 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4596 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4597 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4598 | return; |
| 4599 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4600 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4601 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4605 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4606 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4607 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4608 | uint64_t *puValue, |
| 4609 | QCBORItem *pItem) |
| 4610 | { |
| 4611 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4612 | return; |
| 4613 | } |
| 4614 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4615 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4616 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4617 | return; |
| 4618 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4619 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4620 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4621 | } |
| 4622 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4623 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4624 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4625 | static QCBORError |
| 4626 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4627 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4628 | switch(pItem->uDataType) { |
| 4629 | |
| 4630 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4631 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4632 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4633 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4634 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4635 | } |
| 4636 | break; |
| 4637 | |
| 4638 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4639 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4640 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4641 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4642 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4643 | } |
| 4644 | break; |
| 4645 | |
| 4646 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4647 | |
| 4648 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4649 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4650 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4651 | pItem->val.expAndMantissa.nExponent, |
| 4652 | puValue, |
| 4653 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4654 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4655 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4656 | } |
| 4657 | break; |
| 4658 | |
| 4659 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4660 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4661 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4662 | pItem->val.expAndMantissa.nExponent, |
| 4663 | puValue, |
| 4664 | Exponentitate2); |
| 4665 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4666 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4667 | } |
| 4668 | break; |
| 4669 | |
| 4670 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4671 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4672 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4673 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4674 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4675 | if(uErr != QCBOR_SUCCESS) { |
| 4676 | return uErr; |
| 4677 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4678 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4679 | pItem->val.expAndMantissa.nExponent, |
| 4680 | puValue, |
| 4681 | Exponentitate10); |
| 4682 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4683 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4684 | } |
| 4685 | break; |
| 4686 | |
| 4687 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4688 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4689 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4690 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4691 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4692 | } |
| 4693 | break; |
| 4694 | |
| 4695 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4696 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4697 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4698 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4699 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4700 | if(uErr != QCBOR_SUCCESS) { |
| 4701 | return uErr; |
| 4702 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4703 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4704 | pItem->val.expAndMantissa.nExponent, |
| 4705 | puValue, |
| 4706 | Exponentitate2); |
| 4707 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4708 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4709 | } |
| 4710 | break; |
| 4711 | |
| 4712 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4713 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4714 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4715 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4716 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4717 | } |
| 4718 | break; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4719 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4720 | default: |
| 4721 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4722 | } |
| 4723 | } |
| 4724 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4725 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4726 | /* |
| 4727 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4728 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4729 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4730 | { |
| 4731 | QCBORItem Item; |
| 4732 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4733 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4734 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4735 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4736 | // The above conversion succeeded |
| 4737 | return; |
| 4738 | } |
| 4739 | |
| 4740 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4741 | // 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] | 4742 | return; |
| 4743 | } |
| 4744 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4745 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4748 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4749 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4750 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4751 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4752 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4753 | int64_t nLabel, |
| 4754 | uint32_t uConvertTypes, |
| 4755 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4756 | { |
| 4757 | QCBORItem Item; |
| 4758 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4759 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 4760 | nLabel, |
| 4761 | uConvertTypes, |
| 4762 | puValue, |
| 4763 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4764 | |
| 4765 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4766 | // The above conversion succeeded |
| 4767 | return; |
| 4768 | } |
| 4769 | |
| 4770 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4771 | // The above conversion failed in a way that code below can't correct |
| 4772 | return; |
| 4773 | } |
| 4774 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4775 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | |
| 4779 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4780 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4781 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4782 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4783 | const char *szLabel, |
| 4784 | uint32_t uConvertTypes, |
| 4785 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4786 | { |
| 4787 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4788 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 4789 | szLabel, |
| 4790 | uConvertTypes, |
| 4791 | puValue, |
| 4792 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4793 | |
| 4794 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4795 | // The above conversion succeeded |
| 4796 | return; |
| 4797 | } |
| 4798 | |
| 4799 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4800 | // The above conversion failed in a way that code below can't correct |
| 4801 | return; |
| 4802 | } |
| 4803 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4804 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4808 | |
| 4809 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4810 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4811 | uint32_t uConvertTypes, |
| 4812 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4813 | { |
| 4814 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4815 | case QCBOR_TYPE_FLOAT: |
| 4816 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4817 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4818 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4819 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4820 | *pdValue = (double)pItem->val.fnum; |
| 4821 | } else { |
| 4822 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4823 | } |
| 4824 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4825 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4826 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 4827 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4828 | break; |
| 4829 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4830 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4831 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4832 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4833 | *pdValue = pItem->val.dfnum; |
| 4834 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4835 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4836 | } |
| 4837 | } |
| 4838 | break; |
| 4839 | |
| 4840 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4841 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4842 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4843 | // A simple cast seems to do the job with no worry of exceptions. |
| 4844 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4845 | *pdValue = (double)pItem->val.int64; |
| 4846 | |
| 4847 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4848 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4849 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4850 | #else |
| 4851 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4852 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4853 | break; |
| 4854 | |
| 4855 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4856 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4857 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4858 | // A simple cast seems to do the job with no worry of exceptions. |
| 4859 | // There will be precision loss for some values. |
| 4860 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4861 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4862 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4863 | } |
| 4864 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4865 | #else |
| 4866 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4867 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4868 | |
| 4869 | default: |
| 4870 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4871 | } |
| 4872 | |
| 4873 | return QCBOR_SUCCESS; |
| 4874 | } |
| 4875 | |
| 4876 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4877 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4878 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4879 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4880 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4881 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4882 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4883 | return; |
| 4884 | } |
| 4885 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4886 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4887 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4888 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4889 | if(uError) { |
| 4890 | pMe->uLastError = (uint8_t)uError; |
| 4891 | return; |
| 4892 | } |
| 4893 | |
| 4894 | if(pItem) { |
| 4895 | *pItem = Item; |
| 4896 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4897 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4898 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4899 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4900 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4901 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4902 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4903 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4904 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4905 | double *pdValue, |
| 4906 | QCBORItem *pItem) |
| 4907 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4908 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4909 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4910 | return; |
| 4911 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4912 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4913 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4914 | } |
| 4915 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4916 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4917 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4918 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4919 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4920 | double *pdValue, |
| 4921 | QCBORItem *pItem) |
| 4922 | { |
| 4923 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4924 | return; |
| 4925 | } |
| 4926 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4927 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4928 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4929 | return; |
| 4930 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4931 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4932 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4936 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4937 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 4938 | { |
| 4939 | double dResult; |
| 4940 | |
| 4941 | dResult = 0.0; |
| 4942 | const uint8_t *pByte = BigNum.ptr; |
| 4943 | size_t uLen = BigNum.len; |
| 4944 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 4945 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4946 | while(uLen--) { |
| 4947 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 4948 | } |
| 4949 | |
| 4950 | return dResult; |
| 4951 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4952 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4953 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4954 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4955 | static QCBORError |
| 4956 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4957 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4958 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4959 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4960 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 4961 | |
| 4962 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4963 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4964 | |
| 4965 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4966 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4967 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4968 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4969 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4970 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 4971 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4972 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4973 | } |
| 4974 | break; |
| 4975 | |
| 4976 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4977 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4978 | // Underflow gives 0, overflow gives infinity |
| 4979 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4980 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4981 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4982 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4983 | } |
| 4984 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4985 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4986 | |
| 4987 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4988 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4989 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 4990 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4991 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4992 | } |
| 4993 | break; |
| 4994 | |
| 4995 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4996 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4997 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4998 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4999 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5000 | } |
| 5001 | break; |
| 5002 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5003 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5004 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5005 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5006 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5007 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5008 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5009 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5010 | } |
| 5011 | break; |
| 5012 | |
| 5013 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5014 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5015 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5016 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5017 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5018 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5019 | } |
| 5020 | break; |
| 5021 | |
| 5022 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5023 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5024 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 5025 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5026 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5027 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5028 | } |
| 5029 | break; |
| 5030 | |
| 5031 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5032 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 5033 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5034 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5035 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5036 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5037 | } |
| 5038 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5039 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 5040 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5041 | default: |
| 5042 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5043 | } |
| 5044 | |
| 5045 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5046 | |
| 5047 | #else |
| 5048 | (void)pItem; |
| 5049 | (void)uConvertTypes; |
| 5050 | (void)pdValue; |
| 5051 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5052 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5053 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5054 | } |
| 5055 | |
| 5056 | |
| 5057 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5058 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5059 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5060 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 5061 | uint32_t uConvertTypes, |
| 5062 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5063 | { |
| 5064 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5065 | QCBORItem Item; |
| 5066 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5067 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5068 | |
| 5069 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5070 | // The above conversion succeeded |
| 5071 | return; |
| 5072 | } |
| 5073 | |
| 5074 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5075 | // The above conversion failed in a way that code below can't correct |
| 5076 | return; |
| 5077 | } |
| 5078 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5079 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5080 | } |
| 5081 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5082 | |
| 5083 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5084 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5085 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5086 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5087 | int64_t nLabel, |
| 5088 | uint32_t uConvertTypes, |
| 5089 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5090 | { |
| 5091 | QCBORItem Item; |
| 5092 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5093 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5094 | |
| 5095 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5096 | // The above conversion succeeded |
| 5097 | return; |
| 5098 | } |
| 5099 | |
| 5100 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5101 | // The above conversion failed in a way that code below can't correct |
| 5102 | return; |
| 5103 | } |
| 5104 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5105 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | |
| 5109 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5110 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5111 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5112 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5113 | const char *szLabel, |
| 5114 | uint32_t uConvertTypes, |
| 5115 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5116 | { |
| 5117 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5118 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5119 | |
| 5120 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5121 | // The above conversion succeeded |
| 5122 | return; |
| 5123 | } |
| 5124 | |
| 5125 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5126 | // The above conversion failed in a way that code below can't correct |
| 5127 | return; |
| 5128 | } |
| 5129 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5130 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5131 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5132 | |
| 5133 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5134 | |
| 5135 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5136 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5137 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 5138 | { |
| 5139 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5140 | uInt = uInt << 8; |
| 5141 | }; |
| 5142 | |
| 5143 | UsefulOutBuf UOB; |
| 5144 | |
| 5145 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5146 | |
| 5147 | while(uInt) { |
| 5148 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5149 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5150 | uInt = uInt << 8; |
| 5151 | (void)xx; |
| 5152 | } |
| 5153 | |
| 5154 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5155 | } |
| 5156 | |
| 5157 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5158 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 5159 | TagSpecification TagSpec, |
| 5160 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5161 | { |
| 5162 | QCBORError uErr; |
| 5163 | // 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] | 5164 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 5165 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5166 | if(uErr != QCBOR_SUCCESS) { |
| 5167 | goto Done; |
| 5168 | } |
| 5169 | |
| 5170 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 5171 | break; // Successful exit. Moving on to finish decoding. |
| 5172 | } |
| 5173 | |
| 5174 | // The item is an array, which means an undecoded |
| 5175 | // mantissa and exponent, so decode it. It will then |
| 5176 | // have a different type and exit the loop if. |
| 5177 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5178 | if(uErr != QCBOR_SUCCESS) { |
| 5179 | goto Done; |
| 5180 | } |
| 5181 | |
| 5182 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5183 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5184 | } |
| 5185 | Done: |
| 5186 | return uErr; |
| 5187 | } |
| 5188 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5189 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5190 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5191 | TagSpecification TagSpec, |
| 5192 | QCBORItem *pItem, |
| 5193 | int64_t *pnMantissa, |
| 5194 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5195 | { |
| 5196 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5197 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5198 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5199 | if(uErr != QCBOR_SUCCESS) { |
| 5200 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5201 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5202 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5203 | switch (pItem->uDataType) { |
| 5204 | |
| 5205 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5206 | case QCBOR_TYPE_BIGFLOAT: |
| 5207 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 5208 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5209 | break; |
| 5210 | |
| 5211 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5212 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5213 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5214 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5215 | break; |
| 5216 | |
| 5217 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5218 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5219 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5220 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5221 | break; |
| 5222 | |
| 5223 | default: |
| 5224 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5225 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5226 | |
| 5227 | Done: |
| 5228 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5229 | } |
| 5230 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5231 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5232 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5233 | TagSpecification TagSpec, |
| 5234 | QCBORItem *pItem, |
| 5235 | UsefulBuf BufferForMantissa, |
| 5236 | UsefulBufC *pMantissa, |
| 5237 | bool *pbIsNegative, |
| 5238 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5239 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5240 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5241 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5242 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5243 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5244 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | uint64_t uMantissa; |
| 5248 | |
| 5249 | switch (pItem->uDataType) { |
| 5250 | |
| 5251 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5252 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5253 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5254 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5255 | *pbIsNegative = false; |
| 5256 | } else { |
| 5257 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5258 | *pbIsNegative = true; |
| 5259 | } |
| 5260 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5261 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5262 | break; |
| 5263 | |
| 5264 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5265 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5266 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5267 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5268 | *pbIsNegative = false; |
| 5269 | break; |
| 5270 | |
| 5271 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5272 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5273 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5274 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5275 | *pbIsNegative = true; |
| 5276 | break; |
| 5277 | |
| 5278 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5279 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5280 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5281 | |
| 5282 | Done: |
| 5283 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5287 | /* |
| 5288 | Public function, see header qcbor/qcbor_decode.h file |
| 5289 | */ |
| 5290 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5291 | uint8_t uTagRequirement, |
| 5292 | int64_t *pnMantissa, |
| 5293 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5294 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5295 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5296 | return; |
| 5297 | } |
| 5298 | |
| 5299 | QCBORItem Item; |
| 5300 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5301 | if(uError) { |
| 5302 | pMe->uLastError = (uint8_t)uError; |
| 5303 | return; |
| 5304 | } |
| 5305 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5306 | const TagSpecification TagSpec = |
| 5307 | { |
| 5308 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5309 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5310 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5311 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5312 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5313 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5314 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5315 | } |
| 5316 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5317 | |
| 5318 | /* |
| 5319 | Public function, see header qcbor/qcbor_decode.h file |
| 5320 | */ |
| 5321 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5322 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5323 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5324 | int64_t *pnMantissa, |
| 5325 | int64_t *pnExponent) |
| 5326 | { |
| 5327 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5328 | return; |
| 5329 | } |
| 5330 | |
| 5331 | QCBORItem Item; |
| 5332 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5333 | |
| 5334 | const TagSpecification TagSpec = |
| 5335 | { |
| 5336 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5337 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5338 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5339 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5340 | }; |
| 5341 | |
| 5342 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5343 | } |
| 5344 | |
| 5345 | |
| 5346 | /* |
| 5347 | Public function, see header qcbor/qcbor_decode.h file |
| 5348 | */ |
| 5349 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5350 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5351 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5352 | int64_t *pnMantissa, |
| 5353 | int64_t *pnExponent) |
| 5354 | { |
| 5355 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5356 | return; |
| 5357 | } |
| 5358 | |
| 5359 | QCBORItem Item; |
| 5360 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5361 | |
| 5362 | const TagSpecification TagSpec = |
| 5363 | { |
| 5364 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5365 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5366 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5367 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5368 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5369 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5370 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5371 | } |
| 5372 | |
| 5373 | |
| 5374 | /* |
| 5375 | Public function, see header qcbor/qcbor_decode.h file |
| 5376 | */ |
| 5377 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5378 | uint8_t uTagRequirement, |
| 5379 | UsefulBuf MantissaBuffer, |
| 5380 | UsefulBufC *pMantissa, |
| 5381 | bool *pbMantissaIsNegative, |
| 5382 | int64_t *pnExponent) |
| 5383 | { |
| 5384 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5385 | return; |
| 5386 | } |
| 5387 | |
| 5388 | QCBORItem Item; |
| 5389 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5390 | if(uError) { |
| 5391 | pMe->uLastError = (uint8_t)uError; |
| 5392 | return; |
| 5393 | } |
| 5394 | |
| 5395 | const TagSpecification TagSpec = |
| 5396 | { |
| 5397 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5398 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5399 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5400 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5401 | }; |
| 5402 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5403 | ProcessMantissaAndExponentBig(pMe, |
| 5404 | TagSpec, |
| 5405 | &Item, |
| 5406 | MantissaBuffer, |
| 5407 | pMantissa, |
| 5408 | pbMantissaIsNegative, |
| 5409 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | |
| 5413 | /* |
| 5414 | Public function, see header qcbor/qcbor_decode.h file |
| 5415 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5416 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5417 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5418 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5419 | UsefulBuf BufferForMantissa, |
| 5420 | UsefulBufC *pMantissa, |
| 5421 | bool *pbIsNegative, |
| 5422 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5423 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5424 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5425 | return; |
| 5426 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5427 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5428 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5429 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5430 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5431 | return; |
| 5432 | } |
| 5433 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5434 | const TagSpecification TagSpec = |
| 5435 | { |
| 5436 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5437 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5438 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5439 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5440 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5441 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5442 | ProcessMantissaAndExponentBig(pMe, |
| 5443 | TagSpec, |
| 5444 | &Item, |
| 5445 | BufferForMantissa, |
| 5446 | pMantissa, |
| 5447 | pbIsNegative, |
| 5448 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5449 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5450 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5451 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5452 | /* |
| 5453 | Public function, see header qcbor/qcbor_decode.h file |
| 5454 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5455 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5456 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5457 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5458 | UsefulBuf BufferForMantissa, |
| 5459 | UsefulBufC *pMantissa, |
| 5460 | bool *pbIsNegative, |
| 5461 | int64_t *pnExponent) |
| 5462 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5463 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5464 | return; |
| 5465 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5466 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5467 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5468 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5469 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5470 | return; |
| 5471 | } |
| 5472 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5473 | const TagSpecification TagSpec = |
| 5474 | { |
| 5475 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5476 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5477 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5478 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5479 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5480 | |
| 5481 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5482 | } |
| 5483 | |
| 5484 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5485 | /* |
| 5486 | Public function, see header qcbor/qcbor_decode.h file |
| 5487 | */ |
| 5488 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5489 | uint8_t uTagRequirement, |
| 5490 | int64_t *pnMantissa, |
| 5491 | int64_t *pnExponent) |
| 5492 | { |
| 5493 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5494 | return; |
| 5495 | } |
| 5496 | |
| 5497 | QCBORItem Item; |
| 5498 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5499 | if(uError) { |
| 5500 | pMe->uLastError = (uint8_t)uError; |
| 5501 | return; |
| 5502 | } |
| 5503 | const TagSpecification TagSpec = |
| 5504 | { |
| 5505 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5506 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5507 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5508 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5509 | }; |
| 5510 | |
| 5511 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5512 | } |
| 5513 | |
| 5514 | |
| 5515 | /* |
| 5516 | Public function, see header qcbor/qcbor_decode.h file |
| 5517 | */ |
| 5518 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5519 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5520 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5521 | int64_t *pnMantissa, |
| 5522 | int64_t *pnExponent) |
| 5523 | { |
| 5524 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5525 | return; |
| 5526 | } |
| 5527 | |
| 5528 | QCBORItem Item; |
| 5529 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5530 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5531 | return; |
| 5532 | } |
| 5533 | |
| 5534 | const TagSpecification TagSpec = |
| 5535 | { |
| 5536 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5537 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5538 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5539 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5540 | }; |
| 5541 | |
| 5542 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5543 | } |
| 5544 | |
| 5545 | |
| 5546 | /* |
| 5547 | Public function, see header qcbor/qcbor_decode.h file |
| 5548 | */ |
| 5549 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5550 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5551 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5552 | int64_t *pnMantissa, |
| 5553 | int64_t *pnExponent) |
| 5554 | { |
| 5555 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5556 | return; |
| 5557 | } |
| 5558 | |
| 5559 | QCBORItem Item; |
| 5560 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5561 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5562 | return; |
| 5563 | } |
| 5564 | |
| 5565 | const TagSpecification TagSpec = |
| 5566 | { |
| 5567 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5568 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5569 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5570 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5571 | }; |
| 5572 | |
| 5573 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5574 | } |
| 5575 | |
| 5576 | |
| 5577 | /* |
| 5578 | Public function, see header qcbor/qcbor_decode.h file |
| 5579 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5580 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5581 | uint8_t uTagRequirement, |
| 5582 | UsefulBuf MantissaBuffer, |
| 5583 | UsefulBufC *pMantissa, |
| 5584 | bool *pbMantissaIsNegative, |
| 5585 | int64_t *pnExponent) |
| 5586 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5587 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5588 | return; |
| 5589 | } |
| 5590 | |
| 5591 | QCBORItem Item; |
| 5592 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5593 | if(uError) { |
| 5594 | pMe->uLastError = (uint8_t)uError; |
| 5595 | return; |
| 5596 | } |
| 5597 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5598 | const TagSpecification TagSpec = |
| 5599 | { |
| 5600 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5601 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5602 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5603 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5604 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5605 | |
| 5606 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5607 | } |
| 5608 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5609 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5610 | /* |
| 5611 | Public function, see header qcbor/qcbor_decode.h file |
| 5612 | */ |
| 5613 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5614 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5615 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5616 | UsefulBuf BufferForMantissa, |
| 5617 | UsefulBufC *pMantissa, |
| 5618 | bool *pbIsNegative, |
| 5619 | int64_t *pnExponent) |
| 5620 | { |
| 5621 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5622 | return; |
| 5623 | } |
| 5624 | |
| 5625 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5626 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5627 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5628 | return; |
| 5629 | } |
| 5630 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5631 | const TagSpecification TagSpec = |
| 5632 | { |
| 5633 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5634 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5635 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5636 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5637 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5638 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5639 | ProcessMantissaAndExponentBig(pMe, |
| 5640 | TagSpec, |
| 5641 | &Item, |
| 5642 | BufferForMantissa, |
| 5643 | pMantissa, |
| 5644 | pbIsNegative, |
| 5645 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5649 | /* |
| 5650 | Public function, see header qcbor/qcbor_decode.h file |
| 5651 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5652 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5653 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5654 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5655 | UsefulBuf BufferForMantissa, |
| 5656 | UsefulBufC *pMantissa, |
| 5657 | bool *pbIsNegative, |
| 5658 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5659 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5660 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5661 | return; |
| 5662 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5663 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5664 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5665 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5666 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5667 | return; |
| 5668 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5669 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5670 | const TagSpecification TagSpec = |
| 5671 | { |
| 5672 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5673 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5674 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5675 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5676 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5677 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5678 | ProcessMantissaAndExponentBig(pMe, |
| 5679 | TagSpec, |
| 5680 | &Item, |
| 5681 | BufferForMantissa, |
| 5682 | pMantissa, |
| 5683 | pbIsNegative, |
| 5684 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5685 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5686 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5687 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |