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 | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 42 | #endif |
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 | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 54 | inline static 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 | |
| 63 | inline static bool |
| 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 | |
| 76 | inline static bool |
| 77 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 78 | { |
| 79 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | if(pMe->val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
| 84 | return false; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 90 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 91 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 92 | ===========================================================================*/ |
| 93 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 94 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 95 | See commecnts about and typedef of QCBORDecodeNesting in qcbor_private.h, |
| 96 | the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 97 | */ |
| 98 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 99 | |
| 100 | inline static uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 101 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 102 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 103 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 104 | /* |
| 105 | Limit in DecodeNesting_Descend against more than |
| 106 | QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
| 107 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 108 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 111 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 112 | inline static uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 113 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 114 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 115 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 116 | /* |
| 117 | Limit in DecodeNesting_Descend against more than |
| 118 | QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
| 119 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 120 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 123 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 124 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 125 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 126 | { |
| 127 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 128 | } |
| 129 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 130 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 131 | static inline bool |
| 132 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 133 | { |
| 134 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 135 | return true; |
| 136 | } else { |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 142 | inline static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 143 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 144 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 145 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } else { |
| 148 | return false; |
| 149 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 152 | |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 153 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 154 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 155 | { |
| 156 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 157 | // Not a map or array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 158 | return false; |
| 159 | } |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 160 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 161 | // Is indefinite |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 164 | // All checks passed; is a definte length map or array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 168 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 169 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 170 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 171 | { |
| 172 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 173 | // is a byte string |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 179 | |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 180 | inline static bool DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 181 | { |
| 182 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 183 | return true; |
| 184 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 185 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 191 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 192 | inline static void DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 193 | { |
| 194 | // Should be only called on maps and arrays |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 195 | /* |
| 196 | DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 197 | larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 198 | uin32_t so the cast is safe. |
| 199 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 200 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 201 | |
| 202 | if(bIsEmpty) { |
| 203 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 204 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 207 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 208 | inline static void DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 209 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 210 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 213 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 214 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 215 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 216 | { |
| 217 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 218 | // No bounded map or array set up |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 222 | // Not a map or array; end of those is by byte count |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 223 | return false; |
| 224 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 225 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
| 226 | // In a traveral at a level deeper than the bounded level |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 227 | return false; |
| 228 | } |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 229 | // Works for both definite and indefinite length maps/arrays |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 230 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 231 | // Count is not zero, still unconsumed item |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 234 | // All checks passed, got to the end of an array or map |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 235 | return true; |
| 236 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 237 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 238 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 239 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 240 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 241 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 242 | // Must only be called on map / array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 243 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 244 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 245 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 246 | return false; |
| 247 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 250 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 251 | inline static bool |
| 252 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 253 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 254 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 255 | return true; |
| 256 | } else { |
| 257 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 258 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 261 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 262 | inline static bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 263 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 264 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 265 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 268 | |
| 269 | if(pNesting->pCurrentBounded->uLevelType != uType) { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 276 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 277 | inline static void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 278 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 279 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 280 | // Only call on a defnite length array / map |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 281 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 282 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 283 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 284 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 285 | inline static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 286 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 287 | { |
| 288 | // Only call on a defnite length array / map |
| 289 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | inline static void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 294 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 295 | { |
| 296 | pNesting->pCurrent--; |
| 297 | } |
| 298 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 299 | |
| 300 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 301 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 302 | { |
| 303 | // Error out if nesting is too deep |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 304 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 305 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // The actual descend |
| 309 | pNesting->pCurrent++; |
| 310 | |
| 311 | pNesting->pCurrent->uLevelType = uType; |
| 312 | |
| 313 | return QCBOR_SUCCESS; |
| 314 | } |
| 315 | |
| 316 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 317 | inline static QCBORError |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 318 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 319 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 320 | /* |
| 321 | Should only be called on map/array. |
| 322 | |
| 323 | Have descended into this before this is called. The job here is |
| 324 | just to mark it in bounded mode. |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 325 | |
| 326 | Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
| 327 | uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 328 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 329 | if(uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 330 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 333 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 334 | |
| 335 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 336 | |
| 337 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 340 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 341 | inline static QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 342 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 343 | uint8_t uQCBORType, |
| 344 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 345 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 346 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 347 | |
| 348 | if(uCount == 0) { |
| 349 | // Nothing to do for empty definite lenth arrays. They are just are |
| 350 | // effectively the same as an item that is not a map or array |
| 351 | goto Done; |
| 352 | // Empty indefinite length maps and arrays are handled elsewhere |
| 353 | } |
| 354 | |
| 355 | // Error out if arrays is too long to handle |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 356 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 357 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 358 | uError = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 359 | goto Done; |
| 360 | } |
| 361 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 362 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 363 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 364 | goto Done; |
| 365 | } |
| 366 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 367 | // Fill in the new map/array level. Check above makes casts OK. |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 368 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 369 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 370 | |
| 371 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 372 | |
| 373 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 374 | return uError;; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | static inline void |
| 379 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 380 | { |
| 381 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | static inline void |
| 386 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 387 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 388 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 389 | pNesting->pCurrentBounded--; |
| 390 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 391 | break; |
| 392 | } |
| 393 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 396 | static inline void |
| 397 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 398 | { |
| 399 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 400 | } |
| 401 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 402 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 403 | inline static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 404 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 405 | uint32_t uEndOffset, |
| 406 | uint32_t uEndOfBstr) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 407 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 408 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 409 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 410 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 411 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 412 | goto Done; |
| 413 | } |
| 414 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 415 | // Fill in the new byte string level |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 416 | pNesting->pCurrent->u.bs.uPreviousEndOffset = uEndOffset; |
| 417 | pNesting->pCurrent->u.bs.uEndOfBstr = 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 | // Bstr wrapped levels are always bounded |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 420 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 421 | |
| 422 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 423 | return uError;; |
| 424 | } |
| 425 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 426 | |
| 427 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 428 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 429 | { |
| 430 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 433 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 434 | inline static void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 435 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 436 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 437 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 438 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 439 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 443 | inline static void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 444 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 445 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 446 | *pSave = *pNesting; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 447 | pNesting->pCurrent = pNesting->pCurrentBounded; |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 448 | pNesting->pCurrent->u.ma.uCountCursor = pNesting->pCurrent->u.ma.uCountTotal; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 451 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 452 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 453 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 454 | { |
| 455 | *pNesting = *pSave; |
| 456 | } |
| 457 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 458 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 459 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 460 | DecodeNesting_GetEndOfBstr(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 461 | { |
| 462 | return pMe->pCurrentBounded->u.bs.uEndOfBstr; |
| 463 | } |
| 464 | |
| 465 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 466 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 467 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 468 | { |
| 469 | return pMe->pCurrentBounded->u.bs.uPreviousEndOffset; |
| 470 | } |
| 471 | |
| 472 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 473 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 474 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 475 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 476 | |
| 477 | The following four functions are pretty wrappers for invocation of |
| 478 | the string allocator supplied by the caller. |
| 479 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 480 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 481 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 482 | static inline void |
| 483 | StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 484 | { |
| 485 | (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0); |
| 486 | } |
| 487 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 488 | // StringAllocator_Reallocate called with pMem NULL is |
| 489 | // equal to StringAllocator_Allocate() |
| 490 | static inline UsefulBuf |
| 491 | StringAllocator_Reallocate(const QCORInternalAllocator *pMe, |
| 492 | void *pMem, |
| 493 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 494 | { |
| 495 | return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize); |
| 496 | } |
| 497 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 498 | static inline UsefulBuf |
| 499 | StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 500 | { |
| 501 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 502 | } |
| 503 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 504 | static inline void |
| 505 | StringAllocator_Destruct(const QCORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 506 | { |
| 507 | if(pMe->pfAllocator) { |
| 508 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 514 | /*=========================================================================== |
| 515 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 516 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 517 | See qcbor/qcbor_decode.h for definition of the object |
| 518 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 519 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 520 | /* |
| 521 | Public function, see header file |
| 522 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 523 | void QCBORDecode_Init(QCBORDecodeContext *me, |
| 524 | UsefulBufC EncodedCBOR, |
| 525 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 526 | { |
| 527 | memset(me, 0, sizeof(QCBORDecodeContext)); |
| 528 | UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 529 | // Don't bother with error check on decode mode. If a bad value is |
| 530 | // 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] | 531 | me->uDecodeMode = (uint8_t)nDecodeMode; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 532 | DecodeNesting_Init(&(me->nesting)); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 533 | for(int i = 0; i < QCBOR_NUM_MAPPED_TAGS; i++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 534 | me->auMappedTags[i] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 535 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
| 539 | /* |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 540 | Public function, see header file |
| 541 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 542 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 543 | QCBORStringAllocate pfAllocateFunction, |
| 544 | void *pAllocateContext, |
| 545 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 546 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 547 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 548 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 549 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 552 | |
| 553 | /* |
| 554 | Public function, see header file |
| 555 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 556 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 557 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 558 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 559 | // This does nothing now. It is retained for backwards compatibility |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 560 | (void)pMe; |
| 561 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 562 | } |
| 563 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 564 | |
| 565 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 566 | This decodes the fundamental part of a CBOR data item, the type and |
| 567 | number |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 568 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 569 | This is the counterpart to QCBOREncode_EncodeHead(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 570 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 571 | This does the network->host byte order conversion. The conversion |
| 572 | here also results in the conversion for floats in addition to that |
| 573 | for lengths, tags and integer values. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 574 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 575 | This returns: |
| 576 | pnMajorType -- the major type for the item |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 577 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 578 | puArgument -- the "number" which is used a the value for integers, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 579 | tags and floats and length for strings and arrays |
| 580 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 581 | pnAdditionalInfo -- Pass this along to know what kind of float or |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 582 | if length is indefinite |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 583 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 584 | The int type is preferred to uint8_t for some variables as this |
| 585 | avoids integer promotions, can reduce code size and makes |
| 586 | static analyzers happier. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 587 | |
| 588 | @retval QCBOR_ERR_UNSUPPORTED |
| 589 | |
| 590 | @retval QCBOR_ERR_HIT_END |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 591 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 592 | inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, |
| 593 | int *pnMajorType, |
| 594 | uint64_t *puArgument, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 595 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 596 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 597 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 598 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 599 | // Get the initial byte that every CBOR data item has |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 600 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 601 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 602 | // Break down the initial byte |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 603 | const int nTmpMajorType = nInitialByte >> 5; |
| 604 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 605 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 606 | // Where the number or argument accumulates |
| 607 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 608 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 609 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 610 | // Need to get 1,2,4 or 8 additional argument bytes. Map |
| 611 | // LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 612 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 613 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 614 | // Loop getting all the bytes in the argument |
| 615 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 616 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 617 | // This shift and add gives the endian conversion |
| 618 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 619 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 620 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 621 | // The reserved and thus-far unused additional info values |
| 622 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 623 | goto Done; |
| 624 | } else { |
| 625 | // Less than 24, additional info is argument or 31, an indefinite length |
| 626 | // No more bytes to get |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 627 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 628 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 629 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 630 | if(UsefulInputBuf_GetError(pUInBuf)) { |
| 631 | nReturn = QCBOR_ERR_HIT_END; |
| 632 | goto Done; |
| 633 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 634 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 635 | // All successful if we got here. |
| 636 | nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 637 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 638 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 639 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 640 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 641 | Done: |
| 642 | return nReturn; |
| 643 | } |
| 644 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 645 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 646 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 647 | CBOR doesn't explicitly specify two's compliment for integers but all |
| 648 | CPUs use it these days and the test vectors in the RFC are so. All |
| 649 | integers in the CBOR structure are positive and the major type |
| 650 | indicates positive or negative. CBOR can express positive integers |
| 651 | up to 2^x - 1 where x is the number of bits and negative integers |
| 652 | down to 2^x. Note that negative numbers can be one more away from |
| 653 | zero than positive. Stdint, as far as I can tell, uses two's |
| 654 | compliment to represent negative integers. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 655 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 656 | See http://www.unix.org/whitepapers/64bit.html for reasons int is |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 657 | used carefully here, and in particular why it isn't used in the interface. |
| 658 | Also see |
| 659 | https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 660 | |
| 661 | Int is used for values that need less than 16-bits and would be subject |
| 662 | to integer promotion and complaining by static analyzers. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 663 | |
| 664 | @retval QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 665 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 666 | inline static QCBORError |
| 667 | DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 668 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 669 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 670 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 671 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
| 672 | if (uNumber <= INT64_MAX) { |
| 673 | pDecodedItem->val.int64 = (int64_t)uNumber; |
| 674 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 675 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 676 | } else { |
| 677 | pDecodedItem->val.uint64 = uNumber; |
| 678 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 679 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 680 | } |
| 681 | } else { |
| 682 | if(uNumber <= INT64_MAX) { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 683 | // CBOR's representation of negative numbers lines up with the |
| 684 | // two-compliment representation. A negative integer has one |
| 685 | // more in range than a positive integer. INT64_MIN is |
| 686 | // equal to (-INT64_MAX) - 1. |
| 687 | pDecodedItem->val.int64 = (-(int64_t)uNumber) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 688 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 689 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 690 | } else { |
| 691 | // C can't represent a negative integer in this range |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 692 | // so it is an error. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 693 | nReturn = QCBOR_ERR_INT_OVERFLOW; |
| 694 | } |
| 695 | } |
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 | return nReturn; |
| 698 | } |
| 699 | |
| 700 | // Make sure #define value line up as DecodeSimple counts on this. |
| 701 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 702 | #error QCBOR_TYPE_FALSE macro value wrong |
| 703 | #endif |
| 704 | |
| 705 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 706 | #error QCBOR_TYPE_TRUE macro value wrong |
| 707 | #endif |
| 708 | |
| 709 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 710 | #error QCBOR_TYPE_NULL macro value wrong |
| 711 | #endif |
| 712 | |
| 713 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 714 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 715 | #endif |
| 716 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 717 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 718 | #error QCBOR_TYPE_BREAK macro value wrong |
| 719 | #endif |
| 720 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 721 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 722 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 723 | #endif |
| 724 | |
| 725 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 726 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 727 | #endif |
| 728 | |
| 729 | /* |
| 730 | Decode true, false, floats, break... |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 731 | |
| 732 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 733 | |
| 734 | @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 735 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 736 | inline static QCBORError |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 737 | DecodeSimple(int nAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 738 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 739 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 740 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 741 | // uAdditionalInfo is 5 bits from the initial byte. Compile time checks |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 742 | // above make sure uAdditionalInfo values line up with uDataType values. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 743 | // DecodeTypeAndNumber() never returns an AdditionalInfo > 0x1f so cast |
| 744 | // is safe |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 745 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 746 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 747 | switch(nAdditionalInfo) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 748 | // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are |
| 749 | // caught before this is called. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 750 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 751 | case HALF_PREC_FLOAT: // 25 |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 752 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 753 | // Half-precision is returned as a double. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 754 | // The cast to uint16_t is safe because the encoded value |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 755 | // 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] | 756 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber); |
| 757 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 758 | #else |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 759 | nReturn = QCBOR_ERR_HALF_PRECISION_DISABLED; |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 760 | #endif |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 761 | break; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 762 | case SINGLE_PREC_FLOAT: // 26 |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 763 | // Single precision is normally returned as a double |
| 764 | // since double is widely supported, there is no loss of |
| 765 | // precision, it makes it easy for the caller in |
| 766 | // most cases and it can be converted back to single |
| 767 | // with no loss of precision |
| 768 | // |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 769 | // The cast to uint32_t is safe because the encoded value |
Laurence Lundblade | 8fa7d5d | 2020-07-11 16:30:47 -0700 | [diff] [blame] | 770 | // 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] | 771 | { |
| 772 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber); |
| 773 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 774 | // In the normal case, use HW to convert float to double. |
| 775 | pDecodedItem->val.dfnum = (double)f; |
| 776 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 777 | #else |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 778 | // Use of float HW is disabled, return as a float. |
| 779 | pDecodedItem->val.fnum = f; |
| 780 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 781 | |
| 782 | // IEEE754_FloatToDouble() could be used here to return |
| 783 | // as a double, but it adds object code and most likely |
| 784 | // anyone disabling FLOAT HW use doesn't care about |
| 785 | // floats and wants to save object code. |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 786 | #endif |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 787 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 788 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 789 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 790 | case DOUBLE_PREC_FLOAT: // 27 |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 791 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 792 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 793 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 794 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 795 | case CBOR_SIMPLEV_FALSE: // 20 |
| 796 | case CBOR_SIMPLEV_TRUE: // 21 |
| 797 | case CBOR_SIMPLEV_NULL: // 22 |
| 798 | case CBOR_SIMPLEV_UNDEF: // 23 |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 799 | case CBOR_SIMPLE_BREAK: // 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 800 | break; // nothing to do |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 801 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 802 | case CBOR_SIMPLEV_ONEBYTE: // 24 |
| 803 | if(uNumber <= CBOR_SIMPLE_BREAK) { |
| 804 | // 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] | 805 | nReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 806 | goto Done; |
| 807 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 808 | /* FALLTHROUGH */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 809 | // fall through intentionally |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 810 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 811 | default: // 0-19 |
| 812 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 813 | /* |
| 814 | DecodeTypeAndNumber will make uNumber equal to |
| 815 | uAdditionalInfo when uAdditionalInfo is < 24 This cast is |
| 816 | safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 817 | the double/float cases above |
| 818 | */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 819 | pDecodedItem->val.uSimple = (uint8_t)uNumber; |
| 820 | break; |
| 821 | } |
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 | Done: |
| 824 | return nReturn; |
| 825 | } |
| 826 | |
| 827 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 828 | /* |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 829 | Decode text and byte strings. Call the string allocator if asked to. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 830 | |
| 831 | @retval QCBOR_ERR_HIT_END |
| 832 | |
| 833 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 834 | |
| 835 | @retval QCBOR_ERR_STRING_TOO_LONG |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 836 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 837 | inline static QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator, |
| 838 | int nMajorType, |
| 839 | uint64_t uStrLen, |
| 840 | UsefulInputBuf *pUInBuf, |
| 841 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 842 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 843 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 844 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 845 | // CBOR lengths can be 64 bits, but size_t is not 64 bits on all CPUs. |
| 846 | // This check makes the casts to size_t below safe. |
| 847 | |
| 848 | // 4 bytes less than the largest sizeof() so this can be tested by |
| 849 | // putting a SIZE_MAX length in the CBOR test input (no one will |
| 850 | // care the limit on strings is 4 bytes shorter). |
| 851 | if(uStrLen > SIZE_MAX-4) { |
| 852 | nReturn = QCBOR_ERR_STRING_TOO_LONG; |
| 853 | goto Done; |
| 854 | } |
| 855 | |
| 856 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 857 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 858 | // Failed to get the bytes for this string item |
| 859 | nReturn = QCBOR_ERR_HIT_END; |
| 860 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 861 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 862 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 863 | if(pAllocator) { |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 864 | // We are asked to use string allocator to make a copy |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 865 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 866 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 867 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 868 | goto Done; |
| 869 | } |
| 870 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 871 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 872 | } else { |
| 873 | // Normal case with no string allocator |
| 874 | pDecodedItem->val.string = Bytes; |
| 875 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 876 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 877 | // Cast because ternary operator causes promotion to integer |
| 878 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 879 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 880 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 881 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 882 | return nReturn; |
| 883 | } |
| 884 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 885 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 886 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 887 | |
| 888 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 889 | |
| 890 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 891 | // Make sure the constants align as this is assumed by |
| 892 | // the GetAnItem() implementation |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 893 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 894 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 895 | #endif |
| 896 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 897 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 898 | #endif |
| 899 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 900 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 901 | This gets a single data item and decodes it including preceding |
| 902 | optional tagging. This does not deal with arrays and maps and nesting |
| 903 | except to decode the data item introducing them. Arrays and maps are |
| 904 | handled at the next level up in GetNext(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 905 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 906 | Errors detected here include: an array that is too long to decode, |
| 907 | hit end of buffer unexpectedly, a few forms of invalid encoded CBOR |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 908 | |
| 909 | @retval QCBOR_ERR_UNSUPPORTED |
| 910 | |
| 911 | @retval QCBOR_ERR_HIT_END |
| 912 | |
| 913 | @retval QCBOR_ERR_INT_OVERFLOW |
| 914 | |
| 915 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 916 | |
| 917 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 918 | |
| 919 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 920 | |
| 921 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 922 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 923 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 924 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, |
| 925 | QCBORItem *pDecodedItem, |
| 926 | const QCORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 927 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 928 | QCBORError nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 929 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 930 | /* |
| 931 | Get the major type and the number. Number could be length of more |
| 932 | bytes or the value depending on the major type nAdditionalInfo is |
| 933 | an encoding of the length of the uNumber and is needed to decode |
| 934 | floats and doubles |
| 935 | */ |
Rob Gilton | 47cc956 | 2020-08-10 12:03:38 +0100 | [diff] [blame] | 936 | int nMajorType = 0; |
| 937 | uint64_t uNumber = 0; |
| 938 | int nAdditionalInfo = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 939 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 940 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 941 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 942 | nReturn = DecodeTypeAndNumber(pUInBuf, &nMajorType, &uNumber, &nAdditionalInfo); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 943 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 944 | // Error out here if we got into trouble on the type and number. The |
| 945 | // 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] | 946 | if(nReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 947 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 948 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 949 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 950 | // At this point the major type and the value are valid. We've got |
| 951 | // the type and the number that starts every CBOR data item. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 952 | switch (nMajorType) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 953 | case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0 |
| 954 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 955 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 956 | nReturn = QCBOR_ERR_BAD_INT; |
| 957 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 958 | nReturn = DecodeInteger(nMajorType, uNumber, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 959 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 960 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 961 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 962 | case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2 |
| 963 | case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 964 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 965 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
| 966 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 967 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 968 | pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 969 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 970 | nReturn = DecodeBytes(pAllocator, nMajorType, uNumber, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 971 | } |
| 972 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 973 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 974 | case CBOR_MAJOR_TYPE_ARRAY: // Major type 4 |
| 975 | case CBOR_MAJOR_TYPE_MAP: // Major type 5 |
| 976 | // Record the number of items in the array or map |
| 977 | if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 978 | nReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 979 | goto Done; |
| 980 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 981 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 982 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 983 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 984 | // type conversion OK because of check above |
| 985 | pDecodedItem->val.uCount = (uint16_t)uNumber; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 986 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 987 | // C preproc #if above makes sure constants for major types align |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 988 | // DecodeTypeAndNumber never returns a major type > 7 so cast is safe |
| 989 | pDecodedItem->uDataType = (uint8_t)nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 990 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 991 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 992 | case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 993 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 994 | nReturn = QCBOR_ERR_BAD_INT; |
| 995 | } else { |
| 996 | pDecodedItem->val.uTagV = uNumber; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 997 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 998 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 999 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1000 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1001 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1002 | // Major type 7, float, double, true, false, null... |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1003 | nReturn = DecodeSimple(nAdditionalInfo, uNumber, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1004 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1005 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1006 | default: |
| 1007 | // Never happens because DecodeTypeAndNumber() should never return > 7 |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1008 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 1009 | break; |
| 1010 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1011 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1012 | Done: |
| 1013 | return nReturn; |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | /* |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1019 | This layer deals with indefinite length strings. It pulls all the |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1020 | individual chunk items together into one QCBORItem using the string |
| 1021 | allocator. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1022 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1023 | Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1024 | |
| 1025 | @retval QCBOR_ERR_UNSUPPORTED |
| 1026 | |
| 1027 | @retval QCBOR_ERR_HIT_END |
| 1028 | |
| 1029 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1030 | |
| 1031 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1032 | |
| 1033 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1034 | |
| 1035 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1036 | |
| 1037 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1038 | |
| 1039 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1040 | |
| 1041 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1042 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1043 | static inline QCBORError |
| 1044 | GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1045 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1046 | // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96 |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | // Get pointer to string allocator. First use is to pass it to |
| 1049 | // GetNext_Item() when option is set to allocate for *every* string. |
| 1050 | // Second use here is to allocate space to coallese indefinite |
| 1051 | // length string items into one. |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1052 | const QCORInternalAllocator *pAllocator = me->StringAllocator.pfAllocator ? |
| 1053 | &(me->StringAllocator) : |
| 1054 | NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1056 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1057 | nReturn = GetNext_Item(&(me->InBuf), |
| 1058 | pDecodedItem, |
| 1059 | me->bStringAllocateAll ? pAllocator: NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1060 | if(nReturn) { |
| 1061 | goto Done; |
| 1062 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1063 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1064 | // To reduce code size by removing support for indefinite length strings, the |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1065 | // code in this function from here down can be eliminated. Run tests, except |
| 1066 | // indefinite length string tests, to be sure all is OK if this is removed. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1067 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1068 | // Only do indefinite length processing on strings |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1069 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1070 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1071 | goto Done; // no need to do any work here on non-string types |
| 1072 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1073 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1074 | // Is this a string with an indefinite length? |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1075 | if(pDecodedItem->val.string.len != SIZE_MAX) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1076 | goto Done; // length is not indefinite, so no work to do here |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1077 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1078 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1079 | // Can't do indefinite length strings without a string allocator |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1080 | if(pAllocator == NULL) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1081 | nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1082 | goto Done; |
| 1083 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1084 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1085 | // Loop getting chunks of the indefinite length string |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1086 | UsefulBufC FullString = NULLUsefulBufC; |
| 1087 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1088 | for(;;) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1089 | // Get item for next chunk |
| 1090 | QCBORItem StringChunkItem; |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1091 | // NULL string allocator passed here. Do not need to allocate |
| 1092 | // chunks even if bStringAllocateAll is set. |
Laurence Lundblade | fae26bf | 2019-02-18 11:15:43 -0800 | [diff] [blame] | 1093 | nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1094 | if(nReturn) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1095 | break; // Error getting the next chunk |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1096 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1097 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1098 | // See if it is a marker at end of indefinite length string |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1099 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1100 | // String is complete |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1101 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1102 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1103 | break; |
| 1104 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1105 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1106 | // Match data type of chunk to type at beginning. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1107 | // Also catches error of other non-string types that don't belong. |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1108 | // Also catches indefinite length strings inside indefinite length strings |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1109 | if(StringChunkItem.uDataType != uStringType || |
| 1110 | StringChunkItem.val.string.len == SIZE_MAX) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1111 | nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1112 | break; |
| 1113 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1114 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1115 | // Alloc new buffer or expand previously allocated buffer so it can fit |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1116 | // The first time throurgh FullString.ptr is NULL and this is |
| 1117 | // equivalent to StringAllocator_Allocate() |
| 1118 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1119 | UNCONST_POINTER(FullString.ptr), |
| 1120 | FullString.len + StringChunkItem.val.string.len); |
| 1121 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1122 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1123 | // Allocation of memory for the string failed |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1124 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1125 | break; |
| 1126 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1127 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1128 | // Copy new string chunk at the end of string so far. |
| 1129 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1130 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1131 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1132 | if(nReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1133 | // Getting the item failed, clean up the allocated memory |
| 1134 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1135 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1136 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1137 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1138 | return nReturn; |
| 1139 | } |
| 1140 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1141 | static uint64_t ConvertTag(const QCBORDecodeContext *me, uint16_t uTagVal) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1142 | if(uTagVal <= QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1143 | return uTagVal; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1144 | } else if(uTagVal == CBOR_TAG_INVALID16) { |
| 1145 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1146 | } else { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1147 | const int x = uTagVal - (QCBOR_LAST_UNMAPPED_TAG + 1); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1148 | return me->auMappedTags[x]; |
| 1149 | } |
| 1150 | } |
| 1151 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1152 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1153 | /* |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1154 | Gets all optional tag data items preceding a data item that is not an |
| 1155 | optional tag and records them as bits in the tag map. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1156 | |
| 1157 | @retval QCBOR_ERR_UNSUPPORTED |
| 1158 | |
| 1159 | @retval QCBOR_ERR_HIT_END |
| 1160 | |
| 1161 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1162 | |
| 1163 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1164 | |
| 1165 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1166 | |
| 1167 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1168 | |
| 1169 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1170 | |
| 1171 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1172 | |
| 1173 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1174 | |
| 1175 | @retval QCBOR_ERR_TOO_MANY_TAGS |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1176 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1177 | static QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1178 | GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1179 | { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1180 | uint16_t auTags[QCBOR_MAX_TAGS_PER_ITEM] = {CBOR_TAG_INVALID16, |
| 1181 | CBOR_TAG_INVALID16, |
| 1182 | CBOR_TAG_INVALID16, |
| 1183 | CBOR_TAG_INVALID16}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1184 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1185 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1186 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1187 | // Loop fetching items until the item fetched is not a tag |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1188 | for(;;) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1189 | QCBORError uErr = GetNext_FullItem(me, pDecodedItem); |
| 1190 | if(uErr != QCBOR_SUCCESS) { |
| 1191 | uReturn = uErr; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1192 | goto Done; // Error out of the loop |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1193 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1194 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1195 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1196 | // Successful exit from loop; maybe got some tags, maybe not |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1197 | memcpy(pDecodedItem->uTags, auTags, sizeof(auTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1200 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1201 | if(auTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1202 | // No room in the tag list |
| 1203 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1204 | // Continue on to get all tags on this item even though |
| 1205 | // it is erroring out in the end. This is a resource limit |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 1206 | // error, not a problem with being well-formed CBOR. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1207 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1208 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1209 | // Slide tags over one in the array to make room at index 0 |
| 1210 | for(size_t uTagIndex = QCBOR_MAX_TAGS_PER_ITEM - 1; uTagIndex > 0; uTagIndex--) { |
| 1211 | auTags[uTagIndex] = auTags[uTagIndex-1]; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1212 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1213 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1214 | // Is the tag > 16 bits? |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1215 | if(pDecodedItem->val.uTagV > QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1216 | size_t uTagMapIndex; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1217 | // 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] | 1218 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1219 | if(me->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1220 | break; |
| 1221 | } |
| 1222 | if(me->auMappedTags[uTagMapIndex] == pDecodedItem->val.uTagV) { |
| 1223 | break; |
| 1224 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1225 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1226 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1227 | // No room for the tag |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1228 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1229 | // Continue on to get all tags on this item even though |
| 1230 | // it is erroring out in the end. This is a resource limit |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 1231 | // error, not a problem with being well-formed CBOR. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1232 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1235 | // 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] | 1236 | me->auMappedTags[uTagMapIndex] = pDecodedItem->val.uTagV; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1237 | auTags[0] = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1238 | |
| 1239 | } else { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1240 | auTags[0] = (uint16_t)pDecodedItem->val.uTagV; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1243 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1244 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1245 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1250 | This layer takes care of map entries. It combines the label and data |
| 1251 | items into one QCBORItem. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1252 | |
| 1253 | @retval QCBOR_ERR_UNSUPPORTED |
| 1254 | |
| 1255 | @retval QCBOR_ERR_HIT_END |
| 1256 | |
| 1257 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1258 | |
| 1259 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1260 | |
| 1261 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1262 | |
| 1263 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1264 | |
| 1265 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1266 | |
| 1267 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1268 | |
| 1269 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1270 | |
| 1271 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1272 | |
| 1273 | @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1274 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1275 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1276 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1277 | static inline QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1278 | GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1279 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1280 | // Stack use: int/ptr 1, QCBORItem -- 56 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1281 | QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1282 | if(nReturn) |
| 1283 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1284 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1285 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1286 | // Break can't be a map entry |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1287 | goto Done; |
| 1288 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1289 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1290 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1291 | // In a map and caller wants maps decoded, not treated as arrays |
| 1292 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1293 | if(DecodeNesting_IsCurrentTypeMap(&(me->nesting))) { |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1294 | // If in a map and the right decoding mode, get the label |
| 1295 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1296 | // Save label in pDecodedItem and get the next which will |
| 1297 | // be the real data |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1298 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1299 | nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1300 | if(QCBORDecode_IsUnrecoverableError(nReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1301 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1302 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1303 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1304 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1305 | |
| 1306 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1307 | // strings are always good labels |
| 1308 | pDecodedItem->label.string = LabelItem.val.string; |
| 1309 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 1310 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1311 | // It's not a string and we only want strings |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1312 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1313 | goto Done; |
| 1314 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1315 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1316 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1317 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1318 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1319 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1320 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1321 | pDecodedItem->label.string = LabelItem.val.string; |
| 1322 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1323 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1324 | } else { |
| 1325 | // label is not an int or a string. It is an arrray |
| 1326 | // or a float or such and this implementation doesn't handle that. |
| 1327 | // Also, tags on labels are ignored. |
| 1328 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1329 | goto Done; |
| 1330 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1331 | } |
| 1332 | } else { |
| 1333 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1334 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1335 | nReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1336 | goto Done; |
| 1337 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1338 | // Decoding a map as an array |
| 1339 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1340 | // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2 |
| 1341 | // Cast is needed because of integer promotion |
| 1342 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1343 | } |
| 1344 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1345 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1346 | Done: |
| 1347 | return nReturn; |
| 1348 | } |
| 1349 | |
| 1350 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1351 | /* |
| 1352 | See if next item is a CBOR break. If it is, it is consumed, |
| 1353 | if not it is not consumed. |
| 1354 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1355 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1356 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1357 | { |
| 1358 | *pbNextIsBreak = false; |
| 1359 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1360 | QCBORItem Peek; |
| 1361 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
| 1362 | QCBORError uReturn = GetNext_Item(pUIB, &Peek, NULL); |
| 1363 | if(uReturn != QCBOR_SUCCESS) { |
| 1364 | return uReturn; |
| 1365 | } |
| 1366 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1367 | // It is not a break, rewind so it can be processed normally. |
| 1368 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1369 | } else { |
| 1370 | *pbNextIsBreak = true; |
| 1371 | } |
| 1372 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1373 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1374 | return QCBOR_SUCCESS; |
| 1375 | } |
| 1376 | |
| 1377 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1378 | /* |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1379 | An item was just consumed, now figure out if it was the |
| 1380 | end of an array or map that can be closed out. That |
| 1381 | may in turn close out another map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1382 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1383 | static QCBORError NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1384 | { |
| 1385 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1386 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1387 | /* This loops ascending nesting levels as long as there is ascending to do */ |
| 1388 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1389 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1390 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1391 | /* Decrement count for definite length maps / arrays */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1392 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1393 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1394 | /* 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] | 1395 | break; |
| 1396 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1397 | /* All of a definite length array was consumed; fall through to |
| 1398 | ascend */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1399 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1400 | } else { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1401 | /* If not definite length, have to check for a CBOR break */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1402 | bool bIsBreak = false; |
| 1403 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1404 | if(uReturn != QCBOR_SUCCESS) { |
| 1405 | goto Done; |
| 1406 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1407 | |
| 1408 | if(!bIsBreak) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1409 | /* 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] | 1410 | break; |
| 1411 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1412 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1413 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1414 | /* |
| 1415 | Break occurred inside a bstr-wrapped CBOR or |
| 1416 | in the top level sequence. This is always an |
| 1417 | error because neither are an indefinte length |
| 1418 | map/array. |
| 1419 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1420 | uReturn = QCBOR_ERR_BAD_BREAK; |
| 1421 | goto Done; |
| 1422 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1423 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1424 | /* It was a break in an indefinite length map / array */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1425 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1426 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1427 | /* All items in the map/array level have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1428 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1429 | /* But ascent in bounded mode is only by explicit call to |
| 1430 | QCBORDecode_ExitBoundedMode() */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1431 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1432 | /* Set the count to zero for definite length arrays to indicate |
| 1433 | cursor is at end of bounded map / array */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1434 | if(bMarkEnd) { |
| 1435 | // Used for definite and indefinite to signal end |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1436 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1437 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1438 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1439 | break; |
| 1440 | } |
| 1441 | |
| 1442 | /* Finally, actually ascend one level. */ |
| 1443 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1444 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1445 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1446 | uReturn = QCBOR_SUCCESS; |
| 1447 | |
| 1448 | Done: |
| 1449 | return uReturn; |
| 1450 | } |
| 1451 | |
| 1452 | |
| 1453 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1454 | This handles the traversal descending into and asecnding out of maps, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1455 | arrays and bstr-wrapped CBOR. It figures out the ends of definite and |
| 1456 | indefinte length maps and arrays by looking at the item count or |
| 1457 | finding CBOR breaks. It detects the ends of the top-level sequence |
| 1458 | and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1459 | |
| 1460 | @retval QCBOR_ERR_UNSUPPORTED X |
| 1461 | |
| 1462 | @retval QCBOR_ERR_HIT_END |
| 1463 | |
| 1464 | @retval QCBOR_ERR_INT_OVERFLOW X |
| 1465 | |
| 1466 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1467 | |
| 1468 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1469 | |
| 1470 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED X |
| 1471 | |
| 1472 | @retval QCBOR_ERR_BAD_TYPE_7 X |
| 1473 | |
| 1474 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1475 | |
| 1476 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1477 | |
| 1478 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1479 | |
| 1480 | @retval QCBOR_ERR_MAP_LABEL_TYPE X |
| 1481 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1482 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1483 | |
| 1484 | @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1485 | |
| 1486 | @retval QCBOR_ERR_BAD_BREAK |
| 1487 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1488 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1489 | static QCBORError |
| 1490 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1491 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1492 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1493 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1494 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1495 | /* |
| 1496 | If out of bytes to consume, it is either the end of the top-level |
| 1497 | sequence of some bstr-wrapped CBOR that was entered. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1498 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1499 | In the case of bstr-wrapped CBOR, the length of the UsefulInputBuf |
| 1500 | was set to that of the bstr-wrapped CBOR. When the bstr-wrapped |
| 1501 | CBOR is exited, the length is set back to the top-level's length |
| 1502 | or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1503 | */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1504 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1505 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1506 | goto Done; |
| 1507 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1508 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1509 | /* |
| 1510 | 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] | 1511 | array. The check for the end of an indefinite length array is |
| 1512 | later. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1513 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1514 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1515 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1516 | goto Done; |
| 1517 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1518 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1519 | /* ==== Next: not at the end so get another item ==== */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1520 | uReturn = GetNext_MapEntry(me, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1521 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1522 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1523 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1524 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1525 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1526 | /* |
| 1527 | Breaks ending arrays/maps are always processed at the end of this |
| 1528 | function. They should never show up here. |
| 1529 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1530 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1531 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1532 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1533 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1534 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1535 | /* |
| 1536 | Record the nesting level for this data item before processing any |
| 1537 | of decrementing and descending. |
| 1538 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1539 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1540 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1541 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1542 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1543 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1544 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1545 | If the new item is a map or array, descend. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1546 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1547 | Empty indefinite length maps and arrays are descended into, but |
| 1548 | then ascended out of in the next chunk of code. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1549 | |
| 1550 | Maps and arrays do count as items in the map/array that |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1551 | encloses them so a decrement needs to be done for them too, but |
| 1552 | that is done only when all the items in them have been |
| 1553 | processed, not when they are opened with the exception of an |
| 1554 | empty map or array. |
| 1555 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1556 | QCBORError uDescendErr; |
| 1557 | uDescendErr = DecodeNesting_DescendMapOrArray(&(me->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1558 | pDecodedItem->uDataType, |
| 1559 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1560 | if(uDescendErr != QCBOR_SUCCESS) { |
| 1561 | /* This error is probably a traversal error and it |
| 1562 | overrides the non-traversal error. */ |
| 1563 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1564 | goto Done; |
| 1565 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1568 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1569 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1570 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1571 | /* |
| 1572 | The following cases are handled here: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1573 | - A non-aggregate like an integer or string |
| 1574 | - An empty definite length map or array |
| 1575 | - 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] | 1576 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1577 | NestLevelAscender() does the work of decrementing the count for an |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1578 | definite length map/array and break detection for an indefinite |
| 1579 | length map/array. If the end of the map/array was reached, then |
| 1580 | it ascends nesting levels, possibly all the way to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1581 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1582 | QCBORError uAscendErr; |
| 1583 | uAscendErr = NestLevelAscender(me, true); |
| 1584 | if(uAscendErr != QCBOR_SUCCESS) { |
| 1585 | /* This error is probably a traversal error and it |
| 1586 | overrides the non-traversal error. */ |
| 1587 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1588 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1589 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1590 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1591 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1592 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1593 | /* |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1594 | Tell the caller what level is next. This tells them what |
| 1595 | maps/arrays were closed out and makes it possible for them to |
| 1596 | reconstruct the tree with just the information returned in |
| 1597 | a QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1598 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1599 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1600 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1601 | pDecodedItem->uNextNestLevel = 0; |
| 1602 | } else { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1603 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1604 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1605 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1606 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1607 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1610 | static void ShiftTags(QCBORItem *pDecodedItem) |
| 1611 | { |
| 1612 | pDecodedItem->uTags[0] = pDecodedItem->uTags[1]; |
| 1613 | pDecodedItem->uTags[1] = pDecodedItem->uTags[2]; |
| 1614 | pDecodedItem->uTags[2] = pDecodedItem->uTags[3]; |
| 1615 | pDecodedItem->uTags[2] = CBOR_TAG_INVALID16; |
| 1616 | } |
| 1617 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1618 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1619 | /* |
| 1620 | Mostly just assign the right data type for the date string. |
| 1621 | */ |
| 1622 | inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem) |
| 1623 | { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1624 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1625 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1626 | } |
| 1627 | |
| 1628 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1629 | pDecodedItem->val.dateString = Temp; |
| 1630 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1631 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1632 | return QCBOR_SUCCESS; |
| 1633 | } |
| 1634 | |
| 1635 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1636 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1637 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1638 | The epoch formatted date. Turns lots of different forms of encoding |
| 1639 | date into uniform one |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1640 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1641 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1642 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1643 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1644 | |
| 1645 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1646 | |
| 1647 | switch (pDecodedItem->uDataType) { |
| 1648 | |
| 1649 | case QCBOR_TYPE_INT64: |
| 1650 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1651 | break; |
| 1652 | |
| 1653 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1654 | // This only happens for CBOR type 0 > INT64_MAX so it is |
| 1655 | // always an overflow. |
| 1656 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1657 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1658 | break; |
| 1659 | |
| 1660 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1661 | case QCBOR_TYPE_FLOAT: |
| 1662 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1663 | { |
| 1664 | // This comparison needs to be done as a float before |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1665 | // conversion to an int64_t to be able to detect doubles that |
| 1666 | // are too large to fit into an int64_t. A double has 52 |
| 1667 | // bits of preceision. An int64_t has 63. Casting INT64_MAX |
| 1668 | // to a double actually causes a round up which is bad and |
| 1669 | // wrong for the comparison because it will allow conversion |
| 1670 | // of doubles that can't fit into a uint64_t. To remedy this |
| 1671 | // INT64_MAX - 0x7ff is used as the cutoff point because if |
| 1672 | // that value rounds up in conversion to double it will still |
| 1673 | // be less than INT64_MAX. 0x7ff is picked because it has 11 |
| 1674 | // bits set. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1675 | // |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1676 | // INT64_MAX seconds is on the order of 10 billion years, and |
| 1677 | // the earth is less than 5 billion years old, so for most |
| 1678 | // uses this conversion error won't occur even though doubles |
| 1679 | // can go much larger. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1680 | // |
| 1681 | // Without the 0x7ff there is a ~30 minute range of time |
| 1682 | // values 10 billion years in the past and in the future |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1683 | // where this code would go wrong. Some compilers |
| 1684 | // will generate warnings or errors without the 0x7ff |
| 1685 | // because of the precision issue. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1686 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
| 1687 | pDecodedItem->val.dfnum : |
| 1688 | (double)pDecodedItem->val.fnum; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1689 | if(isnan(d) || |
| 1690 | d > (double)(INT64_MAX - 0x7ff) || |
| 1691 | d < (double)(INT64_MIN + 0x7ff)) { |
| 1692 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1693 | goto Done; |
| 1694 | } |
| 1695 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1696 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 1697 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1698 | } |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1699 | #else |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1700 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1701 | uReturn = QCBOR_ERR_FLOAT_DATE_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1702 | goto Done; |
| 1703 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1704 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1705 | break; |
| 1706 | |
| 1707 | default: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1708 | uReturn = QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1709 | goto Done; |
| 1710 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1711 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1712 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1713 | |
| 1714 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1715 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1719 | /* |
| 1720 | Mostly just assign the right data type for the bignum. |
| 1721 | */ |
| 1722 | inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem) |
| 1723 | { |
| 1724 | // Stack Use: UsefulBuf 1 -- 16 |
| 1725 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1726 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1727 | } |
| 1728 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1729 | pDecodedItem->val.bigNum = Temp; |
| 1730 | const bool bIsPosBigNum = (bool)(pDecodedItem->uTags[0] == CBOR_TAG_POS_BIGNUM); |
| 1731 | pDecodedItem->uDataType = (uint8_t)(bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM |
| 1732 | : QCBOR_TYPE_NEGBIGNUM); |
| 1733 | return QCBOR_SUCCESS; |
| 1734 | } |
| 1735 | |
| 1736 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1737 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1738 | /* |
| 1739 | Decode decimal fractions and big floats. |
| 1740 | |
| 1741 | When called pDecodedItem must be the array that is tagged as a big |
| 1742 | float or decimal fraction, the array that has the two members, the |
| 1743 | exponent and mantissa. |
| 1744 | |
| 1745 | This will fetch and decode the exponent and mantissa and put the |
| 1746 | result back into pDecodedItem. |
| 1747 | */ |
| 1748 | inline static QCBORError |
| 1749 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
| 1750 | { |
| 1751 | QCBORError nReturn; |
| 1752 | |
| 1753 | // --- Make sure it is an array; track nesting level of members --- |
| 1754 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 1755 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1756 | goto Done; |
| 1757 | } |
| 1758 | |
| 1759 | // A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1760 | // definite length arrays, but not for indefnite. Instead remember |
| 1761 | // the nesting level the two integers must be at, which is one |
| 1762 | // deeper than that of the array. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1763 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 1764 | |
| 1765 | // --- Is it a decimal fraction or a bigfloat? --- |
| 1766 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
| 1767 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 1768 | |
| 1769 | // --- Get the exponent --- |
| 1770 | QCBORItem exponentItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1771 | nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1772 | if(nReturn != QCBOR_SUCCESS) { |
| 1773 | goto Done; |
| 1774 | } |
| 1775 | if(exponentItem.uNestingLevel != nNestLevel) { |
| 1776 | // Array is empty or a map/array encountered when expecting an int |
| 1777 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1778 | goto Done; |
| 1779 | } |
| 1780 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
| 1781 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1782 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1783 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1784 | // will be too large for this to handle and thus an error that will |
| 1785 | // get handled in the next else. |
| 1786 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 1787 | } else { |
| 1788 | // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1789 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1790 | goto Done; |
| 1791 | } |
| 1792 | |
| 1793 | // --- Get the mantissa --- |
| 1794 | QCBORItem mantissaItem; |
| 1795 | nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL); |
| 1796 | if(nReturn != QCBOR_SUCCESS) { |
| 1797 | goto Done; |
| 1798 | } |
| 1799 | if(mantissaItem.uNestingLevel != nNestLevel) { |
| 1800 | // Mantissa missing or map/array encountered when expecting number |
| 1801 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1802 | goto Done; |
| 1803 | } |
| 1804 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
| 1805 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1806 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1807 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1808 | // will be too large for this to handle and thus an error that |
| 1809 | // will get handled in an else below. |
| 1810 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1811 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 1812 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1813 | // Got a good big num mantissa |
| 1814 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
| 1815 | // Depends on numbering of QCBOR_TYPE_XXX |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1816 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 1817 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 1818 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1819 | } else { |
| 1820 | // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1821 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1822 | goto Done; |
| 1823 | } |
| 1824 | |
| 1825 | // --- Check that array only has the two numbers --- |
| 1826 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 1827 | // Extra items in the decimal fraction / big float |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1828 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1829 | goto Done; |
| 1830 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 1831 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1832 | |
| 1833 | Done: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1834 | return nReturn; |
| 1835 | } |
| 1836 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1837 | |
| 1838 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1839 | inline static QCBORError DecodeURI(QCBORItem *pDecodedItem) |
| 1840 | { |
| 1841 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1842 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1843 | } |
| 1844 | pDecodedItem->uDataType = QCBOR_TYPE_URI; |
| 1845 | return QCBOR_SUCCESS; |
| 1846 | } |
| 1847 | |
| 1848 | |
| 1849 | inline static QCBORError DecodeB64URL(QCBORItem *pDecodedItem) |
| 1850 | { |
| 1851 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1852 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1853 | } |
| 1854 | pDecodedItem->uDataType = QCBOR_TYPE_BASE64URL; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1855 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1856 | return QCBOR_SUCCESS; |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | inline static QCBORError DecodeB64(QCBORItem *pDecodedItem) |
| 1861 | { |
| 1862 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1863 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1864 | } |
| 1865 | pDecodedItem->uDataType = QCBOR_TYPE_BASE64; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1866 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1867 | return QCBOR_SUCCESS; |
| 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | inline static QCBORError DecodeRegex(QCBORItem *pDecodedItem) |
| 1872 | { |
| 1873 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1874 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1875 | } |
| 1876 | pDecodedItem->uDataType = QCBOR_TYPE_REGEX; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1877 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1878 | return QCBOR_SUCCESS; |
| 1879 | } |
| 1880 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1881 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1882 | inline static QCBORError DecodeWrappedCBOR(QCBORItem *pDecodedItem) |
| 1883 | { |
| 1884 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1885 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1886 | } |
| 1887 | pDecodedItem->uDataType = QBCOR_TYPE_WRAPPED_CBOR; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1888 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1889 | return QCBOR_SUCCESS; |
| 1890 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1891 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1892 | |
| 1893 | inline static QCBORError DecodeWrappedCBORSequence(QCBORItem *pDecodedItem) |
| 1894 | { |
| 1895 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1896 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1897 | } |
| 1898 | pDecodedItem->uDataType = QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1899 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1900 | return QCBOR_SUCCESS; |
| 1901 | } |
| 1902 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1903 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1904 | inline static QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
| 1905 | { |
| 1906 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1907 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 1908 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1909 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 1910 | } else { |
| 1911 | return QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1912 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1913 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1914 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1915 | return QCBOR_SUCCESS; |
| 1916 | } |
| 1917 | |
| 1918 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1919 | inline static QCBORError DecodeUUID(QCBORItem *pDecodedItem) |
| 1920 | { |
| 1921 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1922 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1923 | } |
| 1924 | pDecodedItem->uDataType = QCBOR_TYPE_UUID; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1925 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1926 | return QCBOR_SUCCESS; |
| 1927 | } |
| 1928 | |
| 1929 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1930 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1931 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1932 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1933 | static QCBORError |
| 1934 | QCBORDecode_GetNextTag(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1935 | { |
| 1936 | QCBORError nReturn; |
| 1937 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1938 | nReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1939 | if(nReturn != QCBOR_SUCCESS) { |
| 1940 | goto Done; |
| 1941 | } |
| 1942 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1943 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1944 | switch(pDecodedItem->uTags[i]) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1945 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1946 | // Many of the functions here only just map a CBOR tag to |
| 1947 | // a QCBOR_TYPE for a string and could probably be |
| 1948 | // implemented with less object code. This implementation |
| 1949 | // of string types takes about 120 bytes of object code |
| 1950 | // (that is always linked and not removed by dead stripping). |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1951 | case CBOR_TAG_DATE_STRING: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1952 | nReturn = DecodeDateString(pDecodedItem); |
| 1953 | break; |
| 1954 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1955 | case CBOR_TAG_DATE_EPOCH: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1956 | nReturn = DecodeDateEpoch(pDecodedItem); |
| 1957 | break; |
| 1958 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1959 | case CBOR_TAG_POS_BIGNUM: |
| 1960 | case CBOR_TAG_NEG_BIGNUM: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1961 | nReturn = DecodeBigNum(pDecodedItem); |
| 1962 | break; |
| 1963 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1964 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1965 | case CBOR_TAG_DECIMAL_FRACTION: |
| 1966 | case CBOR_TAG_BIGFLOAT: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1967 | // For aggregate tagged types, what goes into pTags is only collected |
| 1968 | // from the surrounding data item, not the contents, so pTags is not |
| 1969 | // passed on here. |
| 1970 | |
| 1971 | nReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem); |
| 1972 | break; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1973 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1974 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1975 | case CBOR_TAG_CBOR: |
| 1976 | nReturn = DecodeWrappedCBOR(pDecodedItem); |
| 1977 | break; |
| 1978 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1979 | case CBOR_TAG_CBOR_SEQUENCE: |
| 1980 | nReturn = DecodeWrappedCBORSequence(pDecodedItem); |
| 1981 | break; |
| 1982 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1983 | case CBOR_TAG_URI: |
| 1984 | nReturn = DecodeURI(pDecodedItem); |
| 1985 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1986 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1987 | case CBOR_TAG_B64URL: |
| 1988 | nReturn = DecodeB64URL(pDecodedItem); |
| 1989 | break; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1990 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1991 | case CBOR_TAG_B64: |
| 1992 | nReturn = DecodeB64(pDecodedItem); |
| 1993 | break; |
| 1994 | |
| 1995 | case CBOR_TAG_MIME: |
| 1996 | case CBOR_TAG_BINARY_MIME: |
| 1997 | nReturn = DecodeMIME(pDecodedItem); |
| 1998 | break; |
| 1999 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2000 | case CBOR_TAG_REGEX: |
| 2001 | nReturn = DecodeRegex(pDecodedItem); |
| 2002 | break; |
| 2003 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2004 | case CBOR_TAG_BIN_UUID: |
| 2005 | nReturn = DecodeUUID(pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2006 | break; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2007 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2008 | case CBOR_TAG_INVALID16: |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2009 | // The end of the tag list or no tags |
| 2010 | // Successful exit from the loop. |
| 2011 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2012 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2013 | default: |
| 2014 | // A tag that is not understood |
| 2015 | // A successful exit from the loop |
| 2016 | goto Done; |
| 2017 | |
| 2018 | } |
| 2019 | if(nReturn != QCBOR_SUCCESS) { |
| 2020 | goto Done; |
| 2021 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2022 | // A tag was successfully processed, shift it |
| 2023 | // out of the list of tags returned. |
| 2024 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | Done: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2028 | return nReturn; |
| 2029 | } |
| 2030 | |
| 2031 | |
| 2032 | QCBORError |
| 2033 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2034 | { |
| 2035 | QCBORError uErr; |
| 2036 | uErr = QCBORDecode_GetNextTag(pMe, pDecodedItem); |
| 2037 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2038 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2039 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2040 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2041 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | |
| 2045 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2046 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2047 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2048 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2049 | { |
| 2050 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2051 | return; |
| 2052 | } |
| 2053 | |
| 2054 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2055 | } |
| 2056 | |
| 2057 | |
| 2058 | /* |
| 2059 | Public function, see header qcbor/qcbor_decode.h file |
| 2060 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2061 | QCBORError |
| 2062 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, |
| 2063 | QCBORItem *pDecodedItem, |
| 2064 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2065 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2066 | QCBORError nReturn; |
| 2067 | |
| 2068 | nReturn = QCBORDecode_GetNext(me, pDecodedItem); |
| 2069 | if(nReturn != QCBOR_SUCCESS) { |
| 2070 | return nReturn; |
| 2071 | } |
| 2072 | |
| 2073 | if(pTags != NULL) { |
| 2074 | pTags->uNumUsed = 0; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2075 | // Reverse the order because pTags is reverse of |
| 2076 | // QCBORItem.uTags. |
| 2077 | for(int i = QCBOR_MAX_TAGS_PER_ITEM-1; i >=0 ; i--) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2078 | if(pDecodedItem->uTags[i] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2079 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2080 | } |
| 2081 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2082 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2083 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2084 | pTags->puTags[pTags->uNumUsed] = ConvertTag(me,pDecodedItem->uTags[i]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2085 | pTags->uNumUsed++; |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2093 | /* |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2094 | Decoding items is done in 5 layered functions, one calling the |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2095 | next one down. If a layer has no work to do for a particular item |
| 2096 | it returns quickly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2097 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2098 | - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes |
| 2099 | tagged data items, turning them into the local C representation. |
| 2100 | For the most simple it is just associating a QCBOR_TYPE with the data. For |
| 2101 | the complex ones that an aggregate of data items, there is some further |
| 2102 | decoding and a little bit of recursion. |
| 2103 | |
| 2104 | - QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2105 | ends of maps and arrays. It tracks descending into and ascending |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2106 | out of maps/arrays. It processes all breaks that terminate |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2107 | indefinite length maps and arrays. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2108 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2109 | - GetNext_MapEntry -- This handles the combining of two |
| 2110 | items, the label and the data, that make up a map entry. |
| 2111 | It only does work on maps. It combines the label and data |
| 2112 | items into one labeled item. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2113 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2114 | - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the |
| 2115 | tags into bit flags associated with the data item. No actual decoding |
| 2116 | of the contents of the tagged item is performed here. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2117 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2118 | - GetNext_FullItem -- This assembles the sub-items that make up |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2119 | an indefinte length string into one string item. It uses the |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2120 | string allocater to create contiguous space for the item. It |
| 2121 | processes all breaks that are part of indefinite length strings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2122 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2123 | - GetNext_Item -- This decodes the atomic data items in CBOR. Each |
| 2124 | atomic data item has a "major type", an integer "argument" and optionally |
| 2125 | some content. For text and byte strings, the content is the bytes |
| 2126 | that make up the string. These are the smallest data items that are |
| 2127 | considered to be well-formed. The content may also be other data items in |
| 2128 | the case of aggregate types. They are not handled in this layer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2129 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2130 | Roughly this takes 300 bytes of stack for vars. Need to |
| 2131 | evaluate this more carefully and correctly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2132 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2133 | */ |
| 2134 | |
| 2135 | |
| 2136 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2137 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2138 | */ |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2139 | bool QCBORDecode_IsTagged(QCBORDecodeContext *me, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2140 | const QCBORItem *pItem, |
| 2141 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2142 | { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2143 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2144 | if(pItem->uTags[i] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2145 | break; |
| 2146 | } |
| 2147 | if(ConvertTag(me, pItem->uTags[i]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2148 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2149 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2150 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2151 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2152 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | |
| 2156 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2157 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2158 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2159 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2160 | { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2161 | QCBORError uReturn = me->uLastError; |
| 2162 | |
| 2163 | if(uReturn != QCBOR_SUCCESS) { |
| 2164 | goto Done; |
| 2165 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2166 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2167 | // Error out if all the maps/arrays are not closed out |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2168 | if(!DecodeNesting_IsCurrentAtTop(&(me->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2169 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2170 | goto Done; |
| 2171 | } |
| 2172 | |
| 2173 | // Error out if not all the bytes are consumed |
| 2174 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2175 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2176 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2177 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2178 | Done: |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2179 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2180 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2181 | StringAllocator_Destruct(&(me->StringAllocator)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2182 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2183 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2187 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2188 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2189 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2190 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2191 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2192 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2193 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2194 | { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2195 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2196 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2197 | } else { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 2198 | return ConvertTag(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2202 | /* |
| 2203 | Public function, see header qcbor/qcbor_decode.h file |
| 2204 | */ |
| 2205 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2206 | uint32_t uIndex) |
| 2207 | { |
| 2208 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2209 | return CBOR_TAG_INVALID64; |
| 2210 | } else { |
| 2211 | return ConvertTag(pMe, pMe->uLastTags[uIndex]); |
| 2212 | } |
| 2213 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2214 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2215 | /* |
| 2216 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2217 | Decoder errors handled in this file |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2218 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2219 | - Hit end of input before it was expected while decoding type and |
| 2220 | number QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2221 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2222 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2223 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2224 | - Hit end of input while decoding a text or byte string |
| 2225 | QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2226 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2227 | - Encountered conflicting tags -- e.g., an item is tagged both a date |
| 2228 | string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2229 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2230 | - Encontered an array or mapp that has too many items |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2231 | QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2232 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2233 | - Encountered array/map nesting that is too deep |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2234 | QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2235 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2236 | - An epoch date > INT64_MAX or < INT64_MIN was encountered |
| 2237 | QCBOR_ERR_DATE_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2238 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2239 | - The type of a map label is not a string or int |
| 2240 | QCBOR_ERR_MAP_LABEL_TYPE |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2241 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2242 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2243 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2244 | */ |
| 2245 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2246 | |
| 2247 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2248 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2249 | /* =========================================================================== |
| 2250 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2251 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2252 | This implements a simple sting allocator for indefinite length |
| 2253 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2254 | implements the function type QCBORStringAllocate and allows easy |
| 2255 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2256 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2257 | This particular allocator is built-in for convenience. The caller |
| 2258 | can implement their own. All of this following code will get |
| 2259 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2260 | |
| 2261 | This is a very primitive memory allocator. It does not track |
| 2262 | individual allocations, only a high-water mark. A free or |
| 2263 | reallocation must be of the last chunk allocated. |
| 2264 | |
| 2265 | The size of the pool and offset to free memory are packed into the |
| 2266 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2267 | the decode context. Since the address of the pool may not be |
| 2268 | aligned, they have to be packed and unpacked as if they were |
| 2269 | serialized data of the wire or such. |
| 2270 | |
| 2271 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2272 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2273 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2274 | |
| 2275 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2276 | static inline int |
| 2277 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2278 | { |
| 2279 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2280 | UsefulInputBuf UIB; |
| 2281 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2282 | // Just assume the size here. It was checked during SetUp so |
| 2283 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2284 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2285 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2286 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2287 | return UsefulInputBuf_GetError(&UIB); |
| 2288 | } |
| 2289 | |
| 2290 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2291 | static inline int |
| 2292 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2293 | { |
| 2294 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2295 | // length check performed here is useful. |
| 2296 | UsefulOutBuf UOB; |
| 2297 | |
| 2298 | UsefulOutBuf_Init(&UOB, Pool); |
| 2299 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2300 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2301 | return UsefulOutBuf_GetError(&UOB); |
| 2302 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2303 | |
| 2304 | |
| 2305 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2306 | Internal function for an allocation, reallocation free and destuct. |
| 2307 | |
| 2308 | Having only one function rather than one each per mode saves space in |
| 2309 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2310 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2311 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2312 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2313 | static UsefulBuf |
| 2314 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2315 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2316 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2317 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2318 | uint32_t uPoolSize; |
| 2319 | uint32_t uFreeOffset; |
| 2320 | |
| 2321 | if(uNewSize > UINT32_MAX) { |
| 2322 | // This allocator is only good up to 4GB. This check should |
| 2323 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2324 | goto Done; |
| 2325 | } |
| 2326 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2327 | |
| 2328 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2329 | goto Done; |
| 2330 | } |
| 2331 | |
| 2332 | if(uNewSize) { |
| 2333 | if(pMem) { |
| 2334 | // REALLOCATION MODE |
| 2335 | // Calculate pointer to the end of the memory pool. It is |
| 2336 | // assumed that pPool + uPoolSize won't wrap around by |
| 2337 | // assuming the caller won't pass a pool buffer in that is |
| 2338 | // not in legitimate memory space. |
| 2339 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2340 | |
| 2341 | // Check that the pointer for reallocation is in the range of the |
| 2342 | // pool. This also makes sure that pointer math further down |
| 2343 | // doesn't wrap under or over. |
| 2344 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2345 | // Offset to start of chunk for reallocation. This won't |
| 2346 | // wrap under because of check that pMem >= pPool. Cast |
| 2347 | // is safe because the pool is always less than UINT32_MAX |
| 2348 | // because of check in QCBORDecode_SetMemPool(). |
| 2349 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2350 | |
| 2351 | // Check to see if the allocation will fit. uPoolSize - |
| 2352 | // uMemOffset will not wrap under because of check that |
| 2353 | // pMem is in the range of the uPoolSize by check above. |
| 2354 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2355 | ReturnValue.ptr = pMem; |
| 2356 | ReturnValue.len = uNewSize; |
| 2357 | |
| 2358 | // Addition won't wrap around over because uNewSize was |
| 2359 | // checked to be sure it is less than the pool size. |
| 2360 | uFreeOffset = uMemOffset + uNewSize32; |
| 2361 | } |
| 2362 | } |
| 2363 | } else { |
| 2364 | // ALLOCATION MODE |
| 2365 | // uPoolSize - uFreeOffset will not underflow because this |
| 2366 | // pool implementation makes sure uFreeOffset is always |
| 2367 | // smaller than uPoolSize through this check here and |
| 2368 | // reallocation case. |
| 2369 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2370 | ReturnValue.len = uNewSize; |
| 2371 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2372 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2373 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2374 | } |
| 2375 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2376 | if(pMem) { |
| 2377 | // FREE MODE |
| 2378 | // Cast is safe because of limit on pool size in |
| 2379 | // QCBORDecode_SetMemPool() |
| 2380 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2381 | } else { |
| 2382 | // DESTRUCT MODE |
| 2383 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2384 | } |
| 2385 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2386 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2387 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2388 | MemPool_Pack(Pool, uFreeOffset); |
| 2389 | |
| 2390 | Done: |
| 2391 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2392 | } |
| 2393 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2394 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2395 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2396 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2397 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2398 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2399 | UsefulBuf Pool, |
| 2400 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2401 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2402 | // The pool size and free mem offset are packed into the beginning |
| 2403 | // of the pool memory. This compile time check make sure the |
| 2404 | // constant in the header is correct. This check should optimize |
| 2405 | // down to nothing. |
| 2406 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2407 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2408 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2409 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2410 | // The pool size and free offset packed in to the beginning of pool |
| 2411 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2412 | // machines. |
| 2413 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2414 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2415 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2416 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2417 | // This checks that the pool buffer given is big enough. |
| 2418 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2419 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2420 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2421 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2422 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2423 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2424 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2425 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2426 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2427 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2428 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2429 | |
| 2430 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2431 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2432 | { |
| 2433 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2434 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2435 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2436 | |
| 2437 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2438 | Consume an entire map or array (and do next to |
| 2439 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2440 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2441 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2442 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2443 | const QCBORItem *pItemToConsume, |
| 2444 | uint_fast8_t *puNextNestLevel) |
| 2445 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2446 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2447 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2448 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2449 | // If it is a map or array, this will tell if it is empty. |
| 2450 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2451 | |
| 2452 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2453 | /* 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] | 2454 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2455 | /* This works for definite and indefinite length |
| 2456 | * maps and arrays by using the nesting level |
| 2457 | */ |
| 2458 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2459 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2460 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2461 | goto Done; |
| 2462 | } |
| 2463 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2464 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2465 | if(puNextNestLevel != NULL) { |
| 2466 | *puNextNestLevel = Item.uNextNestLevel; |
| 2467 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2468 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2469 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2470 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2471 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2472 | if(puNextNestLevel != NULL) { |
| 2473 | /* Just pass the nesting level through */ |
| 2474 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2475 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2476 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2477 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2478 | |
| 2479 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2480 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2484 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2485 | Works only for integer and string labels. Returns false |
| 2486 | for any other type. */ |
| 2487 | static inline bool |
| 2488 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2489 | { |
| 2490 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2491 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2492 | return true; |
| 2493 | } |
| 2494 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2495 | 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] | 2496 | return true; |
| 2497 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2498 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2499 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2500 | return true; |
| 2501 | } |
| 2502 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2503 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2504 | return true; |
| 2505 | } |
| 2506 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2507 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2508 | /* Other label types are never matched */ |
| 2509 | return false; |
| 2510 | } |
| 2511 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2512 | |
| 2513 | /* |
| 2514 | Returns true if Item1 and Item2 are the same type |
| 2515 | or if either are of QCBOR_TYPE_ANY. |
| 2516 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2517 | static inline bool |
| 2518 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2519 | { |
| 2520 | if(Item1.uDataType == Item2.uDataType) { |
| 2521 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2522 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2523 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2524 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2525 | return true; |
| 2526 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2527 | return false; |
| 2528 | } |
| 2529 | |
| 2530 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2531 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2532 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2533 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2534 | @param[in] pMe The decode context to search. |
| 2535 | @param[in,out] pItemArray The items to search for and the items found. |
| 2536 | @param[out] puOffset Byte offset of last item matched. |
| 2537 | @param[in] pCBContext Context for the not-found item call back. |
| 2538 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2539 | |
| 2540 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2541 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2542 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2543 | were found for one of the labels being |
| 2544 | search for. This duplicate detection is |
| 2545 | only performed for items in pItemArray, |
| 2546 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2547 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2548 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2549 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2550 | |
| 2551 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2552 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2553 | On input pItemArray contains a list of labels and data types |
| 2554 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2555 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2556 | On output the fully retrieved items are filled in with |
| 2557 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2558 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2559 | If an item was not found, its data type is set to QCBOR_TYPE_NONE. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2560 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2561 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2562 | MapSearch(QCBORDecodeContext *pMe, |
| 2563 | QCBORItem *pItemArray, |
| 2564 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2565 | void *pCBContext, |
| 2566 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2567 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2568 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2569 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2570 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2571 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2572 | uReturn = pMe->uLastError; |
| 2573 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2574 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2575 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2576 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2577 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2578 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2579 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2580 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2581 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2584 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2585 | // It is an empty bounded array or map |
| 2586 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2587 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2588 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2589 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2590 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2591 | // Nothing is ever found in an empty array or map. All items |
| 2592 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2593 | uReturn = QCBOR_SUCCESS; |
| 2594 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2595 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2596 | } |
| 2597 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2598 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2599 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2600 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2601 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2602 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2603 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2604 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2605 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2606 | Loop over all the items in the map or array. Each item |
| 2607 | could be a map or array, but label matching is only at |
| 2608 | the main level. This handles definite and indefinite |
| 2609 | length maps and arrays. The only reason this is ever |
| 2610 | called on arrays is to find their end position. |
| 2611 | |
| 2612 | This will always run over all items in order to do |
| 2613 | duplicate detection. |
| 2614 | |
| 2615 | This will exit with failure if it encounters an |
| 2616 | unrecoverable error, but continue on for recoverable |
| 2617 | errors. |
| 2618 | |
| 2619 | If a recoverable error occurs on a matched item, then |
| 2620 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2621 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2622 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2623 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2624 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2625 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2626 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2627 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2628 | /* Get the item */ |
| 2629 | QCBORItem Item; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2630 | QCBORError uResult = QCBORDecode_GetNextTag(pMe, &Item); |
| 2631 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2632 | /* Got non-well-formed CBOR so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2633 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2634 | goto Done; |
| 2635 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2636 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2637 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2638 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2639 | goto Done; |
| 2640 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2641 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2642 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2643 | bool bMatched = false; |
| 2644 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 2645 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2646 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2647 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2648 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2649 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2650 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2651 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2652 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2653 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2654 | goto Done; |
| 2655 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2656 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2657 | if(uResult != QCBOR_SUCCESS) { |
| 2658 | uReturn = uResult; |
| 2659 | goto Done; |
| 2660 | } |
| 2661 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2662 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2663 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2664 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2665 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2666 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2667 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2668 | bMatched = true; |
| 2669 | } |
| 2670 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2671 | |
| 2672 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2673 | if(!bMatched && pfCallback != NULL) { |
| 2674 | /* |
| 2675 | Call the callback on unmatched labels. |
| 2676 | (It is tempting to do duplicate detection here, but that would |
| 2677 | require dynamic memory allocation because the number of labels |
| 2678 | that might be encountered is unbounded.) |
| 2679 | */ |
| 2680 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2681 | if(uReturn != QCBOR_SUCCESS) { |
| 2682 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2683 | } |
| 2684 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2685 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2686 | /* |
| 2687 | Consume the item whether matched or not. This |
| 2688 | does the work of traversing maps and array and |
| 2689 | everything in them. In this loop only the |
| 2690 | items at the current nesting level are examined |
| 2691 | to match the labels. |
| 2692 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2693 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2694 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2695 | goto Done; |
| 2696 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2697 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2698 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2699 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2700 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2701 | |
| 2702 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2703 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2704 | // Check here makes sure that this won't accidentally be |
| 2705 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2706 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
| 2707 | if(uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 2708 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 2709 | goto Done; |
| 2710 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2711 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 2712 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2713 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2714 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2715 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 2716 | |
| 2717 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2718 | /* 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] | 2719 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2720 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2721 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 2722 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2723 | } |
| 2724 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2725 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2726 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2729 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2730 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2731 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2732 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2733 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 2734 | int64_t nLabel, |
| 2735 | uint8_t uQcborType, |
| 2736 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2737 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2738 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2739 | return; |
| 2740 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2741 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2742 | QCBORItem OneItemSeach[2]; |
| 2743 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2744 | OneItemSeach[0].label.int64 = nLabel; |
| 2745 | OneItemSeach[0].uDataType = uQcborType; |
| 2746 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2747 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2748 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2749 | |
| 2750 | *pItem = OneItemSeach[0]; |
| 2751 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2752 | if(uReturn != QCBOR_SUCCESS) { |
| 2753 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2754 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2755 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2756 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2759 | Done: |
| 2760 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2764 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2765 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2766 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2767 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 2768 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2769 | uint8_t uQcborType, |
| 2770 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2771 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2772 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2773 | return; |
| 2774 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2775 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2776 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2777 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2778 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2779 | OneItemSeach[0].uDataType = uQcborType; |
| 2780 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2781 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2782 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2783 | if(uReturn != QCBOR_SUCCESS) { |
| 2784 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2785 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2786 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2787 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2788 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2791 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2792 | |
| 2793 | Done: |
| 2794 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2798 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2799 | static QCBORError |
| 2800 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2801 | { |
| 2802 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 2803 | if(uDataType == puTypeList[i]) { |
| 2804 | return QCBOR_SUCCESS; |
| 2805 | } |
| 2806 | } |
| 2807 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2808 | } |
| 2809 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 2810 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2811 | /** |
| 2812 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2813 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2814 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2815 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 2816 | @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] | 2817 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2818 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered tag value. |
| 2819 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2820 | static QCBORError |
| 2821 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2822 | { |
| 2823 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 2824 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 2825 | /* There are tags that QCBOR couldn't process on this item and |
| 2826 | the caller has told us there should not be. */ |
| 2827 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2828 | } |
| 2829 | |
| 2830 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 2831 | const int nItemType = pItem->uDataType; |
| 2832 | |
| 2833 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 2834 | // Must match the tag and only the tag |
| 2835 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2836 | } |
| 2837 | |
| 2838 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 2839 | if(uReturn == QCBOR_SUCCESS) { |
| 2840 | return QCBOR_SUCCESS; |
| 2841 | } |
| 2842 | |
| 2843 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 2844 | /* Must match the content type and only the content type. |
| 2845 | There was no match just above so it is a fail. */ |
| 2846 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2847 | } |
| 2848 | |
| 2849 | /* If here it can match either the tag or the content |
| 2850 | and it hasn't matched the content, so the end |
| 2851 | result is whether it matches the tag. This is |
| 2852 | also the case that the CBOR standard discourages. */ |
| 2853 | |
| 2854 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2855 | } |
| 2856 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2857 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2858 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 2859 | // This could be semi-private if need be |
| 2860 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2861 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 2862 | int64_t nLabel, |
| 2863 | TagSpecification TagSpec, |
| 2864 | QCBORItem *pItem) |
| 2865 | { |
| 2866 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 2867 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2868 | return; |
| 2869 | } |
| 2870 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2871 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 2874 | |
| 2875 | // This could be semi-private if need be |
| 2876 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2877 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 2878 | const char *szLabel, |
| 2879 | TagSpecification TagSpec, |
| 2880 | QCBORItem *pItem) |
| 2881 | { |
| 2882 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 2883 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2884 | return; |
| 2885 | } |
| 2886 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2887 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2890 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2891 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 2892 | int64_t nLabel, |
| 2893 | TagSpecification TagSpec, |
| 2894 | UsefulBufC *pString) |
| 2895 | { |
| 2896 | QCBORItem Item; |
| 2897 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 2898 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2899 | *pString = Item.val.string; |
| 2900 | } |
| 2901 | } |
| 2902 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2903 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2904 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 2905 | const char * szLabel, |
| 2906 | TagSpecification TagSpec, |
| 2907 | UsefulBufC *pString) |
| 2908 | { |
| 2909 | QCBORItem Item; |
| 2910 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 2911 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2912 | *pString = Item.val.string; |
| 2913 | } |
| 2914 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2915 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2916 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2917 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2918 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2919 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2920 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2921 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 2922 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2923 | } |
| 2924 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2925 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2926 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2927 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2928 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 2929 | QCBORItem *pItemList, |
| 2930 | void *pCallbackCtx, |
| 2931 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2932 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2933 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 2934 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 2938 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2939 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2940 | // The first item in pSearch is the one that is to be |
| 2941 | // entered. It should be the only one filled in. Any other |
| 2942 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 2943 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2944 | // Already in error state; do nothing. |
| 2945 | return; |
| 2946 | } |
| 2947 | |
| 2948 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2949 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 2950 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2951 | return; |
| 2952 | } |
| 2953 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2954 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2955 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2956 | return; |
| 2957 | } |
| 2958 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2959 | /* Need to get the current pre-order nesting level and cursor to be |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 2960 | at the map/array about to be entered. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2961 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 2962 | Also need the current map nesting level and start cursor to |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2963 | be at the right place. |
| 2964 | |
| 2965 | The UsefulInBuf offset could be anywhere, so no assumption is |
| 2966 | made about it. |
| 2967 | |
| 2968 | No assumption is made about the pre-order nesting level either. |
| 2969 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 2970 | However the bounded mode nesting level is assumed to be one above |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2971 | the map level that is being entered. |
| 2972 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2973 | /* Seek to the data item that is the map or array */ |
| 2974 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 2975 | |
| 2976 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2977 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame^] | 2978 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2982 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 2983 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2984 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2985 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2986 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2987 | QCBORItem OneItemSeach[2]; |
| 2988 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2989 | OneItemSeach[0].label.int64 = nLabel; |
| 2990 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 2991 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2992 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2993 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2994 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2998 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 2999 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3000 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3001 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3002 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3003 | QCBORItem OneItemSeach[2]; |
| 3004 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3005 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3006 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3007 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3008 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3009 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3012 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3013 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3014 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3015 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3016 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3017 | QCBORItem OneItemSeach[2]; |
| 3018 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3019 | OneItemSeach[0].label.int64 = nLabel; |
| 3020 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3021 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3022 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3023 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3026 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3027 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3028 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3029 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3030 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3031 | QCBORItem OneItemSeach[2]; |
| 3032 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3033 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3034 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3035 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3036 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3037 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3041 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame^] | 3042 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3043 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3044 | QCBORError uErr; |
| 3045 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3046 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3047 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3048 | // Already in error state; do nothing. |
| 3049 | return; |
| 3050 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3051 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3052 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3053 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3054 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3055 | if(uErr != QCBOR_SUCCESS) { |
| 3056 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3057 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3058 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3059 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3060 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3063 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3064 | |
| 3065 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3066 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3067 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3068 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3069 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3070 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3071 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3072 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3073 | // Special case to increment nesting level for zero-length maps |
| 3074 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3075 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3076 | } |
| 3077 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3078 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3079 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3080 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3081 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3082 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame^] | 3083 | if(pItem != NULL) { |
| 3084 | *pItem = Item; |
| 3085 | } |
| 3086 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3087 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3088 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3089 | } |
| 3090 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3091 | |
| 3092 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3093 | This is the common work for exiting a level that is a bounded map, |
| 3094 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3095 | |
| 3096 | One chunk of work is to set up the pre-order traversal so it is at |
| 3097 | the item just after the bounded map, array or bstr that is being |
| 3098 | exited. This is somewhat complex. |
| 3099 | |
| 3100 | The other work is to level-up the bounded mode to next higest bounded |
| 3101 | mode or the top level if there isn't one. |
| 3102 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3103 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3104 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3105 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3106 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3107 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3108 | /* |
| 3109 | First the pre-order-traversal byte offset is positioned to the |
| 3110 | item just after the bounded mode item that was just consumed. |
| 3111 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3112 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3113 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3114 | /* |
| 3115 | Next, set the current nesting level to one above the bounded level |
| 3116 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3117 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3118 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3119 | makes sure pCurrentBounded is valid. |
| 3120 | */ |
| 3121 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3122 | |
| 3123 | /* |
| 3124 | This does the complex work of leveling up the pre-order traversal |
| 3125 | when the end of a map or array or another bounded level is |
| 3126 | reached. It may do nothing, or ascend all the way to the top |
| 3127 | level. |
| 3128 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 3129 | uErr = NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3130 | if(uErr != QCBOR_SUCCESS) { |
| 3131 | goto Done; |
| 3132 | } |
| 3133 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3134 | /* |
| 3135 | This makes the next highest bounded level the current bounded |
| 3136 | level. If there is no next highest level, then no bounded mode is |
| 3137 | in effect. |
| 3138 | */ |
| 3139 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3140 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3141 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3142 | |
| 3143 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3144 | return uErr; |
| 3145 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3146 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3147 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3148 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3149 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3150 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3151 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3152 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3153 | return; |
| 3154 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3155 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3156 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3157 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3158 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3159 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3160 | goto Done; |
| 3161 | } |
| 3162 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3163 | /* |
| 3164 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3165 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3166 | from previous map search, then do a dummy search. |
| 3167 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3168 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3169 | QCBORItem Dummy; |
| 3170 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3171 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3172 | if(uErr != QCBOR_SUCCESS) { |
| 3173 | goto Done; |
| 3174 | } |
| 3175 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3176 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3177 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3178 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3179 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3180 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3184 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3185 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3186 | const QCBORItem *pItem, |
| 3187 | uint8_t uTagRequirement, |
| 3188 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3189 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3190 | if(pBstr) { |
| 3191 | *pBstr = NULLUsefulBufC; |
| 3192 | } |
| 3193 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3194 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3195 | // Already in error state; do nothing. |
| 3196 | return pMe->uLastError; |
| 3197 | } |
| 3198 | |
| 3199 | QCBORError uError = QCBOR_SUCCESS; |
| 3200 | |
| 3201 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3202 | uError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3203 | goto Done;; |
| 3204 | } |
| 3205 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3206 | const TagSpecification TagSpec = |
| 3207 | { |
| 3208 | uTagRequirement, |
| 3209 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3210 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3211 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3212 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3213 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3214 | if(uError != QCBOR_SUCCESS) { |
| 3215 | goto Done; |
| 3216 | } |
| 3217 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3218 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3219 | // Reverse the decrement done by GetNext() for the bstr so the |
| 3220 | // increment in NestLevelAscender() called by ExitBoundedLevel() |
| 3221 | // will work right. |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3222 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3223 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3224 | |
| 3225 | if(pBstr) { |
| 3226 | *pBstr = pItem->val.string; |
| 3227 | } |
| 3228 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3229 | // This saves the current length of the UsefulInputBuf and then |
| 3230 | // narrows the UsefulInputBuf to start and length of the wrapped |
| 3231 | // CBOR that is being entered. |
| 3232 | // |
| 3233 | // This makes sure the length is less than |
| 3234 | // QCBOR_MAX_DECODE_INPUT_SIZE which is slighly less than |
| 3235 | // UINT32_MAX. The value UINT32_MAX is used as a special indicator |
| 3236 | // value. The checks against QCBOR_MAX_DECODE_INPUT_SIZE also make |
| 3237 | // the casts safe. uEndOfBstr will always be less than |
| 3238 | // uPreviousLength because of the way UsefulInputBuf works so there |
| 3239 | // is no need to check it. There is also a range check in the |
| 3240 | // seek. |
| 3241 | // |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3242 | // Most of these calls are simple inline accessors so this doesn't |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3243 | // amount to much code. |
| 3244 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 3245 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3246 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3247 | goto Done; |
| 3248 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3249 | const size_t uEndOfBstr = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3250 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOfBstr - pItem->val.string.len); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3251 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3252 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3253 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3254 | (uint32_t)uPreviousLength, |
| 3255 | (uint32_t)uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3256 | Done: |
| 3257 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3261 | /* |
| 3262 | Public function, see header qcbor/qcbor_decode.h file |
| 3263 | */ |
| 3264 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3265 | uint8_t uTagRequirement, |
| 3266 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3267 | { |
| 3268 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3269 | // Already in error state; do nothing. |
| 3270 | return; |
| 3271 | } |
| 3272 | |
| 3273 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3274 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3275 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3276 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3277 | return; |
| 3278 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3279 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3280 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3281 | &Item, |
| 3282 | uTagRequirement, |
| 3283 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3287 | /* |
| 3288 | Public function, see header qcbor/qcbor_decode.h file |
| 3289 | */ |
| 3290 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3291 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3292 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3293 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3294 | { |
| 3295 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3296 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3297 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3298 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3299 | &Item, |
| 3300 | uTagRequirement, |
| 3301 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3302 | } |
| 3303 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3304 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3305 | /* |
| 3306 | Public function, see header qcbor/qcbor_decode.h file |
| 3307 | */ |
| 3308 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3309 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3310 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3311 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3312 | { |
| 3313 | QCBORItem Item; |
| 3314 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3315 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3316 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3317 | &Item, |
| 3318 | uTagRequirement, |
| 3319 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3320 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3321 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3322 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3323 | /* |
| 3324 | Public function, see header qcbor/qcbor_decode.h file |
| 3325 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3326 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3327 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3328 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3329 | // Already in error state; do nothing. |
| 3330 | return; |
| 3331 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3332 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3333 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3334 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3335 | return; |
| 3336 | } |
| 3337 | |
| 3338 | /* |
| 3339 | Reset the length of the UsefulInputBuf to what it was before |
| 3340 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3341 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3342 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3343 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3344 | |
| 3345 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3346 | QCBORError uErr = ExitBoundedLevel(pMe, DecodeNesting_GetEndOfBstr(&(pMe->nesting))); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3347 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3350 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3351 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3352 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3353 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3354 | |
Laurence Lundblade | 11a064e | 2020-05-07 13:13:42 -0700 | [diff] [blame] | 3355 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3356 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3357 | static QCBORError |
| 3358 | InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3359 | { |
| 3360 | switch(pItem->uDataType) { |
| 3361 | case QCBOR_TYPE_TRUE: |
| 3362 | *pBool = true; |
| 3363 | return QCBOR_SUCCESS; |
| 3364 | break; |
| 3365 | |
| 3366 | case QCBOR_TYPE_FALSE: |
| 3367 | *pBool = false; |
| 3368 | return QCBOR_SUCCESS; |
| 3369 | break; |
| 3370 | |
| 3371 | default: |
| 3372 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3373 | break; |
| 3374 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3375 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3376 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3377 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3378 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3379 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3380 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3381 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3382 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3383 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3384 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3385 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3386 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3387 | return; |
| 3388 | } |
| 3389 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3390 | QCBORError nError; |
| 3391 | QCBORItem Item; |
| 3392 | |
| 3393 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3394 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3395 | pMe->uLastError = (uint8_t)nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3396 | return; |
| 3397 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3398 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3399 | } |
| 3400 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3401 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3402 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3403 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3404 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3405 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3406 | { |
| 3407 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3408 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3409 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3410 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3413 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3414 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3415 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3416 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3417 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3418 | { |
| 3419 | QCBORItem Item; |
| 3420 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3421 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3422 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | |
| 3426 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3427 | |
| 3428 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3429 | QCBORItem *pItem, |
| 3430 | uint8_t uTagRequirement, |
| 3431 | int64_t *pnTime) |
| 3432 | { |
| 3433 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3434 | // Already in error state, do nothing |
| 3435 | return; |
| 3436 | } |
| 3437 | |
| 3438 | QCBORError uErr; |
| 3439 | |
| 3440 | const TagSpecification TagSpec = |
| 3441 | { |
| 3442 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3443 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3444 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3445 | }; |
| 3446 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3447 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3448 | if(uErr != QCBOR_SUCCESS) { |
| 3449 | goto Done; |
| 3450 | } |
| 3451 | |
| 3452 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3453 | uErr = DecodeDateEpoch(pItem); |
| 3454 | if(uErr != QCBOR_SUCCESS) { |
| 3455 | goto Done; |
| 3456 | } |
| 3457 | } |
| 3458 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3459 | // Save the tags in the last item's tags in the decode context |
| 3460 | // for QCBORDecode_GetNthTagOfLast() |
| 3461 | CopyTags(pMe, pItem); |
| 3462 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3463 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3464 | |
| 3465 | Done: |
| 3466 | pMe->uLastError = (uint8_t)uErr; |
| 3467 | } |
| 3468 | |
| 3469 | |
| 3470 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
| 3471 | uint8_t uTagRequirement, |
| 3472 | int64_t *pnTime) |
| 3473 | { |
| 3474 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3475 | // Already in error state, do nothing |
| 3476 | return; |
| 3477 | } |
| 3478 | |
| 3479 | QCBORItem Item; |
| 3480 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3481 | |
| 3482 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3483 | } |
| 3484 | |
| 3485 | |
| 3486 | void |
| 3487 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3488 | int64_t nLabel, |
| 3489 | uint8_t uTagRequirement, |
| 3490 | int64_t *pnTime) |
| 3491 | { |
| 3492 | QCBORItem Item; |
| 3493 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3494 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3495 | } |
| 3496 | |
| 3497 | |
| 3498 | void |
| 3499 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3500 | const char *szLabel, |
| 3501 | uint8_t uTagRequirement, |
| 3502 | int64_t *pnTime) |
| 3503 | { |
| 3504 | QCBORItem Item; |
| 3505 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3506 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3507 | } |
| 3508 | |
| 3509 | |
| 3510 | |
| 3511 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3512 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3513 | TagSpecification TagSpec, |
| 3514 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3515 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3516 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3517 | // Already in error state, do nothing |
| 3518 | return; |
| 3519 | } |
| 3520 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3521 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3522 | QCBORItem Item; |
| 3523 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3524 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3525 | if(uError != QCBOR_SUCCESS) { |
| 3526 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3527 | return; |
| 3528 | } |
| 3529 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3530 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3531 | |
| 3532 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3533 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 3534 | } else { |
| 3535 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3539 | |
| 3540 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3541 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3542 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3543 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3544 | UsefulBufC *pValue, |
| 3545 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3546 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3547 | const TagSpecification TagSpec = |
| 3548 | { |
| 3549 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3550 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3551 | {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] | 3552 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3553 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3554 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3555 | if(uErr != QCBOR_SUCCESS) { |
| 3556 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3557 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3558 | |
| 3559 | *pValue = pItem->val.string; |
| 3560 | |
| 3561 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3562 | *pbIsNegative = false; |
| 3563 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3564 | *pbIsNegative = true; |
| 3565 | } |
| 3566 | |
| 3567 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3571 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3572 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3573 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3574 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3575 | uint8_t uTagRequirement, |
| 3576 | UsefulBufC *pValue, |
| 3577 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3578 | { |
| 3579 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3580 | // Already in error state, do nothing |
| 3581 | return; |
| 3582 | } |
| 3583 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3584 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3585 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3586 | if(uError != QCBOR_SUCCESS) { |
| 3587 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3588 | return; |
| 3589 | } |
| 3590 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3591 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3592 | } |
| 3593 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3594 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3595 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3596 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3597 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3598 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3599 | int64_t nLabel, |
| 3600 | uint8_t uTagRequirement, |
| 3601 | UsefulBufC *pValue, |
| 3602 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3603 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3604 | QCBORItem Item; |
| 3605 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3606 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3607 | return; |
| 3608 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3609 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3610 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3611 | } |
| 3612 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3613 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3614 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3615 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3616 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3617 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3618 | const char *szLabel, |
| 3619 | uint8_t uTagRequirement, |
| 3620 | UsefulBufC *pValue, |
| 3621 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3622 | { |
| 3623 | QCBORItem Item; |
| 3624 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3625 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3626 | return; |
| 3627 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3628 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3629 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | |
| 3633 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3634 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3635 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3636 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3637 | const QCBORItem *pItem, |
| 3638 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3639 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3640 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3641 | const TagSpecification TagSpecText = |
| 3642 | { |
| 3643 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3644 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3645 | {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] | 3646 | }; |
| 3647 | const TagSpecification TagSpecBinary = |
| 3648 | { |
| 3649 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3650 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3651 | {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] | 3652 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3653 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3654 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3655 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3656 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3657 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3658 | if(pbIsTag257 != NULL) { |
| 3659 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3660 | } |
| 3661 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3662 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3663 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3664 | if(pbIsTag257 != NULL) { |
| 3665 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3666 | } |
| 3667 | uReturn = QCBOR_SUCCESS; |
| 3668 | |
| 3669 | } else { |
| 3670 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3671 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3672 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3673 | return uReturn; |
| 3674 | } |
| 3675 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3676 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 3677 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3678 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3679 | |
| 3680 | |
| 3681 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3682 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3683 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3684 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3685 | |
| 3686 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3687 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3688 | static QCBORError |
| 3689 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3690 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3691 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3692 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3693 | if(uResult != 0) { |
| 3694 | /* This loop will run a maximum of 19 times because |
| 3695 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 3696 | * exit with the overflow error |
| 3697 | */ |
| 3698 | for(; nExponent > 0; nExponent--) { |
| 3699 | if(uResult > UINT64_MAX / 10) { |
| 3700 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 3701 | } |
| 3702 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3703 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3704 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3705 | for(; nExponent < 0; nExponent++) { |
| 3706 | uResult = uResult / 10; |
| 3707 | if(uResult == 0) { |
| 3708 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3709 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3710 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3711 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3712 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3713 | |
| 3714 | *puResult = uResult; |
| 3715 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3716 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3720 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3721 | static QCBORError |
| 3722 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3723 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3724 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3725 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3726 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3727 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3728 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3729 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3730 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3731 | */ |
| 3732 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3733 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3734 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3735 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3736 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3737 | nExponent--; |
| 3738 | } |
| 3739 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3740 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3741 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3742 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3743 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3744 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3745 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3748 | *puResult = uResult; |
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 | return QCBOR_SUCCESS; |
| 3751 | } |
| 3752 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3753 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3754 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3755 | Compute value with signed mantissa and signed result. Works with |
| 3756 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3757 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3758 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 3759 | int64_t nExponent, |
| 3760 | int64_t *pnResult, |
| 3761 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3762 | { |
| 3763 | uint64_t uResult; |
| 3764 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3765 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3766 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3767 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 3768 | |
| 3769 | // Do the exponentiation of the positive mantissa |
| 3770 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 3771 | if(uReturn) { |
| 3772 | return uReturn; |
| 3773 | } |
| 3774 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3775 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 3776 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 3777 | of INT64_MIN. This assumes two's compliment representation where |
| 3778 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 3779 | Trying to write -INT64_MIN doesn't work to get this because the |
| 3780 | compiler tries to work with an int64_t which can't represent |
| 3781 | -INT64_MIN. |
| 3782 | */ |
| 3783 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 3784 | |
| 3785 | // Error out if too large |
| 3786 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3787 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 3788 | } |
| 3789 | |
| 3790 | // Casts are safe because of checks above |
| 3791 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 3792 | |
| 3793 | return QCBOR_SUCCESS; |
| 3794 | } |
| 3795 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3796 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3797 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3798 | Compute value with signed mantissa and unsigned result. Works with |
| 3799 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3800 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3801 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 3802 | int64_t nExponent, |
| 3803 | uint64_t *puResult, |
| 3804 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3805 | { |
| 3806 | if(nMantissa < 0) { |
| 3807 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 3808 | } |
| 3809 | |
| 3810 | // Cast to unsigned is OK because of check for negative |
| 3811 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 3812 | // Exponentiation is straight forward |
| 3813 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 3814 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3815 | |
| 3816 | |
| 3817 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3818 | Compute value with signed mantissa and unsigned result. Works with |
| 3819 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3820 | */ |
| 3821 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 3822 | int64_t nExponent, |
| 3823 | uint64_t *puResult, |
| 3824 | fExponentiator pfExp) |
| 3825 | { |
| 3826 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 3827 | } |
| 3828 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3829 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 3830 | |
| 3831 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3832 | |
| 3833 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3834 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3835 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3836 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3837 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3838 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3839 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3840 | const uint8_t *pByte = BigNum.ptr; |
| 3841 | size_t uLen = BigNum.len; |
| 3842 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 3843 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3844 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3845 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 3846 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3847 | } |
| 3848 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3849 | *pResult = uResult; |
| 3850 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3851 | } |
| 3852 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3853 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 3854 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3855 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3856 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3857 | } |
| 3858 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3859 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3860 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3861 | { |
| 3862 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3863 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 3864 | if(uError) { |
| 3865 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3866 | } |
| 3867 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 3868 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3869 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3873 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3874 | { |
| 3875 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3876 | /* The negative integer furthest from zero for a C int64_t is |
| 3877 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 3878 | negative number in CBOR is computed as -n - 1 where n is the |
| 3879 | encoded integer, where n is what is in the variable BigNum. When |
| 3880 | converting BigNum to a uint64_t, the maximum value is thus |
| 3881 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 3882 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3883 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3884 | -n - 1 <= INT64_MIN. |
| 3885 | -n - 1 <= -INT64_MAX - 1 |
| 3886 | n <= INT64_MAX. |
| 3887 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3888 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3889 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3890 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3891 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3892 | |
| 3893 | /// Now apply -n - 1. The cast is safe because |
| 3894 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 3895 | // is the largest positive integer that an int64_t can |
| 3896 | // represent. */ |
| 3897 | *pnResult = -(int64_t)uResult - 1; |
| 3898 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3899 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3902 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3903 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3904 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3905 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3906 | /* |
| 3907 | Convert a integers and floats to an int64_t. |
| 3908 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3909 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3910 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3911 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 3912 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3913 | |
| 3914 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 3915 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3916 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 3917 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3918 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3919 | static QCBORError |
| 3920 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3921 | { |
| 3922 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3923 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3924 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3925 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3926 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3927 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 3928 | http://www.cplusplus.com/reference/cmath/llround/ |
| 3929 | */ |
| 3930 | // Not interested in FE_INEXACT |
| 3931 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3932 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 3933 | *pnValue = llround(pItem->val.dfnum); |
| 3934 | } else { |
| 3935 | *pnValue = lroundf(pItem->val.fnum); |
| 3936 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3937 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 3938 | // llround() shouldn't result in divide by zero, but catch |
| 3939 | // it here in case it unexpectedly does. Don't try to |
| 3940 | // distinguish between the various exceptions because it seems |
| 3941 | // they vary by CPU, compiler and OS. |
| 3942 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3943 | } |
| 3944 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3945 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3946 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3947 | #else |
| 3948 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 3949 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3950 | break; |
| 3951 | |
| 3952 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3953 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3954 | *pnValue = pItem->val.int64; |
| 3955 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3956 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3957 | } |
| 3958 | break; |
| 3959 | |
| 3960 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3961 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3962 | if(pItem->val.uint64 < INT64_MAX) { |
| 3963 | *pnValue = pItem->val.int64; |
| 3964 | } else { |
| 3965 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 3966 | } |
| 3967 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3968 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3969 | } |
| 3970 | break; |
| 3971 | |
| 3972 | default: |
| 3973 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3974 | } |
| 3975 | return QCBOR_SUCCESS; |
| 3976 | } |
| 3977 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3978 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3979 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3980 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3981 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3982 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3983 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3984 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3985 | return; |
| 3986 | } |
| 3987 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3988 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3989 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3990 | if(uError) { |
| 3991 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3992 | return; |
| 3993 | } |
| 3994 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3995 | if(pItem) { |
| 3996 | *pItem = Item; |
| 3997 | } |
| 3998 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3999 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4000 | } |
| 4001 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4002 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4003 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4004 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4005 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4006 | int64_t *pnValue, |
| 4007 | QCBORItem *pItem) |
| 4008 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4009 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4010 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4011 | return; |
| 4012 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4013 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4014 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4017 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4018 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4019 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4020 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4021 | int64_t *pnValue, |
| 4022 | QCBORItem *pItem) |
| 4023 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4024 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4025 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4026 | return; |
| 4027 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4028 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4029 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4033 | /* |
| 4034 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4035 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4036 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4037 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4038 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4039 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4040 | |
| 4041 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4042 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4043 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4044 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4045 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4046 | static QCBORError |
| 4047 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4048 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4049 | switch(pItem->uDataType) { |
| 4050 | |
| 4051 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4052 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4053 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4054 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4055 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4056 | } |
| 4057 | break; |
| 4058 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4059 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4060 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4061 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4062 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4063 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4064 | } |
| 4065 | break; |
| 4066 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4067 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4068 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4069 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4070 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4071 | pItem->val.expAndMantissa.nExponent, |
| 4072 | pnValue, |
| 4073 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4074 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4075 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4076 | } |
| 4077 | break; |
| 4078 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4079 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4080 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4081 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4082 | pItem->val.expAndMantissa.nExponent, |
| 4083 | pnValue, |
| 4084 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4085 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4086 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4087 | } |
| 4088 | break; |
| 4089 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4090 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4091 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4092 | int64_t nMantissa; |
| 4093 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4094 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4095 | if(uErr) { |
| 4096 | return uErr; |
| 4097 | } |
| 4098 | return ExponentiateNN(nMantissa, |
| 4099 | pItem->val.expAndMantissa.nExponent, |
| 4100 | pnValue, |
| 4101 | Exponentitate10); |
| 4102 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4103 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4104 | } |
| 4105 | break; |
| 4106 | |
| 4107 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4108 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4109 | int64_t nMantissa; |
| 4110 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4111 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4112 | if(uErr) { |
| 4113 | return uErr; |
| 4114 | } |
| 4115 | return ExponentiateNN(nMantissa, |
| 4116 | pItem->val.expAndMantissa.nExponent, |
| 4117 | pnValue, |
| 4118 | Exponentitate10); |
| 4119 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4120 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4121 | } |
| 4122 | break; |
| 4123 | |
| 4124 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4125 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4126 | int64_t nMantissa; |
| 4127 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4128 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4129 | if(uErr) { |
| 4130 | return uErr; |
| 4131 | } |
| 4132 | return ExponentiateNN(nMantissa, |
| 4133 | pItem->val.expAndMantissa.nExponent, |
| 4134 | pnValue, |
| 4135 | Exponentitate2); |
| 4136 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4137 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4138 | } |
| 4139 | break; |
| 4140 | |
| 4141 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4142 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4143 | int64_t nMantissa; |
| 4144 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4145 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4146 | if(uErr) { |
| 4147 | return uErr; |
| 4148 | } |
| 4149 | return ExponentiateNN(nMantissa, |
| 4150 | pItem->val.expAndMantissa.nExponent, |
| 4151 | pnValue, |
| 4152 | Exponentitate2); |
| 4153 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4154 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4155 | } |
| 4156 | break; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4157 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4158 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4159 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4160 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4161 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4162 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4163 | |
| 4164 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4165 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4166 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4167 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4168 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4169 | { |
| 4170 | QCBORItem Item; |
| 4171 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4172 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4173 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4174 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4175 | // The above conversion succeeded |
| 4176 | return; |
| 4177 | } |
| 4178 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4179 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4180 | // 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] | 4181 | return; |
| 4182 | } |
| 4183 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4184 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4185 | } |
| 4186 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4187 | |
| 4188 | /* |
| 4189 | Public function, see header qcbor/qcbor_decode.h file |
| 4190 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4191 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4192 | int64_t nLabel, |
| 4193 | uint32_t uConvertTypes, |
| 4194 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4195 | { |
| 4196 | QCBORItem Item; |
| 4197 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4198 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4199 | nLabel, |
| 4200 | uConvertTypes, |
| 4201 | pnValue, |
| 4202 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4203 | |
| 4204 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4205 | // The above conversion succeeded |
| 4206 | return; |
| 4207 | } |
| 4208 | |
| 4209 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4210 | // The above conversion failed in a way that code below can't correct |
| 4211 | return; |
| 4212 | } |
| 4213 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4214 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4218 | /* |
| 4219 | Public function, see header qcbor/qcbor_decode.h file |
| 4220 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4221 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4222 | const char *szLabel, |
| 4223 | uint32_t uConvertTypes, |
| 4224 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4225 | { |
| 4226 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4227 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4228 | szLabel, |
| 4229 | uConvertTypes, |
| 4230 | pnValue, |
| 4231 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4232 | |
| 4233 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4234 | // The above conversion succeeded |
| 4235 | return; |
| 4236 | } |
| 4237 | |
| 4238 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4239 | // The above conversion failed in a way that code below can't correct |
| 4240 | return; |
| 4241 | } |
| 4242 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4243 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4247 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4248 | { |
| 4249 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4250 | case QCBOR_TYPE_DOUBLE: |
| 4251 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4252 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4253 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4254 | // Can't use llround here because it will not convert values |
| 4255 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4256 | // need to be converted so it is more complicated. |
| 4257 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4258 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4259 | if(isnan(pItem->val.dfnum)) { |
| 4260 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4261 | } else if(pItem->val.dfnum < 0) { |
| 4262 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4263 | } else { |
| 4264 | double dRounded = round(pItem->val.dfnum); |
| 4265 | // See discussion in DecodeDateEpoch() for |
| 4266 | // explanation of - 0x7ff |
| 4267 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4268 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4269 | } |
| 4270 | *puValue = (uint64_t)dRounded; |
| 4271 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4272 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4273 | if(isnan(pItem->val.fnum)) { |
| 4274 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4275 | } else if(pItem->val.fnum < 0) { |
| 4276 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4277 | } else { |
| 4278 | float fRounded = roundf(pItem->val.fnum); |
| 4279 | // See discussion in DecodeDateEpoch() for |
| 4280 | // explanation of - 0x7ff |
| 4281 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4282 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4283 | } |
| 4284 | *puValue = (uint64_t)fRounded; |
| 4285 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4286 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4287 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4288 | // round() and roundf() shouldn't result in exceptions here, but |
| 4289 | // catch them to be robust and thorough. Don't try to |
| 4290 | // distinguish between the various exceptions because it seems |
| 4291 | // they vary by CPU, compiler and OS. |
| 4292 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4293 | } |
| 4294 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4295 | } else { |
| 4296 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4297 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4298 | #else |
| 4299 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4300 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4301 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4302 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4303 | case QCBOR_TYPE_INT64: |
| 4304 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4305 | if(pItem->val.int64 >= 0) { |
| 4306 | *puValue = (uint64_t)pItem->val.int64; |
| 4307 | } else { |
| 4308 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4309 | } |
| 4310 | } else { |
| 4311 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4312 | } |
| 4313 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4314 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4315 | case QCBOR_TYPE_UINT64: |
| 4316 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4317 | *puValue = pItem->val.uint64; |
| 4318 | } else { |
| 4319 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4320 | } |
| 4321 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4322 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4323 | default: |
| 4324 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4325 | } |
| 4326 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4327 | return QCBOR_SUCCESS; |
| 4328 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4329 | |
| 4330 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4331 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4332 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4333 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4334 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4335 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4336 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4337 | return; |
| 4338 | } |
| 4339 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4340 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4341 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4342 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4343 | if(uError) { |
| 4344 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4345 | return; |
| 4346 | } |
| 4347 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4348 | if(pItem) { |
| 4349 | *pItem = Item; |
| 4350 | } |
| 4351 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4352 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4353 | } |
| 4354 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4355 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4356 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4357 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4358 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4359 | uint64_t *puValue, |
| 4360 | QCBORItem *pItem) |
| 4361 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4362 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4363 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4364 | return; |
| 4365 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4366 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4367 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4371 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4372 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4373 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4374 | uint64_t *puValue, |
| 4375 | QCBORItem *pItem) |
| 4376 | { |
| 4377 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4378 | return; |
| 4379 | } |
| 4380 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4381 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4382 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4383 | return; |
| 4384 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4385 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4386 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4387 | } |
| 4388 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4389 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4390 | /* |
| 4391 | Public function, see header qcbor/qcbor_decode.h file |
| 4392 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4393 | static QCBORError |
| 4394 | UInt64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4395 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4396 | switch(pItem->uDataType) { |
| 4397 | |
| 4398 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4399 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4400 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4401 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4402 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4403 | } |
| 4404 | break; |
| 4405 | |
| 4406 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4407 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4408 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4409 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4410 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4411 | } |
| 4412 | break; |
| 4413 | |
| 4414 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4415 | |
| 4416 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4417 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4418 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4419 | pItem->val.expAndMantissa.nExponent, |
| 4420 | puValue, |
| 4421 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4422 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4423 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4424 | } |
| 4425 | break; |
| 4426 | |
| 4427 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4428 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4429 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4430 | pItem->val.expAndMantissa.nExponent, |
| 4431 | puValue, |
| 4432 | Exponentitate2); |
| 4433 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4434 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4435 | } |
| 4436 | break; |
| 4437 | |
| 4438 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4439 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4440 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4441 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4442 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4443 | if(uErr != QCBOR_SUCCESS) { |
| 4444 | return uErr; |
| 4445 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4446 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4447 | pItem->val.expAndMantissa.nExponent, |
| 4448 | puValue, |
| 4449 | Exponentitate10); |
| 4450 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4451 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4452 | } |
| 4453 | break; |
| 4454 | |
| 4455 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4456 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4457 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4458 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4459 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4460 | } |
| 4461 | break; |
| 4462 | |
| 4463 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4464 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4465 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4466 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4467 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4468 | if(uErr != QCBOR_SUCCESS) { |
| 4469 | return uErr; |
| 4470 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4471 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4472 | pItem->val.expAndMantissa.nExponent, |
| 4473 | puValue, |
| 4474 | Exponentitate2); |
| 4475 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4476 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4477 | } |
| 4478 | break; |
| 4479 | |
| 4480 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
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 QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4483 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4484 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4485 | } |
| 4486 | break; |
| 4487 | #endif |
| 4488 | default: |
| 4489 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4490 | } |
| 4491 | } |
| 4492 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4493 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4494 | /* |
| 4495 | Public function, see header qcbor/qcbor_decode.h file |
| 4496 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4497 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4498 | { |
| 4499 | QCBORItem Item; |
| 4500 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4501 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4502 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4503 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4504 | // The above conversion succeeded |
| 4505 | return; |
| 4506 | } |
| 4507 | |
| 4508 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4509 | // 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] | 4510 | return; |
| 4511 | } |
| 4512 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4513 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4514 | } |
| 4515 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4516 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4517 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4518 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4519 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4520 | void QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4521 | int64_t nLabel, |
| 4522 | uint32_t uConvertTypes, |
| 4523 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4524 | { |
| 4525 | QCBORItem Item; |
| 4526 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4527 | QCBORDecode_GetUInt64ConvertInternalInMapN(pMe, |
| 4528 | nLabel, |
| 4529 | uConvertTypes, |
| 4530 | puValue, |
| 4531 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4532 | |
| 4533 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4534 | // The above conversion succeeded |
| 4535 | return; |
| 4536 | } |
| 4537 | |
| 4538 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4539 | // The above conversion failed in a way that code below can't correct |
| 4540 | return; |
| 4541 | } |
| 4542 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4543 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | |
| 4547 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4548 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4549 | */ |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4550 | void QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4551 | const char *szLabel, |
| 4552 | uint32_t uConvertTypes, |
| 4553 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4554 | { |
| 4555 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4556 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, |
| 4557 | szLabel, |
| 4558 | uConvertTypes, |
| 4559 | puValue, |
| 4560 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4561 | |
| 4562 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4563 | // The above conversion succeeded |
| 4564 | return; |
| 4565 | } |
| 4566 | |
| 4567 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4568 | // The above conversion failed in a way that code below can't correct |
| 4569 | return; |
| 4570 | } |
| 4571 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4572 | pMe->uLastError = (uint8_t)UInt64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4576 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4577 | uint32_t uConvertTypes, |
| 4578 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4579 | { |
| 4580 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4581 | case QCBOR_TYPE_FLOAT: |
| 4582 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4583 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4584 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4585 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4586 | *pdValue = (double)pItem->val.fnum; |
| 4587 | } else { |
| 4588 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4589 | } |
| 4590 | } |
| 4591 | #else |
| 4592 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4593 | #endif |
| 4594 | break; |
| 4595 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4596 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4597 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4598 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4599 | *pdValue = pItem->val.dfnum; |
| 4600 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4601 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4602 | } |
| 4603 | } |
| 4604 | break; |
| 4605 | |
| 4606 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4607 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4608 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4609 | // A simple cast seems to do the job with no worry of exceptions. |
| 4610 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4611 | *pdValue = (double)pItem->val.int64; |
| 4612 | |
| 4613 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4614 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4615 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4616 | #else |
| 4617 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4618 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4619 | break; |
| 4620 | |
| 4621 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4622 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4623 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4624 | // A simple cast seems to do the job with no worry of exceptions. |
| 4625 | // There will be precision loss for some values. |
| 4626 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4627 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4628 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4629 | } |
| 4630 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4631 | #else |
| 4632 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4633 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4634 | |
| 4635 | default: |
| 4636 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4637 | } |
| 4638 | |
| 4639 | return QCBOR_SUCCESS; |
| 4640 | } |
| 4641 | |
| 4642 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4643 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4644 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4645 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4646 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4647 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4648 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4649 | return; |
| 4650 | } |
| 4651 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4652 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4653 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4654 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4655 | if(uError) { |
| 4656 | pMe->uLastError = (uint8_t)uError; |
| 4657 | return; |
| 4658 | } |
| 4659 | |
| 4660 | if(pItem) { |
| 4661 | *pItem = Item; |
| 4662 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4663 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4664 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4665 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4666 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4667 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4668 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4669 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4670 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4671 | double *pdValue, |
| 4672 | QCBORItem *pItem) |
| 4673 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4674 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4675 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4676 | return; |
| 4677 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4678 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4679 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4682 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4683 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4684 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4685 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4686 | double *pdValue, |
| 4687 | QCBORItem *pItem) |
| 4688 | { |
| 4689 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4690 | return; |
| 4691 | } |
| 4692 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4693 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4694 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4695 | return; |
| 4696 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4697 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4698 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4702 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4703 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 4704 | { |
| 4705 | double dResult; |
| 4706 | |
| 4707 | dResult = 0.0; |
| 4708 | const uint8_t *pByte = BigNum.ptr; |
| 4709 | size_t uLen = BigNum.len; |
| 4710 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 4711 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4712 | while(uLen--) { |
| 4713 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 4714 | } |
| 4715 | |
| 4716 | return dResult; |
| 4717 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4718 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4719 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4720 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4721 | static QCBORError |
| 4722 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4723 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4724 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4725 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4726 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 4727 | |
| 4728 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4729 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4730 | |
| 4731 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4732 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4733 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4734 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4735 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4736 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 4737 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4738 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4739 | } |
| 4740 | break; |
| 4741 | |
| 4742 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4743 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4744 | // Underflow gives 0, overflow gives infinity |
| 4745 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4746 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4747 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4748 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4749 | } |
| 4750 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4751 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4752 | |
| 4753 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4754 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4755 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 4756 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4757 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4758 | } |
| 4759 | break; |
| 4760 | |
| 4761 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4762 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4763 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4764 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4765 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4766 | } |
| 4767 | break; |
| 4768 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4769 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4770 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4771 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4772 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4773 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4774 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4775 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4776 | } |
| 4777 | break; |
| 4778 | |
| 4779 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4780 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4781 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4782 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4783 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4784 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4785 | } |
| 4786 | break; |
| 4787 | |
| 4788 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4789 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4790 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4791 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4792 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4793 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4794 | } |
| 4795 | break; |
| 4796 | |
| 4797 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4798 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4799 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4800 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4801 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4802 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4803 | } |
| 4804 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4805 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4806 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4807 | default: |
| 4808 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4809 | } |
| 4810 | |
| 4811 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4812 | |
| 4813 | #else |
| 4814 | (void)pItem; |
| 4815 | (void)uConvertTypes; |
| 4816 | (void)pdValue; |
| 4817 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4818 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4819 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | |
| 4823 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4824 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4825 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4826 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 4827 | uint32_t uConvertTypes, |
| 4828 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4829 | { |
| 4830 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4831 | QCBORItem Item; |
| 4832 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4833 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4834 | |
| 4835 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4836 | // The above conversion succeeded |
| 4837 | return; |
| 4838 | } |
| 4839 | |
| 4840 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4841 | // The above conversion failed in a way that code below can't correct |
| 4842 | return; |
| 4843 | } |
| 4844 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4845 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4846 | } |
| 4847 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4848 | |
| 4849 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4850 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4851 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4852 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4853 | int64_t nLabel, |
| 4854 | uint32_t uConvertTypes, |
| 4855 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4856 | { |
| 4857 | QCBORItem Item; |
| 4858 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4859 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4860 | |
| 4861 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4862 | // The above conversion succeeded |
| 4863 | return; |
| 4864 | } |
| 4865 | |
| 4866 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4867 | // The above conversion failed in a way that code below can't correct |
| 4868 | return; |
| 4869 | } |
| 4870 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4871 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | |
| 4875 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4876 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4877 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4878 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4879 | const char *szLabel, |
| 4880 | uint32_t uConvertTypes, |
| 4881 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4882 | { |
| 4883 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4884 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4885 | |
| 4886 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4887 | // The above conversion succeeded |
| 4888 | return; |
| 4889 | } |
| 4890 | |
| 4891 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4892 | // The above conversion failed in a way that code below can't correct |
| 4893 | return; |
| 4894 | } |
| 4895 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4896 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4897 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4898 | |
| 4899 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4900 | |
| 4901 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4902 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4903 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 4904 | { |
| 4905 | while((uInt & 0xff00000000000000UL) == 0) { |
| 4906 | uInt = uInt << 8; |
| 4907 | }; |
| 4908 | |
| 4909 | UsefulOutBuf UOB; |
| 4910 | |
| 4911 | UsefulOutBuf_Init(&UOB, Buffer); |
| 4912 | |
| 4913 | while(uInt) { |
| 4914 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 4915 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 4916 | uInt = uInt << 8; |
| 4917 | (void)xx; |
| 4918 | } |
| 4919 | |
| 4920 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4921 | } |
| 4922 | |
| 4923 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4924 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 4925 | TagSpecification TagSpec, |
| 4926 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4927 | { |
| 4928 | QCBORError uErr; |
| 4929 | // 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] | 4930 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4931 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4932 | if(uErr != QCBOR_SUCCESS) { |
| 4933 | goto Done; |
| 4934 | } |
| 4935 | |
| 4936 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 4937 | break; // Successful exit. Moving on to finish decoding. |
| 4938 | } |
| 4939 | |
| 4940 | // The item is an array, which means an undecoded |
| 4941 | // mantissa and exponent, so decode it. It will then |
| 4942 | // have a different type and exit the loop if. |
| 4943 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 4944 | if(uErr != QCBOR_SUCCESS) { |
| 4945 | goto Done; |
| 4946 | } |
| 4947 | |
| 4948 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4949 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4950 | } |
| 4951 | Done: |
| 4952 | return uErr; |
| 4953 | } |
| 4954 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4955 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4956 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4957 | TagSpecification TagSpec, |
| 4958 | QCBORItem *pItem, |
| 4959 | int64_t *pnMantissa, |
| 4960 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4961 | { |
| 4962 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4963 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4964 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4965 | if(uErr != QCBOR_SUCCESS) { |
| 4966 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4967 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4968 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4969 | switch (pItem->uDataType) { |
| 4970 | |
| 4971 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 4972 | case QCBOR_TYPE_BIGFLOAT: |
| 4973 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 4974 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 4975 | break; |
| 4976 | |
| 4977 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 4978 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 4979 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 4980 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 4981 | break; |
| 4982 | |
| 4983 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 4984 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 4985 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 4986 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 4987 | break; |
| 4988 | |
| 4989 | default: |
| 4990 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4991 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4992 | |
| 4993 | Done: |
| 4994 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4995 | } |
| 4996 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4997 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4998 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4999 | TagSpecification TagSpec, |
| 5000 | QCBORItem *pItem, |
| 5001 | UsefulBuf BufferForMantissa, |
| 5002 | UsefulBufC *pMantissa, |
| 5003 | bool *pbIsNegative, |
| 5004 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5005 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5006 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5007 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5008 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5009 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5010 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | uint64_t uMantissa; |
| 5014 | |
| 5015 | switch (pItem->uDataType) { |
| 5016 | |
| 5017 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5018 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5019 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5020 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5021 | *pbIsNegative = false; |
| 5022 | } else { |
| 5023 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5024 | *pbIsNegative = true; |
| 5025 | } |
| 5026 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5027 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5028 | break; |
| 5029 | |
| 5030 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5031 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5032 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5033 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5034 | *pbIsNegative = false; |
| 5035 | break; |
| 5036 | |
| 5037 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5038 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5039 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5040 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5041 | *pbIsNegative = true; |
| 5042 | break; |
| 5043 | |
| 5044 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5045 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5046 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5047 | |
| 5048 | Done: |
| 5049 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5053 | /* |
| 5054 | Public function, see header qcbor/qcbor_decode.h file |
| 5055 | */ |
| 5056 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5057 | uint8_t uTagRequirement, |
| 5058 | int64_t *pnMantissa, |
| 5059 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5060 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5061 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5062 | return; |
| 5063 | } |
| 5064 | |
| 5065 | QCBORItem Item; |
| 5066 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5067 | if(uError) { |
| 5068 | pMe->uLastError = (uint8_t)uError; |
| 5069 | return; |
| 5070 | } |
| 5071 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5072 | const TagSpecification TagSpec = |
| 5073 | { |
| 5074 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5075 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5076 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5077 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5078 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5079 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5080 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5081 | } |
| 5082 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5083 | |
| 5084 | /* |
| 5085 | Public function, see header qcbor/qcbor_decode.h file |
| 5086 | */ |
| 5087 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5088 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5089 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5090 | int64_t *pnMantissa, |
| 5091 | int64_t *pnExponent) |
| 5092 | { |
| 5093 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5094 | return; |
| 5095 | } |
| 5096 | |
| 5097 | QCBORItem Item; |
| 5098 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5099 | |
| 5100 | const TagSpecification TagSpec = |
| 5101 | { |
| 5102 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5103 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5104 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5105 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5106 | }; |
| 5107 | |
| 5108 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5109 | } |
| 5110 | |
| 5111 | |
| 5112 | /* |
| 5113 | Public function, see header qcbor/qcbor_decode.h file |
| 5114 | */ |
| 5115 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5116 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5117 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5118 | int64_t *pnMantissa, |
| 5119 | int64_t *pnExponent) |
| 5120 | { |
| 5121 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5122 | return; |
| 5123 | } |
| 5124 | |
| 5125 | QCBORItem Item; |
| 5126 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5127 | |
| 5128 | const TagSpecification TagSpec = |
| 5129 | { |
| 5130 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5131 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5132 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5133 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5134 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5135 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5136 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5137 | } |
| 5138 | |
| 5139 | |
| 5140 | /* |
| 5141 | Public function, see header qcbor/qcbor_decode.h file |
| 5142 | */ |
| 5143 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5144 | uint8_t uTagRequirement, |
| 5145 | UsefulBuf MantissaBuffer, |
| 5146 | UsefulBufC *pMantissa, |
| 5147 | bool *pbMantissaIsNegative, |
| 5148 | int64_t *pnExponent) |
| 5149 | { |
| 5150 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5151 | return; |
| 5152 | } |
| 5153 | |
| 5154 | QCBORItem Item; |
| 5155 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5156 | if(uError) { |
| 5157 | pMe->uLastError = (uint8_t)uError; |
| 5158 | return; |
| 5159 | } |
| 5160 | |
| 5161 | const TagSpecification TagSpec = |
| 5162 | { |
| 5163 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5164 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5165 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5166 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5167 | }; |
| 5168 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5169 | ProcessMantissaAndExponentBig(pMe, |
| 5170 | TagSpec, |
| 5171 | &Item, |
| 5172 | MantissaBuffer, |
| 5173 | pMantissa, |
| 5174 | pbMantissaIsNegative, |
| 5175 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | |
| 5179 | /* |
| 5180 | Public function, see header qcbor/qcbor_decode.h file |
| 5181 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5182 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5183 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5184 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5185 | UsefulBuf BufferForMantissa, |
| 5186 | UsefulBufC *pMantissa, |
| 5187 | bool *pbIsNegative, |
| 5188 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5189 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5190 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5191 | return; |
| 5192 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5193 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5194 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5195 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5196 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5197 | return; |
| 5198 | } |
| 5199 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5200 | const TagSpecification TagSpec = |
| 5201 | { |
| 5202 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5203 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5204 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5205 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5206 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5207 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5208 | ProcessMantissaAndExponentBig(pMe, |
| 5209 | TagSpec, |
| 5210 | &Item, |
| 5211 | BufferForMantissa, |
| 5212 | pMantissa, |
| 5213 | pbIsNegative, |
| 5214 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5215 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5216 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5217 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5218 | /* |
| 5219 | Public function, see header qcbor/qcbor_decode.h file |
| 5220 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5221 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5222 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5223 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5224 | UsefulBuf BufferForMantissa, |
| 5225 | UsefulBufC *pMantissa, |
| 5226 | bool *pbIsNegative, |
| 5227 | int64_t *pnExponent) |
| 5228 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5229 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5230 | return; |
| 5231 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5232 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5233 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5234 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5235 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5236 | return; |
| 5237 | } |
| 5238 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5239 | const TagSpecification TagSpec = |
| 5240 | { |
| 5241 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5242 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5243 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5244 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5245 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5246 | |
| 5247 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5248 | } |
| 5249 | |
| 5250 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5251 | /* |
| 5252 | Public function, see header qcbor/qcbor_decode.h file |
| 5253 | */ |
| 5254 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5255 | uint8_t uTagRequirement, |
| 5256 | int64_t *pnMantissa, |
| 5257 | int64_t *pnExponent) |
| 5258 | { |
| 5259 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5260 | return; |
| 5261 | } |
| 5262 | |
| 5263 | QCBORItem Item; |
| 5264 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5265 | if(uError) { |
| 5266 | pMe->uLastError = (uint8_t)uError; |
| 5267 | return; |
| 5268 | } |
| 5269 | const TagSpecification TagSpec = |
| 5270 | { |
| 5271 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5272 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5273 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5274 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5275 | }; |
| 5276 | |
| 5277 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5278 | } |
| 5279 | |
| 5280 | |
| 5281 | /* |
| 5282 | Public function, see header qcbor/qcbor_decode.h file |
| 5283 | */ |
| 5284 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5285 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5286 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5287 | int64_t *pnMantissa, |
| 5288 | int64_t *pnExponent) |
| 5289 | { |
| 5290 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5291 | return; |
| 5292 | } |
| 5293 | |
| 5294 | QCBORItem Item; |
| 5295 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5296 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5297 | return; |
| 5298 | } |
| 5299 | |
| 5300 | const TagSpecification TagSpec = |
| 5301 | { |
| 5302 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5303 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5304 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5305 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5306 | }; |
| 5307 | |
| 5308 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5309 | } |
| 5310 | |
| 5311 | |
| 5312 | /* |
| 5313 | Public function, see header qcbor/qcbor_decode.h file |
| 5314 | */ |
| 5315 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5316 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5317 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5318 | int64_t *pnMantissa, |
| 5319 | int64_t *pnExponent) |
| 5320 | { |
| 5321 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5322 | return; |
| 5323 | } |
| 5324 | |
| 5325 | QCBORItem Item; |
| 5326 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5327 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5328 | return; |
| 5329 | } |
| 5330 | |
| 5331 | const TagSpecification TagSpec = |
| 5332 | { |
| 5333 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5334 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5335 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5336 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5337 | }; |
| 5338 | |
| 5339 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5340 | } |
| 5341 | |
| 5342 | |
| 5343 | /* |
| 5344 | Public function, see header qcbor/qcbor_decode.h file |
| 5345 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5346 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5347 | uint8_t uTagRequirement, |
| 5348 | UsefulBuf MantissaBuffer, |
| 5349 | UsefulBufC *pMantissa, |
| 5350 | bool *pbMantissaIsNegative, |
| 5351 | int64_t *pnExponent) |
| 5352 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5353 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5354 | return; |
| 5355 | } |
| 5356 | |
| 5357 | QCBORItem Item; |
| 5358 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5359 | if(uError) { |
| 5360 | pMe->uLastError = (uint8_t)uError; |
| 5361 | return; |
| 5362 | } |
| 5363 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5364 | const TagSpecification TagSpec = |
| 5365 | { |
| 5366 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5367 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5368 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5369 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5370 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5371 | |
| 5372 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5373 | } |
| 5374 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5375 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5376 | /* |
| 5377 | Public function, see header qcbor/qcbor_decode.h file |
| 5378 | */ |
| 5379 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5380 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5381 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5382 | UsefulBuf BufferForMantissa, |
| 5383 | UsefulBufC *pMantissa, |
| 5384 | bool *pbIsNegative, |
| 5385 | int64_t *pnExponent) |
| 5386 | { |
| 5387 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5388 | return; |
| 5389 | } |
| 5390 | |
| 5391 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5392 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5393 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5394 | return; |
| 5395 | } |
| 5396 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 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 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5404 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5405 | ProcessMantissaAndExponentBig(pMe, |
| 5406 | TagSpec, |
| 5407 | &Item, |
| 5408 | BufferForMantissa, |
| 5409 | pMantissa, |
| 5410 | pbIsNegative, |
| 5411 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5415 | /* |
| 5416 | Public function, see header qcbor/qcbor_decode.h file |
| 5417 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5418 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5419 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5420 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5421 | UsefulBuf BufferForMantissa, |
| 5422 | UsefulBufC *pMantissa, |
| 5423 | bool *pbIsNegative, |
| 5424 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5425 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5426 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5427 | return; |
| 5428 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5429 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5430 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5431 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5432 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5433 | return; |
| 5434 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5435 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5436 | const TagSpecification TagSpec = |
| 5437 | { |
| 5438 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5439 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5440 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5441 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5442 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5443 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5444 | ProcessMantissaAndExponentBig(pMe, |
| 5445 | TagSpec, |
| 5446 | &Item, |
| 5447 | BufferForMantissa, |
| 5448 | pMantissa, |
| 5449 | pbIsNegative, |
| 5450 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5451 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5452 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5453 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |