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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 54 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 55 | QCBORItem_IsMapOrArray(const QCBORItem *pMe) |
| 56 | { |
| 57 | const uint8_t uDataType = pMe->uDataType; |
| 58 | return uDataType == QCBOR_TYPE_MAP || |
| 59 | uDataType == QCBOR_TYPE_ARRAY || |
| 60 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
| 61 | } |
| 62 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 63 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 64 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 65 | { |
| 66 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if(pMe->val.uCount != 0) { |
| 71 | return false; |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 76 | static inline bool |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 77 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 78 | { |
| 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 95 | See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 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 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 100 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 112 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 142 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 153 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 169 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 180 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 192 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 208 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 214 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 239 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 251 | static inline bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 262 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 277 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 285 | static inline 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 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 293 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 317 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 341 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 403 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 434 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 443 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 592 | static inline QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 666 | static inline QCBORError |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 736 | static inline 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 837 | static inline QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator, |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1147 | // This won't be negative because of code below in GetNext_TaggedItem() |
| 1148 | const unsigned uIndex = uTagVal - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1149 | return me->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1153 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1154 | /* |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1155 | Gets all optional tag data items preceding a data item that is not an |
| 1156 | optional tag and records them as bits in the tag map. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1157 | |
| 1158 | @retval QCBOR_ERR_UNSUPPORTED |
| 1159 | |
| 1160 | @retval QCBOR_ERR_HIT_END |
| 1161 | |
| 1162 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1163 | |
| 1164 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1165 | |
| 1166 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1167 | |
| 1168 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1169 | |
| 1170 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1171 | |
| 1172 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1173 | |
| 1174 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1175 | |
| 1176 | @retval QCBOR_ERR_TOO_MANY_TAGS |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1177 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1178 | static QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1179 | GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1180 | { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1181 | uint16_t auTags[QCBOR_MAX_TAGS_PER_ITEM] = {CBOR_TAG_INVALID16, |
| 1182 | CBOR_TAG_INVALID16, |
| 1183 | CBOR_TAG_INVALID16, |
| 1184 | CBOR_TAG_INVALID16}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1185 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1186 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1187 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1188 | // Loop fetching items until the item fetched is not a tag |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1189 | for(;;) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1190 | QCBORError uErr = GetNext_FullItem(me, pDecodedItem); |
| 1191 | if(uErr != QCBOR_SUCCESS) { |
| 1192 | uReturn = uErr; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1193 | goto Done; // Error out of the loop |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1194 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1195 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1196 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1197 | // Successful exit from loop; maybe got some tags, maybe not |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1198 | memcpy(pDecodedItem->uTags, auTags, sizeof(auTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1199 | break; |
| 1200 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1201 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1202 | if(auTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1203 | // No room in the tag list |
| 1204 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1205 | // Continue on to get all tags on this item even though |
| 1206 | // it is erroring out in the end. This is a resource limit |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 1207 | // error, not a problem with being well-formed CBOR. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1208 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1209 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1210 | // Slide tags over one in the array to make room at index 0 |
| 1211 | for(size_t uTagIndex = QCBOR_MAX_TAGS_PER_ITEM - 1; uTagIndex > 0; uTagIndex--) { |
| 1212 | auTags[uTagIndex] = auTags[uTagIndex-1]; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1213 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1214 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1215 | // Is the tag > 16 bits? |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1216 | if(pDecodedItem->val.uTagV > QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1217 | size_t uTagMapIndex; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1218 | // 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] | 1219 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1220 | if(me->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1221 | break; |
| 1222 | } |
| 1223 | if(me->auMappedTags[uTagMapIndex] == pDecodedItem->val.uTagV) { |
| 1224 | break; |
| 1225 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1226 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1227 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1228 | // No room for the tag |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1229 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1230 | // Continue on to get all tags on this item even though |
| 1231 | // it is erroring out in the end. This is a resource limit |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 1232 | // error, not a problem with being well-formed CBOR. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1233 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1236 | // 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] | 1237 | me->auMappedTags[uTagMapIndex] = pDecodedItem->val.uTagV; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1238 | auTags[0] = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1239 | |
| 1240 | } else { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1241 | auTags[0] = (uint16_t)pDecodedItem->val.uTagV; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1242 | } |
| 1243 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1244 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1245 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1246 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1251 | This layer takes care of map entries. It combines the label and data |
| 1252 | items into one QCBORItem. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1253 | |
| 1254 | @retval QCBOR_ERR_UNSUPPORTED |
| 1255 | |
| 1256 | @retval QCBOR_ERR_HIT_END |
| 1257 | |
| 1258 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1259 | |
| 1260 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1261 | |
| 1262 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1263 | |
| 1264 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1265 | |
| 1266 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1267 | |
| 1268 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1269 | |
| 1270 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1271 | |
| 1272 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1273 | |
| 1274 | @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1275 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1276 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1277 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1278 | static inline QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1279 | GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1280 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1281 | // Stack use: int/ptr 1, QCBORItem -- 56 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1282 | QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1283 | if(nReturn) |
| 1284 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1285 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1286 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1287 | // Break can't be a map entry |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1288 | goto Done; |
| 1289 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1290 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1291 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1292 | // In a map and caller wants maps decoded, not treated as arrays |
| 1293 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1294 | if(DecodeNesting_IsCurrentTypeMap(&(me->nesting))) { |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1295 | // If in a map and the right decoding mode, get the label |
| 1296 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1297 | // Save label in pDecodedItem and get the next which will |
| 1298 | // be the real data |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1299 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1300 | nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1301 | if(QCBORDecode_IsUnrecoverableError(nReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1302 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1303 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1304 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1305 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1306 | |
| 1307 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1308 | // strings are always good labels |
| 1309 | pDecodedItem->label.string = LabelItem.val.string; |
| 1310 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 1311 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1312 | // It's not a string and we only want strings |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1313 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1314 | goto Done; |
| 1315 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1316 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1317 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1318 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1319 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1320 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1321 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1322 | pDecodedItem->label.string = LabelItem.val.string; |
| 1323 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1324 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1325 | } else { |
| 1326 | // label is not an int or a string. It is an arrray |
| 1327 | // or a float or such and this implementation doesn't handle that. |
| 1328 | // Also, tags on labels are ignored. |
| 1329 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1330 | goto Done; |
| 1331 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1332 | } |
| 1333 | } else { |
| 1334 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1335 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1336 | nReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1337 | goto Done; |
| 1338 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1339 | // Decoding a map as an array |
| 1340 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1341 | // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2 |
| 1342 | // Cast is needed because of integer promotion |
| 1343 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1344 | } |
| 1345 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1346 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1347 | Done: |
| 1348 | return nReturn; |
| 1349 | } |
| 1350 | |
| 1351 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1352 | /* |
| 1353 | See if next item is a CBOR break. If it is, it is consumed, |
| 1354 | if not it is not consumed. |
| 1355 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1356 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1357 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1358 | { |
| 1359 | *pbNextIsBreak = false; |
| 1360 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1361 | QCBORItem Peek; |
| 1362 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
| 1363 | QCBORError uReturn = GetNext_Item(pUIB, &Peek, NULL); |
| 1364 | if(uReturn != QCBOR_SUCCESS) { |
| 1365 | return uReturn; |
| 1366 | } |
| 1367 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1368 | // It is not a break, rewind so it can be processed normally. |
| 1369 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1370 | } else { |
| 1371 | *pbNextIsBreak = true; |
| 1372 | } |
| 1373 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1374 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1375 | return QCBOR_SUCCESS; |
| 1376 | } |
| 1377 | |
| 1378 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1379 | /* |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1380 | An item was just consumed, now figure out if it was the |
| 1381 | end of an array or map that can be closed out. That |
| 1382 | may in turn close out another map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1383 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1384 | static QCBORError NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1385 | { |
| 1386 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1387 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1388 | /* This loops ascending nesting levels as long as there is ascending to do */ |
| 1389 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1390 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1391 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1392 | /* Decrement count for definite length maps / arrays */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1393 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1394 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1395 | /* 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] | 1396 | break; |
| 1397 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1398 | /* All of a definite length array was consumed; fall through to |
| 1399 | ascend */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1400 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1401 | } else { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1402 | /* If not definite length, have to check for a CBOR break */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1403 | bool bIsBreak = false; |
| 1404 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1405 | if(uReturn != QCBOR_SUCCESS) { |
| 1406 | goto Done; |
| 1407 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1408 | |
| 1409 | if(!bIsBreak) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1410 | /* 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] | 1411 | break; |
| 1412 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1413 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1414 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1415 | /* |
| 1416 | Break occurred inside a bstr-wrapped CBOR or |
| 1417 | in the top level sequence. This is always an |
| 1418 | error because neither are an indefinte length |
| 1419 | map/array. |
| 1420 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1421 | uReturn = QCBOR_ERR_BAD_BREAK; |
| 1422 | goto Done; |
| 1423 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1424 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1425 | /* It was a break in an indefinite length map / array */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1426 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1427 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1428 | /* All items in the map/array level have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1429 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1430 | /* But ascent in bounded mode is only by explicit call to |
| 1431 | QCBORDecode_ExitBoundedMode() */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1432 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1433 | /* Set the count to zero for definite length arrays to indicate |
| 1434 | cursor is at end of bounded map / array */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1435 | if(bMarkEnd) { |
| 1436 | // Used for definite and indefinite to signal end |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1437 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1438 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1439 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1440 | break; |
| 1441 | } |
| 1442 | |
| 1443 | /* Finally, actually ascend one level. */ |
| 1444 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1445 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1446 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1447 | uReturn = QCBOR_SUCCESS; |
| 1448 | |
| 1449 | Done: |
| 1450 | return uReturn; |
| 1451 | } |
| 1452 | |
| 1453 | |
| 1454 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1455 | This handles the traversal descending into and asecnding out of maps, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1456 | arrays and bstr-wrapped CBOR. It figures out the ends of definite and |
| 1457 | indefinte length maps and arrays by looking at the item count or |
| 1458 | finding CBOR breaks. It detects the ends of the top-level sequence |
| 1459 | and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1460 | |
| 1461 | @retval QCBOR_ERR_UNSUPPORTED X |
| 1462 | |
| 1463 | @retval QCBOR_ERR_HIT_END |
| 1464 | |
| 1465 | @retval QCBOR_ERR_INT_OVERFLOW X |
| 1466 | |
| 1467 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1468 | |
| 1469 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1470 | |
| 1471 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED X |
| 1472 | |
| 1473 | @retval QCBOR_ERR_BAD_TYPE_7 X |
| 1474 | |
| 1475 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1476 | |
| 1477 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1478 | |
| 1479 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1480 | |
| 1481 | @retval QCBOR_ERR_MAP_LABEL_TYPE X |
| 1482 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1483 | @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1484 | |
| 1485 | @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1486 | |
| 1487 | @retval QCBOR_ERR_BAD_BREAK |
| 1488 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1489 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1490 | static QCBORError |
| 1491 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1492 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1493 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1494 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1495 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1496 | /* |
| 1497 | If out of bytes to consume, it is either the end of the top-level |
| 1498 | sequence of some bstr-wrapped CBOR that was entered. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1499 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1500 | In the case of bstr-wrapped CBOR, the length of the UsefulInputBuf |
| 1501 | was set to that of the bstr-wrapped CBOR. When the bstr-wrapped |
| 1502 | CBOR is exited, the length is set back to the top-level's length |
| 1503 | or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1504 | */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1505 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1506 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1507 | goto Done; |
| 1508 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1509 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1510 | /* |
| 1511 | 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] | 1512 | array. The check for the end of an indefinite length array is |
| 1513 | later. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1514 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1515 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1516 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1517 | goto Done; |
| 1518 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1519 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1520 | /* ==== Next: not at the end so get another item ==== */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1521 | uReturn = GetNext_MapEntry(me, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1522 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1523 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1524 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1525 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1526 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | Breaks ending arrays/maps are always processed at the end of this |
| 1529 | function. They should never show up here. |
| 1530 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1531 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1532 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1533 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1534 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1535 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1536 | /* |
| 1537 | Record the nesting level for this data item before processing any |
| 1538 | of decrementing and descending. |
| 1539 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1540 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1541 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1542 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1543 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1544 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1545 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1546 | If the new item is a map or array, descend. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1547 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1548 | Empty indefinite length maps and arrays are descended into, but |
| 1549 | then ascended out of in the next chunk of code. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1550 | |
| 1551 | Maps and arrays do count as items in the map/array that |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1552 | encloses them so a decrement needs to be done for them too, but |
| 1553 | that is done only when all the items in them have been |
| 1554 | processed, not when they are opened with the exception of an |
| 1555 | empty map or array. |
| 1556 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1557 | QCBORError uDescendErr; |
| 1558 | uDescendErr = DecodeNesting_DescendMapOrArray(&(me->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1559 | pDecodedItem->uDataType, |
| 1560 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1561 | if(uDescendErr != QCBOR_SUCCESS) { |
| 1562 | /* This error is probably a traversal error and it |
| 1563 | overrides the non-traversal error. */ |
| 1564 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1565 | goto Done; |
| 1566 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1569 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1570 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1571 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1572 | /* |
| 1573 | The following cases are handled here: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1574 | - A non-aggregate like an integer or string |
| 1575 | - An empty definite length map or array |
| 1576 | - 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] | 1577 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1578 | NestLevelAscender() does the work of decrementing the count for an |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1579 | definite length map/array and break detection for an indefinite |
| 1580 | length map/array. If the end of the map/array was reached, then |
| 1581 | it ascends nesting levels, possibly all the way to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1582 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1583 | QCBORError uAscendErr; |
| 1584 | uAscendErr = NestLevelAscender(me, true); |
| 1585 | if(uAscendErr != QCBOR_SUCCESS) { |
| 1586 | /* This error is probably a traversal error and it |
| 1587 | overrides the non-traversal error. */ |
| 1588 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1589 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1590 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1591 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1592 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1593 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1594 | /* |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1595 | Tell the caller what level is next. This tells them what |
| 1596 | maps/arrays were closed out and makes it possible for them to |
| 1597 | reconstruct the tree with just the information returned in |
| 1598 | a QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1599 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1600 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1601 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1602 | pDecodedItem->uNextNestLevel = 0; |
| 1603 | } else { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1604 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1605 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1606 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1607 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1608 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1611 | static void ShiftTags(QCBORItem *pDecodedItem) |
| 1612 | { |
| 1613 | pDecodedItem->uTags[0] = pDecodedItem->uTags[1]; |
| 1614 | pDecodedItem->uTags[1] = pDecodedItem->uTags[2]; |
| 1615 | pDecodedItem->uTags[2] = pDecodedItem->uTags[3]; |
| 1616 | pDecodedItem->uTags[2] = CBOR_TAG_INVALID16; |
| 1617 | } |
| 1618 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1619 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1620 | /* |
| 1621 | Mostly just assign the right data type for the date string. |
| 1622 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1623 | static inline QCBORError DecodeDateString(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1624 | { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1625 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1626 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1627 | } |
| 1628 | |
| 1629 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1630 | pDecodedItem->val.dateString = Temp; |
| 1631 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1632 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1633 | return QCBOR_SUCCESS; |
| 1634 | } |
| 1635 | |
| 1636 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1637 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1638 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1639 | The epoch formatted date. Turns lots of different forms of encoding |
| 1640 | date into uniform one |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1641 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1642 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1643 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1644 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1645 | |
| 1646 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1647 | |
| 1648 | switch (pDecodedItem->uDataType) { |
| 1649 | |
| 1650 | case QCBOR_TYPE_INT64: |
| 1651 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1652 | break; |
| 1653 | |
| 1654 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1655 | // This only happens for CBOR type 0 > INT64_MAX so it is |
| 1656 | // always an overflow. |
| 1657 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1658 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1659 | break; |
| 1660 | |
| 1661 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1662 | case QCBOR_TYPE_FLOAT: |
| 1663 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1664 | { |
| 1665 | // This comparison needs to be done as a float before |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1666 | // conversion to an int64_t to be able to detect doubles that |
| 1667 | // are too large to fit into an int64_t. A double has 52 |
| 1668 | // bits of preceision. An int64_t has 63. Casting INT64_MAX |
| 1669 | // to a double actually causes a round up which is bad and |
| 1670 | // wrong for the comparison because it will allow conversion |
| 1671 | // of doubles that can't fit into a uint64_t. To remedy this |
| 1672 | // INT64_MAX - 0x7ff is used as the cutoff point because if |
| 1673 | // that value rounds up in conversion to double it will still |
| 1674 | // be less than INT64_MAX. 0x7ff is picked because it has 11 |
| 1675 | // bits set. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1676 | // |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1677 | // INT64_MAX seconds is on the order of 10 billion years, and |
| 1678 | // the earth is less than 5 billion years old, so for most |
| 1679 | // uses this conversion error won't occur even though doubles |
| 1680 | // can go much larger. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1681 | // |
| 1682 | // Without the 0x7ff there is a ~30 minute range of time |
| 1683 | // values 10 billion years in the past and in the future |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1684 | // where this code would go wrong. Some compilers |
| 1685 | // will generate warnings or errors without the 0x7ff |
| 1686 | // because of the precision issue. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1687 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
| 1688 | pDecodedItem->val.dfnum : |
| 1689 | (double)pDecodedItem->val.fnum; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1690 | if(isnan(d) || |
| 1691 | d > (double)(INT64_MAX - 0x7ff) || |
| 1692 | d < (double)(INT64_MIN + 0x7ff)) { |
| 1693 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1694 | goto Done; |
| 1695 | } |
| 1696 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1697 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 1698 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1699 | } |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1700 | #else |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1701 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1702 | uReturn = QCBOR_ERR_FLOAT_DATE_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1703 | goto Done; |
| 1704 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1705 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1706 | break; |
| 1707 | |
| 1708 | default: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1709 | uReturn = QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1710 | goto Done; |
| 1711 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1712 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1713 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1714 | |
| 1715 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1716 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1720 | /* |
| 1721 | Mostly just assign the right data type for the bignum. |
| 1722 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1723 | static inline QCBORError DecodeBigNum(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1724 | { |
| 1725 | // Stack Use: UsefulBuf 1 -- 16 |
| 1726 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1727 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1728 | } |
| 1729 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1730 | pDecodedItem->val.bigNum = Temp; |
| 1731 | const bool bIsPosBigNum = (bool)(pDecodedItem->uTags[0] == CBOR_TAG_POS_BIGNUM); |
| 1732 | pDecodedItem->uDataType = (uint8_t)(bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM |
| 1733 | : QCBOR_TYPE_NEGBIGNUM); |
| 1734 | return QCBOR_SUCCESS; |
| 1735 | } |
| 1736 | |
| 1737 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1738 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1739 | /* |
| 1740 | Decode decimal fractions and big floats. |
| 1741 | |
| 1742 | When called pDecodedItem must be the array that is tagged as a big |
| 1743 | float or decimal fraction, the array that has the two members, the |
| 1744 | exponent and mantissa. |
| 1745 | |
| 1746 | This will fetch and decode the exponent and mantissa and put the |
| 1747 | result back into pDecodedItem. |
| 1748 | */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1749 | static inline QCBORError |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1750 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
| 1751 | { |
| 1752 | QCBORError nReturn; |
| 1753 | |
| 1754 | // --- Make sure it is an array; track nesting level of members --- |
| 1755 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 1756 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1757 | goto Done; |
| 1758 | } |
| 1759 | |
| 1760 | // A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1761 | // definite length arrays, but not for indefnite. Instead remember |
| 1762 | // the nesting level the two integers must be at, which is one |
| 1763 | // deeper than that of the array. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1764 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 1765 | |
| 1766 | // --- Is it a decimal fraction or a bigfloat? --- |
| 1767 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
| 1768 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 1769 | |
| 1770 | // --- Get the exponent --- |
| 1771 | QCBORItem exponentItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1772 | nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1773 | if(nReturn != QCBOR_SUCCESS) { |
| 1774 | goto Done; |
| 1775 | } |
| 1776 | if(exponentItem.uNestingLevel != nNestLevel) { |
| 1777 | // Array is empty or a map/array encountered when expecting an int |
| 1778 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1779 | goto Done; |
| 1780 | } |
| 1781 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
| 1782 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1783 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1784 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1785 | // will be too large for this to handle and thus an error that will |
| 1786 | // get handled in the next else. |
| 1787 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 1788 | } else { |
| 1789 | // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1790 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1791 | goto Done; |
| 1792 | } |
| 1793 | |
| 1794 | // --- Get the mantissa --- |
| 1795 | QCBORItem mantissaItem; |
| 1796 | nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL); |
| 1797 | if(nReturn != QCBOR_SUCCESS) { |
| 1798 | goto Done; |
| 1799 | } |
| 1800 | if(mantissaItem.uNestingLevel != nNestLevel) { |
| 1801 | // Mantissa missing or map/array encountered when expecting number |
| 1802 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1803 | goto Done; |
| 1804 | } |
| 1805 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
| 1806 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1807 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1808 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1809 | // will be too large for this to handle and thus an error that |
| 1810 | // will get handled in an else below. |
| 1811 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1812 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 1813 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1814 | // Got a good big num mantissa |
| 1815 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
| 1816 | // Depends on numbering of QCBOR_TYPE_XXX |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1817 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 1818 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 1819 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1820 | } else { |
| 1821 | // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1822 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1823 | goto Done; |
| 1824 | } |
| 1825 | |
| 1826 | // --- Check that array only has the two numbers --- |
| 1827 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 1828 | // Extra items in the decimal fraction / big float |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1829 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1830 | goto Done; |
| 1831 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 1832 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1833 | |
| 1834 | Done: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1835 | return nReturn; |
| 1836 | } |
| 1837 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1838 | |
| 1839 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1840 | static inline QCBORError DecodeURI(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1841 | { |
| 1842 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1843 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1844 | } |
| 1845 | pDecodedItem->uDataType = QCBOR_TYPE_URI; |
| 1846 | return QCBOR_SUCCESS; |
| 1847 | } |
| 1848 | |
| 1849 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1850 | static inline QCBORError DecodeB64URL(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1851 | { |
| 1852 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1853 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1854 | } |
| 1855 | pDecodedItem->uDataType = QCBOR_TYPE_BASE64URL; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1856 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1857 | return QCBOR_SUCCESS; |
| 1858 | } |
| 1859 | |
| 1860 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1861 | static inline QCBORError DecodeB64(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1862 | { |
| 1863 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1864 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1865 | } |
| 1866 | pDecodedItem->uDataType = QCBOR_TYPE_BASE64; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1867 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1868 | return QCBOR_SUCCESS; |
| 1869 | } |
| 1870 | |
| 1871 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1872 | static inline QCBORError DecodeRegex(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1873 | { |
| 1874 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1875 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1876 | } |
| 1877 | pDecodedItem->uDataType = QCBOR_TYPE_REGEX; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1878 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1879 | return QCBOR_SUCCESS; |
| 1880 | } |
| 1881 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1882 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1883 | static inline QCBORError DecodeWrappedCBOR(QCBORItem *pDecodedItem) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1884 | { |
| 1885 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1886 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1887 | } |
| 1888 | pDecodedItem->uDataType = QBCOR_TYPE_WRAPPED_CBOR; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1889 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1890 | return QCBOR_SUCCESS; |
| 1891 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1892 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1893 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1894 | static inline QCBORError DecodeWrappedCBORSequence(QCBORItem *pDecodedItem) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1895 | { |
| 1896 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1897 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1898 | } |
| 1899 | pDecodedItem->uDataType = QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1900 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1901 | return QCBOR_SUCCESS; |
| 1902 | } |
| 1903 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1904 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1905 | static inline QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1906 | { |
| 1907 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1908 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 1909 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1910 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 1911 | } else { |
| 1912 | return QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1913 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1914 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1915 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1916 | return QCBOR_SUCCESS; |
| 1917 | } |
| 1918 | |
| 1919 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1920 | static inline QCBORError DecodeUUID(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1921 | { |
| 1922 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1923 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1924 | } |
| 1925 | pDecodedItem->uDataType = QCBOR_TYPE_UUID; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1926 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1927 | return QCBOR_SUCCESS; |
| 1928 | } |
| 1929 | |
| 1930 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1931 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1932 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1933 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1934 | static QCBORError |
| 1935 | QCBORDecode_GetNextTag(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1936 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1937 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1938 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1939 | uReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem); |
| 1940 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1941 | goto Done; |
| 1942 | } |
| 1943 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1944 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 1945 | switch(pDecodedItem->uTags[uTagIndex]) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1946 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1947 | // Many of the functions here only just map a CBOR tag to |
| 1948 | // a QCBOR_TYPE for a string and could probably be |
| 1949 | // implemented with less object code. This implementation |
| 1950 | // of string types takes about 120 bytes of object code |
| 1951 | // (that is always linked and not removed by dead stripping). |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1952 | case CBOR_TAG_DATE_STRING: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1953 | uReturn = DecodeDateString(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1954 | break; |
| 1955 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1956 | case CBOR_TAG_DATE_EPOCH: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1957 | uReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1958 | break; |
| 1959 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1960 | case CBOR_TAG_POS_BIGNUM: |
| 1961 | case CBOR_TAG_NEG_BIGNUM: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1962 | uReturn = DecodeBigNum(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1963 | break; |
| 1964 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1965 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1966 | case CBOR_TAG_DECIMAL_FRACTION: |
| 1967 | case CBOR_TAG_BIGFLOAT: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1968 | // For aggregate tagged types, what goes into pTags is only collected |
| 1969 | // from the surrounding data item, not the contents, so pTags is not |
| 1970 | // passed on here. |
| 1971 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1972 | uReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1973 | break; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1974 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1975 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1976 | case CBOR_TAG_CBOR: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1977 | uReturn = DecodeWrappedCBOR(pDecodedItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1978 | break; |
| 1979 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1980 | case CBOR_TAG_CBOR_SEQUENCE: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1981 | uReturn = DecodeWrappedCBORSequence(pDecodedItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1982 | break; |
| 1983 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1984 | case CBOR_TAG_URI: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1985 | uReturn = DecodeURI(pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1986 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1987 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1988 | case CBOR_TAG_B64URL: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1989 | uReturn = DecodeB64URL(pDecodedItem); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1990 | break; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1991 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1992 | case CBOR_TAG_B64: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1993 | uReturn = DecodeB64(pDecodedItem); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1994 | break; |
| 1995 | |
| 1996 | case CBOR_TAG_MIME: |
| 1997 | case CBOR_TAG_BINARY_MIME: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 1998 | uReturn = DecodeMIME(pDecodedItem); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1999 | break; |
| 2000 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2001 | case CBOR_TAG_REGEX: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2002 | uReturn = DecodeRegex(pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2003 | break; |
| 2004 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2005 | case CBOR_TAG_BIN_UUID: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2006 | uReturn = DecodeUUID(pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2007 | break; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2008 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2009 | case CBOR_TAG_INVALID16: |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2010 | // The end of the tag list or no tags |
| 2011 | // Successful exit from the loop. |
| 2012 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2013 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2014 | default: |
| 2015 | // A tag that is not understood |
| 2016 | // A successful exit from the loop |
| 2017 | goto Done; |
| 2018 | |
| 2019 | } |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2020 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2021 | goto Done; |
| 2022 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2023 | // A tag was successfully processed, shift it |
| 2024 | // out of the list of tags returned. |
| 2025 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2029 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | |
| 2033 | QCBORError |
| 2034 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2035 | { |
| 2036 | QCBORError uErr; |
| 2037 | uErr = QCBORDecode_GetNextTag(pMe, pDecodedItem); |
| 2038 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2039 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2040 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2041 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2042 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | |
| 2046 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2047 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2048 | */ |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2049 | void QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2050 | { |
| 2051 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2052 | return; |
| 2053 | } |
| 2054 | |
| 2055 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2056 | } |
| 2057 | |
| 2058 | |
| 2059 | /* |
| 2060 | Public function, see header qcbor/qcbor_decode.h file |
| 2061 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2062 | QCBORError |
| 2063 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, |
| 2064 | QCBORItem *pDecodedItem, |
| 2065 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2066 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2067 | QCBORError nReturn; |
| 2068 | |
| 2069 | nReturn = QCBORDecode_GetNext(me, pDecodedItem); |
| 2070 | if(nReturn != QCBOR_SUCCESS) { |
| 2071 | return nReturn; |
| 2072 | } |
| 2073 | |
| 2074 | if(pTags != NULL) { |
| 2075 | pTags->uNumUsed = 0; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2076 | // Reverse the order because pTags is reverse of |
| 2077 | // QCBORItem.uTags. |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2078 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2079 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2080 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2081 | } |
| 2082 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2083 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2084 | } |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2085 | pTags->puTags[pTags->uNumUsed] = ConvertTag(me,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2086 | pTags->uNumUsed++; |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2094 | /* |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2095 | Decoding items is done in 5 layered functions, one calling the |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2096 | next one down. If a layer has no work to do for a particular item |
| 2097 | it returns quickly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2098 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2099 | - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes |
| 2100 | tagged data items, turning them into the local C representation. |
| 2101 | For the most simple it is just associating a QCBOR_TYPE with the data. For |
| 2102 | the complex ones that an aggregate of data items, there is some further |
| 2103 | decoding and a little bit of recursion. |
| 2104 | |
| 2105 | - QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2106 | ends of maps and arrays. It tracks descending into and ascending |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2107 | out of maps/arrays. It processes all breaks that terminate |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2108 | indefinite length maps and arrays. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2109 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2110 | - GetNext_MapEntry -- This handles the combining of two |
| 2111 | items, the label and the data, that make up a map entry. |
| 2112 | It only does work on maps. It combines the label and data |
| 2113 | items into one labeled item. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2114 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2115 | - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the |
| 2116 | tags into bit flags associated with the data item. No actual decoding |
| 2117 | of the contents of the tagged item is performed here. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2118 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2119 | - GetNext_FullItem -- This assembles the sub-items that make up |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2120 | an indefinte length string into one string item. It uses the |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2121 | string allocater to create contiguous space for the item. It |
| 2122 | processes all breaks that are part of indefinite length strings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2123 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2124 | - GetNext_Item -- This decodes the atomic data items in CBOR. Each |
| 2125 | atomic data item has a "major type", an integer "argument" and optionally |
| 2126 | some content. For text and byte strings, the content is the bytes |
| 2127 | that make up the string. These are the smallest data items that are |
| 2128 | considered to be well-formed. The content may also be other data items in |
| 2129 | the case of aggregate types. They are not handled in this layer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2130 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2131 | Roughly this takes 300 bytes of stack for vars. Need to |
| 2132 | evaluate this more carefully and correctly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2133 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2134 | */ |
| 2135 | |
| 2136 | |
| 2137 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2138 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2139 | */ |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2140 | bool QCBORDecode_IsTagged(QCBORDecodeContext *me, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2141 | const QCBORItem *pItem, |
| 2142 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2143 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2144 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2145 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2146 | break; |
| 2147 | } |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2148 | if(ConvertTag(me, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2149 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2150 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2151 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2152 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2153 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | |
| 2157 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2158 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2159 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2160 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2161 | { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2162 | QCBORError uReturn = me->uLastError; |
| 2163 | |
| 2164 | if(uReturn != QCBOR_SUCCESS) { |
| 2165 | goto Done; |
| 2166 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2167 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2168 | // Error out if all the maps/arrays are not closed out |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2169 | if(!DecodeNesting_IsCurrentAtTop(&(me->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2170 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2171 | goto Done; |
| 2172 | } |
| 2173 | |
| 2174 | // Error out if not all the bytes are consumed |
| 2175 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2176 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2177 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2178 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2179 | Done: |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2180 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2181 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2182 | StringAllocator_Destruct(&(me->StringAllocator)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2183 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2184 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2188 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2189 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2190 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2191 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2192 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2193 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2194 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2195 | { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2196 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2197 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2198 | } else { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 2199 | return ConvertTag(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2203 | /* |
| 2204 | Public function, see header qcbor/qcbor_decode.h file |
| 2205 | */ |
| 2206 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2207 | uint32_t uIndex) |
| 2208 | { |
| 2209 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2210 | return CBOR_TAG_INVALID64; |
| 2211 | } else { |
| 2212 | return ConvertTag(pMe, pMe->uLastTags[uIndex]); |
| 2213 | } |
| 2214 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2215 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2216 | /* |
| 2217 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2218 | Decoder errors handled in this file |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2219 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2220 | - Hit end of input before it was expected while decoding type and |
| 2221 | number QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2222 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2223 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2224 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2225 | - Hit end of input while decoding a text or byte string |
| 2226 | QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2227 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2228 | - Encountered conflicting tags -- e.g., an item is tagged both a date |
| 2229 | string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2230 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2231 | - Encontered an array or mapp that has too many items |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2232 | QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2233 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2234 | - Encountered array/map nesting that is too deep |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2235 | QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2236 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2237 | - An epoch date > INT64_MAX or < INT64_MIN was encountered |
| 2238 | QCBOR_ERR_DATE_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2239 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2240 | - The type of a map label is not a string or int |
| 2241 | QCBOR_ERR_MAP_LABEL_TYPE |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2242 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2243 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2244 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2245 | */ |
| 2246 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2247 | |
| 2248 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2249 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2250 | /* =========================================================================== |
| 2251 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2252 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2253 | This implements a simple sting allocator for indefinite length |
| 2254 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2255 | implements the function type QCBORStringAllocate and allows easy |
| 2256 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2257 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2258 | This particular allocator is built-in for convenience. The caller |
| 2259 | can implement their own. All of this following code will get |
| 2260 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2261 | |
| 2262 | This is a very primitive memory allocator. It does not track |
| 2263 | individual allocations, only a high-water mark. A free or |
| 2264 | reallocation must be of the last chunk allocated. |
| 2265 | |
| 2266 | The size of the pool and offset to free memory are packed into the |
| 2267 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2268 | the decode context. Since the address of the pool may not be |
| 2269 | aligned, they have to be packed and unpacked as if they were |
| 2270 | serialized data of the wire or such. |
| 2271 | |
| 2272 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2273 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2274 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2275 | |
| 2276 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2277 | static inline int |
| 2278 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2279 | { |
| 2280 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2281 | UsefulInputBuf UIB; |
| 2282 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2283 | // Just assume the size here. It was checked during SetUp so |
| 2284 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2285 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2286 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2287 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2288 | return UsefulInputBuf_GetError(&UIB); |
| 2289 | } |
| 2290 | |
| 2291 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2292 | static inline int |
| 2293 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2294 | { |
| 2295 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2296 | // length check performed here is useful. |
| 2297 | UsefulOutBuf UOB; |
| 2298 | |
| 2299 | UsefulOutBuf_Init(&UOB, Pool); |
| 2300 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2301 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2302 | return UsefulOutBuf_GetError(&UOB); |
| 2303 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2304 | |
| 2305 | |
| 2306 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2307 | Internal function for an allocation, reallocation free and destuct. |
| 2308 | |
| 2309 | Having only one function rather than one each per mode saves space in |
| 2310 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2311 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2312 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2313 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2314 | static UsefulBuf |
| 2315 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2316 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2317 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2318 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2319 | uint32_t uPoolSize; |
| 2320 | uint32_t uFreeOffset; |
| 2321 | |
| 2322 | if(uNewSize > UINT32_MAX) { |
| 2323 | // This allocator is only good up to 4GB. This check should |
| 2324 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2325 | goto Done; |
| 2326 | } |
| 2327 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2328 | |
| 2329 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2330 | goto Done; |
| 2331 | } |
| 2332 | |
| 2333 | if(uNewSize) { |
| 2334 | if(pMem) { |
| 2335 | // REALLOCATION MODE |
| 2336 | // Calculate pointer to the end of the memory pool. It is |
| 2337 | // assumed that pPool + uPoolSize won't wrap around by |
| 2338 | // assuming the caller won't pass a pool buffer in that is |
| 2339 | // not in legitimate memory space. |
| 2340 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2341 | |
| 2342 | // Check that the pointer for reallocation is in the range of the |
| 2343 | // pool. This also makes sure that pointer math further down |
| 2344 | // doesn't wrap under or over. |
| 2345 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2346 | // Offset to start of chunk for reallocation. This won't |
| 2347 | // wrap under because of check that pMem >= pPool. Cast |
| 2348 | // is safe because the pool is always less than UINT32_MAX |
| 2349 | // because of check in QCBORDecode_SetMemPool(). |
| 2350 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2351 | |
| 2352 | // Check to see if the allocation will fit. uPoolSize - |
| 2353 | // uMemOffset will not wrap under because of check that |
| 2354 | // pMem is in the range of the uPoolSize by check above. |
| 2355 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2356 | ReturnValue.ptr = pMem; |
| 2357 | ReturnValue.len = uNewSize; |
| 2358 | |
| 2359 | // Addition won't wrap around over because uNewSize was |
| 2360 | // checked to be sure it is less than the pool size. |
| 2361 | uFreeOffset = uMemOffset + uNewSize32; |
| 2362 | } |
| 2363 | } |
| 2364 | } else { |
| 2365 | // ALLOCATION MODE |
| 2366 | // uPoolSize - uFreeOffset will not underflow because this |
| 2367 | // pool implementation makes sure uFreeOffset is always |
| 2368 | // smaller than uPoolSize through this check here and |
| 2369 | // reallocation case. |
| 2370 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2371 | ReturnValue.len = uNewSize; |
| 2372 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2373 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2374 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2375 | } |
| 2376 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2377 | if(pMem) { |
| 2378 | // FREE MODE |
| 2379 | // Cast is safe because of limit on pool size in |
| 2380 | // QCBORDecode_SetMemPool() |
| 2381 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2382 | } else { |
| 2383 | // DESTRUCT MODE |
| 2384 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2385 | } |
| 2386 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2387 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2388 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2389 | MemPool_Pack(Pool, uFreeOffset); |
| 2390 | |
| 2391 | Done: |
| 2392 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2395 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2396 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2397 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2398 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2399 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2400 | UsefulBuf Pool, |
| 2401 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2402 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2403 | // The pool size and free mem offset are packed into the beginning |
| 2404 | // of the pool memory. This compile time check make sure the |
| 2405 | // constant in the header is correct. This check should optimize |
| 2406 | // down to nothing. |
| 2407 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2408 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2409 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2410 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2411 | // The pool size and free offset packed in to the beginning of pool |
| 2412 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2413 | // machines. |
| 2414 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2415 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2416 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2417 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2418 | // This checks that the pool buffer given is big enough. |
| 2419 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2420 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2421 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2422 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2423 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2424 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2425 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2426 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2427 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2428 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2429 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2430 | |
| 2431 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2432 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2433 | { |
| 2434 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2435 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2436 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2437 | |
| 2438 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2439 | Consume an entire map or array (and do next to |
| 2440 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2441 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2442 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2443 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2444 | const QCBORItem *pItemToConsume, |
| 2445 | uint_fast8_t *puNextNestLevel) |
| 2446 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2447 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2448 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2449 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 2450 | // If it is a map or array, this will tell if it is empty. |
| 2451 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2452 | |
| 2453 | if(QCBORItem_IsMapOrArray(pItemToConsume) && !bIsEmpty) { |
| 2454 | /* 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] | 2455 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2456 | /* This works for definite and indefinite length |
| 2457 | * maps and arrays by using the nesting level |
| 2458 | */ |
| 2459 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2460 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2461 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2462 | goto Done; |
| 2463 | } |
| 2464 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2465 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2472 | /* Just pass the nesting level through */ |
| 2473 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2474 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2475 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2476 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2477 | |
| 2478 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2479 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2483 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2484 | Works only for integer and string labels. Returns false |
| 2485 | for any other type. */ |
| 2486 | static inline bool |
| 2487 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2488 | { |
| 2489 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2490 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2491 | return true; |
| 2492 | } |
| 2493 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2494 | 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] | 2495 | return true; |
| 2496 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2497 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2498 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2499 | return true; |
| 2500 | } |
| 2501 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2502 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2503 | return true; |
| 2504 | } |
| 2505 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2506 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2507 | /* Other label types are never matched */ |
| 2508 | return false; |
| 2509 | } |
| 2510 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2511 | |
| 2512 | /* |
| 2513 | Returns true if Item1 and Item2 are the same type |
| 2514 | or if either are of QCBOR_TYPE_ANY. |
| 2515 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2516 | static inline bool |
| 2517 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2518 | { |
| 2519 | if(Item1.uDataType == Item2.uDataType) { |
| 2520 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2521 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2522 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2523 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2524 | return true; |
| 2525 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2526 | return false; |
| 2527 | } |
| 2528 | |
| 2529 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2530 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2531 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2532 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2533 | @param[in] pMe The decode context to search. |
| 2534 | @param[in,out] pItemArray The items to search for and the items found. |
| 2535 | @param[out] puOffset Byte offset of last item matched. |
| 2536 | @param[in] pCBContext Context for the not-found item call back. |
| 2537 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2538 | |
| 2539 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2540 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2541 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 2542 | were found for one of the labels being |
| 2543 | search for. This duplicate detection is |
| 2544 | only performed for items in pItemArray, |
| 2545 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2546 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2547 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 2548 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2549 | |
| 2550 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2551 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2552 | On input pItemArray contains a list of labels and data types |
| 2553 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2554 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2555 | On output the fully retrieved items are filled in with |
| 2556 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2557 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2558 | If an item was not found, its data type is set to QCBOR_TYPE_NONE. |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2559 | |
| 2560 | This also finds the ends of maps and arrays when they are exited. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2561 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2562 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2563 | MapSearch(QCBORDecodeContext *pMe, |
| 2564 | QCBORItem *pItemArray, |
| 2565 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2566 | void *pCBContext, |
| 2567 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2568 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2569 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2570 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2571 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2572 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2573 | uReturn = pMe->uLastError; |
| 2574 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2575 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2576 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2577 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2578 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2579 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2580 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2581 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2582 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2583 | } |
| 2584 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2585 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2586 | // It is an empty bounded array or map |
| 2587 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2588 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2589 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2590 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2591 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2592 | // Nothing is ever found in an empty array or map. All items |
| 2593 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2594 | uReturn = QCBOR_SUCCESS; |
| 2595 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2596 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2599 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2600 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2601 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2602 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2603 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2604 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2605 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2606 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2607 | Loop over all the items in the map or array. Each item |
| 2608 | could be a map or array, but label matching is only at |
| 2609 | the main level. This handles definite and indefinite |
| 2610 | length maps and arrays. The only reason this is ever |
| 2611 | called on arrays is to find their end position. |
| 2612 | |
| 2613 | This will always run over all items in order to do |
| 2614 | duplicate detection. |
| 2615 | |
| 2616 | This will exit with failure if it encounters an |
| 2617 | unrecoverable error, but continue on for recoverable |
| 2618 | errors. |
| 2619 | |
| 2620 | If a recoverable error occurs on a matched item, then |
| 2621 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2622 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2623 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2624 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2625 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2626 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2627 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2628 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2629 | /* Get the item */ |
| 2630 | QCBORItem Item; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2631 | QCBORError uResult = QCBORDecode_GetNextTag(pMe, &Item); |
| 2632 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2633 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2634 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2635 | goto Done; |
| 2636 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2637 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2638 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2639 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2640 | goto Done; |
| 2641 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2642 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2643 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2644 | bool bMatched = false; |
| 2645 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
| 2646 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2647 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2648 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2649 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2650 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2651 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2652 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2653 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2654 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2655 | goto Done; |
| 2656 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2657 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2658 | if(uResult != QCBOR_SUCCESS) { |
| 2659 | uReturn = uResult; |
| 2660 | goto Done; |
| 2661 | } |
| 2662 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2663 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2664 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2665 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2666 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2667 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2668 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2669 | bMatched = true; |
| 2670 | } |
| 2671 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2672 | |
| 2673 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2674 | if(!bMatched && pfCallback != NULL) { |
| 2675 | /* |
| 2676 | Call the callback on unmatched labels. |
| 2677 | (It is tempting to do duplicate detection here, but that would |
| 2678 | require dynamic memory allocation because the number of labels |
| 2679 | that might be encountered is unbounded.) |
| 2680 | */ |
| 2681 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2682 | if(uReturn != QCBOR_SUCCESS) { |
| 2683 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2684 | } |
| 2685 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2686 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2687 | /* |
| 2688 | Consume the item whether matched or not. This |
| 2689 | does the work of traversing maps and array and |
| 2690 | everything in them. In this loop only the |
| 2691 | items at the current nesting level are examined |
| 2692 | to match the labels. |
| 2693 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2694 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2695 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2696 | goto Done; |
| 2697 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2698 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2699 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2700 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2701 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2702 | |
| 2703 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2704 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2705 | // Check here makes sure that this won't accidentally be |
| 2706 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 2707 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
| 2708 | if(uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 2709 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 2710 | goto Done; |
| 2711 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2712 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 2713 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2714 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2715 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2716 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 2717 | |
| 2718 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2719 | /* 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] | 2720 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2721 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2722 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 2723 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2724 | } |
| 2725 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2726 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2727 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2730 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2731 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2732 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2733 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2734 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 2735 | int64_t nLabel, |
| 2736 | uint8_t uQcborType, |
| 2737 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2738 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2739 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2740 | return; |
| 2741 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2742 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2743 | QCBORItem OneItemSeach[2]; |
| 2744 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2745 | OneItemSeach[0].label.int64 = nLabel; |
| 2746 | OneItemSeach[0].uDataType = uQcborType; |
| 2747 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2748 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2749 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2750 | |
| 2751 | *pItem = OneItemSeach[0]; |
| 2752 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2753 | if(uReturn != QCBOR_SUCCESS) { |
| 2754 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2755 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2756 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2757 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2760 | Done: |
| 2761 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2765 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2766 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2767 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2768 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 2769 | const char *szLabel, |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2770 | uint8_t uQcborType, |
| 2771 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2772 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2773 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2774 | return; |
| 2775 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2776 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2777 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2778 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2779 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2780 | OneItemSeach[0].uDataType = uQcborType; |
| 2781 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2782 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2783 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2784 | if(uReturn != QCBOR_SUCCESS) { |
| 2785 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2786 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2787 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2788 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2789 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2790 | } |
| 2791 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2792 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2793 | |
| 2794 | Done: |
| 2795 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2799 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2800 | static QCBORError |
| 2801 | CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2802 | { |
| 2803 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 2804 | if(uDataType == puTypeList[i]) { |
| 2805 | return QCBOR_SUCCESS; |
| 2806 | } |
| 2807 | } |
| 2808 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2809 | } |
| 2810 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 2811 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2812 | /** |
| 2813 | @param[in] TagSpec Specification for matching tags. |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2814 | @param[in] pItem The item to check. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2815 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2816 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 2817 | @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] | 2818 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 2819 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered |
| 2820 | tag value. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2821 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2822 | static QCBORError |
| 2823 | CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2824 | { |
| 2825 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 2826 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 2827 | /* There are tags that QCBOR couldn't process on this item and |
| 2828 | the caller has told us there should not be. */ |
| 2829 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2830 | } |
| 2831 | |
| 2832 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 2833 | const int nItemType = pItem->uDataType; |
| 2834 | |
| 2835 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 2836 | // Must match the tag and only the tag |
| 2837 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2838 | } |
| 2839 | |
| 2840 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 2841 | if(uReturn == QCBOR_SUCCESS) { |
| 2842 | return QCBOR_SUCCESS; |
| 2843 | } |
| 2844 | |
| 2845 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 2846 | /* Must match the content type and only the content type. |
| 2847 | There was no match just above so it is a fail. */ |
| 2848 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2849 | } |
| 2850 | |
| 2851 | /* If here it can match either the tag or the content |
| 2852 | and it hasn't matched the content, so the end |
| 2853 | result is whether it matches the tag. This is |
| 2854 | also the case that the CBOR standard discourages. */ |
| 2855 | |
| 2856 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2857 | } |
| 2858 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2859 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2860 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 2861 | // This could be semi-private if need be |
| 2862 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2863 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 2864 | int64_t nLabel, |
| 2865 | TagSpecification TagSpec, |
| 2866 | QCBORItem *pItem) |
| 2867 | { |
| 2868 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 2869 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2870 | return; |
| 2871 | } |
| 2872 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2873 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2874 | } |
| 2875 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 2876 | |
| 2877 | // This could be semi-private if need be |
| 2878 | static inline |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2879 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 2880 | const char *szLabel, |
| 2881 | TagSpecification TagSpec, |
| 2882 | QCBORItem *pItem) |
| 2883 | { |
| 2884 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 2885 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2886 | return; |
| 2887 | } |
| 2888 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2889 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2890 | } |
| 2891 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2892 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2893 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 2894 | int64_t nLabel, |
| 2895 | TagSpecification TagSpec, |
| 2896 | UsefulBufC *pString) |
| 2897 | { |
| 2898 | QCBORItem Item; |
| 2899 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 2900 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2901 | *pString = Item.val.string; |
| 2902 | } |
| 2903 | } |
| 2904 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2905 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2906 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 2907 | const char * szLabel, |
| 2908 | TagSpecification TagSpec, |
| 2909 | UsefulBufC *pString) |
| 2910 | { |
| 2911 | QCBORItem Item; |
| 2912 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 2913 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2914 | *pString = Item.val.string; |
| 2915 | } |
| 2916 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2917 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2918 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2919 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2920 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2921 | void QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2922 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2923 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, NULL, NULL); |
| 2924 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2925 | } |
| 2926 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2927 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2928 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2929 | */ |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2930 | void QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 2931 | QCBORItem *pItemList, |
| 2932 | void *pCallbackCtx, |
| 2933 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2934 | { |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 2935 | QCBORError uErr = MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
| 2936 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 2940 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2941 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 2942 | // The first item in pSearch is the one that is to be |
| 2943 | // entered. It should be the only one filled in. Any other |
| 2944 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 2945 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2946 | // Already in error state; do nothing. |
| 2947 | return; |
| 2948 | } |
| 2949 | |
| 2950 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2951 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 2952 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2953 | return; |
| 2954 | } |
| 2955 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2956 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2957 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2958 | return; |
| 2959 | } |
| 2960 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2961 | /* 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] | 2962 | at the map/array about to be entered. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2963 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 2964 | Also need the current map nesting level and start cursor to |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2965 | be at the right place. |
| 2966 | |
| 2967 | The UsefulInBuf offset could be anywhere, so no assumption is |
| 2968 | made about it. |
| 2969 | |
| 2970 | No assumption is made about the pre-order nesting level either. |
| 2971 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 2972 | However the bounded mode nesting level is assumed to be one above |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2973 | the map level that is being entered. |
| 2974 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2975 | /* Seek to the data item that is the map or array */ |
| 2976 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 2977 | |
| 2978 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2979 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 2980 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2984 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 2985 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2986 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2987 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2988 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2989 | QCBORItem OneItemSeach[2]; |
| 2990 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2991 | OneItemSeach[0].label.int64 = nLabel; |
| 2992 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 2993 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2994 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2995 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2996 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3000 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3001 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3002 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3003 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3004 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3005 | QCBORItem OneItemSeach[2]; |
| 3006 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3007 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3008 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3009 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3010 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3011 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3012 | } |
| 3013 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3014 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3015 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3016 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3017 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3018 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3019 | QCBORItem OneItemSeach[2]; |
| 3020 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3021 | OneItemSeach[0].label.int64 = nLabel; |
| 3022 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3023 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3024 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3025 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3026 | } |
| 3027 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3028 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3029 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3030 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3031 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3032 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3033 | QCBORItem OneItemSeach[2]; |
| 3034 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3035 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3036 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3037 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3038 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3039 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3043 | // Semi-private function |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3044 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3045 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3046 | QCBORError uErr; |
| 3047 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3048 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3049 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3050 | // Already in error state; do nothing. |
| 3051 | return; |
| 3052 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3053 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3054 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3055 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3056 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3057 | if(uErr != QCBOR_SUCCESS) { |
| 3058 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3059 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3060 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3061 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3062 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3065 | CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3066 | |
| 3067 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3068 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3069 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3070 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3071 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3072 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3073 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3074 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3075 | // Special case to increment nesting level for zero-length maps |
| 3076 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3077 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3078 | } |
| 3079 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3080 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3081 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3082 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3083 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3084 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3085 | if(pItem != NULL) { |
| 3086 | *pItem = Item; |
| 3087 | } |
| 3088 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3089 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3090 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3093 | |
| 3094 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3095 | This is the common work for exiting a level that is a bounded map, |
| 3096 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3097 | |
| 3098 | One chunk of work is to set up the pre-order traversal so it is at |
| 3099 | the item just after the bounded map, array or bstr that is being |
| 3100 | exited. This is somewhat complex. |
| 3101 | |
| 3102 | The other work is to level-up the bounded mode to next higest bounded |
| 3103 | mode or the top level if there isn't one. |
| 3104 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3105 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3106 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3107 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3108 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3109 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3110 | /* |
| 3111 | First the pre-order-traversal byte offset is positioned to the |
| 3112 | item just after the bounded mode item that was just consumed. |
| 3113 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3114 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3115 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3116 | /* |
| 3117 | Next, set the current nesting level to one above the bounded level |
| 3118 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3119 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3120 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3121 | makes sure pCurrentBounded is valid. |
| 3122 | */ |
| 3123 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3124 | |
| 3125 | /* |
| 3126 | This does the complex work of leveling up the pre-order traversal |
| 3127 | when the end of a map or array or another bounded level is |
| 3128 | reached. It may do nothing, or ascend all the way to the top |
| 3129 | level. |
| 3130 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 3131 | uErr = NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3132 | if(uErr != QCBOR_SUCCESS) { |
| 3133 | goto Done; |
| 3134 | } |
| 3135 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3136 | /* |
| 3137 | This makes the next highest bounded level the current bounded |
| 3138 | level. If there is no next highest level, then no bounded mode is |
| 3139 | in effect. |
| 3140 | */ |
| 3141 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3142 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3143 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3144 | |
| 3145 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3146 | return uErr; |
| 3147 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3148 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3149 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3150 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3151 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3152 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3153 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3154 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3155 | return; |
| 3156 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3157 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3158 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3159 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3160 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3161 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3162 | goto Done; |
| 3163 | } |
| 3164 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3165 | /* |
| 3166 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3167 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3168 | from previous map search, then do a dummy search. |
| 3169 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3170 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3171 | QCBORItem Dummy; |
| 3172 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3173 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3174 | if(uErr != QCBOR_SUCCESS) { |
| 3175 | goto Done; |
| 3176 | } |
| 3177 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3178 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3179 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3180 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3181 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3182 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3186 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3187 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3188 | const QCBORItem *pItem, |
| 3189 | uint8_t uTagRequirement, |
| 3190 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3191 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3192 | if(pBstr) { |
| 3193 | *pBstr = NULLUsefulBufC; |
| 3194 | } |
| 3195 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3196 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3197 | // Already in error state; do nothing. |
| 3198 | return pMe->uLastError; |
| 3199 | } |
| 3200 | |
| 3201 | QCBORError uError = QCBOR_SUCCESS; |
| 3202 | |
| 3203 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3204 | uError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3205 | goto Done;; |
| 3206 | } |
| 3207 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3208 | const TagSpecification TagSpec = |
| 3209 | { |
| 3210 | uTagRequirement, |
| 3211 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3212 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3213 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3214 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3215 | uError = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3216 | if(uError != QCBOR_SUCCESS) { |
| 3217 | goto Done; |
| 3218 | } |
| 3219 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3220 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3221 | // Reverse the decrement done by GetNext() for the bstr so the |
| 3222 | // increment in NestLevelAscender() called by ExitBoundedLevel() |
| 3223 | // will work right. |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3224 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3225 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3226 | |
| 3227 | if(pBstr) { |
| 3228 | *pBstr = pItem->val.string; |
| 3229 | } |
| 3230 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3231 | // This saves the current length of the UsefulInputBuf and then |
| 3232 | // narrows the UsefulInputBuf to start and length of the wrapped |
| 3233 | // CBOR that is being entered. |
| 3234 | // |
| 3235 | // This makes sure the length is less than |
| 3236 | // QCBOR_MAX_DECODE_INPUT_SIZE which is slighly less than |
| 3237 | // UINT32_MAX. The value UINT32_MAX is used as a special indicator |
| 3238 | // value. The checks against QCBOR_MAX_DECODE_INPUT_SIZE also make |
| 3239 | // the casts safe. uEndOfBstr will always be less than |
| 3240 | // uPreviousLength because of the way UsefulInputBuf works so there |
| 3241 | // is no need to check it. There is also a range check in the |
| 3242 | // seek. |
| 3243 | // |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3244 | // Most of these calls are simple inline accessors so this doesn't |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3245 | // amount to much code. |
| 3246 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 3247 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3248 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3249 | goto Done; |
| 3250 | } |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3251 | const size_t uEndOfBstr = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3252 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOfBstr - pItem->val.string.len); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3253 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3254 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3255 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3256 | (uint32_t)uPreviousLength, |
| 3257 | (uint32_t)uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3258 | Done: |
| 3259 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3263 | /* |
| 3264 | Public function, see header qcbor/qcbor_decode.h file |
| 3265 | */ |
| 3266 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3267 | uint8_t uTagRequirement, |
| 3268 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3269 | { |
| 3270 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3271 | // Already in error state; do nothing. |
| 3272 | return; |
| 3273 | } |
| 3274 | |
| 3275 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3276 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3277 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3278 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3279 | return; |
| 3280 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3281 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3282 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3283 | &Item, |
| 3284 | uTagRequirement, |
| 3285 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3289 | /* |
| 3290 | Public function, see header qcbor/qcbor_decode.h file |
| 3291 | */ |
| 3292 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3293 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3294 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3295 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3296 | { |
| 3297 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3298 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3299 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3300 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3301 | &Item, |
| 3302 | uTagRequirement, |
| 3303 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3304 | } |
| 3305 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3306 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3307 | /* |
| 3308 | Public function, see header qcbor/qcbor_decode.h file |
| 3309 | */ |
| 3310 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3311 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3312 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3313 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3314 | { |
| 3315 | QCBORItem Item; |
| 3316 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3317 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3318 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
| 3319 | &Item, |
| 3320 | uTagRequirement, |
| 3321 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3322 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3323 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3324 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3325 | /* |
| 3326 | Public function, see header qcbor/qcbor_decode.h file |
| 3327 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3328 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3329 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3330 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3331 | // Already in error state; do nothing. |
| 3332 | return; |
| 3333 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3334 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3335 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3336 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3337 | return; |
| 3338 | } |
| 3339 | |
| 3340 | /* |
| 3341 | Reset the length of the UsefulInputBuf to what it was before |
| 3342 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3343 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3344 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3345 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3346 | |
| 3347 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3348 | QCBORError uErr = ExitBoundedLevel(pMe, DecodeNesting_GetEndOfBstr(&(pMe->nesting))); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3349 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3352 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3353 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3354 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3355 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3356 | |
Laurence Lundblade | 11a064e | 2020-05-07 13:13:42 -0700 | [diff] [blame] | 3357 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3358 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3359 | static QCBORError |
| 3360 | InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3361 | { |
| 3362 | switch(pItem->uDataType) { |
| 3363 | case QCBOR_TYPE_TRUE: |
| 3364 | *pBool = true; |
| 3365 | return QCBOR_SUCCESS; |
| 3366 | break; |
| 3367 | |
| 3368 | case QCBOR_TYPE_FALSE: |
| 3369 | *pBool = false; |
| 3370 | return QCBOR_SUCCESS; |
| 3371 | break; |
| 3372 | |
| 3373 | default: |
| 3374 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3375 | break; |
| 3376 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3377 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3378 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3379 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3380 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3381 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3382 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3383 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3384 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3385 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3386 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3387 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3388 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3389 | return; |
| 3390 | } |
| 3391 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3392 | QCBORError nError; |
| 3393 | QCBORItem Item; |
| 3394 | |
| 3395 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3396 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3397 | pMe->uLastError = (uint8_t)nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3398 | return; |
| 3399 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3400 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3401 | } |
| 3402 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3403 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3404 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3405 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3406 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3407 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3408 | { |
| 3409 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3410 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3411 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3412 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3413 | } |
| 3414 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3415 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3416 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3417 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3418 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3419 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3420 | { |
| 3421 | QCBORItem Item; |
| 3422 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3423 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3424 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | |
| 3428 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3429 | |
| 3430 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3431 | QCBORItem *pItem, |
| 3432 | uint8_t uTagRequirement, |
| 3433 | int64_t *pnTime) |
| 3434 | { |
| 3435 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3436 | // Already in error state, do nothing |
| 3437 | return; |
| 3438 | } |
| 3439 | |
| 3440 | QCBORError uErr; |
| 3441 | |
| 3442 | const TagSpecification TagSpec = |
| 3443 | { |
| 3444 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3445 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3446 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3447 | }; |
| 3448 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3449 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3450 | if(uErr != QCBOR_SUCCESS) { |
| 3451 | goto Done; |
| 3452 | } |
| 3453 | |
| 3454 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3455 | uErr = DecodeDateEpoch(pItem); |
| 3456 | if(uErr != QCBOR_SUCCESS) { |
| 3457 | goto Done; |
| 3458 | } |
| 3459 | } |
| 3460 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3461 | // Save the tags in the last item's tags in the decode context |
| 3462 | // for QCBORDecode_GetNthTagOfLast() |
| 3463 | CopyTags(pMe, pItem); |
| 3464 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3465 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3466 | |
| 3467 | Done: |
| 3468 | pMe->uLastError = (uint8_t)uErr; |
| 3469 | } |
| 3470 | |
| 3471 | |
| 3472 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 3473 | uint8_t uTagRequirement, |
| 3474 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3475 | { |
| 3476 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3477 | // Already in error state, do nothing |
| 3478 | return; |
| 3479 | } |
| 3480 | |
| 3481 | QCBORItem Item; |
| 3482 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3483 | |
| 3484 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3485 | } |
| 3486 | |
| 3487 | |
| 3488 | void |
| 3489 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3490 | int64_t nLabel, |
| 3491 | uint8_t uTagRequirement, |
| 3492 | int64_t *pnTime) |
| 3493 | { |
| 3494 | QCBORItem Item; |
| 3495 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3496 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3497 | } |
| 3498 | |
| 3499 | |
| 3500 | void |
| 3501 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3502 | const char *szLabel, |
| 3503 | uint8_t uTagRequirement, |
| 3504 | int64_t *pnTime) |
| 3505 | { |
| 3506 | QCBORItem Item; |
| 3507 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3508 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3509 | } |
| 3510 | |
| 3511 | |
| 3512 | |
| 3513 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3514 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3515 | TagSpecification TagSpec, |
| 3516 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3517 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3518 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3519 | // Already in error state, do nothing |
| 3520 | return; |
| 3521 | } |
| 3522 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3523 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3524 | QCBORItem Item; |
| 3525 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3526 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3527 | if(uError != QCBOR_SUCCESS) { |
| 3528 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3529 | return; |
| 3530 | } |
| 3531 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3532 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3533 | |
| 3534 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3535 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 3536 | } else { |
| 3537 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3538 | } |
| 3539 | } |
| 3540 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3541 | |
| 3542 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3543 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3544 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3545 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3546 | UsefulBufC *pValue, |
| 3547 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3548 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3549 | const TagSpecification TagSpec = |
| 3550 | { |
| 3551 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3552 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3553 | {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] | 3554 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3555 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3556 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3557 | if(uErr != QCBOR_SUCCESS) { |
| 3558 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3559 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3560 | |
| 3561 | *pValue = pItem->val.string; |
| 3562 | |
| 3563 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3564 | *pbIsNegative = false; |
| 3565 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3566 | *pbIsNegative = true; |
| 3567 | } |
| 3568 | |
| 3569 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3573 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3574 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3575 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3576 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3577 | uint8_t uTagRequirement, |
| 3578 | UsefulBufC *pValue, |
| 3579 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3580 | { |
| 3581 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3582 | // Already in error state, do nothing |
| 3583 | return; |
| 3584 | } |
| 3585 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3586 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3587 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3588 | if(uError != QCBOR_SUCCESS) { |
| 3589 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3590 | return; |
| 3591 | } |
| 3592 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3593 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3596 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3597 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3598 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3599 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3600 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3601 | int64_t nLabel, |
| 3602 | uint8_t uTagRequirement, |
| 3603 | UsefulBufC *pValue, |
| 3604 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3605 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3606 | QCBORItem Item; |
| 3607 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3608 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3609 | return; |
| 3610 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3611 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3612 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3613 | } |
| 3614 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3615 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3616 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3617 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3618 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3619 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3620 | const char *szLabel, |
| 3621 | uint8_t uTagRequirement, |
| 3622 | UsefulBufC *pValue, |
| 3623 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3624 | { |
| 3625 | QCBORItem Item; |
| 3626 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3627 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3628 | return; |
| 3629 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3630 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3631 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | |
| 3635 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3636 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3637 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3638 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3639 | const QCBORItem *pItem, |
| 3640 | UsefulBufC *pMessage, |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3641 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3642 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3643 | const TagSpecification TagSpecText = |
| 3644 | { |
| 3645 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3646 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3647 | {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] | 3648 | }; |
| 3649 | const TagSpecification TagSpecBinary = |
| 3650 | { |
| 3651 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 3652 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3653 | {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] | 3654 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3655 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3656 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3657 | |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3658 | if(CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3659 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3660 | if(pbIsTag257 != NULL) { |
| 3661 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3662 | } |
| 3663 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 3664 | } else if(CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3665 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3666 | if(pbIsTag257 != NULL) { |
| 3667 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3668 | } |
| 3669 | uReturn = QCBOR_SUCCESS; |
| 3670 | |
| 3671 | } else { |
| 3672 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3673 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3674 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3675 | return uReturn; |
| 3676 | } |
| 3677 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3678 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 3679 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3680 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3681 | |
| 3682 | |
| 3683 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3684 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3685 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3686 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3687 | |
| 3688 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3689 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3690 | static QCBORError |
| 3691 | Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
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 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3694 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3695 | if(uResult != 0) { |
| 3696 | /* This loop will run a maximum of 19 times because |
| 3697 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 3698 | * exit with the overflow error |
| 3699 | */ |
| 3700 | for(; nExponent > 0; nExponent--) { |
| 3701 | if(uResult > UINT64_MAX / 10) { |
| 3702 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 3703 | } |
| 3704 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3705 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3706 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3707 | for(; nExponent < 0; nExponent++) { |
| 3708 | uResult = uResult / 10; |
| 3709 | if(uResult == 0) { |
| 3710 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3711 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3712 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3713 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3714 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3715 | |
| 3716 | *puResult = uResult; |
| 3717 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3718 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3722 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3723 | static QCBORError |
| 3724 | Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
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 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3727 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3728 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3729 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3730 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3731 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3732 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3733 | */ |
| 3734 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3735 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3736 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3737 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3738 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3739 | nExponent--; |
| 3740 | } |
| 3741 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3742 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3743 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3744 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3745 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3746 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3747 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3750 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3751 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3752 | return QCBOR_SUCCESS; |
| 3753 | } |
| 3754 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3755 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3756 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3757 | Compute value with signed mantissa and signed result. Works with |
| 3758 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3759 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3760 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 3761 | int64_t nExponent, |
| 3762 | int64_t *pnResult, |
| 3763 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3764 | { |
| 3765 | uint64_t uResult; |
| 3766 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3767 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3768 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3769 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 3770 | |
| 3771 | // Do the exponentiation of the positive mantissa |
| 3772 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 3773 | if(uReturn) { |
| 3774 | return uReturn; |
| 3775 | } |
| 3776 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3777 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 3778 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 3779 | of INT64_MIN. This assumes two's compliment representation where |
| 3780 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 3781 | Trying to write -INT64_MIN doesn't work to get this because the |
| 3782 | compiler tries to work with an int64_t which can't represent |
| 3783 | -INT64_MIN. |
| 3784 | */ |
| 3785 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 3786 | |
| 3787 | // Error out if too large |
| 3788 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3789 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 3790 | } |
| 3791 | |
| 3792 | // Casts are safe because of checks above |
| 3793 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 3794 | |
| 3795 | return QCBOR_SUCCESS; |
| 3796 | } |
| 3797 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3798 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3799 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3800 | Compute value with signed mantissa and unsigned result. Works with |
| 3801 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3802 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3803 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 3804 | int64_t nExponent, |
| 3805 | uint64_t *puResult, |
| 3806 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3807 | { |
| 3808 | if(nMantissa < 0) { |
| 3809 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 3810 | } |
| 3811 | |
| 3812 | // Cast to unsigned is OK because of check for negative |
| 3813 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 3814 | // Exponentiation is straight forward |
| 3815 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 3816 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3817 | |
| 3818 | |
| 3819 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3820 | Compute value with signed mantissa and unsigned result. Works with |
| 3821 | exponent of 2 or 10 based on exponentiator. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3822 | */ |
| 3823 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 3824 | int64_t nExponent, |
| 3825 | uint64_t *puResult, |
| 3826 | fExponentiator pfExp) |
| 3827 | { |
| 3828 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 3829 | } |
| 3830 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3831 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 3832 | |
| 3833 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3834 | |
| 3835 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3836 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3837 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
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 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3840 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3841 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3842 | const uint8_t *pByte = BigNum.ptr; |
| 3843 | size_t uLen = BigNum.len; |
| 3844 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 3845 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3846 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3847 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 3848 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3851 | *pResult = uResult; |
| 3852 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3853 | } |
| 3854 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3855 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 3856 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3857 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3858 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3859 | } |
| 3860 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3861 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3862 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3863 | { |
| 3864 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3865 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 3866 | if(uError) { |
| 3867 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3868 | } |
| 3869 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 3870 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3871 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3875 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3876 | { |
| 3877 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3878 | /* The negative integer furthest from zero for a C int64_t is |
| 3879 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 3880 | negative number in CBOR is computed as -n - 1 where n is the |
| 3881 | encoded integer, where n is what is in the variable BigNum. When |
| 3882 | converting BigNum to a uint64_t, the maximum value is thus |
| 3883 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 3884 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3885 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3886 | -n - 1 <= INT64_MIN. |
| 3887 | -n - 1 <= -INT64_MAX - 1 |
| 3888 | n <= INT64_MAX. |
| 3889 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3890 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3891 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3892 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3893 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3894 | |
| 3895 | /// Now apply -n - 1. The cast is safe because |
| 3896 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 3897 | // is the largest positive integer that an int64_t can |
| 3898 | // represent. */ |
| 3899 | *pnResult = -(int64_t)uResult - 1; |
| 3900 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3901 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3902 | } |
| 3903 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3904 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3905 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3906 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3907 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3908 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 3909 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3910 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3911 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3912 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3913 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 3914 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3915 | |
| 3916 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 3917 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3918 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 3919 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3920 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3921 | static QCBORError |
| 3922 | ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3923 | { |
| 3924 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3925 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3926 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3927 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3928 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3929 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 3930 | http://www.cplusplus.com/reference/cmath/llround/ |
| 3931 | */ |
| 3932 | // Not interested in FE_INEXACT |
| 3933 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3934 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 3935 | *pnValue = llround(pItem->val.dfnum); |
| 3936 | } else { |
| 3937 | *pnValue = lroundf(pItem->val.fnum); |
| 3938 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3939 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 3940 | // llround() shouldn't result in divide by zero, but catch |
| 3941 | // it here in case it unexpectedly does. Don't try to |
| 3942 | // distinguish between the various exceptions because it seems |
| 3943 | // they vary by CPU, compiler and OS. |
| 3944 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3945 | } |
| 3946 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3947 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3948 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3949 | #else |
| 3950 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 3951 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3952 | break; |
| 3953 | |
| 3954 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3955 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3956 | *pnValue = pItem->val.int64; |
| 3957 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3958 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3959 | } |
| 3960 | break; |
| 3961 | |
| 3962 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3963 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3964 | if(pItem->val.uint64 < INT64_MAX) { |
| 3965 | *pnValue = pItem->val.int64; |
| 3966 | } else { |
| 3967 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 3968 | } |
| 3969 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3970 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3971 | } |
| 3972 | break; |
| 3973 | |
| 3974 | default: |
| 3975 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3976 | } |
| 3977 | return QCBOR_SUCCESS; |
| 3978 | } |
| 3979 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3980 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3981 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 3982 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3983 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3984 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3985 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3986 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3987 | return; |
| 3988 | } |
| 3989 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3990 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3991 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3992 | if(uError) { |
| 3993 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3994 | return; |
| 3995 | } |
| 3996 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3997 | if(pItem) { |
| 3998 | *pItem = Item; |
| 3999 | } |
| 4000 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4001 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4002 | } |
| 4003 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4004 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4005 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4006 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4007 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4008 | int64_t *pnValue, |
| 4009 | QCBORItem *pItem) |
| 4010 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4011 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4012 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4013 | return; |
| 4014 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4015 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4016 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4019 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4020 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4021 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4022 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4023 | int64_t *pnValue, |
| 4024 | QCBORItem *pItem) |
| 4025 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4026 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4027 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4028 | return; |
| 4029 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4030 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4031 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4035 | /* |
| 4036 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4037 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4038 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4039 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4040 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4041 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4042 | |
| 4043 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4044 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4045 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4046 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4047 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4048 | static QCBORError |
| 4049 | Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4050 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4051 | switch(pItem->uDataType) { |
| 4052 | |
| 4053 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4054 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4055 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4056 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4057 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4058 | } |
| 4059 | break; |
| 4060 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4061 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4062 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4063 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4064 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4065 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4066 | } |
| 4067 | break; |
| 4068 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4069 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4070 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4071 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4072 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4073 | pItem->val.expAndMantissa.nExponent, |
| 4074 | pnValue, |
| 4075 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4076 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4077 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4078 | } |
| 4079 | break; |
| 4080 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4081 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4082 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4083 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4084 | pItem->val.expAndMantissa.nExponent, |
| 4085 | pnValue, |
| 4086 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4087 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4088 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4089 | } |
| 4090 | break; |
| 4091 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4092 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4093 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4094 | int64_t nMantissa; |
| 4095 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4096 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4097 | if(uErr) { |
| 4098 | return uErr; |
| 4099 | } |
| 4100 | return ExponentiateNN(nMantissa, |
| 4101 | pItem->val.expAndMantissa.nExponent, |
| 4102 | pnValue, |
| 4103 | Exponentitate10); |
| 4104 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4105 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4106 | } |
| 4107 | break; |
| 4108 | |
| 4109 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4110 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4111 | int64_t nMantissa; |
| 4112 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4113 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4114 | if(uErr) { |
| 4115 | return uErr; |
| 4116 | } |
| 4117 | return ExponentiateNN(nMantissa, |
| 4118 | pItem->val.expAndMantissa.nExponent, |
| 4119 | pnValue, |
| 4120 | Exponentitate10); |
| 4121 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4122 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4123 | } |
| 4124 | break; |
| 4125 | |
| 4126 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4127 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4128 | int64_t nMantissa; |
| 4129 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4130 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4131 | if(uErr) { |
| 4132 | return uErr; |
| 4133 | } |
| 4134 | return ExponentiateNN(nMantissa, |
| 4135 | pItem->val.expAndMantissa.nExponent, |
| 4136 | pnValue, |
| 4137 | Exponentitate2); |
| 4138 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4139 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4140 | } |
| 4141 | break; |
| 4142 | |
| 4143 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4144 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4145 | int64_t nMantissa; |
| 4146 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4147 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4148 | if(uErr) { |
| 4149 | return uErr; |
| 4150 | } |
| 4151 | return ExponentiateNN(nMantissa, |
| 4152 | pItem->val.expAndMantissa.nExponent, |
| 4153 | pnValue, |
| 4154 | Exponentitate2); |
| 4155 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4156 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4157 | } |
| 4158 | break; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4159 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4160 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4161 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4162 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4163 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4164 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4165 | |
| 4166 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4167 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4168 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4169 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4170 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4171 | { |
| 4172 | QCBORItem Item; |
| 4173 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4174 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4175 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4176 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4177 | // The above conversion succeeded |
| 4178 | return; |
| 4179 | } |
| 4180 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4181 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4182 | // 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] | 4183 | return; |
| 4184 | } |
| 4185 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4186 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4187 | } |
| 4188 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4189 | |
| 4190 | /* |
| 4191 | Public function, see header qcbor/qcbor_decode.h file |
| 4192 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4193 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4194 | int64_t nLabel, |
| 4195 | uint32_t uConvertTypes, |
| 4196 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4197 | { |
| 4198 | QCBORItem Item; |
| 4199 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4200 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, |
| 4201 | nLabel, |
| 4202 | uConvertTypes, |
| 4203 | pnValue, |
| 4204 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4205 | |
| 4206 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4207 | // The above conversion succeeded |
| 4208 | return; |
| 4209 | } |
| 4210 | |
| 4211 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4212 | // The above conversion failed in a way that code below can't correct |
| 4213 | return; |
| 4214 | } |
| 4215 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4216 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4220 | /* |
| 4221 | Public function, see header qcbor/qcbor_decode.h file |
| 4222 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4223 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4224 | const char *szLabel, |
| 4225 | uint32_t uConvertTypes, |
| 4226 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4227 | { |
| 4228 | QCBORItem Item; |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4229 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, |
| 4230 | szLabel, |
| 4231 | uConvertTypes, |
| 4232 | pnValue, |
| 4233 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4234 | |
| 4235 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4236 | // The above conversion succeeded |
| 4237 | return; |
| 4238 | } |
| 4239 | |
| 4240 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4241 | // The above conversion failed in a way that code below can't correct |
| 4242 | return; |
| 4243 | } |
| 4244 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4245 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4249 | static QCBORError ConvertUInt64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4250 | { |
| 4251 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4252 | case QCBOR_TYPE_DOUBLE: |
| 4253 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4254 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4255 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4256 | // Can't use llround here because it will not convert values |
| 4257 | // greater than INT64_MAX and less than UINT64_MAX that |
| 4258 | // need to be converted so it is more complicated. |
| 4259 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 4260 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4261 | if(isnan(pItem->val.dfnum)) { |
| 4262 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4263 | } else if(pItem->val.dfnum < 0) { |
| 4264 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4265 | } else { |
| 4266 | double dRounded = round(pItem->val.dfnum); |
| 4267 | // See discussion in DecodeDateEpoch() for |
| 4268 | // explanation of - 0x7ff |
| 4269 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 4270 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4271 | } |
| 4272 | *puValue = (uint64_t)dRounded; |
| 4273 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4274 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4275 | if(isnan(pItem->val.fnum)) { |
| 4276 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4277 | } else if(pItem->val.fnum < 0) { |
| 4278 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4279 | } else { |
| 4280 | float fRounded = roundf(pItem->val.fnum); |
| 4281 | // See discussion in DecodeDateEpoch() for |
| 4282 | // explanation of - 0x7ff |
| 4283 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 4284 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4285 | } |
| 4286 | *puValue = (uint64_t)fRounded; |
| 4287 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4288 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4289 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4290 | // round() and roundf() shouldn't result in exceptions here, but |
| 4291 | // catch them to be robust and thorough. Don't try to |
| 4292 | // distinguish between the various exceptions because it seems |
| 4293 | // they vary by CPU, compiler and OS. |
| 4294 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 4295 | } |
| 4296 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4297 | } else { |
| 4298 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4299 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4300 | #else |
| 4301 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4302 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4303 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4304 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4305 | case QCBOR_TYPE_INT64: |
| 4306 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4307 | if(pItem->val.int64 >= 0) { |
| 4308 | *puValue = (uint64_t)pItem->val.int64; |
| 4309 | } else { |
| 4310 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4311 | } |
| 4312 | } else { |
| 4313 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4314 | } |
| 4315 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4316 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4317 | case QCBOR_TYPE_UINT64: |
| 4318 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4319 | *puValue = pItem->val.uint64; |
| 4320 | } else { |
| 4321 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4322 | } |
| 4323 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4324 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4325 | default: |
| 4326 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4327 | } |
| 4328 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4329 | return QCBOR_SUCCESS; |
| 4330 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4331 | |
| 4332 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4333 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4334 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4335 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4336 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4337 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4338 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4339 | return; |
| 4340 | } |
| 4341 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4342 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4343 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4344 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4345 | if(uError) { |
| 4346 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4347 | return; |
| 4348 | } |
| 4349 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4350 | if(pItem) { |
| 4351 | *pItem = Item; |
| 4352 | } |
| 4353 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4354 | pMe->uLastError = (uint8_t)ConvertUInt64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4355 | } |
| 4356 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4357 | |
Laurence Lundblade | 95b64bb | 2020-08-30 00:17:38 -0700 | [diff] [blame] | 4358 | void QCBORDecode_GetUInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4359 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4360 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4361 | uint64_t *puValue, |
| 4362 | QCBORItem *pItem) |
| 4363 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4364 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4365 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4366 | return; |
| 4367 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4368 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4369 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4373 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4374 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4375 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4376 | uint64_t *puValue, |
| 4377 | QCBORItem *pItem) |
| 4378 | { |
| 4379 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4380 | return; |
| 4381 | } |
| 4382 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4383 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4384 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4385 | return; |
| 4386 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4387 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4388 | pMe->uLastError = (uint8_t)ConvertUInt64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4389 | } |
| 4390 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4391 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 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 |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 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 | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame^] | 4576 | |
| 4577 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4578 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4579 | uint32_t uConvertTypes, |
| 4580 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4581 | { |
| 4582 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4583 | case QCBOR_TYPE_FLOAT: |
| 4584 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4585 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4586 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4587 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4588 | *pdValue = (double)pItem->val.fnum; |
| 4589 | } else { |
| 4590 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4591 | } |
| 4592 | } |
| 4593 | #else |
| 4594 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4595 | #endif |
| 4596 | break; |
| 4597 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4598 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4599 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4600 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4601 | *pdValue = pItem->val.dfnum; |
| 4602 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4603 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4604 | } |
| 4605 | } |
| 4606 | break; |
| 4607 | |
| 4608 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4609 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4610 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4611 | // A simple cast seems to do the job with no worry of exceptions. |
| 4612 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4613 | *pdValue = (double)pItem->val.int64; |
| 4614 | |
| 4615 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4616 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4617 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4618 | #else |
| 4619 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4620 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4621 | break; |
| 4622 | |
| 4623 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4624 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4625 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4626 | // A simple cast seems to do the job with no worry of exceptions. |
| 4627 | // There will be precision loss for some values. |
| 4628 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4629 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4630 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4631 | } |
| 4632 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4633 | #else |
| 4634 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4635 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4636 | |
| 4637 | default: |
| 4638 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4639 | } |
| 4640 | |
| 4641 | return QCBOR_SUCCESS; |
| 4642 | } |
| 4643 | |
| 4644 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4645 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4646 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4647 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4648 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4649 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4650 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4651 | return; |
| 4652 | } |
| 4653 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4654 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4655 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4656 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4657 | if(uError) { |
| 4658 | pMe->uLastError = (uint8_t)uError; |
| 4659 | return; |
| 4660 | } |
| 4661 | |
| 4662 | if(pItem) { |
| 4663 | *pItem = Item; |
| 4664 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4665 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4666 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4667 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4668 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4669 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4670 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4671 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4672 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4673 | double *pdValue, |
| 4674 | QCBORItem *pItem) |
| 4675 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4676 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4677 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4678 | return; |
| 4679 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4680 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4681 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4682 | } |
| 4683 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4684 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4685 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4686 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4687 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4688 | double *pdValue, |
| 4689 | QCBORItem *pItem) |
| 4690 | { |
| 4691 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4692 | return; |
| 4693 | } |
| 4694 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4695 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4696 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4697 | return; |
| 4698 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4699 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4700 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4704 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4705 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 4706 | { |
| 4707 | double dResult; |
| 4708 | |
| 4709 | dResult = 0.0; |
| 4710 | const uint8_t *pByte = BigNum.ptr; |
| 4711 | size_t uLen = BigNum.len; |
| 4712 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 4713 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4714 | while(uLen--) { |
| 4715 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 4716 | } |
| 4717 | |
| 4718 | return dResult; |
| 4719 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4720 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4721 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4722 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4723 | static QCBORError |
| 4724 | DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4725 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4726 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4727 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4728 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 4729 | |
| 4730 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4731 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4732 | |
| 4733 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4734 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4735 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4736 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4737 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4738 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 4739 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4740 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4741 | } |
| 4742 | break; |
| 4743 | |
| 4744 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4745 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 4746 | // Underflow gives 0, overflow gives infinity |
| 4747 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4748 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4749 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4750 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4751 | } |
| 4752 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4753 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4754 | |
| 4755 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4756 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4757 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 4758 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4759 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4760 | } |
| 4761 | break; |
| 4762 | |
| 4763 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4764 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4765 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4766 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4767 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4768 | } |
| 4769 | break; |
| 4770 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4771 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4772 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4773 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4774 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4775 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4776 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4777 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4778 | } |
| 4779 | break; |
| 4780 | |
| 4781 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4782 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4783 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4784 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4785 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4786 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4787 | } |
| 4788 | break; |
| 4789 | |
| 4790 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4791 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4792 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4793 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4794 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4795 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4796 | } |
| 4797 | break; |
| 4798 | |
| 4799 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4800 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4801 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4802 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4803 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4804 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4805 | } |
| 4806 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4807 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4808 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4809 | default: |
| 4810 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4811 | } |
| 4812 | |
| 4813 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4814 | |
| 4815 | #else |
| 4816 | (void)pItem; |
| 4817 | (void)uConvertTypes; |
| 4818 | (void)pdValue; |
| 4819 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4820 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4821 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | |
| 4825 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4826 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4827 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4828 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 4829 | uint32_t uConvertTypes, |
| 4830 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4831 | { |
| 4832 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4833 | QCBORItem Item; |
| 4834 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4835 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4836 | |
| 4837 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4838 | // The above conversion succeeded |
| 4839 | return; |
| 4840 | } |
| 4841 | |
| 4842 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4843 | // The above conversion failed in a way that code below can't correct |
| 4844 | return; |
| 4845 | } |
| 4846 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4847 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4848 | } |
| 4849 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4850 | |
| 4851 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4852 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4853 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4854 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4855 | int64_t nLabel, |
| 4856 | uint32_t uConvertTypes, |
| 4857 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4858 | { |
| 4859 | QCBORItem Item; |
| 4860 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4861 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4862 | |
| 4863 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4864 | // The above conversion succeeded |
| 4865 | return; |
| 4866 | } |
| 4867 | |
| 4868 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4869 | // The above conversion failed in a way that code below can't correct |
| 4870 | return; |
| 4871 | } |
| 4872 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4873 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | |
| 4877 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4878 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4879 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4880 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4881 | const char *szLabel, |
| 4882 | uint32_t uConvertTypes, |
| 4883 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4884 | { |
| 4885 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4886 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4887 | |
| 4888 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4889 | // The above conversion succeeded |
| 4890 | return; |
| 4891 | } |
| 4892 | |
| 4893 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4894 | // The above conversion failed in a way that code below can't correct |
| 4895 | return; |
| 4896 | } |
| 4897 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4898 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4899 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4900 | |
| 4901 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4902 | |
| 4903 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4904 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4905 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 4906 | { |
| 4907 | while((uInt & 0xff00000000000000UL) == 0) { |
| 4908 | uInt = uInt << 8; |
| 4909 | }; |
| 4910 | |
| 4911 | UsefulOutBuf UOB; |
| 4912 | |
| 4913 | UsefulOutBuf_Init(&UOB, Buffer); |
| 4914 | |
| 4915 | while(uInt) { |
| 4916 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 4917 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 4918 | uInt = uInt << 8; |
| 4919 | (void)xx; |
| 4920 | } |
| 4921 | |
| 4922 | return UsefulOutBuf_OutUBuf(&UOB); |
| 4923 | } |
| 4924 | |
| 4925 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4926 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 4927 | TagSpecification TagSpec, |
| 4928 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4929 | { |
| 4930 | QCBORError uErr; |
| 4931 | // 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] | 4932 | while(1) { |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 4933 | uErr = CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4934 | if(uErr != QCBOR_SUCCESS) { |
| 4935 | goto Done; |
| 4936 | } |
| 4937 | |
| 4938 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 4939 | break; // Successful exit. Moving on to finish decoding. |
| 4940 | } |
| 4941 | |
| 4942 | // The item is an array, which means an undecoded |
| 4943 | // mantissa and exponent, so decode it. It will then |
| 4944 | // have a different type and exit the loop if. |
| 4945 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 4946 | if(uErr != QCBOR_SUCCESS) { |
| 4947 | goto Done; |
| 4948 | } |
| 4949 | |
| 4950 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4951 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4952 | } |
| 4953 | Done: |
| 4954 | return uErr; |
| 4955 | } |
| 4956 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4957 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4958 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4959 | TagSpecification TagSpec, |
| 4960 | QCBORItem *pItem, |
| 4961 | int64_t *pnMantissa, |
| 4962 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4963 | { |
| 4964 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4965 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4966 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4967 | if(uErr != QCBOR_SUCCESS) { |
| 4968 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4969 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4970 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4971 | switch (pItem->uDataType) { |
| 4972 | |
| 4973 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 4974 | case QCBOR_TYPE_BIGFLOAT: |
| 4975 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 4976 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 4977 | break; |
| 4978 | |
| 4979 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 4980 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 4981 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 4982 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 4983 | break; |
| 4984 | |
| 4985 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 4986 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 4987 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 4988 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 4989 | break; |
| 4990 | |
| 4991 | default: |
| 4992 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4993 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 4994 | |
| 4995 | Done: |
| 4996 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4997 | } |
| 4998 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4999 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5000 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5001 | TagSpecification TagSpec, |
| 5002 | QCBORItem *pItem, |
| 5003 | UsefulBuf BufferForMantissa, |
| 5004 | UsefulBufC *pMantissa, |
| 5005 | bool *pbIsNegative, |
| 5006 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5007 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5008 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5009 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5010 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5011 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5012 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | uint64_t uMantissa; |
| 5016 | |
| 5017 | switch (pItem->uDataType) { |
| 5018 | |
| 5019 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5020 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5021 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5022 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5023 | *pbIsNegative = false; |
| 5024 | } else { |
| 5025 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5026 | *pbIsNegative = true; |
| 5027 | } |
| 5028 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5029 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5030 | break; |
| 5031 | |
| 5032 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5033 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5034 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5035 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5036 | *pbIsNegative = false; |
| 5037 | break; |
| 5038 | |
| 5039 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5040 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5041 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5042 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5043 | *pbIsNegative = true; |
| 5044 | break; |
| 5045 | |
| 5046 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5047 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5048 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5049 | |
| 5050 | Done: |
| 5051 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5055 | /* |
| 5056 | Public function, see header qcbor/qcbor_decode.h file |
| 5057 | */ |
| 5058 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5059 | uint8_t uTagRequirement, |
| 5060 | int64_t *pnMantissa, |
| 5061 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5062 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5063 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5064 | return; |
| 5065 | } |
| 5066 | |
| 5067 | QCBORItem Item; |
| 5068 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5069 | if(uError) { |
| 5070 | pMe->uLastError = (uint8_t)uError; |
| 5071 | return; |
| 5072 | } |
| 5073 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5074 | const TagSpecification TagSpec = |
| 5075 | { |
| 5076 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5077 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5078 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5079 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5080 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5081 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5082 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5083 | } |
| 5084 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5085 | |
| 5086 | /* |
| 5087 | Public function, see header qcbor/qcbor_decode.h file |
| 5088 | */ |
| 5089 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5090 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5091 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5092 | int64_t *pnMantissa, |
| 5093 | int64_t *pnExponent) |
| 5094 | { |
| 5095 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5096 | return; |
| 5097 | } |
| 5098 | |
| 5099 | QCBORItem Item; |
| 5100 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5101 | |
| 5102 | const TagSpecification TagSpec = |
| 5103 | { |
| 5104 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5105 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5106 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5107 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5108 | }; |
| 5109 | |
| 5110 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5111 | } |
| 5112 | |
| 5113 | |
| 5114 | /* |
| 5115 | Public function, see header qcbor/qcbor_decode.h file |
| 5116 | */ |
| 5117 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5118 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5119 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5120 | int64_t *pnMantissa, |
| 5121 | int64_t *pnExponent) |
| 5122 | { |
| 5123 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5124 | return; |
| 5125 | } |
| 5126 | |
| 5127 | QCBORItem Item; |
| 5128 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5129 | |
| 5130 | const TagSpecification TagSpec = |
| 5131 | { |
| 5132 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5133 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5134 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5135 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5136 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5137 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5138 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5139 | } |
| 5140 | |
| 5141 | |
| 5142 | /* |
| 5143 | Public function, see header qcbor/qcbor_decode.h file |
| 5144 | */ |
| 5145 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5146 | uint8_t uTagRequirement, |
| 5147 | UsefulBuf MantissaBuffer, |
| 5148 | UsefulBufC *pMantissa, |
| 5149 | bool *pbMantissaIsNegative, |
| 5150 | int64_t *pnExponent) |
| 5151 | { |
| 5152 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5153 | return; |
| 5154 | } |
| 5155 | |
| 5156 | QCBORItem Item; |
| 5157 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5158 | if(uError) { |
| 5159 | pMe->uLastError = (uint8_t)uError; |
| 5160 | return; |
| 5161 | } |
| 5162 | |
| 5163 | const TagSpecification TagSpec = |
| 5164 | { |
| 5165 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5166 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5167 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5168 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5169 | }; |
| 5170 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5171 | ProcessMantissaAndExponentBig(pMe, |
| 5172 | TagSpec, |
| 5173 | &Item, |
| 5174 | MantissaBuffer, |
| 5175 | pMantissa, |
| 5176 | pbMantissaIsNegative, |
| 5177 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5178 | } |
| 5179 | |
| 5180 | |
| 5181 | /* |
| 5182 | Public function, see header qcbor/qcbor_decode.h file |
| 5183 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5184 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5185 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5186 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5187 | UsefulBuf BufferForMantissa, |
| 5188 | UsefulBufC *pMantissa, |
| 5189 | bool *pbIsNegative, |
| 5190 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5191 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5192 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5193 | return; |
| 5194 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5195 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5196 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5197 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5198 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5199 | return; |
| 5200 | } |
| 5201 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5202 | const TagSpecification TagSpec = |
| 5203 | { |
| 5204 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5205 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5206 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5207 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5208 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5209 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5210 | ProcessMantissaAndExponentBig(pMe, |
| 5211 | TagSpec, |
| 5212 | &Item, |
| 5213 | BufferForMantissa, |
| 5214 | pMantissa, |
| 5215 | pbIsNegative, |
| 5216 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5217 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5218 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5219 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5220 | /* |
| 5221 | Public function, see header qcbor/qcbor_decode.h file |
| 5222 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5223 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5224 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5225 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5226 | UsefulBuf BufferForMantissa, |
| 5227 | UsefulBufC *pMantissa, |
| 5228 | bool *pbIsNegative, |
| 5229 | int64_t *pnExponent) |
| 5230 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5231 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5232 | return; |
| 5233 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5234 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5235 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5236 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5237 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5238 | return; |
| 5239 | } |
| 5240 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5241 | const TagSpecification TagSpec = |
| 5242 | { |
| 5243 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5244 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 5245 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5246 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5247 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5248 | |
| 5249 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5250 | } |
| 5251 | |
| 5252 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5253 | /* |
| 5254 | Public function, see header qcbor/qcbor_decode.h file |
| 5255 | */ |
| 5256 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5257 | uint8_t uTagRequirement, |
| 5258 | int64_t *pnMantissa, |
| 5259 | int64_t *pnExponent) |
| 5260 | { |
| 5261 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5262 | return; |
| 5263 | } |
| 5264 | |
| 5265 | QCBORItem Item; |
| 5266 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5267 | if(uError) { |
| 5268 | pMe->uLastError = (uint8_t)uError; |
| 5269 | return; |
| 5270 | } |
| 5271 | const TagSpecification TagSpec = |
| 5272 | { |
| 5273 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5274 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5275 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5276 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5277 | }; |
| 5278 | |
| 5279 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5280 | } |
| 5281 | |
| 5282 | |
| 5283 | /* |
| 5284 | Public function, see header qcbor/qcbor_decode.h file |
| 5285 | */ |
| 5286 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5287 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5288 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5289 | int64_t *pnMantissa, |
| 5290 | int64_t *pnExponent) |
| 5291 | { |
| 5292 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5293 | return; |
| 5294 | } |
| 5295 | |
| 5296 | QCBORItem Item; |
| 5297 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5298 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5299 | return; |
| 5300 | } |
| 5301 | |
| 5302 | const TagSpecification TagSpec = |
| 5303 | { |
| 5304 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5305 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5306 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5307 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5308 | }; |
| 5309 | |
| 5310 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5311 | } |
| 5312 | |
| 5313 | |
| 5314 | /* |
| 5315 | Public function, see header qcbor/qcbor_decode.h file |
| 5316 | */ |
| 5317 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5318 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5319 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5320 | int64_t *pnMantissa, |
| 5321 | int64_t *pnExponent) |
| 5322 | { |
| 5323 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5324 | return; |
| 5325 | } |
| 5326 | |
| 5327 | QCBORItem Item; |
| 5328 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5329 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5330 | return; |
| 5331 | } |
| 5332 | |
| 5333 | const TagSpecification TagSpec = |
| 5334 | { |
| 5335 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5336 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5337 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5338 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5339 | }; |
| 5340 | |
| 5341 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5342 | } |
| 5343 | |
| 5344 | |
| 5345 | /* |
| 5346 | Public function, see header qcbor/qcbor_decode.h file |
| 5347 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5348 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5349 | uint8_t uTagRequirement, |
| 5350 | UsefulBuf MantissaBuffer, |
| 5351 | UsefulBufC *pMantissa, |
| 5352 | bool *pbMantissaIsNegative, |
| 5353 | int64_t *pnExponent) |
| 5354 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5355 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5356 | return; |
| 5357 | } |
| 5358 | |
| 5359 | QCBORItem Item; |
| 5360 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5361 | if(uError) { |
| 5362 | pMe->uLastError = (uint8_t)uError; |
| 5363 | return; |
| 5364 | } |
| 5365 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5366 | const TagSpecification TagSpec = |
| 5367 | { |
| 5368 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5369 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5370 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5371 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5372 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5373 | |
| 5374 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5375 | } |
| 5376 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5377 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5378 | /* |
| 5379 | Public function, see header qcbor/qcbor_decode.h file |
| 5380 | */ |
| 5381 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5382 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5383 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5384 | UsefulBuf BufferForMantissa, |
| 5385 | UsefulBufC *pMantissa, |
| 5386 | bool *pbIsNegative, |
| 5387 | int64_t *pnExponent) |
| 5388 | { |
| 5389 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5390 | return; |
| 5391 | } |
| 5392 | |
| 5393 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5394 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5395 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5396 | return; |
| 5397 | } |
| 5398 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5399 | const TagSpecification TagSpec = |
| 5400 | { |
| 5401 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5402 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5403 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5404 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5405 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5406 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5407 | ProcessMantissaAndExponentBig(pMe, |
| 5408 | TagSpec, |
| 5409 | &Item, |
| 5410 | BufferForMantissa, |
| 5411 | pMantissa, |
| 5412 | pbIsNegative, |
| 5413 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5417 | /* |
| 5418 | Public function, see header qcbor/qcbor_decode.h file |
| 5419 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5420 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5421 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5422 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5423 | UsefulBuf BufferForMantissa, |
| 5424 | UsefulBufC *pMantissa, |
| 5425 | bool *pbIsNegative, |
| 5426 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5427 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5428 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5429 | return; |
| 5430 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5431 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5432 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5433 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5434 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5435 | return; |
| 5436 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5437 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5438 | const TagSpecification TagSpec = |
| 5439 | { |
| 5440 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 5441 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 5442 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 5443 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5444 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5445 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5446 | ProcessMantissaAndExponentBig(pMe, |
| 5447 | TagSpec, |
| 5448 | &Item, |
| 5449 | BufferForMantissa, |
| 5450 | pMantissa, |
| 5451 | pbIsNegative, |
| 5452 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5453 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5454 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5455 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |