Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2020, Laurence Lundblade. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 4 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following |
| 13 | disclaimer in the documentation and/or other materials provided |
| 14 | with the distribution. |
| 15 | * Neither the name of The Linux Foundation nor the names of its |
| 16 | contributors, nor the name "Laurence Lundblade" may be used to |
| 17 | endorse or promote products derived from this software without |
| 18 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 31 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 32 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 33 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 34 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 35 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 36 | #include "ieee754.h" // Does not use math.h |
| 37 | |
| 38 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 39 | #include <math.h> // For isnan(), llround(), llroudf(), round(), roundf(), |
| 40 | // pow(), exp2() |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 41 | #include <fenv.h> // feclearexcept(), fetestexcept() |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 42 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 43 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 44 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 45 | /* |
| 46 | This casts away the const-ness of a pointer, usually so it can be |
| 47 | freed or realloced. |
| 48 | */ |
| 49 | #define UNCONST_POINTER(ptr) ((void *)(ptr)) |
| 50 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 51 | #define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type)) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 52 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 53 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 54 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 55 | QCBORItem_IsMapOrArray(const QCBORItem *pMe) |
| 56 | { |
| 57 | const uint8_t uDataType = pMe->uDataType; |
| 58 | return uDataType == QCBOR_TYPE_MAP || |
| 59 | uDataType == QCBOR_TYPE_ARRAY || |
| 60 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
| 61 | } |
| 62 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 63 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 64 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 65 | { |
| 66 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if(pMe->val.uCount != 0) { |
| 71 | return false; |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 76 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 77 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 78 | { |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 79 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 80 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if(pMe->val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
| 85 | return false; |
| 86 | } |
| 87 | return true; |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 88 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 89 | (void)pMe; |
| 90 | return false; |
| 91 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 95 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 96 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 97 | ===========================================================================*/ |
| 98 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 99 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 100 | See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 101 | the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 102 | */ |
| 103 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 104 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 105 | static inline uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 106 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 107 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 108 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 109 | /* |
| 110 | Limit in DecodeNesting_Descend against more than |
| 111 | QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
| 112 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 113 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 116 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 117 | static inline uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 118 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 119 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 120 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 121 | /* |
| 122 | Limit in DecodeNesting_Descend against more than |
| 123 | QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
| 124 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 125 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 128 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 129 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 130 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 131 | { |
| 132 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 133 | } |
| 134 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 135 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 136 | static inline bool |
| 137 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 138 | { |
| 139 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 140 | return true; |
| 141 | } else { |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 147 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 148 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 149 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 150 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 151 | return true; |
| 152 | } else { |
| 153 | return false; |
| 154 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 157 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 158 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 159 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 160 | { |
| 161 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 162 | // Not a map or array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 163 | return false; |
| 164 | } |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 165 | |
| 166 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 167 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 168 | // Is indefinite |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 171 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 172 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 173 | // All checks passed; is a definte length map or array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 177 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 178 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 179 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 180 | { |
| 181 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 182 | // is a byte string |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | return false; |
| 186 | } |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 187 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 188 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 189 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 190 | static inline bool DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 191 | { |
| 192 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 193 | return true; |
| 194 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 195 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 201 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 202 | static inline void DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 203 | { |
| 204 | // Should be only called on maps and arrays |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 205 | /* |
| 206 | DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 207 | larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 208 | uin32_t so the cast is safe. |
| 209 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 210 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 211 | |
| 212 | if(bIsEmpty) { |
| 213 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 214 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 217 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 218 | static inline void DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 219 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 220 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 223 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 224 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 225 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 226 | { |
| 227 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 228 | // No bounded map or array set up |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 232 | // Not a map or array; end of those is by byte count |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 235 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
| 236 | // In a traveral at a level deeper than the bounded level |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 237 | return false; |
| 238 | } |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 239 | // Works for both definite and indefinite length maps/arrays |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 240 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0 && |
| 241 | pNesting->pCurrentBounded->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 242 | // Count is not zero, still unconsumed item |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 245 | // All checks passed, got to the end of an array or map |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 246 | return true; |
| 247 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 248 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 249 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 250 | static inline bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 251 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 252 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 253 | // Must only be called on map / array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 254 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 255 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 256 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 257 | return false; |
| 258 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 261 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 262 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 263 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 264 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 265 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 266 | return true; |
| 267 | } else { |
| 268 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 269 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 272 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 273 | static inline bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 274 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 275 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 276 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 279 | |
| 280 | if(pNesting->pCurrentBounded->uLevelType != uType) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 287 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 288 | static inline void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 289 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 290 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 291 | // Only call on a defnite length array / map |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 292 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 293 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 294 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 295 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 296 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 297 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 298 | { |
| 299 | // Only call on a defnite length array / map |
| 300 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 301 | } |
| 302 | |
| 303 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 304 | static inline void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 305 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 306 | { |
| 307 | pNesting->pCurrent--; |
| 308 | } |
| 309 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 310 | |
| 311 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 312 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 313 | { |
| 314 | // Error out if nesting is too deep |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 315 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 316 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // The actual descend |
| 320 | pNesting->pCurrent++; |
| 321 | |
| 322 | pNesting->pCurrent->uLevelType = uType; |
| 323 | |
| 324 | return QCBOR_SUCCESS; |
| 325 | } |
| 326 | |
| 327 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 328 | static inline QCBORError |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 329 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 330 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 331 | /* |
| 332 | Should only be called on map/array. |
| 333 | |
| 334 | Have descended into this before this is called. The job here is |
| 335 | just to mark it in bounded mode. |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 336 | |
| 337 | Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
| 338 | uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 339 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 340 | if(uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 341 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 344 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 345 | |
| 346 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 347 | |
| 348 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 351 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 352 | static inline QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 353 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 354 | uint8_t uQCBORType, |
| 355 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 356 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 357 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 358 | |
| 359 | if(uCount == 0) { |
| 360 | // Nothing to do for empty definite lenth arrays. They are just are |
| 361 | // effectively the same as an item that is not a map or array |
| 362 | goto Done; |
| 363 | // Empty indefinite length maps and arrays are handled elsewhere |
| 364 | } |
| 365 | |
| 366 | // Error out if arrays is too long to handle |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 367 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 368 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 369 | uError = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 370 | goto Done; |
| 371 | } |
| 372 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 373 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 374 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 375 | goto Done; |
| 376 | } |
| 377 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 378 | // Fill in the new map/array level. Check above makes casts OK. |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 379 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 380 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 381 | |
| 382 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 383 | |
| 384 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 385 | return uError;; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | static inline void |
| 390 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 391 | { |
| 392 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | static inline void |
| 397 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 398 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 399 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 400 | pNesting->pCurrentBounded--; |
| 401 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 402 | break; |
| 403 | } |
| 404 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 407 | static inline void |
| 408 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 409 | { |
| 410 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 411 | } |
| 412 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 413 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 414 | static inline QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 415 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 416 | uint32_t uEndOffset, |
| 417 | uint32_t uEndOfBstr) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 418 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 419 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 420 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 421 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 422 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 423 | goto Done; |
| 424 | } |
| 425 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 426 | // Fill in the new byte string level |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 427 | pNesting->pCurrent->u.bs.uPreviousEndOffset = uEndOffset; |
| 428 | pNesting->pCurrent->u.bs.uEndOfBstr = uEndOfBstr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 429 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 430 | // Bstr wrapped levels are always bounded |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 431 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 432 | |
| 433 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 434 | return uError;; |
| 435 | } |
| 436 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 437 | |
| 438 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 439 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 440 | { |
| 441 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 444 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 445 | static inline void |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 446 | DecodeNesting_ResetMapOrArrayCount(QCBORDecodeNesting *pNesting) |
| 447 | { |
| 448 | pNesting->pCurrentBounded->u.ma.uCountCursor = pNesting->pCurrentBounded->u.ma.uCountTotal; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | static inline void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 453 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 454 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 455 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 456 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 457 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 461 | static inline void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 462 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 463 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 464 | *pSave = *pNesting; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 465 | pNesting->pCurrent = pNesting->pCurrentBounded; |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 466 | DecodeNesting_ResetMapOrArrayCount(pNesting); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 469 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 470 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 471 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 472 | { |
| 473 | *pNesting = *pSave; |
| 474 | } |
| 475 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 476 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 477 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 478 | DecodeNesting_GetEndOfBstr(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 479 | { |
| 480 | return pMe->pCurrentBounded->u.bs.uEndOfBstr; |
| 481 | } |
| 482 | |
| 483 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 484 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 485 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 486 | { |
| 487 | return pMe->pCurrentBounded->u.bs.uPreviousEndOffset; |
| 488 | } |
| 489 | |
| 490 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 491 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 492 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 493 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 494 | |
| 495 | The following four functions are pretty wrappers for invocation of |
| 496 | the string allocator supplied by the caller. |
| 497 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 498 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 499 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 500 | static inline void |
| 501 | StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 502 | { |
| 503 | (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0); |
| 504 | } |
| 505 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 506 | // StringAllocator_Reallocate called with pMem NULL is |
| 507 | // equal to StringAllocator_Allocate() |
| 508 | static inline UsefulBuf |
| 509 | StringAllocator_Reallocate(const QCORInternalAllocator *pMe, |
| 510 | void *pMem, |
| 511 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 512 | { |
| 513 | return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize); |
| 514 | } |
| 515 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 516 | static inline UsefulBuf |
| 517 | StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 518 | { |
| 519 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 520 | } |
| 521 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 522 | static inline void |
| 523 | StringAllocator_Destruct(const QCORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 524 | { |
| 525 | if(pMe->pfAllocator) { |
| 526 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 527 | } |
| 528 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 529 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 530 | |
| 531 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 532 | /*=========================================================================== |
| 533 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 534 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 535 | See qcbor/qcbor_decode.h for definition of the object |
| 536 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 537 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 538 | /* |
| 539 | Public function, see header file |
| 540 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 541 | void QCBORDecode_Init(QCBORDecodeContext *me, |
| 542 | UsefulBufC EncodedCBOR, |
| 543 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 544 | { |
| 545 | memset(me, 0, sizeof(QCBORDecodeContext)); |
| 546 | UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 547 | // Don't bother with error check on decode mode. If a bad value is |
| 548 | // passed it will just act as if the default normal mode of 0 was set. |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 549 | me->uDecodeMode = (uint8_t)nDecodeMode; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 550 | DecodeNesting_Init(&(me->nesting)); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 551 | for(int i = 0; i < QCBOR_NUM_MAPPED_TAGS; i++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 552 | me->auMappedTags[i] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 553 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 557 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 558 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 559 | /* |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 560 | Public function, see header file |
| 561 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 562 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 563 | QCBORStringAllocate pfAllocateFunction, |
| 564 | void *pAllocateContext, |
| 565 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 566 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 567 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 568 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 569 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 570 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 571 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 572 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | Public function, see header file |
| 576 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 577 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 578 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 579 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 580 | // This does nothing now. It is retained for backwards compatibility |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 581 | (void)pMe; |
| 582 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 583 | } |
| 584 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 585 | |
| 586 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 587 | This decodes the fundamental part of a CBOR data item, the type and |
| 588 | number |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 589 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 590 | This is the counterpart to QCBOREncode_EncodeHead(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 591 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 592 | This does the network->host byte order conversion. The conversion |
| 593 | here also results in the conversion for floats in addition to that |
| 594 | for lengths, tags and integer values. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 595 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 596 | This returns: |
| 597 | pnMajorType -- the major type for the item |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 598 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 599 | puArgument -- the "number" which is used a the value for integers, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 600 | tags and floats and length for strings and arrays |
| 601 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 602 | pnAdditionalInfo -- Pass this along to know what kind of float or |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 603 | if length is indefinite |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 604 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 605 | The int type is preferred to uint8_t for some variables as this |
| 606 | avoids integer promotions, can reduce code size and makes |
| 607 | static analyzers happier. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 608 | |
| 609 | @retval QCBOR_ERR_UNSUPPORTED |
| 610 | |
| 611 | @retval QCBOR_ERR_HIT_END |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 612 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 613 | static inline QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 614 | int *pnMajorType, |
| 615 | uint64_t *puArgument, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 616 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 617 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 618 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 619 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 620 | // Get the initial byte that every CBOR data item has |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 621 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 622 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 623 | // Break down the initial byte |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 624 | const int nTmpMajorType = nInitialByte >> 5; |
| 625 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 626 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 627 | // Where the number or argument accumulates |
| 628 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 629 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 630 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 631 | // Need to get 1,2,4 or 8 additional argument bytes. Map |
| 632 | // LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 633 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 634 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 635 | // Loop getting all the bytes in the argument |
| 636 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 637 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 638 | // This shift and add gives the endian conversion |
| 639 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 640 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 641 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 642 | // The reserved and thus-far unused additional info values |
| 643 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 644 | goto Done; |
| 645 | } else { |
| 646 | // Less than 24, additional info is argument or 31, an indefinite length |
| 647 | // No more bytes to get |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 648 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 649 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 650 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 651 | if(UsefulInputBuf_GetError(pUInBuf)) { |
| 652 | nReturn = QCBOR_ERR_HIT_END; |
| 653 | goto Done; |
| 654 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 655 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 656 | // All successful if we got here. |
| 657 | nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 658 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 659 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 660 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 661 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 662 | Done: |
| 663 | return nReturn; |
| 664 | } |
| 665 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 666 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 667 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 668 | CBOR doesn't explicitly specify two's compliment for integers but all |
| 669 | CPUs use it these days and the test vectors in the RFC are so. All |
| 670 | integers in the CBOR structure are positive and the major type |
| 671 | indicates positive or negative. CBOR can express positive integers |
| 672 | up to 2^x - 1 where x is the number of bits and negative integers |
| 673 | down to 2^x. Note that negative numbers can be one more away from |
| 674 | zero than positive. Stdint, as far as I can tell, uses two's |
| 675 | compliment to represent negative integers. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 676 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 677 | See http://www.unix.org/whitepapers/64bit.html for reasons int is |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 678 | used carefully here, and in particular why it isn't used in the interface. |
| 679 | Also see |
| 680 | https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 681 | |
| 682 | Int is used for values that need less than 16-bits and would be subject |
| 683 | to integer promotion and complaining by static analyzers. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 684 | |
| 685 | @retval QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 686 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 687 | static inline QCBORError |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 688 | DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 689 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 690 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 691 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 692 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
| 693 | if (uNumber <= INT64_MAX) { |
| 694 | pDecodedItem->val.int64 = (int64_t)uNumber; |
| 695 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 696 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 697 | } else { |
| 698 | pDecodedItem->val.uint64 = uNumber; |
| 699 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 700 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 701 | } |
| 702 | } else { |
| 703 | if(uNumber <= INT64_MAX) { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 704 | // CBOR's representation of negative numbers lines up with the |
| 705 | // two-compliment representation. A negative integer has one |
| 706 | // more in range than a positive integer. INT64_MIN is |
| 707 | // equal to (-INT64_MAX) - 1. |
| 708 | pDecodedItem->val.int64 = (-(int64_t)uNumber) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 709 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 710 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 711 | } else { |
| 712 | // C can't represent a negative integer in this range |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 713 | // so it is an error. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 714 | nReturn = QCBOR_ERR_INT_OVERFLOW; |
| 715 | } |
| 716 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 717 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 718 | return nReturn; |
| 719 | } |
| 720 | |
| 721 | // Make sure #define value line up as DecodeSimple counts on this. |
| 722 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 723 | #error QCBOR_TYPE_FALSE macro value wrong |
| 724 | #endif |
| 725 | |
| 726 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 727 | #error QCBOR_TYPE_TRUE macro value wrong |
| 728 | #endif |
| 729 | |
| 730 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 731 | #error QCBOR_TYPE_NULL macro value wrong |
| 732 | #endif |
| 733 | |
| 734 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 735 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 736 | #endif |
| 737 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 738 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 739 | #error QCBOR_TYPE_BREAK macro value wrong |
| 740 | #endif |
| 741 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 742 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 743 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 744 | #endif |
| 745 | |
| 746 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 747 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 748 | #endif |
| 749 | |
| 750 | /* |
| 751 | Decode true, false, floats, break... |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 752 | |
| 753 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 754 | |
| 755 | @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 756 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 757 | static inline QCBORError |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 758 | DecodeSimple(int nAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 759 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 760 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 761 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 762 | // uAdditionalInfo is 5 bits from the initial byte. Compile time checks |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 763 | // above make sure uAdditionalInfo values line up with uDataType values. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 764 | // DecodeTypeAndNumber() never returns an AdditionalInfo > 0x1f so cast |
| 765 | // is safe |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 766 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 767 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 768 | switch(nAdditionalInfo) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 769 | // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are |
| 770 | // caught before this is called. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 771 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 772 | case HALF_PREC_FLOAT: // 25 |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 773 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 774 | // Half-precision is returned as a double. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 775 | // The cast to uint16_t is safe because the encoded value |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 776 | // was 16 bits. It was widened to 64 bits to be passed in here. |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 777 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber); |
| 778 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 779 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 780 | nReturn = QCBOR_ERR_HALF_PRECISION_DISABLED; |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 781 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 782 | break; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 783 | case SINGLE_PREC_FLOAT: // 26 |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 784 | // Single precision is normally returned as a double |
| 785 | // since double is widely supported, there is no loss of |
| 786 | // precision, it makes it easy for the caller in |
| 787 | // most cases and it can be converted back to single |
| 788 | // with no loss of precision |
| 789 | // |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 790 | // The cast to uint32_t is safe because the encoded value |
Laurence Lundblade | 8fa7d5d | 2020-07-11 16:30:47 -0700 | [diff] [blame] | 791 | // was 32 bits. It was widened to 64 bits to be passed in here. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 792 | { |
| 793 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber); |
| 794 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 795 | // In the normal case, use HW to convert float to double. |
| 796 | pDecodedItem->val.dfnum = (double)f; |
| 797 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 798 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 799 | // Use of float HW is disabled, return as a float. |
| 800 | pDecodedItem->val.fnum = f; |
| 801 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 802 | |
| 803 | // IEEE754_FloatToDouble() could be used here to return |
| 804 | // as a double, but it adds object code and most likely |
| 805 | // anyone disabling FLOAT HW use doesn't care about |
| 806 | // floats and wants to save object code. |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 807 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 808 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 809 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 810 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 811 | case DOUBLE_PREC_FLOAT: // 27 |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 812 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 813 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 814 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 815 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 816 | case CBOR_SIMPLEV_FALSE: // 20 |
| 817 | case CBOR_SIMPLEV_TRUE: // 21 |
| 818 | case CBOR_SIMPLEV_NULL: // 22 |
| 819 | case CBOR_SIMPLEV_UNDEF: // 23 |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 820 | case CBOR_SIMPLE_BREAK: // 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 821 | break; // nothing to do |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 822 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 823 | case CBOR_SIMPLEV_ONEBYTE: // 24 |
| 824 | if(uNumber <= CBOR_SIMPLE_BREAK) { |
| 825 | // This takes out f8 00 ... f8 1f which should be encoded as e0 … f7 |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 826 | nReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 827 | goto Done; |
| 828 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 829 | /* FALLTHROUGH */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 830 | // fall through intentionally |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 831 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 832 | default: // 0-19 |
| 833 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 834 | /* |
| 835 | DecodeTypeAndNumber will make uNumber equal to |
| 836 | uAdditionalInfo when uAdditionalInfo is < 24 This cast is |
| 837 | safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 838 | the double/float cases above |
| 839 | */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 840 | pDecodedItem->val.uSimple = (uint8_t)uNumber; |
| 841 | break; |
| 842 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 843 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 844 | Done: |
| 845 | return nReturn; |
| 846 | } |
| 847 | |
| 848 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 849 | /* |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 850 | Decode text and byte strings. Call the string allocator if asked to. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 851 | |
| 852 | @retval QCBOR_ERR_HIT_END |
| 853 | |
| 854 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 855 | |
| 856 | @retval QCBOR_ERR_STRING_TOO_LONG |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 857 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 858 | static inline QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator, |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 859 | uint64_t uStrLen, |
| 860 | UsefulInputBuf *pUInBuf, |
| 861 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 862 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 863 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 864 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 865 | // CBOR lengths can be 64 bits, but size_t is not 64 bits on all CPUs. |
| 866 | // This check makes the casts to size_t below safe. |
| 867 | |
| 868 | // 4 bytes less than the largest sizeof() so this can be tested by |
| 869 | // putting a SIZE_MAX length in the CBOR test input (no one will |
| 870 | // care the limit on strings is 4 bytes shorter). |
| 871 | if(uStrLen > SIZE_MAX-4) { |
| 872 | nReturn = QCBOR_ERR_STRING_TOO_LONG; |
| 873 | goto Done; |
| 874 | } |
| 875 | |
| 876 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 877 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 878 | // Failed to get the bytes for this string item |
| 879 | nReturn = QCBOR_ERR_HIT_END; |
| 880 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 881 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 882 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 883 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 884 | if(pAllocator) { |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 885 | // We are asked to use string allocator to make a copy |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 886 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 887 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 888 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 889 | goto Done; |
| 890 | } |
| 891 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 892 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 893 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 894 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 895 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 896 | (void)pAllocator; |
| 897 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 898 | |
| 899 | // Normal case with no string allocator |
| 900 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 901 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 902 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 903 | return nReturn; |
| 904 | } |
| 905 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 906 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 907 | /* Map the CBOR major types for strings to the QCBOR types for strngs */ |
| 908 | static inline uint8_t MapStringMajorTypes(int nCBORMajorType) |
| 909 | { |
| 910 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 911 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 912 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 913 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 914 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 915 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 916 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 917 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 918 | return (uint8_t)(nCBORMajorType + 4); |
| 919 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 920 | |
| 921 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 922 | // Make sure the constants align as this is assumed by |
| 923 | // the GetAnItem() implementation |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 924 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 925 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 926 | #endif |
| 927 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 928 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 929 | #endif |
| 930 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 931 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 932 | This gets a single data item and decodes it including preceding |
| 933 | optional tagging. This does not deal with arrays and maps and nesting |
| 934 | except to decode the data item introducing them. Arrays and maps are |
| 935 | handled at the next level up in GetNext(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 936 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 937 | Errors detected here include: an array that is too long to decode, |
| 938 | hit end of buffer unexpectedly, a few forms of invalid encoded CBOR |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 939 | |
| 940 | @retval QCBOR_ERR_UNSUPPORTED |
| 941 | |
| 942 | @retval QCBOR_ERR_HIT_END |
| 943 | |
| 944 | @retval QCBOR_ERR_INT_OVERFLOW |
| 945 | |
| 946 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 947 | |
| 948 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 949 | |
| 950 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 951 | |
| 952 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 953 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 954 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 955 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, |
| 956 | QCBORItem *pDecodedItem, |
| 957 | const QCORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 958 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 959 | QCBORError nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 960 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 961 | /* |
| 962 | Get the major type and the number. Number could be length of more |
| 963 | bytes or the value depending on the major type nAdditionalInfo is |
| 964 | an encoding of the length of the uNumber and is needed to decode |
| 965 | floats and doubles |
| 966 | */ |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 967 | int nMajorType = 0; |
| 968 | uint64_t uNumber = 0; |
| 969 | int nAdditionalInfo = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 970 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 971 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 972 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 973 | nReturn = DecodeTypeAndNumber(pUInBuf, &nMajorType, &uNumber, &nAdditionalInfo); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 974 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 975 | // Error out here if we got into trouble on the type and number. The |
| 976 | // code after this will not work if the type and number is not good. |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 977 | if(nReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 978 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 979 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 980 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 981 | // At this point the major type and the value are valid. We've got |
| 982 | // the type and the number that starts every CBOR data item. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 983 | switch (nMajorType) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 984 | case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0 |
| 985 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 986 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 987 | nReturn = QCBOR_ERR_BAD_INT; |
| 988 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 989 | nReturn = DecodeInteger(nMajorType, uNumber, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 990 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 991 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 992 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 993 | case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2 |
| 994 | case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3 |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 995 | pDecodedItem->uDataType = MapStringMajorTypes(nMajorType); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 996 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 997 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 998 | } else { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 999 | nReturn = DecodeBytes(pAllocator, uNumber, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1000 | } |
| 1001 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1002 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1003 | case CBOR_MAJOR_TYPE_ARRAY: // Major type 4 |
| 1004 | case CBOR_MAJOR_TYPE_MAP: // Major type 5 |
| 1005 | // Record the number of items in the array or map |
| 1006 | if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1007 | nReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1008 | goto Done; |
| 1009 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1010 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1011 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1012 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 1013 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1014 | nReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
| 1015 | break; |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 1016 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1017 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1018 | // type conversion OK because of check above |
| 1019 | pDecodedItem->val.uCount = (uint16_t)uNumber; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1020 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1021 | // C preproc #if above makes sure constants for major types align |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1022 | // DecodeTypeAndNumber never returns a major type > 7 so cast is safe |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1023 | pDecodedItem->uDataType = (uint8_t)nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1024 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1025 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1026 | case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1027 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1028 | nReturn = QCBOR_ERR_BAD_INT; |
| 1029 | } else { |
| 1030 | pDecodedItem->val.uTagV = uNumber; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1031 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1032 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1033 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1034 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1035 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1036 | // Major type 7, float, double, true, false, null... |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1037 | nReturn = DecodeSimple(nAdditionalInfo, uNumber, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1038 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1039 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1040 | default: |
| 1041 | // Never happens because DecodeTypeAndNumber() should never return > 7 |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1042 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 1043 | break; |
| 1044 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1045 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1046 | Done: |
| 1047 | return nReturn; |
| 1048 | } |
| 1049 | |
| 1050 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1051 | /** |
| 1052 | * @brief Process indefinite length strings |
| 1053 | * |
| 1054 | * @param[in] pMe Decoder context |
| 1055 | * @param[in,out] pDecodedItem The decoded item that work is done on. |
| 1056 | * |
| 1057 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1058 | * @retval QCBOR_ERR_HIT_END |
| 1059 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1060 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1061 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1062 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1063 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1064 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1065 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1066 | * |
| 1067 | * If @c pDecodedItem is not an indefinite length string, this does nothing. |
| 1068 | * |
| 1069 | * If it is, this loops getting the subsequent chunks that make up the |
| 1070 | * string. The string allocator is used to make a contiguous buffer for |
| 1071 | * the chunks. When this completes @c pDecodedItem contains the |
| 1072 | * put-together string. |
| 1073 | * |
| 1074 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1075 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1076 | static inline QCBORError |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1077 | GetNext_FullItem(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1078 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1079 | /* Aproximate stack usage |
| 1080 | * 64-bit 32-bit |
| 1081 | * local vars 32 16 |
| 1082 | * 2 UsefulBufs 32 16 |
| 1083 | * QCBORItem 56 52 |
| 1084 | * TOTAL 120 74 |
| 1085 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1086 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1087 | /* The string allocator is used here for two purposes: 1) |
| 1088 | * coalescing the chunks of an indefinite length string, 2) |
| 1089 | * allocating storage for every string returned. |
| 1090 | * |
| 1091 | * The first use is below in this function. Indefinite length |
| 1092 | * strings cannot be processed at all without a string allocator. |
| 1093 | * |
| 1094 | * The second used is in DecodeBytes() which is called by |
| 1095 | * GetNext_Item() below. This second use unneccessary for most use |
| 1096 | * and only happens when requested in the call to |
| 1097 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1098 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1099 | * |
| 1100 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1101 | * allocator altogether and thus both of these uses. It reduced the |
| 1102 | * decoder object code by about 400 bytes. |
| 1103 | */ |
| 1104 | const QCORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1105 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1106 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 1107 | const QCORInternalAllocator *pAllocator = NULL; |
| 1108 | |
| 1109 | if(pMe->StringAllocator.pfAllocator) { |
| 1110 | pAllocator = &(pMe->StringAllocator); |
| 1111 | if(pMe->bStringAllocateAll) { |
| 1112 | pAllocatorForGetNext = pAllocator; |
| 1113 | } |
| 1114 | } |
| 1115 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1116 | |
| 1117 | QCBORError uReturn; |
| 1118 | uReturn = GetNext_Item(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
| 1119 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1120 | goto Done; |
| 1121 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1122 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1123 | /* Only do indefinite length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1124 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1125 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1126 | goto Done; |
| 1127 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1128 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1129 | /* Is this a string with an indefinite length? */ |
| 1130 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1131 | goto Done; |
| 1132 | } |
| 1133 | |
| 1134 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 1135 | /* Can't do indefinite length strings without a string allocator */ |
| 1136 | if(pAllocator == NULL) { |
| 1137 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1138 | goto Done; |
| 1139 | } |
| 1140 | |
| 1141 | /* Loop getting chunks of the indefinite length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1142 | UsefulBufC FullString = NULLUsefulBufC; |
| 1143 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1144 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1145 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1146 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1147 | /* Pass a NULL string allocator to GetNext_Item() because the |
| 1148 | * individual string chunks in an indefinite length should not |
| 1149 | * be allocated. They are always copied in the the contiguous |
| 1150 | * buffer allocated here. |
| 1151 | */ |
| 1152 | uReturn = GetNext_Item(&(pMe->InBuf), &StringChunkItem, NULL); |
| 1153 | if(uReturn) { |
| 1154 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1155 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1156 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1157 | /* Is item is the marker for end of the indefinite length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1158 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1159 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1160 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1161 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1162 | break; |
| 1163 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1164 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1165 | /* All chunks must be of the same type, the type of the item |
| 1166 | * that introduces the indefinite length string. This also |
| 1167 | * catches errors where the chunk is not a string at all and an |
| 1168 | * indefinite length string inside an indefinite length string. |
| 1169 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1170 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1171 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1172 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1173 | break; |
| 1174 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1175 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1176 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1177 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1178 | * not NULL and a reallocation happens. |
| 1179 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1180 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1181 | UNCONST_POINTER(FullString.ptr), |
| 1182 | FullString.len + StringChunkItem.val.string.len); |
| 1183 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1184 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1185 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1186 | break; |
| 1187 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1188 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1189 | /* Copy new string chunk to the end of accumulated string */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1190 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1191 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1192 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1193 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1194 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1195 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1196 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1197 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1198 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1199 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1200 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1201 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1202 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1205 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1206 | static uint64_t ConvertTag(const QCBORDecodeContext *me, uint16_t uTagVal) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1207 | if(uTagVal <= QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1208 | return uTagVal; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1209 | } else if(uTagVal == CBOR_TAG_INVALID16) { |
| 1210 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1211 | } else { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 1212 | // This won't be negative because of code below in GetNext_TaggedItem() |
| 1213 | const unsigned uIndex = uTagVal - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1214 | return me->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1218 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1219 | /* |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1220 | Gets all optional tag data items preceding a data item that is not an |
| 1221 | optional tag and records them as bits in the tag map. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1222 | |
| 1223 | @retval QCBOR_ERR_UNSUPPORTED |
| 1224 | |
| 1225 | @retval QCBOR_ERR_HIT_END |
| 1226 | |
| 1227 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1228 | |
| 1229 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1230 | |
| 1231 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1232 | |
| 1233 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1234 | |
| 1235 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1236 | |
| 1237 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1238 | |
| 1239 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1240 | |
| 1241 | @retval QCBOR_ERR_TOO_MANY_TAGS |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1242 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1243 | static QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1244 | GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1245 | { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1246 | uint16_t auTags[QCBOR_MAX_TAGS_PER_ITEM] = {CBOR_TAG_INVALID16, |
| 1247 | CBOR_TAG_INVALID16, |
| 1248 | CBOR_TAG_INVALID16, |
| 1249 | CBOR_TAG_INVALID16}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1250 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1251 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1252 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1253 | // Loop fetching items until the item fetched is not a tag |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1254 | for(;;) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1255 | QCBORError uErr = GetNext_FullItem(me, pDecodedItem); |
| 1256 | if(uErr != QCBOR_SUCCESS) { |
| 1257 | uReturn = uErr; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1258 | goto Done; // Error out of the loop |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1259 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1260 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1261 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1262 | // Successful exit from loop; maybe got some tags, maybe not |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1263 | memcpy(pDecodedItem->uTags, auTags, sizeof(auTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1264 | break; |
| 1265 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1266 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1267 | if(auTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1268 | // No room in the tag list |
| 1269 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1270 | // Continue on to get all tags on this item even though |
| 1271 | // it is erroring out in the end. This is a resource limit |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 1272 | // error, not a problem with being well-formed CBOR. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1273 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1274 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1275 | // Slide tags over one in the array to make room at index 0 |
| 1276 | for(size_t uTagIndex = QCBOR_MAX_TAGS_PER_ITEM - 1; uTagIndex > 0; uTagIndex--) { |
| 1277 | auTags[uTagIndex] = auTags[uTagIndex-1]; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1278 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1279 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1280 | // Is the tag > 16 bits? |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1281 | if(pDecodedItem->val.uTagV > QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1282 | size_t uTagMapIndex; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1283 | // Is there room in the tag map, or is it in it already? |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1284 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1285 | if(me->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1286 | break; |
| 1287 | } |
| 1288 | if(me->auMappedTags[uTagMapIndex] == pDecodedItem->val.uTagV) { |
| 1289 | break; |
| 1290 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1291 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1292 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1293 | // No room for the tag |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1294 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1295 | // Continue on to get all tags on this item even though |
| 1296 | // it is erroring out in the end. This is a resource limit |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 1297 | // error, not a problem with being well-formed CBOR. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1298 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1301 | // Covers the cases where tag is new and were it is already in the map |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1302 | me->auMappedTags[uTagMapIndex] = pDecodedItem->val.uTagV; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1303 | auTags[0] = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1304 | |
| 1305 | } else { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1306 | auTags[0] = (uint16_t)pDecodedItem->val.uTagV; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1307 | } |
| 1308 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1309 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1310 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1311 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1316 | This layer takes care of map entries. It combines the label and data |
| 1317 | items into one QCBORItem. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1318 | |
| 1319 | @retval QCBOR_ERR_UNSUPPORTED |
| 1320 | |
| 1321 | @retval QCBOR_ERR_HIT_END |
| 1322 | |
| 1323 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1324 | |
| 1325 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1326 | |
| 1327 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1328 | |
| 1329 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1330 | |
| 1331 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1332 | |
| 1333 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1334 | |
| 1335 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1336 | |
| 1337 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1338 | |
| 1339 | @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1340 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1341 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1342 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1343 | static inline QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1344 | GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1345 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1346 | // Stack use: int/ptr 1, QCBORItem -- 56 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1347 | QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1348 | if(nReturn) |
| 1349 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1350 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1351 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1352 | // Break can't be a map entry |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1353 | goto Done; |
| 1354 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1355 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1356 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1357 | // In a map and caller wants maps decoded, not treated as arrays |
| 1358 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1359 | if(DecodeNesting_IsCurrentTypeMap(&(me->nesting))) { |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1360 | // If in a map and the right decoding mode, get the label |
| 1361 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1362 | // Save label in pDecodedItem and get the next which will |
| 1363 | // be the real data |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1364 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1365 | nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1366 | if(QCBORDecode_IsUnrecoverableError(nReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1367 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1368 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1369 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1370 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1371 | |
| 1372 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1373 | // strings are always good labels |
| 1374 | pDecodedItem->label.string = LabelItem.val.string; |
| 1375 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 1376 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1377 | // It's not a string and we only want strings |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1378 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1379 | goto Done; |
| 1380 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1381 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1382 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1383 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1384 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1385 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1386 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1387 | pDecodedItem->label.string = LabelItem.val.string; |
| 1388 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1389 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1390 | } else { |
| 1391 | // label is not an int or a string. It is an arrray |
| 1392 | // or a float or such and this implementation doesn't handle that. |
| 1393 | // Also, tags on labels are ignored. |
| 1394 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1395 | goto Done; |
| 1396 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1397 | } |
| 1398 | } else { |
| 1399 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1400 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1401 | nReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1402 | goto Done; |
| 1403 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1404 | // Decoding a map as an array |
| 1405 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1406 | // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2 |
| 1407 | // Cast is needed because of integer promotion |
| 1408 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1411 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1412 | Done: |
| 1413 | return nReturn; |
| 1414 | } |
| 1415 | |
| 1416 | |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1417 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1418 | /* |
| 1419 | See if next item is a CBOR break. If it is, it is consumed, |
| 1420 | if not it is not consumed. |
| 1421 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1422 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1423 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1424 | { |
| 1425 | *pbNextIsBreak = false; |
| 1426 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1427 | QCBORItem Peek; |
| 1428 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
| 1429 | QCBORError uReturn = GetNext_Item(pUIB, &Peek, NULL); |
| 1430 | if(uReturn != QCBOR_SUCCESS) { |
| 1431 | return uReturn; |
| 1432 | } |
| 1433 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1434 | // It is not a break, rewind so it can be processed normally. |
| 1435 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1436 | } else { |
| 1437 | *pbNextIsBreak = true; |
| 1438 | } |
| 1439 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1440 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1441 | return QCBOR_SUCCESS; |
| 1442 | } |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1443 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1444 | |
| 1445 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1446 | /* |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1447 | An item was just consumed, now figure out if it was the |
| 1448 | end of an array or map that can be closed out. That |
| 1449 | may in turn close out another map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1450 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1451 | static QCBORError NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1452 | { |
| 1453 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1454 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1455 | /* This loops ascending nesting levels as long as there is ascending to do */ |
| 1456 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1457 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1458 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1459 | /* Decrement count for definite length maps / arrays */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1460 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1461 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1462 | /* Didn't close out map or array, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1465 | /* All of a definite length array was consumed; fall through to |
| 1466 | ascend */ |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1467 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1468 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1469 | } else { |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1470 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1471 | /* If not definite length, have to check for a CBOR break */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1472 | bool bIsBreak = false; |
| 1473 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1474 | if(uReturn != QCBOR_SUCCESS) { |
| 1475 | goto Done; |
| 1476 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1477 | |
| 1478 | if(!bIsBreak) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1479 | /* It's not a break so nothing closes out and all work is done */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1480 | break; |
| 1481 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1482 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1483 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1484 | /* |
| 1485 | Break occurred inside a bstr-wrapped CBOR or |
| 1486 | in the top level sequence. This is always an |
| 1487 | error because neither are an indefinte length |
| 1488 | map/array. |
| 1489 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1490 | uReturn = QCBOR_ERR_BAD_BREAK; |
| 1491 | goto Done; |
| 1492 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1493 | /* It was a break in an indefinite length map / array */ |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1494 | |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 1495 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1496 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1497 | |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1498 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1499 | /* All items in the map/array level have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1500 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1501 | /* But ascent in bounded mode is only by explicit call to |
| 1502 | QCBORDecode_ExitBoundedMode() */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1503 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1504 | /* Set the count to zero for definite length arrays to indicate |
| 1505 | cursor is at end of bounded map / array */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1506 | if(bMarkEnd) { |
| 1507 | // Used for definite and indefinite to signal end |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1508 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1509 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1510 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | /* Finally, actually ascend one level. */ |
| 1515 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1516 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1517 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1518 | uReturn = QCBOR_SUCCESS; |
| 1519 | |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1520 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1521 | Done: |
Laurence Lundblade | c7fc588 | 2020-12-03 04:31:37 -0800 | [diff] [blame] | 1522 | #endif /* #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1523 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1524 | return uReturn; |
| 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1529 | This handles the traversal descending into and asecnding out of maps, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1530 | arrays and bstr-wrapped CBOR. It figures out the ends of definite and |
| 1531 | indefinte length maps and arrays by looking at the item count or |
| 1532 | finding CBOR breaks. It detects the ends of the top-level sequence |
| 1533 | and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1534 | |
| 1535 | @retval QCBOR_ERR_UNSUPPORTED X |
| 1536 | |
| 1537 | @retval QCBOR_ERR_HIT_END |
| 1538 | |
| 1539 | @retval QCBOR_ERR_INT_OVERFLOW X |
| 1540 | |
| 1541 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1542 | |
| 1543 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1544 | |
| 1545 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED X |
| 1546 | |
| 1547 | @retval QCBOR_ERR_BAD_TYPE_7 X |
| 1548 | |
| 1549 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1550 | |
| 1551 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1552 | |
| 1553 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1554 | |
| 1555 | @retval QCBOR_ERR_MAP_LABEL_TYPE X |
| 1556 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1557 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1558 | |
| 1559 | @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1560 | |
| 1561 | @retval QCBOR_ERR_BAD_BREAK |
| 1562 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1563 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1564 | static QCBORError |
| 1565 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1566 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1567 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1568 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1569 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1570 | /* |
| 1571 | If out of bytes to consume, it is either the end of the top-level |
| 1572 | sequence of some bstr-wrapped CBOR that was entered. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1573 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1574 | In the case of bstr-wrapped CBOR, the length of the UsefulInputBuf |
| 1575 | was set to that of the bstr-wrapped CBOR. When the bstr-wrapped |
| 1576 | CBOR is exited, the length is set back to the top-level's length |
| 1577 | or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1578 | */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1579 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1580 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1581 | goto Done; |
| 1582 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1583 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1584 | /* |
| 1585 | Check to see if at the end of a bounded definite length map or |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1586 | array. The check for the end of an indefinite length array is |
| 1587 | later. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1588 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1589 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1590 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1591 | goto Done; |
| 1592 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1593 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1594 | /* ==== Next: not at the end so get another item ==== */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1595 | uReturn = GetNext_MapEntry(me, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1596 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1597 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1598 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1599 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1600 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1601 | /* |
| 1602 | Breaks ending arrays/maps are always processed at the end of this |
| 1603 | function. They should never show up here. |
| 1604 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1605 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1606 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1607 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1608 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1609 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1610 | /* |
| 1611 | Record the nesting level for this data item before processing any |
| 1612 | of decrementing and descending. |
| 1613 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1614 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1615 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1616 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1617 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1618 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1619 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1620 | If the new item is a map or array, descend. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1621 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1622 | Empty indefinite length maps and arrays are descended into, but |
| 1623 | then ascended out of in the next chunk of code. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1624 | |
| 1625 | Maps and arrays do count as items in the map/array that |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1626 | encloses them so a decrement needs to be done for them too, but |
| 1627 | that is done only when all the items in them have been |
| 1628 | processed, not when they are opened with the exception of an |
| 1629 | empty map or array. |
| 1630 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1631 | QCBORError uDescendErr; |
| 1632 | uDescendErr = DecodeNesting_DescendMapOrArray(&(me->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1633 | pDecodedItem->uDataType, |
| 1634 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1635 | if(uDescendErr != QCBOR_SUCCESS) { |
| 1636 | /* This error is probably a traversal error and it |
| 1637 | overrides the non-traversal error. */ |
| 1638 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1639 | goto Done; |
| 1640 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1643 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1644 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1645 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1646 | /* |
| 1647 | The following cases are handled here: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1648 | - A non-aggregate like an integer or string |
| 1649 | - An empty definite length map or array |
| 1650 | - An indefinite length map or array that might be empty or might not. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1651 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1652 | NestLevelAscender() does the work of decrementing the count for an |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1653 | definite length map/array and break detection for an indefinite |
| 1654 | length map/array. If the end of the map/array was reached, then |
| 1655 | it ascends nesting levels, possibly all the way to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1656 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1657 | QCBORError uAscendErr; |
| 1658 | uAscendErr = NestLevelAscender(me, true); |
| 1659 | if(uAscendErr != QCBOR_SUCCESS) { |
| 1660 | /* This error is probably a traversal error and it |
| 1661 | overrides the non-traversal error. */ |
| 1662 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1663 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1664 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1665 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1666 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1667 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1668 | /* |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1669 | Tell the caller what level is next. This tells them what |
| 1670 | maps/arrays were closed out and makes it possible for them to |
| 1671 | reconstruct the tree with just the information returned in |
| 1672 | a QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1673 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1674 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1675 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1676 | pDecodedItem->uNextNestLevel = 0; |
| 1677 | } else { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1678 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1679 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1680 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1681 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1682 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1685 | static void ShiftTags(QCBORItem *pDecodedItem) |
| 1686 | { |
| 1687 | pDecodedItem->uTags[0] = pDecodedItem->uTags[1]; |
| 1688 | pDecodedItem->uTags[1] = pDecodedItem->uTags[2]; |
| 1689 | pDecodedItem->uTags[2] = pDecodedItem->uTags[3]; |
| 1690 | pDecodedItem->uTags[2] = CBOR_TAG_INVALID16; |
| 1691 | } |
| 1692 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1693 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1694 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1695 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1696 | The epoch formatted date. Turns lots of different forms of encoding |
| 1697 | date into uniform one |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1698 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1699 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1700 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1701 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1702 | |
| 1703 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1704 | |
| 1705 | switch (pDecodedItem->uDataType) { |
| 1706 | |
| 1707 | case QCBOR_TYPE_INT64: |
| 1708 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1709 | break; |
| 1710 | |
| 1711 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1712 | // This only happens for CBOR type 0 > INT64_MAX so it is |
| 1713 | // always an overflow. |
| 1714 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1715 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1716 | break; |
| 1717 | |
| 1718 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1719 | case QCBOR_TYPE_FLOAT: |
| 1720 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1721 | { |
| 1722 | // This comparison needs to be done as a float before |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1723 | // conversion to an int64_t to be able to detect doubles that |
| 1724 | // are too large to fit into an int64_t. A double has 52 |
| 1725 | // bits of preceision. An int64_t has 63. Casting INT64_MAX |
| 1726 | // to a double actually causes a round up which is bad and |
| 1727 | // wrong for the comparison because it will allow conversion |
| 1728 | // of doubles that can't fit into a uint64_t. To remedy this |
| 1729 | // INT64_MAX - 0x7ff is used as the cutoff point because if |
| 1730 | // that value rounds up in conversion to double it will still |
| 1731 | // be less than INT64_MAX. 0x7ff is picked because it has 11 |
| 1732 | // bits set. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1733 | // |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1734 | // INT64_MAX seconds is on the order of 10 billion years, and |
| 1735 | // the earth is less than 5 billion years old, so for most |
| 1736 | // uses this conversion error won't occur even though doubles |
| 1737 | // can go much larger. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1738 | // |
| 1739 | // Without the 0x7ff there is a ~30 minute range of time |
| 1740 | // values 10 billion years in the past and in the future |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1741 | // where this code would go wrong. Some compilers |
| 1742 | // will generate warnings or errors without the 0x7ff |
| 1743 | // because of the precision issue. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1744 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
| 1745 | pDecodedItem->val.dfnum : |
| 1746 | (double)pDecodedItem->val.fnum; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1747 | if(isnan(d) || |
| 1748 | d > (double)(INT64_MAX - 0x7ff) || |
| 1749 | d < (double)(INT64_MIN + 0x7ff)) { |
| 1750 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1751 | goto Done; |
| 1752 | } |
| 1753 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1754 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 1755 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1756 | } |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1757 | #else |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1758 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1759 | uReturn = QCBOR_ERR_FLOAT_DATE_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1760 | goto Done; |
| 1761 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1762 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1763 | break; |
| 1764 | |
| 1765 | default: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1766 | uReturn = QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1767 | goto Done; |
| 1768 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1769 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1770 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1771 | |
| 1772 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1773 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1778 | /* |
| 1779 | Decode decimal fractions and big floats. |
| 1780 | |
| 1781 | When called pDecodedItem must be the array that is tagged as a big |
| 1782 | float or decimal fraction, the array that has the two members, the |
| 1783 | exponent and mantissa. |
| 1784 | |
| 1785 | This will fetch and decode the exponent and mantissa and put the |
| 1786 | result back into pDecodedItem. |
| 1787 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 1788 | static inline QCBORError |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1789 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
| 1790 | { |
| 1791 | QCBORError nReturn; |
| 1792 | |
| 1793 | // --- Make sure it is an array; track nesting level of members --- |
| 1794 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 1795 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1796 | goto Done; |
| 1797 | } |
| 1798 | |
| 1799 | // A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1800 | // definite length arrays, but not for indefnite. Instead remember |
| 1801 | // the nesting level the two integers must be at, which is one |
| 1802 | // deeper than that of the array. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1803 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 1804 | |
| 1805 | // --- Is it a decimal fraction or a bigfloat? --- |
| 1806 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
| 1807 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 1808 | |
| 1809 | // --- Get the exponent --- |
| 1810 | QCBORItem exponentItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1811 | nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1812 | if(nReturn != QCBOR_SUCCESS) { |
| 1813 | goto Done; |
| 1814 | } |
| 1815 | if(exponentItem.uNestingLevel != nNestLevel) { |
| 1816 | // Array is empty or a map/array encountered when expecting an int |
| 1817 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1818 | goto Done; |
| 1819 | } |
| 1820 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
| 1821 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1822 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1823 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1824 | // will be too large for this to handle and thus an error that will |
| 1825 | // get handled in the next else. |
| 1826 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 1827 | } else { |
| 1828 | // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1829 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1830 | goto Done; |
| 1831 | } |
| 1832 | |
| 1833 | // --- Get the mantissa --- |
| 1834 | QCBORItem mantissaItem; |
| 1835 | nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL); |
| 1836 | if(nReturn != QCBOR_SUCCESS) { |
| 1837 | goto Done; |
| 1838 | } |
| 1839 | if(mantissaItem.uNestingLevel != nNestLevel) { |
| 1840 | // Mantissa missing or map/array encountered when expecting number |
| 1841 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1842 | goto Done; |
| 1843 | } |
| 1844 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
| 1845 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1846 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1847 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1848 | // will be too large for this to handle and thus an error that |
| 1849 | // will get handled in an else below. |
| 1850 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1851 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 1852 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1853 | // Got a good big num mantissa |
| 1854 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
| 1855 | // Depends on numbering of QCBOR_TYPE_XXX |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1856 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 1857 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 1858 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1859 | } else { |
| 1860 | // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1861 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1862 | goto Done; |
| 1863 | } |
| 1864 | |
| 1865 | // --- Check that array only has the two numbers --- |
| 1866 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 1867 | // Extra items in the decimal fraction / big float |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1868 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1869 | goto Done; |
| 1870 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 1871 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1872 | |
| 1873 | Done: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1874 | return nReturn; |
| 1875 | } |
| 1876 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1877 | |
| 1878 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 1879 | static inline QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1880 | { |
| 1881 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1882 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 1883 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1884 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 1885 | } else { |
| 1886 | return QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1887 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1888 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1889 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1890 | return QCBOR_SUCCESS; |
| 1891 | } |
| 1892 | |
| 1893 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 1894 | /* |
| 1895 | * Table of CBOR tags whose content is either a text string or a byte |
| 1896 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 1897 | * of uQCBORtype indicates the content should be a byte string rather |
| 1898 | * than a text string |
| 1899 | */ |
| 1900 | struct StringTagMapEntry { |
| 1901 | uint16_t uTagNumber; |
| 1902 | uint8_t uQCBORtype; |
| 1903 | }; |
| 1904 | |
| 1905 | #define IS_BYTE_STRING_BIT 0x80 |
| 1906 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 1907 | |
| 1908 | static const struct StringTagMapEntry StringTagMap[] = { |
| 1909 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
| 1910 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 1911 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 1912 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 1913 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
| 1914 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 1915 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 1916 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 1917 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
| 1918 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 1919 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 1920 | }; |
| 1921 | |
| 1922 | |
| 1923 | /* |
| 1924 | * Process the CBOR tags that whose content is a byte string or a text |
| 1925 | * string and for which the string is just passed on to the caller. |
| 1926 | * |
| 1927 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 1928 | * type. Nothing more. It may not be the most important |
| 1929 | * functionality, but it part of implementing as much of RFC 7049 as |
| 1930 | * possible. |
| 1931 | * |
| 1932 | * This returns QCBOR_SUCCESS if the tag was procssed, |
| 1933 | * QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
| 1934 | * QCBOR_ERR_BAD_OPT_TAG if the content type was wrong for the tag. |
| 1935 | */ |
| 1936 | static inline |
| 1937 | QCBORError ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1938 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 1939 | /* This only works on tags that were not mapped; no need for other yet */ |
| 1940 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1941 | return QCBOR_ERR_UNSUPPORTED; |
| 1942 | } |
| 1943 | |
| 1944 | unsigned uIndex; |
| 1945 | for(uIndex = 0; StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 1946 | if(StringTagMap[uIndex].uTagNumber == uTag) { |
| 1947 | break; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | const uint8_t uQCBORType = StringTagMap[uIndex].uQCBORtype; |
| 1952 | if(uQCBORType == QCBOR_TYPE_NONE) { |
| 1953 | /* repurpose this error to mean, not handled here */ |
| 1954 | return QCBOR_ERR_UNSUPPORTED; |
| 1955 | } |
| 1956 | |
| 1957 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 1958 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 1959 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 1960 | } |
| 1961 | |
| 1962 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1963 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1964 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1965 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 1966 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1967 | return QCBOR_SUCCESS; |
| 1968 | } |
| 1969 | |
| 1970 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1971 | /* |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 1972 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 1973 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 1974 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 1975 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1976 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1977 | static QCBORError |
| 1978 | QCBORDecode_GetNextTag(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1979 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 1980 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1981 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 1982 | uReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem); |
| 1983 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1984 | goto Done; |
| 1985 | } |
| 1986 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 1987 | /* When there are no tag numbers for the item, this exits first |
| 1988 | * thing and effectively does nothing. |
| 1989 | * |
| 1990 | * This loops over all the tag numbers accumulated for this item |
| 1991 | * trying to decode and interpret them. This stops at the end of |
| 1992 | * the list or at the first tag number that can't be interpreted by |
| 1993 | * this code. This is effectively a recursive processing of the |
| 1994 | * tags number list that handles nested tags. |
| 1995 | */ |
| 1996 | while(1) { |
| 1997 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 1998 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 1999 | */ |
| 2000 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2001 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2002 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2003 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2004 | break; |
| 2005 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2006 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2007 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2008 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2009 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2010 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2011 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2012 | uReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem); |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2013 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2014 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2015 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2016 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2017 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2018 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2019 | } else { |
| 2020 | /* See if it is a pass-through byte/text string tag; process if so */ |
| 2021 | uReturn = ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2022 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2023 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
| 2024 | /* It wasn't a pass-through byte/text string tag so it is |
| 2025 | * an unknown tag. This is the exit from the loop on the |
| 2026 | * first unknown tag. It is a successful exit. |
| 2027 | */ |
| 2028 | uReturn = QCBOR_SUCCESS; |
| 2029 | break; |
| 2030 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2031 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2032 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2033 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2034 | /* Error exit from the loop */ |
| 2035 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2036 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2037 | |
| 2038 | /* A tag was successfully processed, shift it out of the list of |
| 2039 | * tags returned. This is the loop increment. |
| 2040 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2041 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2045 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2049 | /* |
| 2050 | Public function, see header qcbor/qcbor_decode.h file |
| 2051 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2052 | QCBORError |
| 2053 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2054 | { |
| 2055 | QCBORError uErr; |
| 2056 | uErr = QCBORDecode_GetNextTag(pMe, pDecodedItem); |
| 2057 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2058 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2059 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2060 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2061 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | |
| 2065 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2066 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2067 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2068 | QCBORError |
| 2069 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2070 | { |
| 2071 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2072 | const UsefulInputBuf Save = pMe->InBuf; |
| 2073 | |
| 2074 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2075 | |
| 2076 | pMe->nesting = SaveNesting; |
| 2077 | pMe->InBuf = Save; |
| 2078 | |
| 2079 | return uErr; |
| 2080 | } |
| 2081 | |
| 2082 | |
| 2083 | /* |
| 2084 | Public function, see header qcbor/qcbor_decode.h file |
| 2085 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2086 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2087 | { |
| 2088 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2089 | return; |
| 2090 | } |
| 2091 | |
| 2092 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2093 | } |
| 2094 | |
| 2095 | |
| 2096 | /* |
| 2097 | Public function, see header qcbor/qcbor_decode.h file |
| 2098 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2099 | QCBORError |
| 2100 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, |
| 2101 | QCBORItem *pDecodedItem, |
| 2102 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2103 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2104 | QCBORError nReturn; |
| 2105 | |
| 2106 | nReturn = QCBORDecode_GetNext(me, pDecodedItem); |
| 2107 | if(nReturn != QCBOR_SUCCESS) { |
| 2108 | return nReturn; |
| 2109 | } |
| 2110 | |
| 2111 | if(pTags != NULL) { |
| 2112 | pTags->uNumUsed = 0; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2113 | // Reverse the order because pTags is reverse of |
| 2114 | // QCBORItem.uTags. |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2115 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2116 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2117 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2118 | } |
| 2119 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2120 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2121 | } |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2122 | pTags->puTags[pTags->uNumUsed] = ConvertTag(me,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2123 | pTags->uNumUsed++; |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2131 | /* |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2132 | Decoding items is done in 5 layered functions, one calling the |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2133 | next one down. If a layer has no work to do for a particular item |
| 2134 | it returns quickly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2135 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2136 | - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes |
| 2137 | tagged data items, turning them into the local C representation. |
| 2138 | For the most simple it is just associating a QCBOR_TYPE with the data. For |
| 2139 | the complex ones that an aggregate of data items, there is some further |
| 2140 | decoding and a little bit of recursion. |
| 2141 | |
| 2142 | - QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2143 | ends of maps and arrays. It tracks descending into and ascending |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2144 | out of maps/arrays. It processes all breaks that terminate |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2145 | indefinite length maps and arrays. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2146 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2147 | - GetNext_MapEntry -- This handles the combining of two |
| 2148 | items, the label and the data, that make up a map entry. |
| 2149 | It only does work on maps. It combines the label and data |
| 2150 | items into one labeled item. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2151 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2152 | - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the |
| 2153 | tags into bit flags associated with the data item. No actual decoding |
| 2154 | of the contents of the tagged item is performed here. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2155 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2156 | - GetNext_FullItem -- This assembles the sub-items that make up |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2157 | an indefinte length string into one string item. It uses the |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2158 | string allocater to create contiguous space for the item. It |
| 2159 | processes all breaks that are part of indefinite length strings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2160 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2161 | - GetNext_Item -- This decodes the atomic data items in CBOR. Each |
| 2162 | atomic data item has a "major type", an integer "argument" and optionally |
| 2163 | some content. For text and byte strings, the content is the bytes |
| 2164 | that make up the string. These are the smallest data items that are |
| 2165 | considered to be well-formed. The content may also be other data items in |
| 2166 | the case of aggregate types. They are not handled in this layer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2167 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2168 | Roughly this takes 300 bytes of stack for vars. Need to |
| 2169 | evaluate this more carefully and correctly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2170 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2171 | */ |
| 2172 | |
| 2173 | |
| 2174 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2175 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2176 | */ |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2177 | bool QCBORDecode_IsTagged(QCBORDecodeContext *me, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2178 | const QCBORItem *pItem, |
| 2179 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2180 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2181 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2182 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2183 | break; |
| 2184 | } |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2185 | if(ConvertTag(me, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2186 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2187 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2188 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2189 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2190 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | |
| 2194 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2195 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2196 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2197 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2198 | { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2199 | QCBORError uReturn = me->uLastError; |
| 2200 | |
| 2201 | if(uReturn != QCBOR_SUCCESS) { |
| 2202 | goto Done; |
| 2203 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2204 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2205 | // Error out if all the maps/arrays are not closed out |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2206 | if(!DecodeNesting_IsCurrentAtTop(&(me->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2207 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2208 | goto Done; |
| 2209 | } |
| 2210 | |
| 2211 | // Error out if not all the bytes are consumed |
| 2212 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2213 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2214 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2215 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2216 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2217 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2218 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2219 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2220 | StringAllocator_Destruct(&(me->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2221 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2222 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2223 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2227 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2228 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2229 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2230 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2231 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2232 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2233 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2234 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2235 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2236 | return CBOR_TAG_INVALID64; |
| 2237 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2238 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2239 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2240 | } else { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 2241 | return ConvertTag(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2242 | } |
| 2243 | } |
| 2244 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2245 | /* |
| 2246 | Public function, see header qcbor/qcbor_decode.h file |
| 2247 | */ |
| 2248 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2249 | uint32_t uIndex) |
| 2250 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2251 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2252 | return CBOR_TAG_INVALID64; |
| 2253 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2254 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2255 | return CBOR_TAG_INVALID64; |
| 2256 | } else { |
| 2257 | return ConvertTag(pMe, pMe->uLastTags[uIndex]); |
| 2258 | } |
| 2259 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2260 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2261 | /* |
| 2262 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2263 | Decoder errors handled in this file |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2264 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2265 | - Hit end of input before it was expected while decoding type and |
| 2266 | number QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2267 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2268 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2269 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2270 | - Hit end of input while decoding a text or byte string |
| 2271 | QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2272 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2273 | - Encountered conflicting tags -- e.g., an item is tagged both a date |
| 2274 | string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2275 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2276 | - Encontered an array or mapp that has too many items |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2277 | QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2278 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2279 | - Encountered array/map nesting that is too deep |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2280 | QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2281 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2282 | - An epoch date > INT64_MAX or < INT64_MIN was encountered |
| 2283 | QCBOR_ERR_DATE_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2284 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2285 | - The type of a map label is not a string or int |
| 2286 | QCBOR_ERR_MAP_LABEL_TYPE |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2287 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2288 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2289 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2290 | */ |
| 2291 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2292 | |
| 2293 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2294 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2295 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2296 | /* =========================================================================== |
| 2297 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2298 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2299 | This implements a simple sting allocator for indefinite length |
| 2300 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2301 | implements the function type QCBORStringAllocate and allows easy |
| 2302 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2303 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2304 | This particular allocator is built-in for convenience. The caller |
| 2305 | can implement their own. All of this following code will get |
| 2306 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2307 | |
| 2308 | This is a very primitive memory allocator. It does not track |
| 2309 | individual allocations, only a high-water mark. A free or |
| 2310 | reallocation must be of the last chunk allocated. |
| 2311 | |
| 2312 | The size of the pool and offset to free memory are packed into the |
| 2313 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2314 | the decode context. Since the address of the pool may not be |
| 2315 | aligned, they have to be packed and unpacked as if they were |
| 2316 | serialized data of the wire or such. |
| 2317 | |
| 2318 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2319 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2320 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2321 | |
| 2322 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2323 | static inline int |
| 2324 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2325 | { |
| 2326 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2327 | UsefulInputBuf UIB; |
| 2328 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2329 | // Just assume the size here. It was checked during SetUp so |
| 2330 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2331 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2332 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2333 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2334 | return UsefulInputBuf_GetError(&UIB); |
| 2335 | } |
| 2336 | |
| 2337 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2338 | static inline int |
| 2339 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2340 | { |
| 2341 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2342 | // length check performed here is useful. |
| 2343 | UsefulOutBuf UOB; |
| 2344 | |
| 2345 | UsefulOutBuf_Init(&UOB, Pool); |
| 2346 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2347 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2348 | return UsefulOutBuf_GetError(&UOB); |
| 2349 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2350 | |
| 2351 | |
| 2352 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2353 | Internal function for an allocation, reallocation free and destuct. |
| 2354 | |
| 2355 | Having only one function rather than one each per mode saves space in |
| 2356 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2357 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2358 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2359 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2360 | static UsefulBuf |
| 2361 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2362 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2363 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2364 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2365 | uint32_t uPoolSize; |
| 2366 | uint32_t uFreeOffset; |
| 2367 | |
| 2368 | if(uNewSize > UINT32_MAX) { |
| 2369 | // This allocator is only good up to 4GB. This check should |
| 2370 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2371 | goto Done; |
| 2372 | } |
| 2373 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2374 | |
| 2375 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2376 | goto Done; |
| 2377 | } |
| 2378 | |
| 2379 | if(uNewSize) { |
| 2380 | if(pMem) { |
| 2381 | // REALLOCATION MODE |
| 2382 | // Calculate pointer to the end of the memory pool. It is |
| 2383 | // assumed that pPool + uPoolSize won't wrap around by |
| 2384 | // assuming the caller won't pass a pool buffer in that is |
| 2385 | // not in legitimate memory space. |
| 2386 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2387 | |
| 2388 | // Check that the pointer for reallocation is in the range of the |
| 2389 | // pool. This also makes sure that pointer math further down |
| 2390 | // doesn't wrap under or over. |
| 2391 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2392 | // Offset to start of chunk for reallocation. This won't |
| 2393 | // wrap under because of check that pMem >= pPool. Cast |
| 2394 | // is safe because the pool is always less than UINT32_MAX |
| 2395 | // because of check in QCBORDecode_SetMemPool(). |
| 2396 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2397 | |
| 2398 | // Check to see if the allocation will fit. uPoolSize - |
| 2399 | // uMemOffset will not wrap under because of check that |
| 2400 | // pMem is in the range of the uPoolSize by check above. |
| 2401 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2402 | ReturnValue.ptr = pMem; |
| 2403 | ReturnValue.len = uNewSize; |
| 2404 | |
| 2405 | // Addition won't wrap around over because uNewSize was |
| 2406 | // checked to be sure it is less than the pool size. |
| 2407 | uFreeOffset = uMemOffset + uNewSize32; |
| 2408 | } |
| 2409 | } |
| 2410 | } else { |
| 2411 | // ALLOCATION MODE |
| 2412 | // uPoolSize - uFreeOffset will not underflow because this |
| 2413 | // pool implementation makes sure uFreeOffset is always |
| 2414 | // smaller than uPoolSize through this check here and |
| 2415 | // reallocation case. |
| 2416 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2417 | ReturnValue.len = uNewSize; |
| 2418 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2419 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2420 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2421 | } |
| 2422 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2423 | if(pMem) { |
| 2424 | // FREE MODE |
| 2425 | // Cast is safe because of limit on pool size in |
| 2426 | // QCBORDecode_SetMemPool() |
| 2427 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2428 | } else { |
| 2429 | // DESTRUCT MODE |
| 2430 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2431 | } |
| 2432 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2433 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2434 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2435 | MemPool_Pack(Pool, uFreeOffset); |
| 2436 | |
| 2437 | Done: |
| 2438 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2439 | } |
| 2440 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2441 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2442 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2443 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2444 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2445 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2446 | UsefulBuf Pool, |
| 2447 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2448 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2449 | // The pool size and free mem offset are packed into the beginning |
| 2450 | // of the pool memory. This compile time check make sure the |
| 2451 | // constant in the header is correct. This check should optimize |
| 2452 | // down to nothing. |
| 2453 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2454 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2455 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2456 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2457 | // The pool size and free offset packed in to the beginning of pool |
| 2458 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2459 | // machines. |
| 2460 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2461 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2462 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2463 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2464 | // This checks that the pool buffer given is big enough. |
| 2465 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2466 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2467 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2468 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2469 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2470 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2471 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2472 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2473 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2474 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2475 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2476 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2477 | |
| 2478 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2479 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2480 | { |
| 2481 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2482 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2483 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2484 | |
| 2485 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2486 | Consume an entire map or array (and do next to |
| 2487 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2488 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2489 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2490 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2491 | const QCBORItem *pItemToConsume, |
| 2492 | uint_fast8_t *puNextNestLevel) |
| 2493 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2494 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2495 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2496 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2497 | // If it is a map or array, this will tell if it is empty. |
| 2498 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2499 | |
| 2500 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2501 | /* 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] | 2502 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2503 | /* This works for definite and indefinite length |
| 2504 | * maps and arrays by using the nesting level |
| 2505 | */ |
| 2506 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2507 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2508 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2509 | goto Done; |
| 2510 | } |
| 2511 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2512 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2513 | *puNextNestLevel = Item.uNextNestLevel; |
| 2514 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2515 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2516 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2517 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2518 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2519 | /* Just pass the nesting level through */ |
| 2520 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2521 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2522 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2523 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2524 | |
| 2525 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2526 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2530 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2531 | Works only for integer and string labels. Returns false |
| 2532 | for any other type. */ |
| 2533 | static inline bool |
| 2534 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2535 | { |
| 2536 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2537 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2538 | return true; |
| 2539 | } |
| 2540 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2541 | 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] | 2542 | return true; |
| 2543 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2544 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2545 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2546 | return true; |
| 2547 | } |
| 2548 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2549 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2550 | return true; |
| 2551 | } |
| 2552 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2553 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2554 | /* Other label types are never matched */ |
| 2555 | return false; |
| 2556 | } |
| 2557 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2558 | |
| 2559 | /* |
| 2560 | Returns true if Item1 and Item2 are the same type |
| 2561 | or if either are of QCBOR_TYPE_ANY. |
| 2562 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2563 | static inline bool |
| 2564 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2565 | { |
| 2566 | if(Item1.uDataType == Item2.uDataType) { |
| 2567 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2568 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2569 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2570 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2571 | return true; |
| 2572 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2573 | return false; |
| 2574 | } |
| 2575 | |
| 2576 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2577 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2578 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2579 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2580 | @param[in] pMe The decode context to search. |
| 2581 | @param[in,out] pItemArray The items to search for and the items found. |
| 2582 | @param[out] puOffset Byte offset of last item matched. |
| 2583 | @param[in] pCBContext Context for the not-found item call back. |
| 2584 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2585 | |
| 2586 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2587 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2588 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2589 | were found for one of the labels being |
| 2590 | search for. This duplicate detection is |
| 2591 | only performed for items in pItemArray, |
| 2592 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2593 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2594 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2595 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2596 | |
| 2597 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2598 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2599 | On input pItemArray contains a list of labels and data types |
| 2600 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2601 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2602 | On output the fully retrieved items are filled in with |
| 2603 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2604 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2605 | 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] | 2606 | |
| 2607 | 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] | 2608 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2609 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2610 | MapSearch(QCBORDecodeContext *pMe, |
| 2611 | QCBORItem *pItemArray, |
| 2612 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2613 | void *pCBContext, |
| 2614 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2615 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2616 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2617 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2618 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2619 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2620 | uReturn = pMe->uLastError; |
| 2621 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2622 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2623 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2624 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2625 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2626 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2627 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2628 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2629 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2632 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2633 | // It is an empty bounded array or map |
| 2634 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2635 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2636 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2637 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2638 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2639 | // Nothing is ever found in an empty array or map. All items |
| 2640 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2641 | uReturn = QCBOR_SUCCESS; |
| 2642 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2643 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2646 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2647 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2648 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2649 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2650 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2651 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2652 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2653 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2654 | Loop over all the items in the map or array. Each item |
| 2655 | could be a map or array, but label matching is only at |
| 2656 | the main level. This handles definite and indefinite |
| 2657 | length maps and arrays. The only reason this is ever |
| 2658 | called on arrays is to find their end position. |
| 2659 | |
| 2660 | This will always run over all items in order to do |
| 2661 | duplicate detection. |
| 2662 | |
| 2663 | This will exit with failure if it encounters an |
| 2664 | unrecoverable error, but continue on for recoverable |
| 2665 | errors. |
| 2666 | |
| 2667 | If a recoverable error occurs on a matched item, then |
| 2668 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2669 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2670 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2671 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2672 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2673 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2674 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2675 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2676 | /* Get the item */ |
| 2677 | QCBORItem Item; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2678 | QCBORError uResult = QCBORDecode_GetNextTag(pMe, &Item); |
| 2679 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2680 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2681 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2682 | goto Done; |
| 2683 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2684 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2685 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2686 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2687 | goto Done; |
| 2688 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2689 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2690 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2691 | bool bMatched = false; |
| 2692 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 2693 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2694 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2695 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2696 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2697 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2698 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2699 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2700 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2701 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2702 | goto Done; |
| 2703 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2704 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2705 | if(uResult != QCBOR_SUCCESS) { |
| 2706 | uReturn = uResult; |
| 2707 | goto Done; |
| 2708 | } |
| 2709 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2710 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2711 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2712 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2713 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2714 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2715 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2716 | bMatched = true; |
| 2717 | } |
| 2718 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2719 | |
| 2720 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2721 | if(!bMatched && pfCallback != NULL) { |
| 2722 | /* |
| 2723 | Call the callback on unmatched labels. |
| 2724 | (It is tempting to do duplicate detection here, but that would |
| 2725 | require dynamic memory allocation because the number of labels |
| 2726 | that might be encountered is unbounded.) |
| 2727 | */ |
| 2728 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2729 | if(uReturn != QCBOR_SUCCESS) { |
| 2730 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2731 | } |
| 2732 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2733 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2734 | /* |
| 2735 | Consume the item whether matched or not. This |
| 2736 | does the work of traversing maps and array and |
| 2737 | everything in them. In this loop only the |
| 2738 | items at the current nesting level are examined |
| 2739 | to match the labels. |
| 2740 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2741 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2742 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2743 | goto Done; |
| 2744 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2745 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2746 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2747 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2748 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2749 | |
| 2750 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2751 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2752 | // Check here makes sure that this won't accidentally be |
| 2753 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2754 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
| 2755 | if(uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 2756 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 2757 | goto Done; |
| 2758 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2759 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 2760 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2761 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2762 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2763 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 2764 | |
| 2765 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2766 | /* 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] | 2767 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2768 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2769 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 2770 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2771 | } |
| 2772 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2773 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2774 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2777 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2778 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2779 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2780 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2781 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 2782 | int64_t nLabel, |
| 2783 | uint8_t uQcborType, |
| 2784 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2785 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2786 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2787 | return; |
| 2788 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2789 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2790 | QCBORItem OneItemSeach[2]; |
| 2791 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2792 | OneItemSeach[0].label.int64 = nLabel; |
| 2793 | OneItemSeach[0].uDataType = uQcborType; |
| 2794 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2795 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2796 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2797 | |
| 2798 | *pItem = OneItemSeach[0]; |
| 2799 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2800 | if(uReturn != QCBOR_SUCCESS) { |
| 2801 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2802 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2803 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2804 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2807 | Done: |
| 2808 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2812 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2813 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2814 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2815 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 2816 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2817 | uint8_t uQcborType, |
| 2818 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2819 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2820 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2821 | return; |
| 2822 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2823 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2824 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2825 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2826 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2827 | OneItemSeach[0].uDataType = uQcborType; |
| 2828 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2829 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2830 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2831 | if(uReturn != QCBOR_SUCCESS) { |
| 2832 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2833 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2834 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2835 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2836 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2837 | } |
| 2838 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2839 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2840 | |
| 2841 | Done: |
| 2842 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2846 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2847 | static QCBORError |
| 2848 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2849 | { |
| 2850 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 2851 | if(uDataType == puTypeList[i]) { |
| 2852 | return QCBOR_SUCCESS; |
| 2853 | } |
| 2854 | } |
| 2855 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2856 | } |
| 2857 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 2858 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2859 | /** |
| 2860 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2861 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2862 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2863 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 2864 | @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] | 2865 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2866 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered |
| 2867 | tag value. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2868 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2869 | static QCBORError |
| 2870 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2871 | { |
| 2872 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 2873 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 2874 | /* There are tags that QCBOR couldn't process on this item and |
| 2875 | the caller has told us there should not be. */ |
| 2876 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2877 | } |
| 2878 | |
| 2879 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 2880 | const int nItemType = pItem->uDataType; |
| 2881 | |
| 2882 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 2883 | // Must match the tag and only the tag |
| 2884 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2885 | } |
| 2886 | |
| 2887 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 2888 | if(uReturn == QCBOR_SUCCESS) { |
| 2889 | return QCBOR_SUCCESS; |
| 2890 | } |
| 2891 | |
| 2892 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 2893 | /* Must match the content type and only the content type. |
| 2894 | There was no match just above so it is a fail. */ |
| 2895 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2896 | } |
| 2897 | |
| 2898 | /* If here it can match either the tag or the content |
| 2899 | and it hasn't matched the content, so the end |
| 2900 | result is whether it matches the tag. This is |
| 2901 | also the case that the CBOR standard discourages. */ |
| 2902 | |
| 2903 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2904 | } |
| 2905 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2906 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2907 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 2908 | // This could be semi-private if need be |
| 2909 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2910 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 2911 | int64_t nLabel, |
| 2912 | TagSpecification TagSpec, |
| 2913 | QCBORItem *pItem) |
| 2914 | { |
| 2915 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 2916 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2917 | return; |
| 2918 | } |
| 2919 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2920 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2921 | } |
| 2922 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 2923 | |
| 2924 | // This could be semi-private if need be |
| 2925 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2926 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 2927 | const char *szLabel, |
| 2928 | TagSpecification TagSpec, |
| 2929 | QCBORItem *pItem) |
| 2930 | { |
| 2931 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 2932 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2933 | return; |
| 2934 | } |
| 2935 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2936 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2939 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2940 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 2941 | int64_t nLabel, |
| 2942 | TagSpecification TagSpec, |
| 2943 | UsefulBufC *pString) |
| 2944 | { |
| 2945 | QCBORItem Item; |
| 2946 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 2947 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2948 | *pString = Item.val.string; |
| 2949 | } |
| 2950 | } |
| 2951 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2952 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2953 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 2954 | const char * szLabel, |
| 2955 | TagSpecification TagSpec, |
| 2956 | UsefulBufC *pString) |
| 2957 | { |
| 2958 | QCBORItem Item; |
| 2959 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 2960 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2961 | *pString = Item.val.string; |
| 2962 | } |
| 2963 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2964 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2965 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2966 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2967 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2968 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2969 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2970 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 2971 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2972 | } |
| 2973 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2974 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2975 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2976 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2977 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 2978 | QCBORItem *pItemList, |
| 2979 | void *pCallbackCtx, |
| 2980 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2981 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2982 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 2983 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 2987 | /** |
| 2988 | * @brief Search for a map/array by label and enter it |
| 2989 | * |
| 2990 | * @param[in] pMe The decode context. |
| 2991 | * @param[in] pSearch The map/array to search for. |
| 2992 | * |
| 2993 | * @c pSearch is expected to contain one item of type map or array |
| 2994 | * with the label specified. The current bounded map will be searched for |
| 2995 | * this and if found will be entered. |
| 2996 | * |
| 2997 | * If the label is not found, or the item found is not a map or array, |
| 2998 | * the error state is set. |
| 2999 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3000 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3001 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3002 | // The first item in pSearch is the one that is to be |
| 3003 | // entered. It should be the only one filled in. Any other |
| 3004 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3005 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3006 | return; |
| 3007 | } |
| 3008 | |
| 3009 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3010 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3011 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3012 | return; |
| 3013 | } |
| 3014 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3015 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3016 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3017 | return; |
| 3018 | } |
| 3019 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3020 | /* |
| 3021 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3022 | * next item for the pre-order traversal cursor to be the map/array |
| 3023 | * found by MapSearch(). The next few lines of code force the |
| 3024 | * cursor to that. |
| 3025 | * |
| 3026 | * There is no need to retain the old cursor because |
| 3027 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3028 | * beginning of the map/array being entered. |
| 3029 | * |
| 3030 | * The cursor is forced by: 1) setting the input buffer position to |
| 3031 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3032 | * counter to the total in the map/array, 3) setting the nesting |
| 3033 | * level. Setting the map/array counter to the total is not |
| 3034 | * strictly correct, but this is OK because this cursor only needs |
| 3035 | * to be used to get one item and MapSearch() has already found it |
| 3036 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3037 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3038 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3039 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3040 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3041 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3042 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3043 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3044 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3048 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3049 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3050 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3051 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3052 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3053 | QCBORItem OneItemSeach[2]; |
| 3054 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3055 | OneItemSeach[0].label.int64 = nLabel; |
| 3056 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3057 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3058 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3059 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3060 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
| 3063 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3064 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3065 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3066 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3067 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3068 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3069 | QCBORItem OneItemSeach[2]; |
| 3070 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3071 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3072 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3073 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3074 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3075 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3078 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3079 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3080 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3081 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3082 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3083 | QCBORItem OneItemSeach[2]; |
| 3084 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3085 | OneItemSeach[0].label.int64 = nLabel; |
| 3086 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3087 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3088 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3089 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3092 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3093 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3094 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3095 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3096 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3097 | QCBORItem OneItemSeach[2]; |
| 3098 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3099 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3100 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3101 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3102 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3103 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3107 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3108 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3109 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3110 | QCBORError uErr; |
| 3111 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3112 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3113 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3114 | // Already in error state; do nothing. |
| 3115 | return; |
| 3116 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3117 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3118 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3119 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3120 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3121 | if(uErr != QCBOR_SUCCESS) { |
| 3122 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3123 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3124 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3125 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3126 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3127 | } |
| 3128 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3129 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3130 | |
| 3131 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3132 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3133 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3134 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3135 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3136 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3137 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3138 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3139 | // Special case to increment nesting level for zero-length maps |
| 3140 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3141 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3142 | } |
| 3143 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3144 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3145 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3146 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3147 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3148 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3149 | if(pItem != NULL) { |
| 3150 | *pItem = Item; |
| 3151 | } |
| 3152 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3153 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3154 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3155 | } |
| 3156 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3157 | |
| 3158 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3159 | This is the common work for exiting a level that is a bounded map, |
| 3160 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3161 | |
| 3162 | One chunk of work is to set up the pre-order traversal so it is at |
| 3163 | the item just after the bounded map, array or bstr that is being |
| 3164 | exited. This is somewhat complex. |
| 3165 | |
| 3166 | The other work is to level-up the bounded mode to next higest bounded |
| 3167 | mode or the top level if there isn't one. |
| 3168 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3169 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3170 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3171 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3172 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3173 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3174 | /* |
| 3175 | First the pre-order-traversal byte offset is positioned to the |
| 3176 | item just after the bounded mode item that was just consumed. |
| 3177 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3178 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3179 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3180 | /* |
| 3181 | Next, set the current nesting level to one above the bounded level |
| 3182 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3183 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3184 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3185 | makes sure pCurrentBounded is valid. |
| 3186 | */ |
| 3187 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3188 | |
| 3189 | /* |
| 3190 | This does the complex work of leveling up the pre-order traversal |
| 3191 | when the end of a map or array or another bounded level is |
| 3192 | reached. It may do nothing, or ascend all the way to the top |
| 3193 | level. |
| 3194 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 3195 | uErr = NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3196 | if(uErr != QCBOR_SUCCESS) { |
| 3197 | goto Done; |
| 3198 | } |
| 3199 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3200 | /* |
| 3201 | This makes the next highest bounded level the current bounded |
| 3202 | level. If there is no next highest level, then no bounded mode is |
| 3203 | in effect. |
| 3204 | */ |
| 3205 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3206 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3207 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3208 | |
| 3209 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3210 | return uErr; |
| 3211 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3212 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3213 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3214 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3215 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3216 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3217 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3218 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3219 | return; |
| 3220 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3221 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3222 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3223 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3224 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3225 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3226 | goto Done; |
| 3227 | } |
| 3228 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3229 | /* |
| 3230 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3231 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3232 | from previous map search, then do a dummy search. |
| 3233 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3234 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3235 | QCBORItem Dummy; |
| 3236 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3237 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3238 | if(uErr != QCBOR_SUCCESS) { |
| 3239 | goto Done; |
| 3240 | } |
| 3241 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3242 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3243 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3244 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3245 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3246 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3250 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3251 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3252 | const QCBORItem *pItem, |
| 3253 | uint8_t uTagRequirement, |
| 3254 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3255 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3256 | if(pBstr) { |
| 3257 | *pBstr = NULLUsefulBufC; |
| 3258 | } |
| 3259 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3260 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3261 | // Already in error state; do nothing. |
| 3262 | return pMe->uLastError; |
| 3263 | } |
| 3264 | |
| 3265 | QCBORError uError = QCBOR_SUCCESS; |
| 3266 | |
| 3267 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3268 | uError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3269 | goto Done;; |
| 3270 | } |
| 3271 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3272 | const TagSpecification TagSpec = |
| 3273 | { |
| 3274 | uTagRequirement, |
| 3275 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3276 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3277 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3278 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3279 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3280 | if(uError != QCBOR_SUCCESS) { |
| 3281 | goto Done; |
| 3282 | } |
| 3283 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3284 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3285 | // Reverse the decrement done by GetNext() for the bstr so the |
| 3286 | // increment in NestLevelAscender() called by ExitBoundedLevel() |
| 3287 | // will work right. |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3288 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3289 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3290 | |
| 3291 | if(pBstr) { |
| 3292 | *pBstr = pItem->val.string; |
| 3293 | } |
| 3294 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3295 | // This saves the current length of the UsefulInputBuf and then |
| 3296 | // narrows the UsefulInputBuf to start and length of the wrapped |
| 3297 | // CBOR that is being entered. |
| 3298 | // |
| 3299 | // This makes sure the length is less than |
| 3300 | // QCBOR_MAX_DECODE_INPUT_SIZE which is slighly less than |
| 3301 | // UINT32_MAX. The value UINT32_MAX is used as a special indicator |
| 3302 | // value. The checks against QCBOR_MAX_DECODE_INPUT_SIZE also make |
| 3303 | // the casts safe. uEndOfBstr will always be less than |
| 3304 | // uPreviousLength because of the way UsefulInputBuf works so there |
| 3305 | // is no need to check it. There is also a range check in the |
| 3306 | // seek. |
| 3307 | // |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3308 | // Most of these calls are simple inline accessors so this doesn't |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3309 | // amount to much code. |
| 3310 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 3311 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3312 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3313 | goto Done; |
| 3314 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3315 | const size_t uEndOfBstr = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3316 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOfBstr - pItem->val.string.len); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3317 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3318 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3319 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3320 | (uint32_t)uPreviousLength, |
| 3321 | (uint32_t)uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3322 | Done: |
| 3323 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3327 | /* |
| 3328 | Public function, see header qcbor/qcbor_decode.h file |
| 3329 | */ |
| 3330 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3331 | uint8_t uTagRequirement, |
| 3332 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3333 | { |
| 3334 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3335 | // Already in error state; do nothing. |
| 3336 | return; |
| 3337 | } |
| 3338 | |
| 3339 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3340 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3341 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3342 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3343 | return; |
| 3344 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3345 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3346 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3347 | &Item, |
| 3348 | uTagRequirement, |
| 3349 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3353 | /* |
| 3354 | Public function, see header qcbor/qcbor_decode.h file |
| 3355 | */ |
| 3356 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3357 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3358 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3359 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3360 | { |
| 3361 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3362 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3363 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3364 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3365 | &Item, |
| 3366 | uTagRequirement, |
| 3367 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3368 | } |
| 3369 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3370 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3371 | /* |
| 3372 | Public function, see header qcbor/qcbor_decode.h file |
| 3373 | */ |
| 3374 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3375 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3376 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3377 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3378 | { |
| 3379 | QCBORItem Item; |
| 3380 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3381 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3382 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3383 | &Item, |
| 3384 | uTagRequirement, |
| 3385 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3386 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3387 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3388 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3389 | /* |
| 3390 | Public function, see header qcbor/qcbor_decode.h file |
| 3391 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3392 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3393 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3394 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3395 | // Already in error state; do nothing. |
| 3396 | return; |
| 3397 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3398 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3399 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3400 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3401 | return; |
| 3402 | } |
| 3403 | |
| 3404 | /* |
| 3405 | Reset the length of the UsefulInputBuf to what it was before |
| 3406 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3407 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3408 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3409 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3410 | |
| 3411 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3412 | QCBORError uErr = ExitBoundedLevel(pMe, DecodeNesting_GetEndOfBstr(&(pMe->nesting))); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3413 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3414 | } |
| 3415 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3416 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3417 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3418 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3419 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3420 | |
Laurence Lundblade | 11a064e | 2020-05-07 13:13:42 -0700 | [diff] [blame] | 3421 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3422 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3423 | static QCBORError |
| 3424 | InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3425 | { |
| 3426 | switch(pItem->uDataType) { |
| 3427 | case QCBOR_TYPE_TRUE: |
| 3428 | *pBool = true; |
| 3429 | return QCBOR_SUCCESS; |
| 3430 | break; |
| 3431 | |
| 3432 | case QCBOR_TYPE_FALSE: |
| 3433 | *pBool = false; |
| 3434 | return QCBOR_SUCCESS; |
| 3435 | break; |
| 3436 | |
| 3437 | default: |
| 3438 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3439 | break; |
| 3440 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3441 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3442 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3443 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3444 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3445 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3446 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3447 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3448 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3449 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3450 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3451 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3452 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3453 | return; |
| 3454 | } |
| 3455 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3456 | QCBORError nError; |
| 3457 | QCBORItem Item; |
| 3458 | |
| 3459 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3460 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3461 | pMe->uLastError = (uint8_t)nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3462 | return; |
| 3463 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3464 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3465 | } |
| 3466 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3467 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3468 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3469 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3470 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3471 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3472 | { |
| 3473 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3474 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3475 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3476 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3479 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3480 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3481 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3482 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3483 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3484 | { |
| 3485 | QCBORItem Item; |
| 3486 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3487 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3488 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | |
| 3492 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3493 | |
| 3494 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3495 | QCBORItem *pItem, |
| 3496 | uint8_t uTagRequirement, |
| 3497 | int64_t *pnTime) |
| 3498 | { |
| 3499 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3500 | // Already in error state, do nothing |
| 3501 | return; |
| 3502 | } |
| 3503 | |
| 3504 | QCBORError uErr; |
| 3505 | |
| 3506 | const TagSpecification TagSpec = |
| 3507 | { |
| 3508 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3509 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3510 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3511 | }; |
| 3512 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3513 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3514 | if(uErr != QCBOR_SUCCESS) { |
| 3515 | goto Done; |
| 3516 | } |
| 3517 | |
| 3518 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3519 | uErr = DecodeDateEpoch(pItem); |
| 3520 | if(uErr != QCBOR_SUCCESS) { |
| 3521 | goto Done; |
| 3522 | } |
| 3523 | } |
| 3524 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3525 | // Save the tags in the last item's tags in the decode context |
| 3526 | // for QCBORDecode_GetNthTagOfLast() |
| 3527 | CopyTags(pMe, pItem); |
| 3528 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3529 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3530 | |
| 3531 | Done: |
| 3532 | pMe->uLastError = (uint8_t)uErr; |
| 3533 | } |
| 3534 | |
| 3535 | |
| 3536 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3537 | uint8_t uTagRequirement, |
| 3538 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3539 | { |
| 3540 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3541 | // Already in error state, do nothing |
| 3542 | return; |
| 3543 | } |
| 3544 | |
| 3545 | QCBORItem Item; |
| 3546 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3547 | |
| 3548 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3549 | } |
| 3550 | |
| 3551 | |
| 3552 | void |
| 3553 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3554 | int64_t nLabel, |
| 3555 | uint8_t uTagRequirement, |
| 3556 | int64_t *pnTime) |
| 3557 | { |
| 3558 | QCBORItem Item; |
| 3559 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3560 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3561 | } |
| 3562 | |
| 3563 | |
| 3564 | void |
| 3565 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3566 | const char *szLabel, |
| 3567 | uint8_t uTagRequirement, |
| 3568 | int64_t *pnTime) |
| 3569 | { |
| 3570 | QCBORItem Item; |
| 3571 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3572 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3573 | } |
| 3574 | |
| 3575 | |
| 3576 | |
| 3577 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3578 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3579 | TagSpecification TagSpec, |
| 3580 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3581 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3582 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3583 | // Already in error state, do nothing |
| 3584 | return; |
| 3585 | } |
| 3586 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3587 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3588 | QCBORItem Item; |
| 3589 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3590 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3591 | if(uError != QCBOR_SUCCESS) { |
| 3592 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3593 | return; |
| 3594 | } |
| 3595 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3596 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3597 | |
| 3598 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3599 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 3600 | } else { |
| 3601 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3602 | } |
| 3603 | } |
| 3604 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3605 | |
| 3606 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3607 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3608 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3609 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3610 | UsefulBufC *pValue, |
| 3611 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3612 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3613 | const TagSpecification TagSpec = |
| 3614 | { |
| 3615 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3616 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3617 | {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] | 3618 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3619 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3620 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3621 | if(uErr != QCBOR_SUCCESS) { |
| 3622 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3623 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3624 | |
| 3625 | *pValue = pItem->val.string; |
| 3626 | |
| 3627 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3628 | *pbIsNegative = false; |
| 3629 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3630 | *pbIsNegative = true; |
| 3631 | } |
| 3632 | |
| 3633 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3637 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3638 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3639 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3640 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3641 | uint8_t uTagRequirement, |
| 3642 | UsefulBufC *pValue, |
| 3643 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3644 | { |
| 3645 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3646 | // Already in error state, do nothing |
| 3647 | return; |
| 3648 | } |
| 3649 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3650 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3651 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3652 | if(uError != QCBOR_SUCCESS) { |
| 3653 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3654 | return; |
| 3655 | } |
| 3656 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3657 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3658 | } |
| 3659 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3660 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3661 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3662 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3663 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3664 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3665 | int64_t nLabel, |
| 3666 | uint8_t uTagRequirement, |
| 3667 | UsefulBufC *pValue, |
| 3668 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3669 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3670 | QCBORItem Item; |
| 3671 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3672 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3673 | return; |
| 3674 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3675 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3676 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3679 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3680 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3681 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3682 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3683 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3684 | const char *szLabel, |
| 3685 | uint8_t uTagRequirement, |
| 3686 | UsefulBufC *pValue, |
| 3687 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3688 | { |
| 3689 | QCBORItem Item; |
| 3690 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3691 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3692 | return; |
| 3693 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3694 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3695 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | |
| 3699 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3700 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3701 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3702 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3703 | const QCBORItem *pItem, |
| 3704 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3705 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3706 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3707 | const TagSpecification TagSpecText = |
| 3708 | { |
| 3709 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3710 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3711 | {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] | 3712 | }; |
| 3713 | const TagSpecification TagSpecBinary = |
| 3714 | { |
| 3715 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3716 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3717 | {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] | 3718 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3719 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3720 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3721 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3722 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3723 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3724 | if(pbIsTag257 != NULL) { |
| 3725 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3726 | } |
| 3727 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3728 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3729 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3730 | if(pbIsTag257 != NULL) { |
| 3731 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3732 | } |
| 3733 | uReturn = QCBOR_SUCCESS; |
| 3734 | |
| 3735 | } else { |
| 3736 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3737 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3738 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3739 | return uReturn; |
| 3740 | } |
| 3741 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3742 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 3743 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3744 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3745 | |
| 3746 | |
| 3747 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3748 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3749 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3750 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3751 | |
| 3752 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3753 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3754 | static QCBORError |
| 3755 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3756 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3757 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3758 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3759 | if(uResult != 0) { |
| 3760 | /* This loop will run a maximum of 19 times because |
| 3761 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 3762 | * exit with the overflow error |
| 3763 | */ |
| 3764 | for(; nExponent > 0; nExponent--) { |
| 3765 | if(uResult > UINT64_MAX / 10) { |
| 3766 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 3767 | } |
| 3768 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3769 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3770 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3771 | for(; nExponent < 0; nExponent++) { |
| 3772 | uResult = uResult / 10; |
| 3773 | if(uResult == 0) { |
| 3774 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3775 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3776 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3777 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3778 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3779 | |
| 3780 | *puResult = uResult; |
| 3781 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3782 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3786 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3787 | static QCBORError |
| 3788 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3789 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3790 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3791 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3792 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3793 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3794 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3795 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3796 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3797 | */ |
| 3798 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3799 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3800 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3801 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3802 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3803 | nExponent--; |
| 3804 | } |
| 3805 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3806 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3807 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3808 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3809 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3810 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3811 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3814 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3815 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3816 | return QCBOR_SUCCESS; |
| 3817 | } |
| 3818 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3819 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3820 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3821 | Compute value with signed mantissa and signed result. Works with |
| 3822 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3823 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3824 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 3825 | int64_t nExponent, |
| 3826 | int64_t *pnResult, |
| 3827 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3828 | { |
| 3829 | uint64_t uResult; |
| 3830 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3831 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3832 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3833 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 3834 | |
| 3835 | // Do the exponentiation of the positive mantissa |
| 3836 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 3837 | if(uReturn) { |
| 3838 | return uReturn; |
| 3839 | } |
| 3840 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3841 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 3842 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 3843 | of INT64_MIN. This assumes two's compliment representation where |
| 3844 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 3845 | Trying to write -INT64_MIN doesn't work to get this because the |
| 3846 | compiler tries to work with an int64_t which can't represent |
| 3847 | -INT64_MIN. |
| 3848 | */ |
| 3849 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 3850 | |
| 3851 | // Error out if too large |
| 3852 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3853 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 3854 | } |
| 3855 | |
| 3856 | // Casts are safe because of checks above |
| 3857 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 3858 | |
| 3859 | return QCBOR_SUCCESS; |
| 3860 | } |
| 3861 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3862 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3863 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3864 | Compute value with signed mantissa and unsigned result. Works with |
| 3865 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3866 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3867 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 3868 | int64_t nExponent, |
| 3869 | uint64_t *puResult, |
| 3870 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3871 | { |
| 3872 | if(nMantissa < 0) { |
| 3873 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 3874 | } |
| 3875 | |
| 3876 | // Cast to unsigned is OK because of check for negative |
| 3877 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 3878 | // Exponentiation is straight forward |
| 3879 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 3880 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3881 | |
| 3882 | |
| 3883 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3884 | Compute value with signed mantissa and unsigned result. Works with |
| 3885 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3886 | */ |
| 3887 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 3888 | int64_t nExponent, |
| 3889 | uint64_t *puResult, |
| 3890 | fExponentiator pfExp) |
| 3891 | { |
| 3892 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 3893 | } |
| 3894 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3895 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 3896 | |
| 3897 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3898 | |
| 3899 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3900 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3901 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3902 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3903 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3904 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3905 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3906 | const uint8_t *pByte = BigNum.ptr; |
| 3907 | size_t uLen = BigNum.len; |
| 3908 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 3909 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3910 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3911 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 3912 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3913 | } |
| 3914 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3915 | *pResult = uResult; |
| 3916 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3919 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 3920 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3921 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3922 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3923 | } |
| 3924 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3925 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3926 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3927 | { |
| 3928 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3929 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 3930 | if(uError) { |
| 3931 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3932 | } |
| 3933 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 3934 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3935 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3939 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3940 | { |
| 3941 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3942 | /* The negative integer furthest from zero for a C int64_t is |
| 3943 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 3944 | negative number in CBOR is computed as -n - 1 where n is the |
| 3945 | encoded integer, where n is what is in the variable BigNum. When |
| 3946 | converting BigNum to a uint64_t, the maximum value is thus |
| 3947 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 3948 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3949 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3950 | -n - 1 <= INT64_MIN. |
| 3951 | -n - 1 <= -INT64_MAX - 1 |
| 3952 | n <= INT64_MAX. |
| 3953 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3954 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3955 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3956 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3957 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3958 | |
| 3959 | /// Now apply -n - 1. The cast is safe because |
| 3960 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 3961 | // is the largest positive integer that an int64_t can |
| 3962 | // represent. */ |
| 3963 | *pnResult = -(int64_t)uResult - 1; |
| 3964 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3965 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3966 | } |
| 3967 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3968 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3969 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3970 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3971 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3972 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3973 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3974 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3975 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3976 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3977 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 3978 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3979 | |
| 3980 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 3981 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3982 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 3983 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3984 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3985 | static QCBORError |
| 3986 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3987 | { |
| 3988 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3989 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3990 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3991 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3992 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3993 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 3994 | http://www.cplusplus.com/reference/cmath/llround/ |
| 3995 | */ |
| 3996 | // Not interested in FE_INEXACT |
| 3997 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3998 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 3999 | *pnValue = llround(pItem->val.dfnum); |
| 4000 | } else { |
| 4001 | *pnValue = lroundf(pItem->val.fnum); |
| 4002 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4003 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4004 | // llround() shouldn't result in divide by zero, but catch |
| 4005 | // it here in case it unexpectedly does. Don't try to |
| 4006 | // distinguish between the various exceptions because it seems |
| 4007 | // they vary by CPU, compiler and OS. |
| 4008 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4009 | } |
| 4010 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4011 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4012 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4013 | #else |
| 4014 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4015 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4016 | break; |
| 4017 | |
| 4018 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4019 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4020 | *pnValue = pItem->val.int64; |
| 4021 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4022 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4023 | } |
| 4024 | break; |
| 4025 | |
| 4026 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4027 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4028 | if(pItem->val.uint64 < INT64_MAX) { |
| 4029 | *pnValue = pItem->val.int64; |
| 4030 | } else { |
| 4031 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4032 | } |
| 4033 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4034 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4035 | } |
| 4036 | break; |
| 4037 | |
| 4038 | default: |
| 4039 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4040 | } |
| 4041 | return QCBOR_SUCCESS; |
| 4042 | } |
| 4043 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4044 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4045 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4046 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4047 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4048 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4049 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4050 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4051 | return; |
| 4052 | } |
| 4053 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4054 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4055 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4056 | if(uError) { |
| 4057 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4058 | return; |
| 4059 | } |
| 4060 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4061 | if(pItem) { |
| 4062 | *pItem = Item; |
| 4063 | } |
| 4064 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4065 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4066 | } |
| 4067 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4068 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4069 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4070 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4071 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4072 | int64_t *pnValue, |
| 4073 | QCBORItem *pItem) |
| 4074 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4075 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4076 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4077 | return; |
| 4078 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4079 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4080 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4083 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4084 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4085 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4086 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4087 | int64_t *pnValue, |
| 4088 | QCBORItem *pItem) |
| 4089 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4090 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4091 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4092 | return; |
| 4093 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4094 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4095 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4099 | /* |
| 4100 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4101 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4102 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4103 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4104 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4105 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4106 | |
| 4107 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4108 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4109 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4110 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4111 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4112 | static QCBORError |
| 4113 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4114 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4115 | switch(pItem->uDataType) { |
| 4116 | |
| 4117 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4118 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4119 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4120 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4121 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4122 | } |
| 4123 | break; |
| 4124 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4125 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4126 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4127 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4128 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4129 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4130 | } |
| 4131 | break; |
| 4132 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4133 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4134 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4135 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4136 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4137 | pItem->val.expAndMantissa.nExponent, |
| 4138 | pnValue, |
| 4139 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4140 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4141 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4142 | } |
| 4143 | break; |
| 4144 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4145 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4146 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4147 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4148 | pItem->val.expAndMantissa.nExponent, |
| 4149 | pnValue, |
| 4150 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4151 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4152 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4153 | } |
| 4154 | break; |
| 4155 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4156 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4157 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4158 | int64_t nMantissa; |
| 4159 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4160 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4161 | if(uErr) { |
| 4162 | return uErr; |
| 4163 | } |
| 4164 | return ExponentiateNN(nMantissa, |
| 4165 | pItem->val.expAndMantissa.nExponent, |
| 4166 | pnValue, |
| 4167 | Exponentitate10); |
| 4168 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4169 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4170 | } |
| 4171 | break; |
| 4172 | |
| 4173 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4174 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4175 | int64_t nMantissa; |
| 4176 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4177 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4178 | if(uErr) { |
| 4179 | return uErr; |
| 4180 | } |
| 4181 | return ExponentiateNN(nMantissa, |
| 4182 | pItem->val.expAndMantissa.nExponent, |
| 4183 | pnValue, |
| 4184 | Exponentitate10); |
| 4185 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4186 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4187 | } |
| 4188 | break; |
| 4189 | |
| 4190 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4191 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4192 | int64_t nMantissa; |
| 4193 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4194 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4195 | if(uErr) { |
| 4196 | return uErr; |
| 4197 | } |
| 4198 | return ExponentiateNN(nMantissa, |
| 4199 | pItem->val.expAndMantissa.nExponent, |
| 4200 | pnValue, |
| 4201 | Exponentitate2); |
| 4202 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4203 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4204 | } |
| 4205 | break; |
| 4206 | |
| 4207 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4208 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4209 | int64_t nMantissa; |
| 4210 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4211 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4212 | if(uErr) { |
| 4213 | return uErr; |
| 4214 | } |
| 4215 | return ExponentiateNN(nMantissa, |
| 4216 | pItem->val.expAndMantissa.nExponent, |
| 4217 | pnValue, |
| 4218 | Exponentitate2); |
| 4219 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4220 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4221 | } |
| 4222 | break; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4223 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4224 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4225 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4226 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4227 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4228 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4229 | |
| 4230 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4231 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4232 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4233 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4234 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4235 | { |
| 4236 | QCBORItem Item; |
| 4237 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4238 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4239 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4240 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4241 | // The above conversion succeeded |
| 4242 | return; |
| 4243 | } |
| 4244 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4245 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4246 | // 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] | 4247 | return; |
| 4248 | } |
| 4249 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4250 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4251 | } |
| 4252 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4253 | |
| 4254 | /* |
| 4255 | Public function, see header qcbor/qcbor_decode.h file |
| 4256 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4257 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4258 | int64_t nLabel, |
| 4259 | uint32_t uConvertTypes, |
| 4260 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4261 | { |
| 4262 | QCBORItem Item; |
| 4263 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4264 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4265 | nLabel, |
| 4266 | uConvertTypes, |
| 4267 | pnValue, |
| 4268 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4269 | |
| 4270 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4271 | // The above conversion succeeded |
| 4272 | return; |
| 4273 | } |
| 4274 | |
| 4275 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4276 | // The above conversion failed in a way that code below can't correct |
| 4277 | return; |
| 4278 | } |
| 4279 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4280 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4284 | /* |
| 4285 | Public function, see header qcbor/qcbor_decode.h file |
| 4286 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4287 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4288 | const char *szLabel, |
| 4289 | uint32_t uConvertTypes, |
| 4290 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4291 | { |
| 4292 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4293 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4294 | szLabel, |
| 4295 | uConvertTypes, |
| 4296 | pnValue, |
| 4297 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4298 | |
| 4299 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4300 | // The above conversion succeeded |
| 4301 | return; |
| 4302 | } |
| 4303 | |
| 4304 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4305 | // The above conversion failed in a way that code below can't correct |
| 4306 | return; |
| 4307 | } |
| 4308 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4309 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4313 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4314 | { |
| 4315 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4316 | case QCBOR_TYPE_DOUBLE: |
| 4317 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4318 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4319 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4320 | // Can't use llround here because it will not convert values |
| 4321 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4322 | // need to be converted so it is more complicated. |
| 4323 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4324 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4325 | if(isnan(pItem->val.dfnum)) { |
| 4326 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4327 | } else if(pItem->val.dfnum < 0) { |
| 4328 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4329 | } else { |
| 4330 | double dRounded = round(pItem->val.dfnum); |
| 4331 | // See discussion in DecodeDateEpoch() for |
| 4332 | // explanation of - 0x7ff |
| 4333 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4334 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4335 | } |
| 4336 | *puValue = (uint64_t)dRounded; |
| 4337 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4338 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4339 | if(isnan(pItem->val.fnum)) { |
| 4340 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4341 | } else if(pItem->val.fnum < 0) { |
| 4342 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4343 | } else { |
| 4344 | float fRounded = roundf(pItem->val.fnum); |
| 4345 | // See discussion in DecodeDateEpoch() for |
| 4346 | // explanation of - 0x7ff |
| 4347 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4348 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4349 | } |
| 4350 | *puValue = (uint64_t)fRounded; |
| 4351 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4352 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4353 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4354 | // round() and roundf() shouldn't result in exceptions here, but |
| 4355 | // catch them to be robust and thorough. Don't try to |
| 4356 | // distinguish between the various exceptions because it seems |
| 4357 | // they vary by CPU, compiler and OS. |
| 4358 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4359 | } |
| 4360 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4361 | } else { |
| 4362 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4363 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4364 | #else |
| 4365 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4366 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4367 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4368 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4369 | case QCBOR_TYPE_INT64: |
| 4370 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4371 | if(pItem->val.int64 >= 0) { |
| 4372 | *puValue = (uint64_t)pItem->val.int64; |
| 4373 | } else { |
| 4374 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4375 | } |
| 4376 | } else { |
| 4377 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4378 | } |
| 4379 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4380 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4381 | case QCBOR_TYPE_UINT64: |
| 4382 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4383 | *puValue = pItem->val.uint64; |
| 4384 | } else { |
| 4385 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4386 | } |
| 4387 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4388 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4389 | default: |
| 4390 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4391 | } |
| 4392 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4393 | return QCBOR_SUCCESS; |
| 4394 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4395 | |
| 4396 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4397 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4398 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4399 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4400 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4401 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4402 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4403 | return; |
| 4404 | } |
| 4405 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4406 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4407 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4408 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4409 | if(uError) { |
| 4410 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4411 | return; |
| 4412 | } |
| 4413 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4414 | if(pItem) { |
| 4415 | *pItem = Item; |
| 4416 | } |
| 4417 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4418 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4419 | } |
| 4420 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4421 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4422 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4423 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4424 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4425 | uint64_t *puValue, |
| 4426 | QCBORItem *pItem) |
| 4427 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4428 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4429 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4430 | return; |
| 4431 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4432 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4433 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4437 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4438 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4439 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4440 | uint64_t *puValue, |
| 4441 | QCBORItem *pItem) |
| 4442 | { |
| 4443 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4444 | return; |
| 4445 | } |
| 4446 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4447 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4448 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4449 | return; |
| 4450 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4451 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4452 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4453 | } |
| 4454 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4455 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4456 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4457 | static QCBORError |
| 4458 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4459 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4460 | switch(pItem->uDataType) { |
| 4461 | |
| 4462 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4463 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4464 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4465 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4466 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4467 | } |
| 4468 | break; |
| 4469 | |
| 4470 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4471 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4472 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4473 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4474 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4475 | } |
| 4476 | break; |
| 4477 | |
| 4478 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4479 | |
| 4480 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4481 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4482 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4483 | pItem->val.expAndMantissa.nExponent, |
| 4484 | puValue, |
| 4485 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4486 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4487 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4488 | } |
| 4489 | break; |
| 4490 | |
| 4491 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4492 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4493 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4494 | pItem->val.expAndMantissa.nExponent, |
| 4495 | puValue, |
| 4496 | Exponentitate2); |
| 4497 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4498 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4499 | } |
| 4500 | break; |
| 4501 | |
| 4502 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4503 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4504 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4505 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4506 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4507 | if(uErr != QCBOR_SUCCESS) { |
| 4508 | return uErr; |
| 4509 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4510 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4511 | pItem->val.expAndMantissa.nExponent, |
| 4512 | puValue, |
| 4513 | Exponentitate10); |
| 4514 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4515 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4516 | } |
| 4517 | break; |
| 4518 | |
| 4519 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4520 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4521 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4522 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4523 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4524 | } |
| 4525 | break; |
| 4526 | |
| 4527 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4528 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4529 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4530 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4531 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4532 | if(uErr != QCBOR_SUCCESS) { |
| 4533 | return uErr; |
| 4534 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4535 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4536 | pItem->val.expAndMantissa.nExponent, |
| 4537 | puValue, |
| 4538 | Exponentitate2); |
| 4539 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4540 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4541 | } |
| 4542 | break; |
| 4543 | |
| 4544 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4545 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4546 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4547 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4548 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4549 | } |
| 4550 | break; |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 4551 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4552 | default: |
| 4553 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4554 | } |
| 4555 | } |
| 4556 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4557 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4558 | /* |
| 4559 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4560 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4561 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4562 | { |
| 4563 | QCBORItem Item; |
| 4564 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4565 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4566 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4567 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4568 | // The above conversion succeeded |
| 4569 | return; |
| 4570 | } |
| 4571 | |
| 4572 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4573 | // 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] | 4574 | return; |
| 4575 | } |
| 4576 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4577 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4578 | } |
| 4579 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4580 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4581 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4582 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4583 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4584 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4585 | int64_t nLabel, |
| 4586 | uint32_t uConvertTypes, |
| 4587 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4588 | { |
| 4589 | QCBORItem Item; |
| 4590 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4591 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 4592 | nLabel, |
| 4593 | uConvertTypes, |
| 4594 | puValue, |
| 4595 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4596 | |
| 4597 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4598 | // The above conversion succeeded |
| 4599 | return; |
| 4600 | } |
| 4601 | |
| 4602 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4603 | // The above conversion failed in a way that code below can't correct |
| 4604 | return; |
| 4605 | } |
| 4606 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4607 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | |
| 4611 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4612 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4613 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4614 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4615 | const char *szLabel, |
| 4616 | uint32_t uConvertTypes, |
| 4617 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4618 | { |
| 4619 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4620 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 4621 | szLabel, |
| 4622 | uConvertTypes, |
| 4623 | puValue, |
| 4624 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4625 | |
| 4626 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4627 | // The above conversion succeeded |
| 4628 | return; |
| 4629 | } |
| 4630 | |
| 4631 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4632 | // The above conversion failed in a way that code below can't correct |
| 4633 | return; |
| 4634 | } |
| 4635 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4636 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4640 | |
| 4641 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4642 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4643 | uint32_t uConvertTypes, |
| 4644 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4645 | { |
| 4646 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4647 | case QCBOR_TYPE_FLOAT: |
| 4648 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4649 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4650 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4651 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4652 | *pdValue = (double)pItem->val.fnum; |
| 4653 | } else { |
| 4654 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4655 | } |
| 4656 | } |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 4657 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4658 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | 1ddc1f2 | 2020-12-06 05:29:05 -0800 | [diff] [blame] | 4659 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4660 | break; |
| 4661 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4662 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4663 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4664 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4665 | *pdValue = pItem->val.dfnum; |
| 4666 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4667 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4668 | } |
| 4669 | } |
| 4670 | break; |
| 4671 | |
| 4672 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4673 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4674 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4675 | // A simple cast seems to do the job with no worry of exceptions. |
| 4676 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4677 | *pdValue = (double)pItem->val.int64; |
| 4678 | |
| 4679 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4680 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4681 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4682 | #else |
| 4683 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4684 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4685 | break; |
| 4686 | |
| 4687 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4688 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4689 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4690 | // A simple cast seems to do the job with no worry of exceptions. |
| 4691 | // There will be precision loss for some values. |
| 4692 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4693 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4694 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4695 | } |
| 4696 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4697 | #else |
| 4698 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4699 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4700 | |
| 4701 | default: |
| 4702 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4703 | } |
| 4704 | |
| 4705 | return QCBOR_SUCCESS; |
| 4706 | } |
| 4707 | |
| 4708 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4709 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4710 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4711 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4712 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4713 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4714 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4715 | return; |
| 4716 | } |
| 4717 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4718 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4719 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4720 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4721 | if(uError) { |
| 4722 | pMe->uLastError = (uint8_t)uError; |
| 4723 | return; |
| 4724 | } |
| 4725 | |
| 4726 | if(pItem) { |
| 4727 | *pItem = Item; |
| 4728 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4729 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4730 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4731 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4732 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4733 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4734 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4735 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4736 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4737 | double *pdValue, |
| 4738 | QCBORItem *pItem) |
| 4739 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4740 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4741 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4742 | return; |
| 4743 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4744 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4745 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4748 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4749 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4750 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4751 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4752 | double *pdValue, |
| 4753 | QCBORItem *pItem) |
| 4754 | { |
| 4755 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4756 | return; |
| 4757 | } |
| 4758 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4759 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4760 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4761 | return; |
| 4762 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4763 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4764 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4768 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4769 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 4770 | { |
| 4771 | double dResult; |
| 4772 | |
| 4773 | dResult = 0.0; |
| 4774 | const uint8_t *pByte = BigNum.ptr; |
| 4775 | size_t uLen = BigNum.len; |
| 4776 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 4777 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4778 | while(uLen--) { |
| 4779 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 4780 | } |
| 4781 | |
| 4782 | return dResult; |
| 4783 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4784 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4785 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4786 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4787 | static QCBORError |
| 4788 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4789 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4790 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4791 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4792 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 4793 | |
| 4794 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4795 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4796 | |
| 4797 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4798 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4799 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4800 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4801 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4802 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 4803 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4804 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4805 | } |
| 4806 | break; |
| 4807 | |
| 4808 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4809 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4810 | // Underflow gives 0, overflow gives infinity |
| 4811 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4812 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4813 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4814 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4815 | } |
| 4816 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4817 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4818 | |
| 4819 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4820 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4821 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 4822 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4823 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4824 | } |
| 4825 | break; |
| 4826 | |
| 4827 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4828 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4829 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4830 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4831 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4832 | } |
| 4833 | break; |
| 4834 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4835 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4836 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4837 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4838 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4839 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4840 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4841 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4842 | } |
| 4843 | break; |
| 4844 | |
| 4845 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4846 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4847 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4848 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4849 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4850 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4851 | } |
| 4852 | break; |
| 4853 | |
| 4854 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4855 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4856 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4857 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4858 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4859 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4860 | } |
| 4861 | break; |
| 4862 | |
| 4863 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4864 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4865 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4866 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4867 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4868 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4869 | } |
| 4870 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4871 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4872 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4873 | default: |
| 4874 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4875 | } |
| 4876 | |
| 4877 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4878 | |
| 4879 | #else |
| 4880 | (void)pItem; |
| 4881 | (void)uConvertTypes; |
| 4882 | (void)pdValue; |
| 4883 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4884 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4885 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | |
| 4889 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4890 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4891 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4892 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 4893 | uint32_t uConvertTypes, |
| 4894 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4895 | { |
| 4896 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4897 | QCBORItem Item; |
| 4898 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4899 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4900 | |
| 4901 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4902 | // The above conversion succeeded |
| 4903 | return; |
| 4904 | } |
| 4905 | |
| 4906 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4907 | // The above conversion failed in a way that code below can't correct |
| 4908 | return; |
| 4909 | } |
| 4910 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4911 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4912 | } |
| 4913 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4914 | |
| 4915 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4916 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4917 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4918 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4919 | int64_t nLabel, |
| 4920 | uint32_t uConvertTypes, |
| 4921 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4922 | { |
| 4923 | QCBORItem Item; |
| 4924 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4925 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4926 | |
| 4927 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4928 | // The above conversion succeeded |
| 4929 | return; |
| 4930 | } |
| 4931 | |
| 4932 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4933 | // The above conversion failed in a way that code below can't correct |
| 4934 | return; |
| 4935 | } |
| 4936 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4937 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | |
| 4941 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4942 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4943 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4944 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4945 | const char *szLabel, |
| 4946 | uint32_t uConvertTypes, |
| 4947 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4948 | { |
| 4949 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4950 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4951 | |
| 4952 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4953 | // The above conversion succeeded |
| 4954 | return; |
| 4955 | } |
| 4956 | |
| 4957 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4958 | // The above conversion failed in a way that code below can't correct |
| 4959 | return; |
| 4960 | } |
| 4961 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4962 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4963 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4964 | |
| 4965 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4966 | |
| 4967 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4968 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4969 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 4970 | { |
| 4971 | while((uInt & 0xff00000000000000UL) == 0) { |
| 4972 | uInt = uInt << 8; |
| 4973 | }; |
| 4974 | |
| 4975 | UsefulOutBuf UOB; |
| 4976 | |
| 4977 | UsefulOutBuf_Init(&UOB, Buffer); |
| 4978 | |
| 4979 | while(uInt) { |
| 4980 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 4981 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 4982 | uInt = uInt << 8; |
| 4983 | (void)xx; |
| 4984 | } |
| 4985 | |
| 4986 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4987 | } |
| 4988 | |
| 4989 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4990 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 4991 | TagSpecification TagSpec, |
| 4992 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4993 | { |
| 4994 | QCBORError uErr; |
| 4995 | // 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] | 4996 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4997 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4998 | if(uErr != QCBOR_SUCCESS) { |
| 4999 | goto Done; |
| 5000 | } |
| 5001 | |
| 5002 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 5003 | break; // Successful exit. Moving on to finish decoding. |
| 5004 | } |
| 5005 | |
| 5006 | // The item is an array, which means an undecoded |
| 5007 | // mantissa and exponent, so decode it. It will then |
| 5008 | // have a different type and exit the loop if. |
| 5009 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5010 | if(uErr != QCBOR_SUCCESS) { |
| 5011 | goto Done; |
| 5012 | } |
| 5013 | |
| 5014 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5015 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5016 | } |
| 5017 | Done: |
| 5018 | return uErr; |
| 5019 | } |
| 5020 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5021 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5022 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5023 | TagSpecification TagSpec, |
| 5024 | QCBORItem *pItem, |
| 5025 | int64_t *pnMantissa, |
| 5026 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5027 | { |
| 5028 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5029 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5030 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5031 | if(uErr != QCBOR_SUCCESS) { |
| 5032 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5033 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5034 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5035 | switch (pItem->uDataType) { |
| 5036 | |
| 5037 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5038 | case QCBOR_TYPE_BIGFLOAT: |
| 5039 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 5040 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5041 | break; |
| 5042 | |
| 5043 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5044 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5045 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5046 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5047 | break; |
| 5048 | |
| 5049 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5050 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5051 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5052 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5053 | break; |
| 5054 | |
| 5055 | default: |
| 5056 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5057 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5058 | |
| 5059 | Done: |
| 5060 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5061 | } |
| 5062 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5063 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5064 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5065 | TagSpecification TagSpec, |
| 5066 | QCBORItem *pItem, |
| 5067 | UsefulBuf BufferForMantissa, |
| 5068 | UsefulBufC *pMantissa, |
| 5069 | bool *pbIsNegative, |
| 5070 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5071 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5072 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5073 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5074 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5075 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5076 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | uint64_t uMantissa; |
| 5080 | |
| 5081 | switch (pItem->uDataType) { |
| 5082 | |
| 5083 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5084 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5085 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5086 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5087 | *pbIsNegative = false; |
| 5088 | } else { |
| 5089 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5090 | *pbIsNegative = true; |
| 5091 | } |
| 5092 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5093 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5094 | break; |
| 5095 | |
| 5096 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5097 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5098 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5099 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5100 | *pbIsNegative = false; |
| 5101 | break; |
| 5102 | |
| 5103 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5104 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5105 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5106 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5107 | *pbIsNegative = true; |
| 5108 | break; |
| 5109 | |
| 5110 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5111 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5112 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5113 | |
| 5114 | Done: |
| 5115 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5119 | /* |
| 5120 | Public function, see header qcbor/qcbor_decode.h file |
| 5121 | */ |
| 5122 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5123 | uint8_t uTagRequirement, |
| 5124 | int64_t *pnMantissa, |
| 5125 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5126 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5127 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5128 | return; |
| 5129 | } |
| 5130 | |
| 5131 | QCBORItem Item; |
| 5132 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5133 | if(uError) { |
| 5134 | pMe->uLastError = (uint8_t)uError; |
| 5135 | return; |
| 5136 | } |
| 5137 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5138 | const TagSpecification TagSpec = |
| 5139 | { |
| 5140 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5141 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5142 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5143 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5144 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5145 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5146 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5147 | } |
| 5148 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5149 | |
| 5150 | /* |
| 5151 | Public function, see header qcbor/qcbor_decode.h file |
| 5152 | */ |
| 5153 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5154 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5155 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5156 | int64_t *pnMantissa, |
| 5157 | int64_t *pnExponent) |
| 5158 | { |
| 5159 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5160 | return; |
| 5161 | } |
| 5162 | |
| 5163 | QCBORItem Item; |
| 5164 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5165 | |
| 5166 | const TagSpecification TagSpec = |
| 5167 | { |
| 5168 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5169 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5170 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5171 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5172 | }; |
| 5173 | |
| 5174 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5175 | } |
| 5176 | |
| 5177 | |
| 5178 | /* |
| 5179 | Public function, see header qcbor/qcbor_decode.h file |
| 5180 | */ |
| 5181 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5182 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5183 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5184 | int64_t *pnMantissa, |
| 5185 | int64_t *pnExponent) |
| 5186 | { |
| 5187 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5188 | return; |
| 5189 | } |
| 5190 | |
| 5191 | QCBORItem Item; |
| 5192 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5193 | |
| 5194 | const TagSpecification TagSpec = |
| 5195 | { |
| 5196 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5197 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5198 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5199 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5200 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5201 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5202 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5203 | } |
| 5204 | |
| 5205 | |
| 5206 | /* |
| 5207 | Public function, see header qcbor/qcbor_decode.h file |
| 5208 | */ |
| 5209 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5210 | uint8_t uTagRequirement, |
| 5211 | UsefulBuf MantissaBuffer, |
| 5212 | UsefulBufC *pMantissa, |
| 5213 | bool *pbMantissaIsNegative, |
| 5214 | int64_t *pnExponent) |
| 5215 | { |
| 5216 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5217 | return; |
| 5218 | } |
| 5219 | |
| 5220 | QCBORItem Item; |
| 5221 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5222 | if(uError) { |
| 5223 | pMe->uLastError = (uint8_t)uError; |
| 5224 | return; |
| 5225 | } |
| 5226 | |
| 5227 | const TagSpecification TagSpec = |
| 5228 | { |
| 5229 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5230 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5231 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5232 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5233 | }; |
| 5234 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5235 | ProcessMantissaAndExponentBig(pMe, |
| 5236 | TagSpec, |
| 5237 | &Item, |
| 5238 | MantissaBuffer, |
| 5239 | pMantissa, |
| 5240 | pbMantissaIsNegative, |
| 5241 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | |
| 5245 | /* |
| 5246 | Public function, see header qcbor/qcbor_decode.h file |
| 5247 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5248 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5249 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5250 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5251 | UsefulBuf BufferForMantissa, |
| 5252 | UsefulBufC *pMantissa, |
| 5253 | bool *pbIsNegative, |
| 5254 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5255 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5256 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5257 | return; |
| 5258 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5259 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5260 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5261 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5262 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5263 | return; |
| 5264 | } |
| 5265 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5266 | const TagSpecification TagSpec = |
| 5267 | { |
| 5268 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5269 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5270 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5271 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5272 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5273 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5274 | ProcessMantissaAndExponentBig(pMe, |
| 5275 | TagSpec, |
| 5276 | &Item, |
| 5277 | BufferForMantissa, |
| 5278 | pMantissa, |
| 5279 | pbIsNegative, |
| 5280 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5281 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5282 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5283 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5284 | /* |
| 5285 | Public function, see header qcbor/qcbor_decode.h file |
| 5286 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5287 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5288 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5289 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5290 | UsefulBuf BufferForMantissa, |
| 5291 | UsefulBufC *pMantissa, |
| 5292 | bool *pbIsNegative, |
| 5293 | int64_t *pnExponent) |
| 5294 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5295 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5296 | return; |
| 5297 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5298 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5299 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5300 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5301 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5302 | return; |
| 5303 | } |
| 5304 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5305 | const TagSpecification TagSpec = |
| 5306 | { |
| 5307 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5308 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5309 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5310 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5311 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5312 | |
| 5313 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5314 | } |
| 5315 | |
| 5316 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5317 | /* |
| 5318 | Public function, see header qcbor/qcbor_decode.h file |
| 5319 | */ |
| 5320 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5321 | uint8_t uTagRequirement, |
| 5322 | int64_t *pnMantissa, |
| 5323 | int64_t *pnExponent) |
| 5324 | { |
| 5325 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5326 | return; |
| 5327 | } |
| 5328 | |
| 5329 | QCBORItem Item; |
| 5330 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5331 | if(uError) { |
| 5332 | pMe->uLastError = (uint8_t)uError; |
| 5333 | return; |
| 5334 | } |
| 5335 | const TagSpecification TagSpec = |
| 5336 | { |
| 5337 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5338 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5339 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5340 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5341 | }; |
| 5342 | |
| 5343 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5344 | } |
| 5345 | |
| 5346 | |
| 5347 | /* |
| 5348 | Public function, see header qcbor/qcbor_decode.h file |
| 5349 | */ |
| 5350 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5351 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5352 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5353 | int64_t *pnMantissa, |
| 5354 | int64_t *pnExponent) |
| 5355 | { |
| 5356 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5357 | return; |
| 5358 | } |
| 5359 | |
| 5360 | QCBORItem Item; |
| 5361 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5362 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5363 | return; |
| 5364 | } |
| 5365 | |
| 5366 | const TagSpecification TagSpec = |
| 5367 | { |
| 5368 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5369 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5370 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5371 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5372 | }; |
| 5373 | |
| 5374 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5375 | } |
| 5376 | |
| 5377 | |
| 5378 | /* |
| 5379 | Public function, see header qcbor/qcbor_decode.h file |
| 5380 | */ |
| 5381 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5382 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5383 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5384 | int64_t *pnMantissa, |
| 5385 | int64_t *pnExponent) |
| 5386 | { |
| 5387 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5388 | return; |
| 5389 | } |
| 5390 | |
| 5391 | QCBORItem Item; |
| 5392 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5393 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5394 | return; |
| 5395 | } |
| 5396 | |
| 5397 | const TagSpecification TagSpec = |
| 5398 | { |
| 5399 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5400 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5401 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5402 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5403 | }; |
| 5404 | |
| 5405 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5406 | } |
| 5407 | |
| 5408 | |
| 5409 | /* |
| 5410 | Public function, see header qcbor/qcbor_decode.h file |
| 5411 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5412 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5413 | uint8_t uTagRequirement, |
| 5414 | UsefulBuf MantissaBuffer, |
| 5415 | UsefulBufC *pMantissa, |
| 5416 | bool *pbMantissaIsNegative, |
| 5417 | int64_t *pnExponent) |
| 5418 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5419 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5420 | return; |
| 5421 | } |
| 5422 | |
| 5423 | QCBORItem Item; |
| 5424 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5425 | if(uError) { |
| 5426 | pMe->uLastError = (uint8_t)uError; |
| 5427 | return; |
| 5428 | } |
| 5429 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5430 | const TagSpecification TagSpec = |
| 5431 | { |
| 5432 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5433 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5434 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5435 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5436 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5437 | |
| 5438 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5439 | } |
| 5440 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5441 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5442 | /* |
| 5443 | Public function, see header qcbor/qcbor_decode.h file |
| 5444 | */ |
| 5445 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5446 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5447 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5448 | UsefulBuf BufferForMantissa, |
| 5449 | UsefulBufC *pMantissa, |
| 5450 | bool *pbIsNegative, |
| 5451 | int64_t *pnExponent) |
| 5452 | { |
| 5453 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5454 | return; |
| 5455 | } |
| 5456 | |
| 5457 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5458 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5459 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5460 | return; |
| 5461 | } |
| 5462 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5463 | const TagSpecification TagSpec = |
| 5464 | { |
| 5465 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5466 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5467 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5468 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5469 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5470 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5471 | ProcessMantissaAndExponentBig(pMe, |
| 5472 | TagSpec, |
| 5473 | &Item, |
| 5474 | BufferForMantissa, |
| 5475 | pMantissa, |
| 5476 | pbIsNegative, |
| 5477 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5481 | /* |
| 5482 | Public function, see header qcbor/qcbor_decode.h file |
| 5483 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5484 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5485 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5486 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5487 | UsefulBuf BufferForMantissa, |
| 5488 | UsefulBufC *pMantissa, |
| 5489 | bool *pbIsNegative, |
| 5490 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5491 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5492 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5493 | return; |
| 5494 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5495 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5496 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5497 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5498 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5499 | return; |
| 5500 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5501 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5502 | const TagSpecification TagSpec = |
| 5503 | { |
| 5504 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5505 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5506 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5507 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5508 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5509 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5510 | ProcessMantissaAndExponentBig(pMe, |
| 5511 | TagSpec, |
| 5512 | &Item, |
| 5513 | BufferForMantissa, |
| 5514 | pMantissa, |
| 5515 | pbIsNegative, |
| 5516 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5517 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5518 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5519 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |