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 |
| 39 | #include <math.h> // For isnan(). TODO: list |
| 40 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 41 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 42 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 43 | /* |
| 44 | This casts away the const-ness of a pointer, usually so it can be |
| 45 | freed or realloced. |
| 46 | */ |
| 47 | #define UNCONST_POINTER(ptr) ((void *)(ptr)) |
| 48 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 49 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 50 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 51 | inline static bool |
| 52 | // TODO: add more tests for QCBOR_TYPE_MAP_AS_ARRAY mode in qcbor_decode_tests.c |
| 53 | QCBORItem_IsMapOrArray(const QCBORItem *pMe) |
| 54 | { |
| 55 | const uint8_t uDataType = pMe->uDataType; |
| 56 | return uDataType == QCBOR_TYPE_MAP || |
| 57 | uDataType == QCBOR_TYPE_ARRAY || |
| 58 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
| 59 | } |
| 60 | |
| 61 | inline static bool |
| 62 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 63 | { |
| 64 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if(pMe->val.uCount != 0) { |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | inline static bool |
| 75 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem *pMe) |
| 76 | { |
| 77 | if(!QCBORItem_IsMapOrArray(pMe)){ |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | if(pMe->val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
| 82 | return false; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 88 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 89 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 90 | ===========================================================================*/ |
| 91 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 92 | /* |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 93 | See commecnts about and typedef of QCBORDecodeNesting in qcbor_private.h, the data structure |
| 94 | all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 95 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 96 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 97 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 98 | */ |
| 99 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 100 | |
| 101 | inline static uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 102 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 103 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 104 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 105 | /* |
| 106 | Limit in DecodeNesting_Descend against more than |
| 107 | QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
| 108 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 109 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 112 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 113 | inline static uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 114 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 115 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 116 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 117 | /* |
| 118 | Limit in DecodeNesting_Descend against more than |
| 119 | QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
| 120 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 121 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 124 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 125 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 126 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 127 | { |
| 128 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 129 | } |
| 130 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 131 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 132 | static inline bool |
| 133 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 134 | { |
| 135 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 136 | return true; |
| 137 | } else { |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 143 | inline static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 144 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 145 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 146 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 147 | return true; |
| 148 | } else { |
| 149 | return false; |
| 150 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 153 | |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 154 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 155 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 156 | { |
| 157 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 158 | // Not a map or array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 161 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 162 | // Is indefinite |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 163 | return false; |
| 164 | } |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 165 | // All checks passed; is a definte length map or array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 169 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 170 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 171 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 172 | { |
| 173 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 174 | // is a byte string |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 180 | |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 181 | inline static bool DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 182 | { |
| 183 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 184 | return true; |
| 185 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 186 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 192 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 193 | inline static void DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 194 | { |
| 195 | // Should be only called on maps and arrays |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 196 | /* |
| 197 | DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 198 | larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 199 | uin32_t so the cast is safe. |
| 200 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 201 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 202 | |
| 203 | if(bIsEmpty) { |
| 204 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 205 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 208 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 209 | inline static void DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 210 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 211 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 214 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 215 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 216 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 217 | { |
| 218 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 219 | // No bounded map or array or... set up |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 223 | // Not a map or array; end of those is by byte count */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { // TODO: pCurrent vs pCurrentBounded |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 227 | // Not at a bounded level |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 228 | return false; |
| 229 | } |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 230 | // Works for both definite and indefinite length maps/arrays |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 231 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 232 | // Count is not zero, still unconsumed item |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 235 | // All checks passed, got to the end of a map/array |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 236 | return true; |
| 237 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 238 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 239 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 240 | inline static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 241 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 242 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 243 | // Must only be called on map / array |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 244 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 245 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 246 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 247 | return false; |
| 248 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 251 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 252 | inline static bool |
| 253 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 254 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 255 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 256 | return true; |
| 257 | } else { |
| 258 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 259 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 262 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 263 | inline static bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 264 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 265 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 266 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 267 | return false; |
| 268 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 269 | |
| 270 | if(pNesting->pCurrentBounded->uLevelType != uType) { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 277 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 278 | inline static void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 279 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 280 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 281 | // Only call on a defnite length array / map |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 282 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 283 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 284 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 285 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 286 | inline static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 287 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 288 | { |
| 289 | // Only call on a defnite length array / map |
| 290 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | inline static void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 295 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 296 | { |
| 297 | pNesting->pCurrent--; |
| 298 | } |
| 299 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 300 | |
| 301 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 302 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 303 | { |
| 304 | // Error out if nesting is too deep |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 305 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 306 | return QCBOR_ERR_ARRAY_NESTING_TOO_DEEP; |
| 307 | } |
| 308 | |
| 309 | // The actual descend |
| 310 | pNesting->pCurrent++; |
| 311 | |
| 312 | pNesting->pCurrent->uLevelType = uType; |
| 313 | |
| 314 | return QCBOR_SUCCESS; |
| 315 | } |
| 316 | |
| 317 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 318 | inline static QCBORError |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 319 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 320 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 321 | /* |
| 322 | Should only be called on map/array. |
| 323 | |
| 324 | Have descended into this before this is called. The job here is |
| 325 | just to mark it in bounded mode. |
| 326 | */ |
Laurence Lundblade | 287b25c | 2020-08-06 13:48:42 -0700 | [diff] [blame] | 327 | if(uOffset >= QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 328 | return QCBOR_ERR_BUFFER_TOO_LARGE; |
| 329 | } |
| 330 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 331 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 332 | |
| 333 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 334 | |
| 335 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 338 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 339 | inline static QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 340 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 341 | uint8_t uQCBORType, |
| 342 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 343 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 344 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 345 | |
| 346 | if(uCount == 0) { |
| 347 | // Nothing to do for empty definite lenth arrays. They are just are |
| 348 | // effectively the same as an item that is not a map or array |
| 349 | goto Done; |
| 350 | // Empty indefinite length maps and arrays are handled elsewhere |
| 351 | } |
| 352 | |
| 353 | // Error out if arrays is too long to handle |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 354 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 355 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 356 | uError = QCBOR_ERR_ARRAY_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 357 | goto Done; |
| 358 | } |
| 359 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 360 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 361 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 362 | goto Done; |
| 363 | } |
| 364 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 365 | // Fill in the new map/array level. Check above makes casts OK. |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 366 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 367 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 368 | |
| 369 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 370 | |
| 371 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 372 | return uError;; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | static inline void |
| 377 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 378 | { |
| 379 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | static inline void |
| 384 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 385 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 386 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 387 | pNesting->pCurrentBounded--; |
| 388 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 389 | break; |
| 390 | } |
| 391 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 394 | static inline void |
| 395 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 396 | { |
| 397 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 398 | } |
| 399 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 400 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 401 | inline static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 402 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 403 | uint32_t uEndOffset, |
| 404 | uint32_t uEndOfBstr) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 405 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 406 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 407 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 408 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 409 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 410 | goto Done; |
| 411 | } |
| 412 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 413 | // Fill in the new byte string level |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 414 | pNesting->pCurrent->u.bs.uPreviousEndOffset = uEndOffset; |
| 415 | pNesting->pCurrent->u.bs.uEndOfBstr = uEndOfBstr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 416 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 417 | // Bstr wrapped levels are always bounded |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 418 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 419 | |
| 420 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 421 | return uError;; |
| 422 | } |
| 423 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 424 | |
| 425 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 426 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 427 | { |
| 428 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 431 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 432 | inline static void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 433 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 434 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 435 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 436 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 437 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 441 | inline static void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 442 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 443 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 444 | *pSave = *pNesting; |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 445 | pNesting->pCurrent = pNesting->pCurrentBounded; |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 446 | pNesting->pCurrent->u.ma.uCountCursor = pNesting->pCurrent->u.ma.uCountTotal; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 449 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 450 | static inline void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 451 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 452 | { |
| 453 | *pNesting = *pSave; |
| 454 | } |
| 455 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 456 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 457 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 458 | DecodeNesting_GetEndOfBstr(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 459 | { |
| 460 | return pMe->pCurrentBounded->u.bs.uEndOfBstr; |
| 461 | } |
| 462 | |
| 463 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 464 | static inline uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 465 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 466 | { |
| 467 | return pMe->pCurrentBounded->u.bs.uPreviousEndOffset; |
| 468 | } |
| 469 | |
| 470 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 471 | #include <stdio.h> |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 472 | |
| 473 | const char *TypeStr(uint8_t type) |
| 474 | { |
| 475 | switch(type) { |
| 476 | case QCBOR_TYPE_MAP: return " map"; |
| 477 | case QCBOR_TYPE_ARRAY: return "array"; |
| 478 | case QCBOR_TYPE_BYTE_STRING: return " bstr"; |
| 479 | default: return " --- "; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static char buf[20]; // Not thread safe, but that is OK |
| 484 | const char *CountString(uint16_t uCount, uint16_t uTotal) |
| 485 | { |
| 486 | if(uTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
| 487 | strcpy(buf, "indefinite"); |
| 488 | } else { |
| 489 | sprintf(buf, "%d/%d", uCount, uTotal); |
| 490 | } |
| 491 | return buf; |
| 492 | } |
| 493 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 494 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 495 | void DecodeNesting_Print(QCBORDecodeNesting *pNesting, UsefulInputBuf *pBuf, const char *szName) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 496 | { |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 497 | #if 0 |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 498 | printf("---%s--%d/%d--\narrow is current bounded level\n", |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 499 | szName, |
| 500 | (uint32_t)pBuf->cursor, |
| 501 | (uint32_t)pBuf->UB.len); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 502 | |
| 503 | printf("Level Type Count Offsets \n"); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 504 | for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 505 | if(&(pNesting->pLevels[i]) > pNesting->pCurrent) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 506 | break; |
| 507 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 508 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 509 | printf("%2s %2d %s ", |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 510 | pNesting->pCurrentBounded == &(pNesting->pLevels[i]) ? "->": " ", |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 511 | i, |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 512 | TypeStr(pNesting->pLevels[i].uLevelType)); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 513 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 514 | if(pNesting->pLevels[i].uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 515 | printf(" %5d %5d", |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 516 | pNesting->pLevels[i].u.bs.uEndOfBstr, |
| 517 | pNesting->pLevels[i].u.bs.uPreviousEndOffset); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 518 | |
| 519 | } else { |
| 520 | printf("%10.10s ", |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 521 | CountString(pNesting->pLevels[i].u.ma.uCountCursor, |
| 522 | pNesting->pLevels[i].u.ma.uCountTotal)); |
| 523 | if(pNesting->pLevels[i].u.ma.uStartOffset != UINT32_MAX) { |
| 524 | printf("Bounded start: %u",pNesting->pLevels[i].u.ma.uStartOffset); |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | printf("\n"); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 529 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 530 | printf("\n"); |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 531 | #endif |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 534 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 535 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 536 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 537 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 538 | |
| 539 | The following four functions are pretty wrappers for invocation of |
| 540 | the string allocator supplied by the caller. |
| 541 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 542 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 543 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 544 | static inline void |
| 545 | StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 546 | { |
| 547 | (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0); |
| 548 | } |
| 549 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 550 | // StringAllocator_Reallocate called with pMem NULL is |
| 551 | // equal to StringAllocator_Allocate() |
| 552 | static inline UsefulBuf |
| 553 | StringAllocator_Reallocate(const QCORInternalAllocator *pMe, |
| 554 | void *pMem, |
| 555 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 556 | { |
| 557 | return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize); |
| 558 | } |
| 559 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 560 | static inline UsefulBuf |
| 561 | StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 562 | { |
| 563 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 564 | } |
| 565 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 566 | static inline void |
| 567 | StringAllocator_Destruct(const QCORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 568 | { |
| 569 | if(pMe->pfAllocator) { |
| 570 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | |
| 575 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 576 | /*=========================================================================== |
| 577 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 578 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 579 | See qcbor/qcbor_decode.h for definition of the object |
| 580 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 581 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 582 | /* |
| 583 | Public function, see header file |
| 584 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 585 | void QCBORDecode_Init(QCBORDecodeContext *me, |
| 586 | UsefulBufC EncodedCBOR, |
| 587 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 588 | { |
| 589 | memset(me, 0, sizeof(QCBORDecodeContext)); |
| 590 | UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 591 | // Don't bother with error check on decode mode. If a bad value is |
| 592 | // 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] | 593 | me->uDecodeMode = (uint8_t)nDecodeMode; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 594 | DecodeNesting_Init(&(me->nesting)); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 595 | for(int i = 0; i < QCBOR_NUM_MAPPED_TAGS; i++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 596 | me->auMappedTags[i] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 597 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | |
| 601 | /* |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 602 | Public function, see header file |
| 603 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 604 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 605 | QCBORStringAllocate pfAllocateFunction, |
| 606 | void *pAllocateContext, |
| 607 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 608 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 609 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 610 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 611 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | Public function, see header file |
| 617 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 618 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 619 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 620 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 621 | // This does nothing now. It is retained for backwards compatibility |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 622 | (void)pMe; |
| 623 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 624 | } |
| 625 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 626 | |
| 627 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 628 | This decodes the fundamental part of a CBOR data item, the type and |
| 629 | number |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 630 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 631 | This is the Counterpart to InsertEncodedTypeAndNumber(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 632 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 633 | This does the network->host byte order conversion. The conversion |
| 634 | here also results in the conversion for floats in addition to that |
| 635 | for lengths, tags and integer values. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 636 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 637 | This returns: |
| 638 | pnMajorType -- the major type for the item |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 639 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 640 | puArgument -- the "number" which is used a the value for integers, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 641 | tags and floats and length for strings and arrays |
| 642 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 643 | pnAdditionalInfo -- Pass this along to know what kind of float or |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 644 | if length is indefinite |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 645 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 646 | The int type is preferred to uint8_t for some variables as this |
| 647 | avoids integer promotions, can reduce code size and makes |
| 648 | static analyzers happier. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 649 | |
| 650 | @retval QCBOR_ERR_UNSUPPORTED |
| 651 | |
| 652 | @retval QCBOR_ERR_HIT_END |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 653 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 654 | inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, |
| 655 | int *pnMajorType, |
| 656 | uint64_t *puArgument, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 657 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 658 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 659 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 660 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 661 | // Get the initial byte that every CBOR data item has |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 662 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 663 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 664 | // Break down the initial byte |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 665 | const int nTmpMajorType = nInitialByte >> 5; |
| 666 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 667 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 668 | // Where the number or argument accumulates |
| 669 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 670 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 671 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 672 | // Need to get 1,2,4 or 8 additional argument bytes. Map |
| 673 | // LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 674 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 675 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 676 | // Loop getting all the bytes in the argument |
| 677 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 678 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 679 | // This shift and add gives the endian conversion |
| 680 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 681 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 682 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 683 | // The reserved and thus-far unused additional info values |
| 684 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 685 | goto Done; |
| 686 | } else { |
| 687 | // Less than 24, additional info is argument or 31, an indefinite length |
| 688 | // No more bytes to get |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 689 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 690 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 691 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 692 | if(UsefulInputBuf_GetError(pUInBuf)) { |
| 693 | nReturn = QCBOR_ERR_HIT_END; |
| 694 | goto Done; |
| 695 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 696 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 697 | // All successful if we got here. |
| 698 | nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 699 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 700 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 701 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 702 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 703 | Done: |
| 704 | return nReturn; |
| 705 | } |
| 706 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 707 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 708 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 709 | CBOR doesn't explicitly specify two's compliment for integers but all |
| 710 | CPUs use it these days and the test vectors in the RFC are so. All |
| 711 | integers in the CBOR structure are positive and the major type |
| 712 | indicates positive or negative. CBOR can express positive integers |
| 713 | up to 2^x - 1 where x is the number of bits and negative integers |
| 714 | down to 2^x. Note that negative numbers can be one more away from |
| 715 | zero than positive. Stdint, as far as I can tell, uses two's |
| 716 | compliment to represent negative integers. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 717 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 718 | See http://www.unix.org/whitepapers/64bit.html for reasons int is |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 719 | used carefully here, and in particular why it isn't used in the interface. |
| 720 | Also see |
| 721 | https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 722 | |
| 723 | Int is used for values that need less than 16-bits and would be subject |
| 724 | to integer promotion and complaining by static analyzers. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 725 | |
| 726 | @retval QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 727 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 728 | inline static QCBORError |
| 729 | DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 730 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 731 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 732 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 733 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
| 734 | if (uNumber <= INT64_MAX) { |
| 735 | pDecodedItem->val.int64 = (int64_t)uNumber; |
| 736 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 737 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 738 | } else { |
| 739 | pDecodedItem->val.uint64 = uNumber; |
| 740 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 741 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 742 | } |
| 743 | } else { |
| 744 | if(uNumber <= INT64_MAX) { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 745 | // CBOR's representation of negative numbers lines up with the |
| 746 | // two-compliment representation. A negative integer has one |
| 747 | // more in range than a positive integer. INT64_MIN is |
| 748 | // equal to (-INT64_MAX) - 1. |
| 749 | pDecodedItem->val.int64 = (-(int64_t)uNumber) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 750 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 751 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 752 | } else { |
| 753 | // C can't represent a negative integer in this range |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 754 | // so it is an error. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 755 | nReturn = QCBOR_ERR_INT_OVERFLOW; |
| 756 | } |
| 757 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 758 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 759 | return nReturn; |
| 760 | } |
| 761 | |
| 762 | // Make sure #define value line up as DecodeSimple counts on this. |
| 763 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 764 | #error QCBOR_TYPE_FALSE macro value wrong |
| 765 | #endif |
| 766 | |
| 767 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 768 | #error QCBOR_TYPE_TRUE macro value wrong |
| 769 | #endif |
| 770 | |
| 771 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 772 | #error QCBOR_TYPE_NULL macro value wrong |
| 773 | #endif |
| 774 | |
| 775 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 776 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 777 | #endif |
| 778 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 779 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 780 | #error QCBOR_TYPE_BREAK macro value wrong |
| 781 | #endif |
| 782 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 783 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 784 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 785 | #endif |
| 786 | |
| 787 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 788 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 789 | #endif |
| 790 | |
| 791 | /* |
| 792 | Decode true, false, floats, break... |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 793 | |
| 794 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 795 | |
| 796 | @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 797 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 798 | inline static QCBORError |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 799 | DecodeSimple(int nAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 800 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 801 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 802 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 803 | // uAdditionalInfo is 5 bits from the initial byte compile time checks |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 804 | // above make sure uAdditionalInfo values line up with uDataType values. |
| 805 | // DecodeTypeAndNumber never returns a major type > 1f so cast is safe |
| 806 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 807 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 808 | switch(nAdditionalInfo) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 809 | // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are |
| 810 | // caught before this is called. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 811 | |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 812 | case HALF_PREC_FLOAT: |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 813 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 814 | // Half-precision is returned as a double. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 815 | // The cast to uint16_t is safe because the encoded value |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 816 | // 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] | 817 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber); |
| 818 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 819 | #else |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 820 | nReturn = QCBOR_ERR_HALF_PRECISION_DISABLED; |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 821 | #endif |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 822 | break; |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 823 | case SINGLE_PREC_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 824 | // Single precision is normally returned as a double |
| 825 | // since double is widely supported, there is no loss of |
| 826 | // precision, it makes it easy for the caller in |
| 827 | // most cases and it can be converted back to single |
| 828 | // with no loss of precision |
| 829 | // |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 830 | // The cast to uint32_t is safe because the encoded value |
Laurence Lundblade | 8fa7d5d | 2020-07-11 16:30:47 -0700 | [diff] [blame] | 831 | // 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] | 832 | { |
| 833 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber); |
| 834 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 835 | // In the normal case, use HW to convert float to double. |
| 836 | pDecodedItem->val.dfnum = (double)f; |
| 837 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 838 | #else |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 839 | // Use of float HW is disabled, return as a float. |
| 840 | pDecodedItem->val.fnum = f; |
| 841 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 842 | |
| 843 | // IEEE754_FloatToDouble() could be used here to return |
| 844 | // as a double, but it adds object code and most likely |
| 845 | // anyone disabling FLOAT HW use doesn't care about |
| 846 | // floats and wants to save object code. |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 847 | #endif |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 848 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 849 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 850 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 851 | case DOUBLE_PREC_FLOAT: |
| 852 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 853 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 854 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 855 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 856 | case CBOR_SIMPLEV_FALSE: // 20 |
| 857 | case CBOR_SIMPLEV_TRUE: // 21 |
| 858 | case CBOR_SIMPLEV_NULL: // 22 |
| 859 | case CBOR_SIMPLEV_UNDEF: // 23 |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 860 | case CBOR_SIMPLE_BREAK: // 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 861 | break; // nothing to do |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 862 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 863 | case CBOR_SIMPLEV_ONEBYTE: // 24 |
| 864 | if(uNumber <= CBOR_SIMPLE_BREAK) { |
| 865 | // 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] | 866 | nReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 867 | goto Done; |
| 868 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 869 | /* FALLTHROUGH */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 870 | // fall through intentionally |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 871 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 872 | default: // 0-19 |
| 873 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 874 | /* |
| 875 | DecodeTypeAndNumber will make uNumber equal to |
| 876 | uAdditionalInfo when uAdditionalInfo is < 24 This cast is |
| 877 | safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 878 | the double/float cases above |
| 879 | */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 880 | pDecodedItem->val.uSimple = (uint8_t)uNumber; |
| 881 | break; |
| 882 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 883 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 884 | Done: |
| 885 | return nReturn; |
| 886 | } |
| 887 | |
| 888 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 889 | /* |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 890 | Decode text and byte strings. Call the string allocator if asked to. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 891 | |
| 892 | @retval QCBOR_ERR_HIT_END |
| 893 | |
| 894 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 895 | |
| 896 | @retval QCBOR_ERR_STRING_TOO_LONG |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 897 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 898 | inline static QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator, |
| 899 | int nMajorType, |
| 900 | uint64_t uStrLen, |
| 901 | UsefulInputBuf *pUInBuf, |
| 902 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 903 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 904 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 905 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 906 | // CBOR lengths can be 64 bits, but size_t is not 64 bits on all CPUs. |
| 907 | // This check makes the casts to size_t below safe. |
| 908 | |
| 909 | // 4 bytes less than the largest sizeof() so this can be tested by |
| 910 | // putting a SIZE_MAX length in the CBOR test input (no one will |
| 911 | // care the limit on strings is 4 bytes shorter). |
| 912 | if(uStrLen > SIZE_MAX-4) { |
| 913 | nReturn = QCBOR_ERR_STRING_TOO_LONG; |
| 914 | goto Done; |
| 915 | } |
| 916 | |
| 917 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 918 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 919 | // Failed to get the bytes for this string item |
| 920 | nReturn = QCBOR_ERR_HIT_END; |
| 921 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 922 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 923 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 924 | if(pAllocator) { |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 925 | // We are asked to use string allocator to make a copy |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 926 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 927 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 928 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 929 | goto Done; |
| 930 | } |
| 931 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 932 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 933 | } else { |
| 934 | // Normal case with no string allocator |
| 935 | pDecodedItem->val.string = Bytes; |
| 936 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 937 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 938 | // Cast because ternary operator causes promotion to integer |
| 939 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 940 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 941 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 942 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 943 | return nReturn; |
| 944 | } |
| 945 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 946 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 947 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 948 | |
| 949 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 950 | |
| 951 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 952 | // Make sure the constants align as this is assumed by |
| 953 | // the GetAnItem() implementation |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 954 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 955 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 956 | #endif |
| 957 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 958 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 959 | #endif |
| 960 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 961 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 962 | This gets a single data item and decodes it including preceding |
| 963 | optional tagging. This does not deal with arrays and maps and nesting |
| 964 | except to decode the data item introducing them. Arrays and maps are |
| 965 | handled at the next level up in GetNext(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 966 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 967 | Errors detected here include: an array that is too long to decode, |
| 968 | hit end of buffer unexpectedly, a few forms of invalid encoded CBOR |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 969 | |
| 970 | @retval QCBOR_ERR_UNSUPPORTED |
| 971 | |
| 972 | @retval QCBOR_ERR_HIT_END |
| 973 | |
| 974 | @retval QCBOR_ERR_INT_OVERFLOW |
| 975 | |
| 976 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 977 | |
| 978 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 979 | |
| 980 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 981 | |
| 982 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 983 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 984 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 985 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, |
| 986 | QCBORItem *pDecodedItem, |
| 987 | const QCORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 988 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 989 | QCBORError nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 990 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 991 | /* |
| 992 | Get the major type and the number. Number could be length of more |
| 993 | bytes or the value depending on the major type nAdditionalInfo is |
| 994 | an encoding of the length of the uNumber and is needed to decode |
| 995 | floats and doubles |
| 996 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 997 | int nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 998 | uint64_t uNumber; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 999 | int nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1000 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 1001 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1002 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1003 | nReturn = DecodeTypeAndNumber(pUInBuf, &nMajorType, &uNumber, &nAdditionalInfo); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1004 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1005 | // Error out here if we got into trouble on the type and number. The |
| 1006 | // 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] | 1007 | if(nReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1008 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1009 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1010 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1011 | // At this point the major type and the value are valid. We've got |
| 1012 | // the type and the number that starts every CBOR data item. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1013 | switch (nMajorType) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1014 | case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0 |
| 1015 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1016 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1017 | nReturn = QCBOR_ERR_BAD_INT; |
| 1018 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1019 | nReturn = DecodeInteger(nMajorType, uNumber, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 1020 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1021 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1022 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1023 | case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2 |
| 1024 | case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1025 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1026 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
| 1027 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 1028 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 1029 | pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1030 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1031 | nReturn = DecodeBytes(pAllocator, nMajorType, uNumber, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1032 | } |
| 1033 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1034 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1035 | case CBOR_MAJOR_TYPE_ARRAY: // Major type 4 |
| 1036 | case CBOR_MAJOR_TYPE_MAP: // Major type 5 |
| 1037 | // Record the number of items in the array or map |
| 1038 | if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1039 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 1040 | goto Done; |
| 1041 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1042 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1043 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1044 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1045 | // type conversion OK because of check above |
| 1046 | pDecodedItem->val.uCount = (uint16_t)uNumber; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1047 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1048 | // C preproc #if above makes sure constants for major types align |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1049 | // DecodeTypeAndNumber never returns a major type > 7 so cast is safe |
| 1050 | pDecodedItem->uDataType = (uint8_t)nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1051 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1052 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1053 | case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1054 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1055 | nReturn = QCBOR_ERR_BAD_INT; |
| 1056 | } else { |
| 1057 | pDecodedItem->val.uTagV = uNumber; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1058 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1059 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1060 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1061 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1062 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1063 | // Major type 7, float, double, true, false, null... |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1064 | nReturn = DecodeSimple(nAdditionalInfo, uNumber, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1065 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1066 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1067 | default: |
| 1068 | // Never happens because DecodeTypeAndNumber() should never return > 7 |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1069 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 1070 | break; |
| 1071 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1072 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1073 | Done: |
| 1074 | return nReturn; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | /* |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1080 | This layer deals with indefinite length strings. It pulls all the |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1081 | individual chunk items together into one QCBORItem using the string |
| 1082 | allocator. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1083 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1084 | Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1085 | |
| 1086 | @retval QCBOR_ERR_UNSUPPORTED |
| 1087 | |
| 1088 | @retval QCBOR_ERR_HIT_END |
| 1089 | |
| 1090 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1091 | |
| 1092 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1093 | |
| 1094 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1095 | |
| 1096 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1097 | |
| 1098 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1099 | |
| 1100 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1101 | |
| 1102 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1103 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1104 | static inline QCBORError |
| 1105 | GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1106 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1107 | // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96 |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1108 | |
| 1109 | // Get pointer to string allocator. First use is to pass it to |
| 1110 | // GetNext_Item() when option is set to allocate for *every* string. |
| 1111 | // Second use here is to allocate space to coallese indefinite |
| 1112 | // length string items into one. |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1113 | const QCORInternalAllocator *pAllocator = me->StringAllocator.pfAllocator ? |
| 1114 | &(me->StringAllocator) : |
| 1115 | NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1116 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1117 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1118 | nReturn = GetNext_Item(&(me->InBuf), |
| 1119 | pDecodedItem, |
| 1120 | me->bStringAllocateAll ? pAllocator: NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1121 | if(nReturn) { |
| 1122 | goto Done; |
| 1123 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1124 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1125 | // To reduce code size by removing support for indefinite length strings, the |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1126 | // code in this function from here down can be eliminated. Run tests, except |
| 1127 | // 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] | 1128 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1129 | // Only do indefinite length processing on strings |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1130 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1131 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1132 | goto Done; // no need to do any work here on non-string types |
| 1133 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1134 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1135 | // Is this a string with an indefinite length? |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1136 | if(pDecodedItem->val.string.len != SIZE_MAX) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1137 | goto Done; // length is not indefinite, so no work to do here |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1138 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1139 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1140 | // Can't do indefinite length strings without a string allocator |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1141 | if(pAllocator == NULL) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1142 | nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1143 | goto Done; |
| 1144 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1145 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1146 | // Loop getting chunks of the indefinite length string |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1147 | UsefulBufC FullString = NULLUsefulBufC; |
| 1148 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1149 | for(;;) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1150 | // Get item for next chunk |
| 1151 | QCBORItem StringChunkItem; |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1152 | // NULL string allocator passed here. Do not need to allocate |
| 1153 | // chunks even if bStringAllocateAll is set. |
Laurence Lundblade | fae26bf | 2019-02-18 11:15:43 -0800 | [diff] [blame] | 1154 | nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1155 | if(nReturn) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1156 | break; // Error getting the next chunk |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1157 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1158 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1159 | // See if it is a marker at end of indefinite length string |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1160 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1161 | // String is complete |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1162 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1163 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1164 | break; |
| 1165 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1166 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1167 | // Match data type of chunk to type at beginning. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1168 | // Also catches error of other non-string types that don't belong. |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1169 | // Also catches indefinite length strings inside indefinite length strings |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1170 | if(StringChunkItem.uDataType != uStringType || |
| 1171 | StringChunkItem.val.string.len == SIZE_MAX) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1172 | nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1173 | break; |
| 1174 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1175 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1176 | // Alloc new buffer or expand previously allocated buffer so it can fit |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1177 | // The first time throurgh FullString.ptr is NULL and this is |
| 1178 | // equivalent to StringAllocator_Allocate() |
| 1179 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1180 | UNCONST_POINTER(FullString.ptr), |
| 1181 | FullString.len + StringChunkItem.val.string.len); |
| 1182 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1183 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1184 | // Allocation of memory for the string failed |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1185 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1186 | break; |
| 1187 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1188 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1189 | // Copy new string chunk at the end of string so far. |
| 1190 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1191 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1192 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1193 | if(nReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1194 | // Getting the item failed, clean up the allocated memory |
| 1195 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1196 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1197 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1198 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1199 | return nReturn; |
| 1200 | } |
| 1201 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1202 | static uint64_t ConvertTag(const QCBORDecodeContext *me, uint16_t uTagVal) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1203 | if(uTagVal <= QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1204 | return uTagVal; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1205 | } else if(uTagVal == CBOR_TAG_INVALID16) { |
| 1206 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1207 | } else { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1208 | const int x = uTagVal - (QCBOR_LAST_UNMAPPED_TAG + 1); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1209 | return me->auMappedTags[x]; |
| 1210 | } |
| 1211 | } |
| 1212 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1213 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1214 | /* |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1215 | Gets all optional tag data items preceding a data item that is not an |
| 1216 | optional tag and records them as bits in the tag map. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1217 | |
| 1218 | @retval QCBOR_ERR_UNSUPPORTED |
| 1219 | |
| 1220 | @retval QCBOR_ERR_HIT_END |
| 1221 | |
| 1222 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1223 | |
| 1224 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1225 | |
| 1226 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1227 | |
| 1228 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1229 | |
| 1230 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1231 | |
| 1232 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1233 | |
| 1234 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1235 | |
| 1236 | @retval QCBOR_ERR_TOO_MANY_TAGS |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1237 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1238 | static QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1239 | GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1240 | { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1241 | uint16_t auTags[QCBOR_MAX_TAGS_PER_ITEM] = {CBOR_TAG_INVALID16, |
| 1242 | CBOR_TAG_INVALID16, |
| 1243 | CBOR_TAG_INVALID16, |
| 1244 | CBOR_TAG_INVALID16}; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1245 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1246 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1247 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1248 | // Loop fetching items until the item fetched is not a tag |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1249 | for(;;) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1250 | QCBORError uErr = GetNext_FullItem(me, pDecodedItem); |
| 1251 | if(uErr != QCBOR_SUCCESS) { |
| 1252 | uReturn = uErr; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1253 | goto Done; // Error out of the loop |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1254 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1255 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1256 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1257 | // Successful exit from loop; maybe got some tags, maybe not |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1258 | memcpy(pDecodedItem->uTags, auTags, sizeof(auTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1259 | break; |
| 1260 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1261 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1262 | if(auTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1263 | // No room in the tag list |
| 1264 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1265 | // Continue on to get all tags on this item even though |
| 1266 | // it is erroring out in the end. This is a resource limit |
| 1267 | // error, not an problem with being well-formed CBOR. |
| 1268 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1269 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1270 | // Slide tags over one in the array to make room at index 0 |
| 1271 | for(size_t uTagIndex = QCBOR_MAX_TAGS_PER_ITEM - 1; uTagIndex > 0; uTagIndex--) { |
| 1272 | auTags[uTagIndex] = auTags[uTagIndex-1]; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1273 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1274 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1275 | // Is the tag > 16 bits? |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1276 | if(pDecodedItem->val.uTagV > QCBOR_LAST_UNMAPPED_TAG) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1277 | size_t uTagMapIndex; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1278 | // 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] | 1279 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 1280 | if(me->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1281 | break; |
| 1282 | } |
| 1283 | if(me->auMappedTags[uTagMapIndex] == pDecodedItem->val.uTagV) { |
| 1284 | break; |
| 1285 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1286 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1287 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1288 | // No room for the tag |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1289 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1290 | // Continue on to get all tags on this item even though |
| 1291 | // it is erroring out in the end. This is a resource limit |
| 1292 | // error, not an problem with being well-formed CBOR. |
| 1293 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 1296 | // 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] | 1297 | me->auMappedTags[uTagMapIndex] = pDecodedItem->val.uTagV; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1298 | auTags[0] = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1299 | |
| 1300 | } else { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1301 | auTags[0] = (uint16_t)pDecodedItem->val.uTagV; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1302 | } |
| 1303 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1304 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1305 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1306 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | |
| 1310 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1311 | This layer takes care of map entries. It combines the label and data |
| 1312 | items into one QCBORItem. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1313 | |
| 1314 | @retval QCBOR_ERR_UNSUPPORTED |
| 1315 | |
| 1316 | @retval QCBOR_ERR_HIT_END |
| 1317 | |
| 1318 | @retval QCBOR_ERR_INT_OVERFLOW |
| 1319 | |
| 1320 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1321 | |
| 1322 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1323 | |
| 1324 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
| 1325 | |
| 1326 | @retval QCBOR_ERR_BAD_TYPE_7 |
| 1327 | |
| 1328 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1329 | |
| 1330 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1331 | |
| 1332 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1333 | |
| 1334 | @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1335 | |
| 1336 | @retval QCBOR_ERR_ARRAY_TOO_LONG |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1337 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1338 | static inline QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1339 | GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1340 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1341 | // Stack use: int/ptr 1, QCBORItem -- 56 |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1342 | QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1343 | if(nReturn) |
| 1344 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1345 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1346 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1347 | // Break can't be a map entry |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1348 | goto Done; |
| 1349 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1350 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1351 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1352 | // In a map and caller wants maps decoded, not treated as arrays |
| 1353 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1354 | if(DecodeNesting_IsCurrentTypeMap(&(me->nesting))) { |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1355 | // If in a map and the right decoding mode, get the label |
| 1356 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1357 | // Save label in pDecodedItem and get the next which will |
| 1358 | // be the real data |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1359 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1360 | nReturn = GetNext_TaggedItem(me, pDecodedItem); |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1361 | if(nReturn) |
| 1362 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1363 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1364 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1365 | |
| 1366 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1367 | // strings are always good labels |
| 1368 | pDecodedItem->label.string = LabelItem.val.string; |
| 1369 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 1370 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1371 | // It's not a string and we only want strings |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1372 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1373 | goto Done; |
| 1374 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1375 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1376 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1377 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1378 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1379 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1380 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1381 | pDecodedItem->label.string = LabelItem.val.string; |
| 1382 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1383 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1384 | } else { |
| 1385 | // label is not an int or a string. It is an arrray |
| 1386 | // or a float or such and this implementation doesn't handle that. |
| 1387 | // Also, tags on labels are ignored. |
| 1388 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1389 | goto Done; |
| 1390 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1391 | } |
| 1392 | } else { |
| 1393 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1394 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
| 1395 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 1396 | goto Done; |
| 1397 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1398 | // Decoding a map as an array |
| 1399 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1400 | // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2 |
| 1401 | // Cast is needed because of integer promotion |
| 1402 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1403 | } |
| 1404 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1405 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1406 | Done: |
| 1407 | return nReturn; |
| 1408 | } |
| 1409 | |
| 1410 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1411 | /* |
| 1412 | See if next item is a CBOR break. If it is, it is consumed, |
| 1413 | if not it is not consumed. |
| 1414 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1415 | static inline QCBORError |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1416 | NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
| 1417 | { |
| 1418 | *pbNextIsBreak = false; |
| 1419 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1420 | QCBORItem Peek; |
| 1421 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
| 1422 | QCBORError uReturn = GetNext_Item(pUIB, &Peek, NULL); |
| 1423 | if(uReturn != QCBOR_SUCCESS) { |
| 1424 | return uReturn; |
| 1425 | } |
| 1426 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1427 | // It is not a break, rewind so it can be processed normally. |
| 1428 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1429 | } else { |
| 1430 | *pbNextIsBreak = true; |
| 1431 | } |
| 1432 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1433 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1434 | return QCBOR_SUCCESS; |
| 1435 | } |
| 1436 | |
| 1437 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1438 | /* |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1439 | An item was just consumed, now figure out if it was the |
| 1440 | end of an array or map that can be closed out. That |
| 1441 | may in turn close out another map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1442 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1443 | static QCBORError NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1444 | { |
| 1445 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1446 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1447 | /* This loops ascending nesting levels as long as there is ascending to do */ |
| 1448 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1449 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1450 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1451 | /* Decrement count for definite length maps / arrays */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1452 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1453 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1454 | /* 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] | 1455 | break; |
| 1456 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1457 | /* All of a definite length array was consumed; fall through to ascend */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1458 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1459 | } else { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1460 | /* If not definite length, have to check for a CBOR break */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1461 | bool bIsBreak = false; |
| 1462 | uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak); |
| 1463 | if(uReturn != QCBOR_SUCCESS) { |
| 1464 | goto Done; |
| 1465 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1466 | |
| 1467 | if(!bIsBreak) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1468 | /* 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] | 1469 | break; |
| 1470 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1471 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1472 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1473 | /* |
| 1474 | Break occurred inside a bstr-wrapped CBOR or |
| 1475 | in the top level sequence. This is always an |
| 1476 | error because neither are an indefinte length |
| 1477 | map/array. |
| 1478 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1479 | uReturn = QCBOR_ERR_BAD_BREAK; |
| 1480 | goto Done; |
| 1481 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1482 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1483 | /* It was a break in an indefinite length map / array */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1484 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1485 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1486 | /* All items in the map/array level have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1487 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1488 | /* But ascent in bounded mode is only by explicit call to QCBORDecode_ExitBoundedMode() */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1489 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1490 | /* Set the count to zero for definite length arrays to indicate cursor is at end of bounded map / array */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1491 | if(bMarkEnd) { |
| 1492 | // Used for definite and indefinite to signal end |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1493 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1494 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1495 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | |
| 1499 | /* Finally, actually ascend one level. */ |
| 1500 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1501 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1502 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1503 | uReturn = QCBOR_SUCCESS; |
| 1504 | |
| 1505 | Done: |
| 1506 | return uReturn; |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1511 | This handles the traversal descending into and asecnding out of maps, |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1512 | arrays and bstr-wrapped CBOR. It figures out the ends of definite and |
| 1513 | indefinte length maps and arrays by looking at the item count or |
| 1514 | finding CBOR breaks. It detects the ends of the top-level sequence |
| 1515 | and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1516 | |
| 1517 | @retval QCBOR_ERR_UNSUPPORTED X |
| 1518 | |
| 1519 | @retval QCBOR_ERR_HIT_END |
| 1520 | |
| 1521 | @retval QCBOR_ERR_INT_OVERFLOW X |
| 1522 | |
| 1523 | @retval QCBOR_ERR_STRING_ALLOCATE |
| 1524 | |
| 1525 | @retval QCBOR_ERR_STRING_TOO_LONG |
| 1526 | |
| 1527 | @retval QCBOR_ERR_HALF_PRECISION_DISABLED X |
| 1528 | |
| 1529 | @retval QCBOR_ERR_BAD_TYPE_7 X |
| 1530 | |
| 1531 | @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1532 | |
| 1533 | @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1534 | |
| 1535 | @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1536 | |
| 1537 | @retval QCBOR_ERR_MAP_LABEL_TYPE X |
| 1538 | |
| 1539 | @retval QCBOR_ERR_ARRAY_TOO_LONG |
| 1540 | |
| 1541 | @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1542 | |
| 1543 | @retval QCBOR_ERR_BAD_BREAK |
| 1544 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1545 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1546 | static QCBORError |
| 1547 | QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1548 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1549 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1550 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1551 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1552 | /* |
| 1553 | If out of bytes to consume, it is either the end of the top-level |
| 1554 | sequence of some bstr-wrapped CBOR that was entered. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1555 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1556 | In the case of bstr-wrapped CBOR, the length of the UsefulInputBuf |
| 1557 | was set to that of the bstr-wrapped CBOR. When the bstr-wrapped |
| 1558 | CBOR is exited, the length is set back to the top-level's length |
| 1559 | or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1560 | */ |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1561 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1562 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1563 | goto Done; |
| 1564 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1565 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1566 | /* |
| 1567 | 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] | 1568 | array. The check for the end of an indefinite length array is |
| 1569 | later. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1570 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1571 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1572 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1573 | goto Done; |
| 1574 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1575 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1576 | /* ==== Next: not at the end so get another item ==== */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1577 | uReturn = GetNext_MapEntry(me, pDecodedItem); |
| 1578 | if(uReturn) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1579 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1580 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1581 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1582 | /* |
| 1583 | Breaks ending arrays/maps are always processed at the end of this |
| 1584 | function. They should never show up here. |
| 1585 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1586 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1587 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1588 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1589 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1590 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1591 | /* |
| 1592 | Record the nesting level for this data item before processing any |
| 1593 | of decrementing and descending. |
| 1594 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1595 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1596 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1597 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1598 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1599 | if(QCBORItem_IsMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1600 | /* |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1601 | If the new item is a map or array, descend. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1602 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1603 | Empty indefinite length maps and arrays are descended into, but then ascended out |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1604 | of in the next chunk of code. |
| 1605 | |
| 1606 | Maps and arrays do count as items in the map/array that |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1607 | encloses them so a decrement needs to be done for them too, but |
| 1608 | that is done only when all the items in them have been |
| 1609 | processed, not when they are opened with the exception of an |
| 1610 | empty map or array. |
| 1611 | */ |
| 1612 | uReturn = DecodeNesting_DescendMapOrArray(&(me->nesting), |
| 1613 | pDecodedItem->uDataType, |
| 1614 | pDecodedItem->val.uCount); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1615 | if(uReturn != QCBOR_SUCCESS) { |
| 1616 | goto Done; |
| 1617 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1620 | if(!QCBORItem_IsMapOrArray(pDecodedItem) || |
| 1621 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(pDecodedItem) || |
| 1622 | QCBORItem_IsIndefiniteLengthMapOrArray(pDecodedItem)) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1623 | /* |
| 1624 | The following cases are handled here: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1625 | - A non-aggregate like an integer or string |
| 1626 | - An empty definite length map or array |
| 1627 | - 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] | 1628 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 1629 | NestLevelAscender() does the work of decrementing the count for an |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1630 | definite length map/array and break detection for an indefinite |
| 1631 | length map/array. If the end of the map/array was reached, then |
| 1632 | it ascends nesting levels, possibly all the way to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1633 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1634 | uReturn = NestLevelAscender(me, true); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1635 | if(uReturn) { |
| 1636 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1637 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1638 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1639 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1640 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1641 | /* |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1642 | Tell the caller what level is next. This tells them what |
| 1643 | maps/arrays were closed out and makes it possible for them to |
| 1644 | reconstruct the tree with just the information returned in |
| 1645 | a QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1646 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1647 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(me->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1648 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1649 | pDecodedItem->uNextNestLevel = 0; |
| 1650 | } else { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1651 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(me->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1652 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1653 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1654 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1655 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1656 | /* This sets uDataType and uLabelType to QCBOR_TYPE_NONE */ |
Laurence Lundblade | e9482dd | 2019-10-11 12:58:46 -0700 | [diff] [blame] | 1657 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1658 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1659 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1662 | static void ShiftTags(QCBORItem *pDecodedItem) |
| 1663 | { |
| 1664 | pDecodedItem->uTags[0] = pDecodedItem->uTags[1]; |
| 1665 | pDecodedItem->uTags[1] = pDecodedItem->uTags[2]; |
| 1666 | pDecodedItem->uTags[2] = pDecodedItem->uTags[3]; |
| 1667 | pDecodedItem->uTags[2] = CBOR_TAG_INVALID16; |
| 1668 | } |
| 1669 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1670 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1671 | /* |
| 1672 | Mostly just assign the right data type for the date string. |
| 1673 | */ |
| 1674 | inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem) |
| 1675 | { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1676 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1677 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1678 | } |
| 1679 | |
| 1680 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1681 | pDecodedItem->val.dateString = Temp; |
| 1682 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1683 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1684 | return QCBOR_SUCCESS; |
| 1685 | } |
| 1686 | |
| 1687 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1688 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1689 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1690 | The epoch formatted date. Turns lots of different forms of encoding |
| 1691 | date into uniform one |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1692 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1693 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1694 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1695 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1696 | |
| 1697 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1698 | |
| 1699 | switch (pDecodedItem->uDataType) { |
| 1700 | |
| 1701 | case QCBOR_TYPE_INT64: |
| 1702 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1703 | break; |
| 1704 | |
| 1705 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1706 | // This only happens for CBOR type 0 > INT64_MAX so it is |
| 1707 | // always an overflow. |
| 1708 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1709 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1710 | break; |
| 1711 | |
| 1712 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1713 | case QCBOR_TYPE_FLOAT: |
| 1714 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1715 | { |
| 1716 | // This comparison needs to be done as a float before |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1717 | // conversion to an int64_t to be able to detect doubles that |
| 1718 | // are too large to fit into an int64_t. A double has 52 |
| 1719 | // bits of preceision. An int64_t has 63. Casting INT64_MAX |
| 1720 | // to a double actually causes a round up which is bad and |
| 1721 | // wrong for the comparison because it will allow conversion |
| 1722 | // of doubles that can't fit into a uint64_t. To remedy this |
| 1723 | // INT64_MAX - 0x7ff is used as the cutoff point because if |
| 1724 | // that value rounds up in conversion to double it will still |
| 1725 | // be less than INT64_MAX. 0x7ff is picked because it has 11 |
| 1726 | // bits set. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1727 | // |
Laurence Lundblade | 7b5a3b6 | 2020-07-22 10:17:16 -0700 | [diff] [blame] | 1728 | // INT64_MAX seconds is on the order of 10 billion years, and |
| 1729 | // the earth is less than 5 billion years old, so for most |
| 1730 | // uses this conversion error won't occur even though doubles |
| 1731 | // can go much larger. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1732 | // |
| 1733 | // Without the 0x7ff there is a ~30 minute range of time |
| 1734 | // values 10 billion years in the past and in the future |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1735 | // where this code would go wrong. Some compilers |
| 1736 | // will generate warnings or errors without the 0x7ff |
| 1737 | // because of the precision issue. |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 1738 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
| 1739 | pDecodedItem->val.dfnum : |
| 1740 | (double)pDecodedItem->val.fnum; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1741 | if(isnan(d) || |
| 1742 | d > (double)(INT64_MAX - 0x7ff) || |
| 1743 | d < (double)(INT64_MIN + 0x7ff)) { |
| 1744 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1745 | goto Done; |
| 1746 | } |
| 1747 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 1748 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 1749 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1750 | } |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1751 | #else |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1752 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1753 | uReturn = QCBOR_ERR_FLOAT_DATE_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 1754 | goto Done; |
| 1755 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 1756 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1757 | break; |
| 1758 | |
| 1759 | default: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1760 | uReturn = QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1761 | goto Done; |
| 1762 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1763 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1764 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1765 | |
| 1766 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 1767 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1771 | /* |
| 1772 | Mostly just assign the right data type for the bignum. |
| 1773 | */ |
| 1774 | inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem) |
| 1775 | { |
| 1776 | // Stack Use: UsefulBuf 1 -- 16 |
| 1777 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1778 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1779 | } |
| 1780 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1781 | pDecodedItem->val.bigNum = Temp; |
| 1782 | const bool bIsPosBigNum = (bool)(pDecodedItem->uTags[0] == CBOR_TAG_POS_BIGNUM); |
| 1783 | pDecodedItem->uDataType = (uint8_t)(bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM |
| 1784 | : QCBOR_TYPE_NEGBIGNUM); |
| 1785 | return QCBOR_SUCCESS; |
| 1786 | } |
| 1787 | |
| 1788 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1789 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1790 | /* |
| 1791 | Decode decimal fractions and big floats. |
| 1792 | |
| 1793 | When called pDecodedItem must be the array that is tagged as a big |
| 1794 | float or decimal fraction, the array that has the two members, the |
| 1795 | exponent and mantissa. |
| 1796 | |
| 1797 | This will fetch and decode the exponent and mantissa and put the |
| 1798 | result back into pDecodedItem. |
| 1799 | */ |
| 1800 | inline static QCBORError |
| 1801 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
| 1802 | { |
| 1803 | QCBORError nReturn; |
| 1804 | |
| 1805 | // --- Make sure it is an array; track nesting level of members --- |
| 1806 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 1807 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1808 | goto Done; |
| 1809 | } |
| 1810 | |
| 1811 | // A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1812 | // definite length arrays, but not for indefnite. Instead remember |
| 1813 | // the nesting level the two integers must be at, which is one |
| 1814 | // deeper than that of the array. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1815 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 1816 | |
| 1817 | // --- Is it a decimal fraction or a bigfloat? --- |
| 1818 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
| 1819 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 1820 | |
| 1821 | // --- Get the exponent --- |
| 1822 | QCBORItem exponentItem; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1823 | nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1824 | if(nReturn != QCBOR_SUCCESS) { |
| 1825 | goto Done; |
| 1826 | } |
| 1827 | if(exponentItem.uNestingLevel != nNestLevel) { |
| 1828 | // Array is empty or a map/array encountered when expecting an int |
| 1829 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1830 | goto Done; |
| 1831 | } |
| 1832 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
| 1833 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1834 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1835 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1836 | // will be too large for this to handle and thus an error that will |
| 1837 | // get handled in the next else. |
| 1838 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 1839 | } else { |
| 1840 | // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1841 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1842 | goto Done; |
| 1843 | } |
| 1844 | |
| 1845 | // --- Get the mantissa --- |
| 1846 | QCBORItem mantissaItem; |
| 1847 | nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL); |
| 1848 | if(nReturn != QCBOR_SUCCESS) { |
| 1849 | goto Done; |
| 1850 | } |
| 1851 | if(mantissaItem.uNestingLevel != nNestLevel) { |
| 1852 | // Mantissa missing or map/array encountered when expecting number |
| 1853 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1854 | goto Done; |
| 1855 | } |
| 1856 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
| 1857 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1858 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1859 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1860 | // will be too large for this to handle and thus an error that |
| 1861 | // will get handled in an else below. |
| 1862 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
| 1863 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 1864 | // Got a good big num mantissa |
| 1865 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
| 1866 | // Depends on numbering of QCBOR_TYPE_XXX |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1867 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 1868 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 1869 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1870 | } else { |
| 1871 | // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1872 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1873 | goto Done; |
| 1874 | } |
| 1875 | |
| 1876 | // --- Check that array only has the two numbers --- |
| 1877 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 1878 | // Extra items in the decimal fraction / big float |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1879 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1880 | goto Done; |
| 1881 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 1882 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; // TODO: make sure this is right |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1883 | |
| 1884 | Done: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1885 | return nReturn; |
| 1886 | } |
| 1887 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1888 | |
| 1889 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1890 | inline static QCBORError DecodeURI(QCBORItem *pDecodedItem) |
| 1891 | { |
| 1892 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1893 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1894 | } |
| 1895 | pDecodedItem->uDataType = QCBOR_TYPE_URI; |
| 1896 | return QCBOR_SUCCESS; |
| 1897 | } |
| 1898 | |
| 1899 | |
| 1900 | inline static QCBORError DecodeB64URL(QCBORItem *pDecodedItem) |
| 1901 | { |
| 1902 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1903 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1904 | } |
| 1905 | pDecodedItem->uDataType = QCBOR_TYPE_BASE64URL; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1906 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1907 | return QCBOR_SUCCESS; |
| 1908 | } |
| 1909 | |
| 1910 | |
| 1911 | inline static QCBORError DecodeB64(QCBORItem *pDecodedItem) |
| 1912 | { |
| 1913 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1914 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1915 | } |
| 1916 | pDecodedItem->uDataType = QCBOR_TYPE_BASE64; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1917 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1918 | return QCBOR_SUCCESS; |
| 1919 | } |
| 1920 | |
| 1921 | |
| 1922 | inline static QCBORError DecodeRegex(QCBORItem *pDecodedItem) |
| 1923 | { |
| 1924 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1925 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1926 | } |
| 1927 | pDecodedItem->uDataType = QCBOR_TYPE_REGEX; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1928 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1929 | return QCBOR_SUCCESS; |
| 1930 | } |
| 1931 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1932 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1933 | inline static QCBORError DecodeWrappedCBOR(QCBORItem *pDecodedItem) |
| 1934 | { |
| 1935 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1936 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1937 | } |
| 1938 | pDecodedItem->uDataType = QBCOR_TYPE_WRAPPED_CBOR; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1939 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 1940 | return QCBOR_SUCCESS; |
| 1941 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1942 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1943 | |
| 1944 | inline static QCBORError DecodeWrappedCBORSequence(QCBORItem *pDecodedItem) |
| 1945 | { |
| 1946 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1947 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1948 | } |
| 1949 | pDecodedItem->uDataType = QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1950 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 1951 | return QCBOR_SUCCESS; |
| 1952 | } |
| 1953 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1954 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1955 | inline static QCBORError DecodeMIME(QCBORItem *pDecodedItem) |
| 1956 | { |
| 1957 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1958 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 1959 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1960 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 1961 | } else { |
| 1962 | return QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1963 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1964 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1965 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1966 | return QCBOR_SUCCESS; |
| 1967 | } |
| 1968 | |
| 1969 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1970 | inline static QCBORError DecodeUUID(QCBORItem *pDecodedItem) |
| 1971 | { |
| 1972 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1973 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1974 | } |
| 1975 | pDecodedItem->uDataType = QCBOR_TYPE_UUID; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1976 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 1977 | return QCBOR_SUCCESS; |
| 1978 | } |
| 1979 | |
| 1980 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1981 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1982 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1983 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1984 | QCBORError |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1985 | QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1986 | { |
| 1987 | QCBORError nReturn; |
| 1988 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1989 | nReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1990 | if(nReturn != QCBOR_SUCCESS) { |
| 1991 | goto Done; |
| 1992 | } |
| 1993 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1994 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 1995 | switch(pDecodedItem->uTags[i]) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1996 | |
Laurence Lundblade | 4a21be1 | 2020-08-05 12:48:33 -0700 | [diff] [blame] | 1997 | // Many of the functions here only just map a CBOR tag to |
| 1998 | // a QCBOR_TYPE for a string and could probably be |
| 1999 | // implemented with less object code. This implementation |
| 2000 | // of string types takes about 120 bytes of object code |
| 2001 | // (that is always linked and not removed by dead stripping). |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2002 | case CBOR_TAG_DATE_STRING: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2003 | nReturn = DecodeDateString(pDecodedItem); |
| 2004 | break; |
| 2005 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2006 | case CBOR_TAG_DATE_EPOCH: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2007 | nReturn = DecodeDateEpoch(pDecodedItem); |
| 2008 | break; |
| 2009 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2010 | case CBOR_TAG_POS_BIGNUM: |
| 2011 | case CBOR_TAG_NEG_BIGNUM: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2012 | nReturn = DecodeBigNum(pDecodedItem); |
| 2013 | break; |
| 2014 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2015 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 2016 | case CBOR_TAG_DECIMAL_FRACTION: |
| 2017 | case CBOR_TAG_BIGFLOAT: |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2018 | // For aggregate tagged types, what goes into pTags is only collected |
| 2019 | // from the surrounding data item, not the contents, so pTags is not |
| 2020 | // passed on here. |
| 2021 | |
| 2022 | nReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem); |
| 2023 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2024 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2025 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 2026 | case CBOR_TAG_CBOR: |
| 2027 | nReturn = DecodeWrappedCBOR(pDecodedItem); |
| 2028 | break; |
| 2029 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2030 | case CBOR_TAG_CBOR_SEQUENCE: |
| 2031 | nReturn = DecodeWrappedCBORSequence(pDecodedItem); |
| 2032 | break; |
| 2033 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2034 | case CBOR_TAG_URI: |
| 2035 | nReturn = DecodeURI(pDecodedItem); |
| 2036 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2037 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2038 | case CBOR_TAG_B64URL: |
| 2039 | nReturn = DecodeB64URL(pDecodedItem); |
| 2040 | break; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2041 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2042 | case CBOR_TAG_B64: |
| 2043 | nReturn = DecodeB64(pDecodedItem); |
| 2044 | break; |
| 2045 | |
| 2046 | case CBOR_TAG_MIME: |
| 2047 | case CBOR_TAG_BINARY_MIME: |
| 2048 | nReturn = DecodeMIME(pDecodedItem); |
| 2049 | break; |
| 2050 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2051 | case CBOR_TAG_REGEX: |
| 2052 | nReturn = DecodeRegex(pDecodedItem); |
| 2053 | break; |
| 2054 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2055 | case CBOR_TAG_BIN_UUID: |
| 2056 | nReturn = DecodeUUID(pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2057 | break; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2058 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2059 | case CBOR_TAG_INVALID16: |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2060 | // The end of the tag list or no tags |
| 2061 | // Successful exit from the loop. |
| 2062 | goto Done; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2063 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2064 | default: |
| 2065 | // A tag that is not understood |
| 2066 | // A successful exit from the loop |
| 2067 | goto Done; |
| 2068 | |
| 2069 | } |
| 2070 | if(nReturn != QCBOR_SUCCESS) { |
| 2071 | goto Done; |
| 2072 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2073 | // A tag was successfully processed, shift it |
| 2074 | // out of the list of tags returned. |
| 2075 | ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | Done: |
| 2079 | if(nReturn != QCBOR_SUCCESS) { |
| 2080 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2081 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2082 | } |
| 2083 | return nReturn; |
| 2084 | } |
| 2085 | |
| 2086 | |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2087 | QCBORError QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2088 | { |
| 2089 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2090 | |
| 2091 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2092 | |
| 2093 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 2094 | // TODO: undo the level tracking (or don't do it) |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2095 | |
| 2096 | return uErr; |
| 2097 | } |
| 2098 | |
| 2099 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2100 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2101 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2102 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2103 | QCBORError |
| 2104 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, |
| 2105 | QCBORItem *pDecodedItem, |
| 2106 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2107 | { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2108 | QCBORError nReturn; |
| 2109 | |
| 2110 | nReturn = QCBORDecode_GetNext(me, pDecodedItem); |
| 2111 | if(nReturn != QCBOR_SUCCESS) { |
| 2112 | return nReturn; |
| 2113 | } |
| 2114 | |
| 2115 | if(pTags != NULL) { |
| 2116 | pTags->uNumUsed = 0; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2117 | // Reverse the order because pTags is reverse of |
| 2118 | // QCBORItem.uTags. |
| 2119 | for(int i = QCBOR_MAX_TAGS_PER_ITEM-1; i >=0 ; i--) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2120 | if(pDecodedItem->uTags[i] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2121 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2122 | } |
| 2123 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2124 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2125 | } |
| 2126 | pTags->puTags[pTags->uNumUsed] = ConvertTag(me, pDecodedItem->uTags[i]); |
| 2127 | pTags->uNumUsed++; |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | return QCBOR_SUCCESS; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2135 | /* |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2136 | Decoding items is done in 5 layered functions, one calling the |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2137 | next one down. If a layer has no work to do for a particular item |
| 2138 | it returns quickly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2139 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2140 | - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes |
| 2141 | tagged data items, turning them into the local C representation. |
| 2142 | For the most simple it is just associating a QCBOR_TYPE with the data. For |
| 2143 | the complex ones that an aggregate of data items, there is some further |
| 2144 | decoding and a little bit of recursion. |
| 2145 | |
| 2146 | - QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2147 | ends of maps and arrays. It tracks descending into and ascending |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2148 | out of maps/arrays. It processes all breaks that terminate |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2149 | indefinite length maps and arrays. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2150 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2151 | - GetNext_MapEntry -- This handles the combining of two |
| 2152 | items, the label and the data, that make up a map entry. |
| 2153 | It only does work on maps. It combines the label and data |
| 2154 | items into one labeled item. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2155 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2156 | - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the |
| 2157 | tags into bit flags associated with the data item. No actual decoding |
| 2158 | of the contents of the tagged item is performed here. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2159 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2160 | - GetNext_FullItem -- This assembles the sub-items that make up |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2161 | an indefinte length string into one string item. It uses the |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2162 | string allocater to create contiguous space for the item. It |
| 2163 | processes all breaks that are part of indefinite length strings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2164 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2165 | - GetNext_Item -- This decodes the atomic data items in CBOR. Each |
| 2166 | atomic data item has a "major type", an integer "argument" and optionally |
| 2167 | some content. For text and byte strings, the content is the bytes |
| 2168 | that make up the string. These are the smallest data items that are |
| 2169 | considered to be well-formed. The content may also be other data items in |
| 2170 | the case of aggregate types. They are not handled in this layer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2171 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2172 | Roughly this takes 300 bytes of stack for vars. Need to |
| 2173 | evaluate this more carefully and correctly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2174 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2175 | */ |
| 2176 | |
| 2177 | |
| 2178 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2179 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2180 | */ |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2181 | bool QCBORDecode_IsTagged(QCBORDecodeContext *me, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2182 | const QCBORItem *pItem, |
| 2183 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2184 | { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2185 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) { |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2186 | if(pItem->uTags[i] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2187 | break; |
| 2188 | } |
| 2189 | if(ConvertTag(me, pItem->uTags[i]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2190 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2191 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2192 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2193 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2194 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | |
| 2198 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2199 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2200 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2201 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2202 | { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2203 | QCBORError uReturn = me->uLastError; |
| 2204 | |
| 2205 | if(uReturn != QCBOR_SUCCESS) { |
| 2206 | goto Done; |
| 2207 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2208 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2209 | // Error out if all the maps/arrays are not closed out |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2210 | if(!DecodeNesting_IsCurrentAtTop(&(me->nesting))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2211 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2212 | goto Done; |
| 2213 | } |
| 2214 | |
| 2215 | // Error out if not all the bytes are consumed |
| 2216 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2217 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2218 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2219 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2220 | Done: |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2221 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2222 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2223 | StringAllocator_Destruct(&(me->StringAllocator)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2224 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2225 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2229 | /* |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2230 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2231 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2232 | // Improvement: make these inline? |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2233 | uint64_t QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2234 | const QCBORItem *pItem, |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2235 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2236 | { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2237 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2238 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2239 | } else { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 2240 | return ConvertTag(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2244 | /* |
| 2245 | Public function, see header qcbor/qcbor_decode.h file |
| 2246 | */ |
| 2247 | uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2248 | uint32_t uIndex) |
| 2249 | { |
| 2250 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2251 | return CBOR_TAG_INVALID64; |
| 2252 | } else { |
| 2253 | return ConvertTag(pMe, pMe->uLastTags[uIndex]); |
| 2254 | } |
| 2255 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2256 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2257 | /* |
| 2258 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2259 | Decoder errors handled in this file |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2260 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2261 | - Hit end of input before it was expected while decoding type and |
| 2262 | number QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2263 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2264 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2265 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2266 | - Hit end of input while decoding a text or byte string |
| 2267 | QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2268 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2269 | - Encountered conflicting tags -- e.g., an item is tagged both a date |
| 2270 | string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2271 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2272 | - Encontered an array or mapp that has too many items |
| 2273 | QCBOR_ERR_ARRAY_TOO_LONG |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2274 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2275 | - Encountered array/map nesting that is too deep |
| 2276 | QCBOR_ERR_ARRAY_NESTING_TOO_DEEP |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2277 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2278 | - An epoch date > INT64_MAX or < INT64_MIN was encountered |
| 2279 | QCBOR_ERR_DATE_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2280 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2281 | - The type of a map label is not a string or int |
| 2282 | QCBOR_ERR_MAP_LABEL_TYPE |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2283 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2284 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2285 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2286 | */ |
| 2287 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2288 | |
| 2289 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2290 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2291 | /* =========================================================================== |
| 2292 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2293 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2294 | This implements a simple sting allocator for indefinite length |
| 2295 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2296 | implements the function type QCBORStringAllocate and allows easy |
| 2297 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2298 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2299 | This particular allocator is built-in for convenience. The caller |
| 2300 | can implement their own. All of this following code will get |
| 2301 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2302 | |
| 2303 | This is a very primitive memory allocator. It does not track |
| 2304 | individual allocations, only a high-water mark. A free or |
| 2305 | reallocation must be of the last chunk allocated. |
| 2306 | |
| 2307 | The size of the pool and offset to free memory are packed into the |
| 2308 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2309 | the decode context. Since the address of the pool may not be |
| 2310 | aligned, they have to be packed and unpacked as if they were |
| 2311 | serialized data of the wire or such. |
| 2312 | |
| 2313 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2314 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2315 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2316 | |
| 2317 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2318 | static inline int |
| 2319 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2320 | { |
| 2321 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2322 | UsefulInputBuf UIB; |
| 2323 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2324 | // Just assume the size here. It was checked during SetUp so |
| 2325 | // the assumption is safe. |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2326 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem, QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
| 2327 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2328 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2329 | return UsefulInputBuf_GetError(&UIB); |
| 2330 | } |
| 2331 | |
| 2332 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2333 | static inline int |
| 2334 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2335 | { |
| 2336 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2337 | // length check performed here is useful. |
| 2338 | UsefulOutBuf UOB; |
| 2339 | |
| 2340 | UsefulOutBuf_Init(&UOB, Pool); |
| 2341 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2342 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2343 | return UsefulOutBuf_GetError(&UOB); |
| 2344 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2345 | |
| 2346 | |
| 2347 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2348 | Internal function for an allocation, reallocation free and destuct. |
| 2349 | |
| 2350 | Having only one function rather than one each per mode saves space in |
| 2351 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2352 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2353 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2354 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2355 | static UsefulBuf |
| 2356 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2357 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2358 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2359 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2360 | uint32_t uPoolSize; |
| 2361 | uint32_t uFreeOffset; |
| 2362 | |
| 2363 | if(uNewSize > UINT32_MAX) { |
| 2364 | // This allocator is only good up to 4GB. This check should |
| 2365 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2366 | goto Done; |
| 2367 | } |
| 2368 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2369 | |
| 2370 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2371 | goto Done; |
| 2372 | } |
| 2373 | |
| 2374 | if(uNewSize) { |
| 2375 | if(pMem) { |
| 2376 | // REALLOCATION MODE |
| 2377 | // Calculate pointer to the end of the memory pool. It is |
| 2378 | // assumed that pPool + uPoolSize won't wrap around by |
| 2379 | // assuming the caller won't pass a pool buffer in that is |
| 2380 | // not in legitimate memory space. |
| 2381 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2382 | |
| 2383 | // Check that the pointer for reallocation is in the range of the |
| 2384 | // pool. This also makes sure that pointer math further down |
| 2385 | // doesn't wrap under or over. |
| 2386 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2387 | // Offset to start of chunk for reallocation. This won't |
| 2388 | // wrap under because of check that pMem >= pPool. Cast |
| 2389 | // is safe because the pool is always less than UINT32_MAX |
| 2390 | // because of check in QCBORDecode_SetMemPool(). |
| 2391 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2392 | |
| 2393 | // Check to see if the allocation will fit. uPoolSize - |
| 2394 | // uMemOffset will not wrap under because of check that |
| 2395 | // pMem is in the range of the uPoolSize by check above. |
| 2396 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2397 | ReturnValue.ptr = pMem; |
| 2398 | ReturnValue.len = uNewSize; |
| 2399 | |
| 2400 | // Addition won't wrap around over because uNewSize was |
| 2401 | // checked to be sure it is less than the pool size. |
| 2402 | uFreeOffset = uMemOffset + uNewSize32; |
| 2403 | } |
| 2404 | } |
| 2405 | } else { |
| 2406 | // ALLOCATION MODE |
| 2407 | // uPoolSize - uFreeOffset will not underflow because this |
| 2408 | // pool implementation makes sure uFreeOffset is always |
| 2409 | // smaller than uPoolSize through this check here and |
| 2410 | // reallocation case. |
| 2411 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2412 | ReturnValue.len = uNewSize; |
| 2413 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2414 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2415 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2416 | } |
| 2417 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2418 | if(pMem) { |
| 2419 | // FREE MODE |
| 2420 | // Cast is safe because of limit on pool size in |
| 2421 | // QCBORDecode_SetMemPool() |
| 2422 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2423 | } else { |
| 2424 | // DESTRUCT MODE |
| 2425 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2426 | } |
| 2427 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2428 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2429 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2430 | MemPool_Pack(Pool, uFreeOffset); |
| 2431 | |
| 2432 | Done: |
| 2433 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2434 | } |
| 2435 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2436 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2437 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2438 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2439 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2440 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2441 | UsefulBuf Pool, |
| 2442 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2443 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2444 | // The pool size and free mem offset are packed into the beginning |
| 2445 | // of the pool memory. This compile time check make sure the |
| 2446 | // constant in the header is correct. This check should optimize |
| 2447 | // down to nothing. |
| 2448 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2449 | return QCBOR_ERR_BUFFER_TOO_SMALL; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2450 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2451 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2452 | // The pool size and free offset packed in to the beginning of pool |
| 2453 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2454 | // machines. |
| 2455 | if(Pool.len > UINT32_MAX) { |
| 2456 | return QCBOR_ERR_BUFFER_TOO_LARGE; |
| 2457 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2458 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2459 | // This checks that the pool buffer given is big enough. |
| 2460 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
| 2461 | return QCBOR_ERR_BUFFER_TOO_SMALL; |
| 2462 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2463 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2464 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 2465 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 2466 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2467 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2468 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2469 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2470 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2471 | |
| 2472 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2473 | static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2474 | { |
| 2475 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
| 2476 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2477 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2478 | /* |
| 2479 | static inline void CopyAllButOneTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
| 2480 | { |
| 2481 | const size_t uSizeLessOne = (QCBOR_MAX_TAGS_PER_ITEM - 1) * sizeof(pItem->uTags[0]); |
| 2482 | |
| 2483 | memcpy(pMe->uLastTags, &(pItem->uTags[1]), uSizeLessOne); |
| 2484 | pMe->uLastTags[QCBOR_MAX_TAGS_PER_ITEM - 1] = CBOR_TAG_INVALID16; |
| 2485 | } */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2486 | |
| 2487 | |
| 2488 | /* |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2489 | Consume an entire map or array (and do next to |
| 2490 | nothing for non-aggregate types). |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2491 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2492 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2493 | ConsumeItem(QCBORDecodeContext *pMe, |
| 2494 | const QCBORItem *pItemToConsume, |
| 2495 | uint_fast8_t *puNextNestLevel) |
| 2496 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2497 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2498 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2499 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2500 | DecodeNesting_Print(&(pMe->nesting), &(pMe->InBuf), "ConsumeItem"); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2501 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2502 | if(QCBORItem_IsMapOrArray(pItemToConsume)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2503 | /* There is only real work to do for maps and arrays */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2504 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2505 | /* This works for definite and indefinite length |
| 2506 | * maps and arrays by using the nesting level |
| 2507 | */ |
| 2508 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2509 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2510 | if( QCBORDecode_IsNotWellFormed(uReturn)) { |
| 2511 | // TODO: also resource limit errors |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2512 | goto Done; |
| 2513 | } |
| 2514 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2515 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2516 | if(puNextNestLevel != NULL) { |
| 2517 | *puNextNestLevel = Item.uNextNestLevel; |
| 2518 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2519 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2520 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2521 | } else { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2522 | /* item_to_consume is not a map or array */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2523 | if(puNextNestLevel != NULL) { |
| 2524 | /* Just pass the nesting level through */ |
| 2525 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2526 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2527 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2528 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2529 | |
| 2530 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2531 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2535 | /* Return true if the labels in Item1 and Item2 are the same. |
| 2536 | Works only for integer and string labels. Returns false |
| 2537 | for any other type. */ |
| 2538 | static inline bool |
| 2539 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 2540 | { |
| 2541 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 2542 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 2543 | return true; |
| 2544 | } |
| 2545 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2546 | 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] | 2547 | return true; |
| 2548 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2549 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2550 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 2551 | return true; |
| 2552 | } |
| 2553 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 2554 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 2555 | return true; |
| 2556 | } |
| 2557 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2558 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2559 | /* Other label types are never matched */ |
| 2560 | return false; |
| 2561 | } |
| 2562 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2563 | |
| 2564 | /* |
| 2565 | Returns true if Item1 and Item2 are the same type |
| 2566 | or if either are of QCBOR_TYPE_ANY. |
| 2567 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2568 | static inline bool |
| 2569 | MatchType(QCBORItem Item1, QCBORItem Item2) |
| 2570 | { |
| 2571 | if(Item1.uDataType == Item2.uDataType) { |
| 2572 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2573 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2574 | return true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2575 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2576 | return true; |
| 2577 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2578 | return false; |
| 2579 | } |
| 2580 | |
| 2581 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2582 | /** |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2583 | \brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2584 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2585 | @param[in] pMe The decode context to search. |
| 2586 | @param[in,out] pItemArray The items to search for and the items found. |
| 2587 | @param[out] puOffset Byte offset of last item matched. |
| 2588 | @param[in] pCBContext Context for the not-found item call back. |
| 2589 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2590 | |
| 2591 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 2592 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2593 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) were found |
| 2594 | for one of the labels being search for. This duplicate detection is only performed for items in pItemArray, |
| 2595 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2596 | |
| 2597 | @retval QCBOR_ERR_UNEXPECTED_TYPE The label was matched, but not the type. |
| 2598 | |
| 2599 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 2600 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2601 | On input pItemArray contains a list of labels and data types |
| 2602 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2603 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2604 | On output the fully retrieved items are filled in with |
| 2605 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2606 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2607 | If an item was not found, its data type is set to QCBOR_TYPE_NONE. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2608 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 2609 | static QCBORError |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2610 | MapSearch(QCBORDecodeContext *pMe, |
| 2611 | QCBORItem *pItemArray, |
| 2612 | size_t *puOffset, |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2613 | void *pCBContext, |
| 2614 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2615 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 2616 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2617 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 2618 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2619 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2620 | uReturn = pMe->uLastError; |
| 2621 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2622 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2623 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2624 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2625 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 2626 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 2627 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2628 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 2629 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2632 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 2633 | // It is an empty bounded array or map |
| 2634 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 2635 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2636 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2637 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2638 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 2639 | // Nothing is ever found in an empty array or map. All items |
| 2640 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2641 | uReturn = QCBOR_SUCCESS; |
| 2642 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2643 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2646 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2647 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 2648 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2649 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2650 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 2651 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2652 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2653 | /* |
| 2654 | Loop over all the items in the map. They could be |
| 2655 | deeply nested and this should handle both definite |
| 2656 | and indefinite length maps and arrays, so this |
| 2657 | adds some complexity. |
| 2658 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2659 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2660 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2661 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2662 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2663 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2664 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2665 | /* Get the item */ |
| 2666 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2667 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2668 | if(QCBORDecode_IsNotWellFormed(uReturn)) { |
| 2669 | /* Got non-well-formed CBOR so map can't even be decoded. */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2670 | // TODO: also bail out on implementation limits like array too big |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2671 | goto Done; |
| 2672 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2673 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2674 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2675 | bool bMatched = false; |
| 2676 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2677 | // TODO: have label filled in on invalid CBOR so error reporting |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2678 | // can work a lot better. |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2679 | if(MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2680 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2681 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 2682 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2683 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2684 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2685 | /* Also try to match its type */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2686 | if(!MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2687 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2688 | goto Done; |
| 2689 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2690 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2691 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2692 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2693 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2694 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2695 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2696 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2697 | bMatched = true; |
| 2698 | } |
| 2699 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2700 | |
| 2701 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2702 | if(!bMatched && pfCallback != NULL) { |
| 2703 | /* |
| 2704 | Call the callback on unmatched labels. |
| 2705 | (It is tempting to do duplicate detection here, but that would |
| 2706 | require dynamic memory allocation because the number of labels |
| 2707 | that might be encountered is unbounded.) |
| 2708 | */ |
| 2709 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 2710 | if(uReturn != QCBOR_SUCCESS) { |
| 2711 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2712 | } |
| 2713 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2714 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2715 | /* |
| 2716 | Consume the item whether matched or not. This |
| 2717 | does the work of traversing maps and array and |
| 2718 | everything in them. In this loop only the |
| 2719 | items at the current nesting level are examined |
| 2720 | to match the labels. |
| 2721 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2722 | uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2723 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2724 | goto Done; |
| 2725 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2726 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2727 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2728 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2729 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2730 | |
| 2731 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2732 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 2733 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2734 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2735 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 2736 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 2737 | |
| 2738 | Done2: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2739 | /* For all items not found, set the data type to QCBOR_TYPE_NONE */ |
| 2740 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2741 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 2742 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2743 | } |
| 2744 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2745 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2746 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2747 | } |
| 2748 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 2749 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2750 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2751 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2752 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2753 | void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 2754 | int64_t nLabel, |
| 2755 | uint8_t uQcborType, |
| 2756 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2757 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2758 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2759 | return; |
| 2760 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2761 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2762 | QCBORItem OneItemSeach[2]; |
| 2763 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 2764 | OneItemSeach[0].label.int64 = nLabel; |
| 2765 | OneItemSeach[0].uDataType = uQcborType; |
| 2766 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2767 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2768 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2769 | if(uReturn != QCBOR_SUCCESS) { |
| 2770 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2771 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2772 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2773 | uReturn = QCBOR_ERR_NOT_FOUND; |
| 2774 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2777 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2778 | |
| 2779 | Done: |
| 2780 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2784 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2785 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2786 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2787 | void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 2788 | const char *szLabel, |
| 2789 | uint8_t uQcborType, |
| 2790 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2791 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 2792 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2793 | return; |
| 2794 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2795 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2796 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2797 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2798 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2799 | OneItemSeach[0].uDataType = uQcborType; |
| 2800 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2801 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2802 | QCBORError uReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
| 2803 | if(uReturn != QCBOR_SUCCESS) { |
| 2804 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2805 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2806 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2807 | uReturn = QCBOR_ERR_NOT_FOUND; |
| 2808 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2811 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 2812 | |
| 2813 | Done: |
| 2814 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2818 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2819 | static QCBORError CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2820 | { |
| 2821 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
| 2822 | if(uDataType == puTypeList[i]) { |
| 2823 | return QCBOR_SUCCESS; |
| 2824 | } |
| 2825 | } |
| 2826 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2827 | } |
| 2828 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 2829 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2830 | /** |
| 2831 | @param[in] TagSpec Specification for matching tags. |
| 2832 | @param[in] uDataType A QCBOR data type |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2833 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2834 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 2835 | @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^] | 2836 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 2837 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered tag value. |
| 2838 | */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 2839 | static QCBORError CheckTagRequirement(const TagSpecification TagSpec, uint8_t uDataType) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2840 | { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2841 | if((TagSpec.uTagRequirement & (~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS)) == QCBOR_TAG_REQUIREMENT_TAG) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2842 | // Must match the tag and only the tag |
| 2843 | return CheckTypeList(uDataType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2844 | } |
| 2845 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2846 | QCBORError uReturn = CheckTypeList(uDataType, TagSpec.uAllowedContentTypes); |
| 2847 | if(uReturn == QCBOR_SUCCESS) { |
| 2848 | return QCBOR_SUCCESS; |
| 2849 | } |
| 2850 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2851 | if((TagSpec.uTagRequirement & (~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS)) == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 2852 | /* Must match the content type and only the content type. |
| 2853 | There was no match just above so it is a fail. */ |
| 2854 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2855 | } |
| 2856 | |
| 2857 | /* If here it can match either the tag or the content |
| 2858 | and it hasn't matched the content, so the end |
| 2859 | result is whether it matches the tag. This is |
| 2860 | also the case that the CBOR standard discourages. */ |
| 2861 | |
| 2862 | return CheckTypeList(uDataType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 2863 | } |
| 2864 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 2865 | static QCBORError CheckTagRequirement2(const TagSpecification TagSpec, const QCBORItem *pItem) |
| 2866 | { |
| 2867 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 2868 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 2869 | /* There are tags that QCBOR couldn't process on this item and |
| 2870 | the caller has told us there should not be. */ |
| 2871 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2872 | } |
| 2873 | |
| 2874 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 2875 | const int nItemType = pItem->uDataType; |
| 2876 | |
| 2877 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 2878 | // Must match the tag and only the tag |
| 2879 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2880 | } |
| 2881 | |
| 2882 | QCBORError uReturn = CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 2883 | if(uReturn == QCBOR_SUCCESS) { |
| 2884 | return QCBOR_SUCCESS; |
| 2885 | } |
| 2886 | |
| 2887 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 2888 | /* Must match the content type and only the content type. |
| 2889 | There was no match just above so it is a fail. */ |
| 2890 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2891 | } |
| 2892 | |
| 2893 | /* If here it can match either the tag or the content |
| 2894 | and it hasn't matched the content, so the end |
| 2895 | result is whether it matches the tag. This is |
| 2896 | also the case that the CBOR standard discourages. */ |
| 2897 | |
| 2898 | return CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
| 2899 | } |
| 2900 | |
| 2901 | #if 0 |
| 2902 | /** |
| 2903 | @param[in] TagSpec Specification for matching tags. |
| 2904 | @param[in] uDataType A QCBOR data type |
| 2905 | |
| 2906 | @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 2907 | @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec |
| 2908 | |
| 2909 | The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered tag value. |
| 2910 | */ |
| 2911 | static QCBORError CheckTagRequirement2(const TagSpecification TagSpec, uint8_t uDataType) |
| 2912 | { |
| 2913 | const uint16_t *pTags; |
| 2914 | /* |
| 2915 | For all the tag-specific accessor methods supported, GetNext will |
| 2916 | process then automatically when encountered during decoding. |
| 2917 | The will have a QCBOR_TYPE and a representation in QCBORItem. |
| 2918 | There are no expections to this (so far). |
| 2919 | |
| 2920 | That means the tag list in the QCBORItem will never have |
| 2921 | these tags in it. The tags in that list never need to |
| 2922 | be examined here. |
| 2923 | |
| 2924 | |
| 2925 | |
| 2926 | 1 Tag list must be empty |
| 2927 | |
| 2928 | 2 and 5, first tag must be in expected list |
| 2929 | |
| 2930 | 4 |
| 2931 | |
| 2932 | 3 tag list must be empty or one in the expected list |
| 2933 | |
| 2934 | 6, if first tag is expected, consume it, pass the rest on |
| 2935 | |
| 2936 | |
| 2937 | */ |
| 2938 | |
| 2939 | /* |
| 2940 | First thing to understand is that GetNext will have processed |
| 2941 | the tags this code knows about. They will not be in the unprocessed |
| 2942 | tags list and the dataType will be of the processed CBOR. |
| 2943 | */ |
| 2944 | |
| 2945 | const bool bUnprocessedTagsEmpty = pTags[0] != CBOR_TAG_INVALID16; |
| 2946 | |
| 2947 | const bool bDataTypeMatchesRequestedTaggedType = !CheckTypeList(uDataType, TagSpec.uTaggedTypes); |
| 2948 | |
| 2949 | const bool bDataTypeMatchesRequestedContentType = !CheckTypeList(uDataType, TagSpec.uAllowedContentTypes); |
| 2950 | |
| 2951 | |
| 2952 | if(TagSpec.uTagRequirement == 1) { |
| 2953 | /* There should be no tags at all, so the unprocessed tag |
| 2954 | list should be empty. |
| 2955 | |
| 2956 | The content has to match the expected content. If |
| 2957 | there was a tag that was interpreted, the content |
| 2958 | wouldn't match. |
| 2959 | |
| 2960 | */ |
| 2961 | if(!bUnprocessedTagsEmpty) { |
| 2962 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2963 | } else { |
| 2964 | if(!bDataTypeMatchesRequestedContentType) { |
| 2965 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2966 | } else { |
| 2967 | return QCBOR_SUCCESS; |
| 2968 | } |
| 2969 | |
| 2970 | } |
| 2971 | |
| 2972 | |
| 2973 | |
| 2974 | } else if(TagSpec.uTagRequirement == 2) { |
| 2975 | /* The tag was present so GetNext will have interpreted it by now. |
| 2976 | The data type should be one of the requested types |
| 2977 | and there should be no other tags. */ |
| 2978 | |
| 2979 | if(!bUnprocessedTagsEmpty) { |
| 2980 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2981 | } else { |
| 2982 | if(!bDataTypeMatchesRequestedTaggedType) { |
| 2983 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2984 | } else { |
| 2985 | return QCBOR_SUCCESS; |
| 2986 | } |
| 2987 | } |
| 2988 | } else if(TagSpec.uTagRequirement == 3) { |
| 2989 | if(!bUnprocessedTagsEmpty) { |
| 2990 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2991 | } else { |
| 2992 | if(bDataTypeMatchesRequestedTaggedType || bDataTypeMatchesRequestedContentType) { |
| 2993 | return true; |
| 2994 | } else { |
| 2995 | return false; |
| 2996 | } |
| 2997 | } |
| 2998 | } else if(TagSpec.uTagRequirement == 4) { |
| 2999 | |
| 3000 | |
| 3001 | |
| 3002 | |
| 3003 | |
| 3004 | |
| 3005 | |
| 3006 | if(TagSpec.uTagRequirement == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3007 | // Must match the tag and only the tag |
| 3008 | return CheckTypeList(uDataType, TagSpec.uTaggedTypes); |
| 3009 | } |
| 3010 | |
| 3011 | QCBORError uReturn = CheckTypeList(uDataType, TagSpec.uAllowedContentTypes); |
| 3012 | if(uReturn == QCBOR_SUCCESS) { |
| 3013 | return QCBOR_SUCCESS; |
| 3014 | } |
| 3015 | |
| 3016 | if(TagSpec.uTagRequirement == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3017 | /* Must match the content type and only the content type. |
| 3018 | There was no match just above so it is a fail. */ |
| 3019 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3020 | } |
| 3021 | |
| 3022 | /* If here it can match either the tag or the content |
| 3023 | and it hasn't matched the content, so the end |
| 3024 | result is whether it matches the tag. This is |
| 3025 | also the case that the CBOR standard discourages. */ |
| 3026 | |
| 3027 | return CheckTypeList(uDataType, TagSpec.uTaggedTypes); |
| 3028 | } |
| 3029 | |
| 3030 | #endif |
| 3031 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3032 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3033 | // Semi-private |
| 3034 | // TODO: inline or collapse with QCBORDecode_GetTaggedStringInMapN? |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3035 | void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3036 | int64_t nLabel, |
| 3037 | TagSpecification TagSpec, |
| 3038 | QCBORItem *pItem) |
| 3039 | { |
| 3040 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3041 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3042 | return; |
| 3043 | } |
| 3044 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3045 | pMe->uLastError = (uint8_t)CheckTagRequirement2(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3048 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3049 | void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3050 | const char *szLabel, |
| 3051 | TagSpecification TagSpec, |
| 3052 | QCBORItem *pItem) |
| 3053 | { |
| 3054 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3055 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3056 | return; |
| 3057 | } |
| 3058 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 3059 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem->uDataType); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3062 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3063 | void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3064 | int64_t nLabel, |
| 3065 | TagSpecification TagSpec, |
| 3066 | UsefulBufC *pString) |
| 3067 | { |
| 3068 | QCBORItem Item; |
| 3069 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3070 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3071 | *pString = Item.val.string; |
| 3072 | } |
| 3073 | } |
| 3074 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3075 | // Semi-private |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3076 | void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3077 | const char * szLabel, |
| 3078 | TagSpecification TagSpec, |
| 3079 | UsefulBufC *pString) |
| 3080 | { |
| 3081 | QCBORItem Item; |
| 3082 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3083 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3084 | *pString = Item.val.string; |
| 3085 | } |
| 3086 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3087 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3088 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3089 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3090 | */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3091 | QCBORError QCBORDecode_GetItemsInMap(QCBORDecodeContext *pCtx, QCBORItem *pItemList) |
| 3092 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3093 | return MapSearch(pCtx, pItemList, NULL, NULL, NULL); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3094 | } |
| 3095 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3096 | /* |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3097 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3098 | */ |
| 3099 | QCBORError QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pCtx, |
| 3100 | QCBORItem *pItemList, |
| 3101 | void *pCallbackCtx, |
| 3102 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3103 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3104 | return MapSearch(pCtx, pItemList, NULL, pCallbackCtx, pfCB); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3108 | static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3109 | { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3110 | // TODO: check that only one item is in pSearch? |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3111 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3112 | // Already in error state; do nothing. |
| 3113 | return; |
| 3114 | } |
| 3115 | |
| 3116 | size_t uOffset; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3117 | pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3118 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3119 | return; |
| 3120 | } |
| 3121 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3122 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
| 3123 | pMe->uLastError = QCBOR_ERR_NOT_FOUND; |
| 3124 | return; |
| 3125 | } |
| 3126 | |
| 3127 | CopyTags(pMe, pSearch); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3128 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3129 | /* 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] | 3130 | at the map/array about to be entered. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3131 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3132 | Also need the current map nesting level and start cursor to |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3133 | be at the right place. |
| 3134 | |
| 3135 | The UsefulInBuf offset could be anywhere, so no assumption is |
| 3136 | made about it. |
| 3137 | |
| 3138 | No assumption is made about the pre-order nesting level either. |
| 3139 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3140 | However the bounded mode nesting level is assumed to be one above |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3141 | the map level that is being entered. |
| 3142 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3143 | /* Seek to the data item that is the map or array */ |
| 3144 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3145 | |
| 3146 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3147 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3148 | QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3152 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3153 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3154 | */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3155 | void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3156 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3157 | QCBORItem OneItemSeach[2]; |
| 3158 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3159 | OneItemSeach[0].label.int64 = nLabel; |
| 3160 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3161 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3162 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3163 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3164 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3168 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3169 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3170 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3171 | void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3172 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3173 | QCBORItem OneItemSeach[2]; |
| 3174 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3175 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3176 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3177 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3178 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3179 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3180 | } |
| 3181 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3182 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3183 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3184 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3185 | void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3186 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3187 | QCBORItem OneItemSeach[2]; |
| 3188 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3189 | OneItemSeach[0].label.int64 = nLabel; |
| 3190 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3191 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3192 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3193 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3196 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3197 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3198 | */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3199 | void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3200 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3201 | QCBORItem OneItemSeach[2]; |
| 3202 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3203 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3204 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3205 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3206 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3207 | SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3211 | // Semi-private function |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3212 | void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3213 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3214 | QCBORError uErr; |
| 3215 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3216 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3217 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3218 | // Already in error state; do nothing. |
| 3219 | return; |
| 3220 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3221 | |
| 3222 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3223 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3224 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3225 | if(uErr != QCBOR_SUCCESS) { |
| 3226 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3227 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3228 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3229 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3230 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3231 | } |
| 3232 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3233 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3234 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3235 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3236 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3237 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3238 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3239 | } |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3240 | // Special case to increment nesting level for zero-length maps and arrays entered in bounded mode. |
| 3241 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3242 | } |
| 3243 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3244 | pMe->uMapEndOffsetCache = MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3245 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3246 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3247 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3248 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3249 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3250 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3253 | |
| 3254 | /* |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3255 | This is the common work for exiting a level that is a bounded map, array or bstr |
| 3256 | wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3257 | |
| 3258 | One chunk of work is to set up the pre-order traversal so it is at |
| 3259 | the item just after the bounded map, array or bstr that is being |
| 3260 | exited. This is somewhat complex. |
| 3261 | |
| 3262 | The other work is to level-up the bounded mode to next higest bounded |
| 3263 | mode or the top level if there isn't one. |
| 3264 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3265 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3266 | ExitBoundedLevel(QCBORDecodeContext *pMe, uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3267 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3268 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3269 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3270 | /* |
| 3271 | First the pre-order-traversal byte offset is positioned to the |
| 3272 | item just after the bounded mode item that was just consumed. |
| 3273 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3274 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3275 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3276 | /* |
| 3277 | Next, set the current nesting level to one above the bounded level |
| 3278 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3279 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3280 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3281 | makes sure pCurrentBounded is valid. |
| 3282 | */ |
| 3283 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3284 | |
| 3285 | /* |
| 3286 | This does the complex work of leveling up the pre-order traversal |
| 3287 | when the end of a map or array or another bounded level is |
| 3288 | reached. It may do nothing, or ascend all the way to the top |
| 3289 | level. |
| 3290 | */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 3291 | uErr = NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3292 | if(uErr != QCBOR_SUCCESS) { |
| 3293 | goto Done; |
| 3294 | } |
| 3295 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3296 | /* |
| 3297 | This makes the next highest bounded level the current bounded |
| 3298 | level. If there is no next highest level, then no bounded mode is |
| 3299 | in effect. |
| 3300 | */ |
| 3301 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3302 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3303 | pMe->uMapEndOffsetCache = MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3304 | |
| 3305 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3306 | DecodeNesting_Print(&(pMe->nesting), &(pMe->InBuf), "ExitBoundedLevel"); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3307 | return uErr; |
| 3308 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3309 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3310 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3311 | // Semi-private function |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3312 | void QCBORDecode_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3313 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3314 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3315 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3316 | return; |
| 3317 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3318 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3319 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3320 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3321 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3322 | uErr = QCBOR_ERR_CLOSE_MISMATCH; |
| 3323 | goto Done; |
| 3324 | } |
| 3325 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3326 | /* |
| 3327 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3328 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3329 | from previous map search, then do a dummy search. |
| 3330 | */ |
| 3331 | if(pMe->uMapEndOffsetCache == MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3332 | QCBORItem Dummy; |
| 3333 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 3334 | uErr = MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
| 3335 | if(uErr != QCBOR_SUCCESS) { |
| 3336 | goto Done; |
| 3337 | } |
| 3338 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3339 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3340 | uErr = ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3341 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3342 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3343 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3347 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3348 | static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3349 | const QCBORItem *pItem, |
| 3350 | uint8_t uTagRequirement, |
| 3351 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3352 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3353 | if(pBstr) { |
| 3354 | *pBstr = NULLUsefulBufC; |
| 3355 | } |
| 3356 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3357 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3358 | // Already in error state; do nothing. |
| 3359 | return pMe->uLastError; |
| 3360 | } |
| 3361 | |
| 3362 | QCBORError uError = QCBOR_SUCCESS; |
| 3363 | |
| 3364 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 3365 | uError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3366 | goto Done;; |
| 3367 | } |
| 3368 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3369 | const TagSpecification TagSpec = |
| 3370 | { |
| 3371 | uTagRequirement, |
| 3372 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3373 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3374 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3375 | |
| 3376 | uError = CheckTagRequirement(TagSpec, pItem->uDataType); |
| 3377 | if(uError != QCBOR_SUCCESS) { |
| 3378 | goto Done; |
| 3379 | } |
| 3380 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3381 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3382 | /* Reverse the decrement done by GetNext() for the bstr as |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 3383 | so the increment in NestLevelAscender called by ExitBoundedLevel() |
| 3384 | will work right. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3385 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3386 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3387 | |
| 3388 | if(pBstr) { |
| 3389 | *pBstr = pItem->val.string; |
| 3390 | } |
| 3391 | |
| 3392 | const size_t uPreviousLength = UsefulInputBuf_GetLength(&(pMe->InBuf)); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3393 | |
| 3394 | // Need to move UIB input cursor to the right place |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3395 | // Most of these calls are simple inline accessors so this doesn't |
| 3396 | // amount to much code. There is a range check in the seek. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3397 | const size_t uEndOfBstr = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3398 | if(uEndOfBstr >= UINT32_MAX || uPreviousLength >= UINT32_MAX) { |
| 3399 | // TODO: test this error condition |
| 3400 | uError = QCBOR_ERR_BUFFER_TOO_LARGE; |
| 3401 | goto Done; |
| 3402 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3403 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOfBstr - pItem->val.string.len); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3404 | UsefulInputBuf_SetBufferLen(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3405 | |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3406 | // Casts are OK because of the checks above. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3407 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3408 | (uint32_t)uPreviousLength, |
| 3409 | (uint32_t)uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3410 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3411 | DecodeNesting_Print(&(pMe->nesting), &(pMe->InBuf), "Entered Bstr"); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3412 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3413 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3417 | /* |
| 3418 | Public function, see header qcbor/qcbor_decode.h file |
| 3419 | */ |
| 3420 | void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3421 | uint8_t uTagRequirement, |
| 3422 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3423 | { |
| 3424 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3425 | // Already in error state; do nothing. |
| 3426 | return; |
| 3427 | } |
| 3428 | |
| 3429 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3430 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3431 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3432 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3433 | return; |
| 3434 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3435 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3436 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3437 | &Item, |
| 3438 | uTagRequirement, |
| 3439 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3443 | /* |
| 3444 | Public function, see header qcbor/qcbor_decode.h file |
| 3445 | */ |
| 3446 | void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3447 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3448 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3449 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3450 | { |
| 3451 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3452 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3453 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3454 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, &Item, uTagRequirement, pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3455 | } |
| 3456 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3457 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3458 | /* |
| 3459 | Public function, see header qcbor/qcbor_decode.h file |
| 3460 | */ |
| 3461 | void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3462 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3463 | uint8_t uTagRequirement, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3464 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3465 | { |
| 3466 | QCBORItem Item; |
| 3467 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3468 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3469 | pMe->uLastError = (uint8_t)InternalEnterBstrWrapped(pMe, &Item, uTagRequirement, pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3470 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3471 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3472 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3473 | /* |
| 3474 | Public function, see header qcbor/qcbor_decode.h file |
| 3475 | */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3476 | void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3477 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3478 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3479 | // Already in error state; do nothing. |
| 3480 | return; |
| 3481 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3482 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3483 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3484 | pMe->uLastError = QCBOR_ERR_CLOSE_MISMATCH; |
| 3485 | return; |
| 3486 | } |
| 3487 | |
| 3488 | /* |
| 3489 | Reset the length of the UsefulInputBuf to what it was before |
| 3490 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3491 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3492 | UsefulInputBuf_SetBufferLen(&(pMe->InBuf), |
| 3493 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3494 | |
| 3495 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3496 | QCBORError uErr = ExitBoundedLevel(pMe, DecodeNesting_GetEndOfBstr(&(pMe->nesting))); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3497 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3498 | } |
| 3499 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3500 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3501 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3502 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3503 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3504 | |
Laurence Lundblade | 11a064e | 2020-05-07 13:13:42 -0700 | [diff] [blame] | 3505 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3506 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3507 | static QCBORError InterpretBool(QCBORDecodeContext *pMe, const QCBORItem *pItem, bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3508 | { |
| 3509 | switch(pItem->uDataType) { |
| 3510 | case QCBOR_TYPE_TRUE: |
| 3511 | *pBool = true; |
| 3512 | return QCBOR_SUCCESS; |
| 3513 | break; |
| 3514 | |
| 3515 | case QCBOR_TYPE_FALSE: |
| 3516 | *pBool = false; |
| 3517 | return QCBOR_SUCCESS; |
| 3518 | break; |
| 3519 | |
| 3520 | default: |
| 3521 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3522 | break; |
| 3523 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3524 | CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3525 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3526 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3527 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3528 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3529 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3530 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3531 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3532 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3533 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3534 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3535 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3536 | return; |
| 3537 | } |
| 3538 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3539 | QCBORError nError; |
| 3540 | QCBORItem Item; |
| 3541 | |
| 3542 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3543 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3544 | pMe->uLastError = (uint8_t)nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3545 | return; |
| 3546 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3547 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3548 | } |
| 3549 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3550 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3551 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3552 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3553 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3554 | void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3555 | { |
| 3556 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3557 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3558 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3559 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3560 | } |
| 3561 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3562 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3563 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3564 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3565 | */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3566 | void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue) |
| 3567 | { |
| 3568 | QCBORItem Item; |
| 3569 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3570 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3571 | pMe->uLastError = (uint8_t)InterpretBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | |
| 3575 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3576 | /* |
| 3577 | A number of methods decode CBOR that is associated with a |
| 3578 | specific tag or tags. |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3579 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3580 | The API of the method returns the |
| 3581 | data in a way specific to the |
| 3582 | |
| 3583 | No tags at all. |
| 3584 | |
| 3585 | |
| 3586 | Require tag for the particular type for the method and no other. |
| 3587 | |
| 3588 | |
| 3589 | Either no tags at all or the particular type for the method and no other. |
| 3590 | |
| 3591 | No tag for particular type; pass other tags along. |
| 3592 | |
| 3593 | Require the tag for the particular type; pass other tags along |
| 3594 | |
| 3595 | Any tagging is OK; consume the tag for the particular type if present, |
| 3596 | pass other tags along. |
| 3597 | |
| 3598 | |
| 3599 | 1) REQUIRED |
| 3600 | - 1 XXXX -- works |
| 3601 | - T 1 XXX -- works, T is returned |
| 3602 | |
| 3603 | if(tag is of interest) { |
| 3604 | process content |
| 3605 | return T if present |
| 3606 | } |
| 3607 | |
| 3608 | |
| 3609 | 2) FORBIDDEN |
| 3610 | - XXX -- works |
| 3611 | - T XXX -- ??? |
| 3612 | |
| 3613 | if(tag is of interest) { |
| 3614 | error out since tag is forbidden |
| 3615 | } else { |
| 3616 | process contents |
| 3617 | return T |
| 3618 | } |
| 3619 | |
| 3620 | 3) OPTIONAL |
| 3621 | - XXX works |
| 3622 | - 1 XXX works |
| 3623 | - T XXX |
| 3624 | - T 1 XXX works, T is returned |
| 3625 | |
| 3626 | if (inner tag is of interest) { |
| 3627 | process content |
| 3628 | return tag T if present |
| 3629 | } else if (there is no tag) { |
| 3630 | process content |
| 3631 | } else { |
| 3632 | process content if possible |
| 3633 | return T |
| 3634 | } |
| 3635 | |
| 3636 | A field is type X |
| 3637 | - tag for type X is REQUIRED |
| 3638 | - tag for type X is FORBIDDEN |
| 3639 | - tag for type X is optional |
| 3640 | - Other tags are FORBIDDEN |
| 3641 | - Other tags are ALLOWED |
| 3642 | |
| 3643 | |
| 3644 | |
| 3645 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3646 | |
| 3647 | static void ProcessEpochDate(QCBORDecodeContext *pMe, |
| 3648 | QCBORItem *pItem, |
| 3649 | uint8_t uTagRequirement, |
| 3650 | int64_t *pnTime) |
| 3651 | { |
| 3652 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3653 | // Already in error state, do nothing |
| 3654 | return; |
| 3655 | } |
| 3656 | |
| 3657 | QCBORError uErr; |
| 3658 | |
| 3659 | const TagSpecification TagSpec = |
| 3660 | { |
| 3661 | uTagRequirement, |
| 3662 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3663 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT} |
| 3664 | }; |
| 3665 | |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 3666 | // TODO: this will give an unexpected type error instead of |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3667 | // overflow error for QCBOR_TYPE_UINT64 because TagSpec |
| 3668 | // only has three target types. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3669 | uErr = CheckTagRequirement2(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3670 | if(uErr != QCBOR_SUCCESS) { |
| 3671 | goto Done; |
| 3672 | } |
| 3673 | |
| 3674 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
| 3675 | uErr = DecodeDateEpoch(pItem); |
| 3676 | if(uErr != QCBOR_SUCCESS) { |
| 3677 | goto Done; |
| 3678 | } |
| 3679 | } |
| 3680 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3681 | // Save the tags in the last item's tags in the decode context |
| 3682 | // for QCBORDecode_GetNthTagOfLast() |
| 3683 | CopyTags(pMe, pItem); |
| 3684 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3685 | *pnTime = pItem->val.epochDate.nSeconds; |
| 3686 | |
| 3687 | Done: |
| 3688 | pMe->uLastError = (uint8_t)uErr; |
| 3689 | } |
| 3690 | |
| 3691 | |
| 3692 | void QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
| 3693 | uint8_t uTagRequirement, |
| 3694 | int64_t *pnTime) |
| 3695 | { |
| 3696 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3697 | // Already in error state, do nothing |
| 3698 | return; |
| 3699 | } |
| 3700 | |
| 3701 | QCBORItem Item; |
| 3702 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3703 | |
| 3704 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3705 | } |
| 3706 | |
| 3707 | |
| 3708 | void |
| 3709 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 3710 | int64_t nLabel, |
| 3711 | uint8_t uTagRequirement, |
| 3712 | int64_t *pnTime) |
| 3713 | { |
| 3714 | QCBORItem Item; |
| 3715 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 3716 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3717 | } |
| 3718 | |
| 3719 | |
| 3720 | void |
| 3721 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 3722 | const char *szLabel, |
| 3723 | uint8_t uTagRequirement, |
| 3724 | int64_t *pnTime) |
| 3725 | { |
| 3726 | QCBORItem Item; |
| 3727 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3728 | ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
| 3729 | } |
| 3730 | |
| 3731 | |
| 3732 | |
| 3733 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3734 | void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, |
| 3735 | TagSpecification TagSpec, |
| 3736 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3737 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3738 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3739 | // Already in error state, do nothing |
| 3740 | return; |
| 3741 | } |
| 3742 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3743 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3744 | QCBORItem Item; |
| 3745 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3746 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 3747 | if(uError != QCBOR_SUCCESS) { |
| 3748 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3749 | return; |
| 3750 | } |
| 3751 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3752 | pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, Item.uDataType); |
| 3753 | |
| 3754 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3755 | *pBstr = Item.val.string; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3759 | |
| 3760 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3761 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3762 | static QCBORError ProcessBigNum(uint8_t uTagRequirement, |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3763 | const QCBORItem *pItem, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3764 | UsefulBufC *pValue, |
| 3765 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3766 | { |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3767 | const TagSpecification TagSpec = |
| 3768 | { |
| 3769 | uTagRequirement, |
| 3770 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE}, |
| 3771 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3772 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3773 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3774 | QCBORError uErr = CheckTagRequirement(TagSpec, pItem->uDataType); |
| 3775 | if(uErr != QCBOR_SUCCESS) { |
| 3776 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3777 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3778 | |
| 3779 | *pValue = pItem->val.string; |
| 3780 | |
| 3781 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 3782 | *pbIsNegative = false; |
| 3783 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 3784 | *pbIsNegative = true; |
| 3785 | } |
| 3786 | |
| 3787 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3791 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3792 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3793 | */ |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3794 | void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 3795 | uint8_t uTagRequirement, |
| 3796 | UsefulBufC *pValue, |
| 3797 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3798 | { |
| 3799 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3800 | // Already in error state, do nothing |
| 3801 | return; |
| 3802 | } |
| 3803 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3804 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3805 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 3806 | if(uError != QCBOR_SUCCESS) { |
| 3807 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3808 | return; |
| 3809 | } |
| 3810 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3811 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3814 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3815 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3816 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3817 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3818 | void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 3819 | int64_t nLabel, |
| 3820 | uint8_t uTagRequirement, |
| 3821 | UsefulBufC *pValue, |
| 3822 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3823 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3824 | QCBORItem Item; |
| 3825 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3826 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3827 | return; |
| 3828 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3829 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3830 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3831 | } |
| 3832 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3833 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3834 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3835 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3836 | */ |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3837 | void QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 3838 | const char *szLabel, |
| 3839 | uint8_t uTagRequirement, |
| 3840 | UsefulBufC *pValue, |
| 3841 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3842 | { |
| 3843 | QCBORItem Item; |
| 3844 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3845 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3846 | return; |
| 3847 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3848 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3849 | pMe->uLastError = (uint8_t)ProcessBigNum(uTagRequirement, &Item, pValue, pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | |
| 3853 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3854 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3855 | // Semi private |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3856 | QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement, |
| 3857 | const QCBORItem *pItem, |
| 3858 | UsefulBufC *pMessage, |
| 3859 | bool *pbIsNot7Bit) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3860 | { |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3861 | const TagSpecification TagSpecText = |
| 3862 | { |
| 3863 | uTagRequirement, |
| 3864 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3865 | {QCBOR_TYPE_TEXT_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3866 | }; |
| 3867 | const TagSpecification TagSpecBinary = |
| 3868 | { |
| 3869 | uTagRequirement, |
| 3870 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 3871 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3872 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3873 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3874 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3875 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3876 | if(CheckTagRequirement(TagSpecText, pItem->uDataType) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3877 | *pMessage = pItem->val.string; |
| 3878 | if(pbIsNot7Bit != NULL) { |
| 3879 | *pbIsNot7Bit = false; |
| 3880 | } |
| 3881 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 3882 | } else if(CheckTagRequirement(TagSpecBinary, pItem->uDataType) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3883 | *pMessage = pItem->val.string; |
| 3884 | if(pbIsNot7Bit != NULL) { |
| 3885 | *pbIsNot7Bit = true; |
| 3886 | } |
| 3887 | uReturn = QCBOR_SUCCESS; |
| 3888 | |
| 3889 | } else { |
| 3890 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3891 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 3892 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3893 | return uReturn; |
| 3894 | } |
| 3895 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3896 | // Improvement: add methods for wrapped CBOR, a simple alternate to EnterBstrWrapped |
| 3897 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 3898 | |
| 3899 | |
| 3900 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 3901 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3902 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3903 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3904 | |
| 3905 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3906 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3907 | static QCBORError Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3908 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3909 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3910 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3911 | if(uResult != 0) { |
| 3912 | /* This loop will run a maximum of 19 times because |
| 3913 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 3914 | * exit with the overflow error |
| 3915 | */ |
| 3916 | for(; nExponent > 0; nExponent--) { |
| 3917 | if(uResult > UINT64_MAX / 10) { |
| 3918 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
| 3919 | } |
| 3920 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3921 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3922 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3923 | for(; nExponent < 0; nExponent++) { |
| 3924 | uResult = uResult / 10; |
| 3925 | if(uResult == 0) { |
| 3926 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3927 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3928 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3929 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3930 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3931 | |
| 3932 | *puResult = uResult; |
| 3933 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3934 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3938 | // The exponentiator that works on only positive numbers |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3939 | static QCBORError Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3940 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3941 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3942 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3943 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3944 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3945 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3946 | * INT64_MAX < 2^31. More than that will cause |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3947 | * exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3948 | */ |
| 3949 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3950 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3951 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3952 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3953 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3954 | nExponent--; |
| 3955 | } |
| 3956 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3957 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3958 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3959 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 3960 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3961 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3962 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3963 | } |
| 3964 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 3965 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3966 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3967 | return QCBOR_SUCCESS; |
| 3968 | } |
| 3969 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3970 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3971 | /* |
| 3972 | Compute value with signed mantissa and signed result. Works with exponent of 2 or 10 based on exponentiator. |
| 3973 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 3974 | static inline QCBORError ExponentiateNN(int64_t nMantissa, |
| 3975 | int64_t nExponent, |
| 3976 | int64_t *pnResult, |
| 3977 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3978 | { |
| 3979 | uint64_t uResult; |
| 3980 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 3981 | // Take the absolute value of the mantissa and convert to unsigned. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 3982 | // Improvement: this should be possible in one instruction |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3983 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 3984 | |
| 3985 | // Do the exponentiation of the positive mantissa |
| 3986 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 3987 | if(uReturn) { |
| 3988 | return uReturn; |
| 3989 | } |
| 3990 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3991 | |
Laurence Lundblade | 983500d | 2020-05-14 11:49:34 -0700 | [diff] [blame] | 3992 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 3993 | of INT64_MIN. This assumes two's compliment representation where |
| 3994 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 3995 | Trying to write -INT64_MIN doesn't work to get this because the |
| 3996 | compiler tries to work with an int64_t which can't represent |
| 3997 | -INT64_MIN. |
| 3998 | */ |
| 3999 | uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1; |
| 4000 | |
| 4001 | // Error out if too large |
| 4002 | if(uResult > uMax) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4003 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4004 | } |
| 4005 | |
| 4006 | // Casts are safe because of checks above |
| 4007 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 4008 | |
| 4009 | return QCBOR_SUCCESS; |
| 4010 | } |
| 4011 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4012 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4013 | /* |
| 4014 | Compute value with signed mantissa and unsigned result. Works with exponent of 2 or 10 based on exponentiator. |
| 4015 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4016 | static inline QCBORError ExponentitateNU(int64_t nMantissa, |
| 4017 | int64_t nExponent, |
| 4018 | uint64_t *puResult, |
| 4019 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4020 | { |
| 4021 | if(nMantissa < 0) { |
| 4022 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4023 | } |
| 4024 | |
| 4025 | // Cast to unsigned is OK because of check for negative |
| 4026 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 4027 | // Exponentiation is straight forward |
| 4028 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4029 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4030 | |
| 4031 | |
| 4032 | /* |
| 4033 | Compute value with signed mantissa and unsigned result. Works with exponent of 2 or 10 based on exponentiator. |
| 4034 | */ |
| 4035 | static inline QCBORError ExponentitateUU(uint64_t uMantissa, |
| 4036 | int64_t nExponent, |
| 4037 | uint64_t *puResult, |
| 4038 | fExponentiator pfExp) |
| 4039 | { |
| 4040 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4041 | } |
| 4042 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4043 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4044 | |
| 4045 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4046 | |
| 4047 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4048 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4049 | static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4050 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4051 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4052 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4053 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4054 | const uint8_t *pByte = BigNum.ptr; |
| 4055 | size_t uLen = BigNum.len; |
| 4056 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4057 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4058 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4059 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4060 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4063 | *pResult = uResult; |
| 4064 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4067 | |
Laurence Lundblade | 887add8 | 2020-05-17 05:50:34 -0700 | [diff] [blame] | 4068 | static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4069 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4070 | return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4071 | } |
| 4072 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4073 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4074 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4075 | { |
| 4076 | uint64_t uResult; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4077 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
| 4078 | if(uError) { |
| 4079 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4080 | } |
| 4081 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 4082 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4083 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4087 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4088 | { |
| 4089 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4090 | /* The negative integer furthest from zero for a C int64_t is |
| 4091 | INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4092 | negative number in CBOR is computed as -n - 1 where n is the |
| 4093 | encoded integer, where n is what is in the variable BigNum. When |
| 4094 | converting BigNum to a uint64_t, the maximum value is thus |
| 4095 | INT64_MAX, so that when it -n - 1 is applied to it the result will |
| 4096 | never be further from 0 than INT64_MIN. |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4097 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4098 | -n - 1 <= INT64_MIN. |
| 4099 | -n - 1 <= -INT64_MAX - 1 |
| 4100 | n <= INT64_MAX. |
| 4101 | */ |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4102 | QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4103 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4104 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4105 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4106 | |
| 4107 | /// Now apply -n - 1. The cast is safe because |
| 4108 | // ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4109 | // is the largest positive integer that an int64_t can |
| 4110 | // represent. */ |
| 4111 | *pnResult = -(int64_t)uResult - 1; |
| 4112 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4113 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4116 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4117 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4118 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4119 | #include "fenv.h" |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4120 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4121 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4122 | /* |
| 4123 | Convert a integers and floats to an int64_t. |
| 4124 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4125 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4126 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4127 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4128 | |
| 4129 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4130 | |
| 4131 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small. |
| 4132 | |
| 4133 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4134 | static QCBORError ConvertInt64(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4135 | { |
| 4136 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4137 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4138 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4139 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4140 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4141 | // TODO: what about under/overflow here? |
| 4142 | // Invokes the floating-point HW and/or compiler-added libraries |
| 4143 | feclearexcept(FE_ALL_EXCEPT); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4144 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4145 | *pnValue = llround(pItem->val.dfnum); |
| 4146 | } else { |
| 4147 | *pnValue = lroundf(pItem->val.fnum); |
| 4148 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4149 | if(fetestexcept(FE_INVALID)) { |
| 4150 | // TODO: better error code |
| 4151 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4152 | } |
| 4153 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4154 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4155 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4156 | #else |
| 4157 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4158 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4159 | break; |
| 4160 | |
| 4161 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4162 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4163 | *pnValue = pItem->val.int64; |
| 4164 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4165 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4166 | } |
| 4167 | break; |
| 4168 | |
| 4169 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4170 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4171 | if(pItem->val.uint64 < INT64_MAX) { |
| 4172 | *pnValue = pItem->val.int64; |
| 4173 | } else { |
| 4174 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4175 | } |
| 4176 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4177 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4178 | } |
| 4179 | break; |
| 4180 | |
| 4181 | default: |
| 4182 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4183 | } |
| 4184 | return QCBOR_SUCCESS; |
| 4185 | } |
| 4186 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4187 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4188 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4189 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4190 | int64_t *pnValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4191 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4192 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4193 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4194 | return; |
| 4195 | } |
| 4196 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4197 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4198 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4199 | if(uError) { |
| 4200 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4201 | return; |
| 4202 | } |
| 4203 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4204 | if(pItem) { |
| 4205 | *pItem = Item; |
| 4206 | } |
| 4207 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4208 | pMe->uLastError = (uint8_t)ConvertInt64(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4209 | } |
| 4210 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4211 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4212 | void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4213 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4214 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4215 | int64_t *pnValue, |
| 4216 | QCBORItem *pItem) |
| 4217 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4218 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4219 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4220 | return; |
| 4221 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4222 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4223 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4224 | } |
| 4225 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4226 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4227 | void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4228 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4229 | uint32_t uConvertTypes, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4230 | int64_t *pnValue, |
| 4231 | QCBORItem *pItem) |
| 4232 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4233 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4234 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4235 | return; |
| 4236 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4237 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4238 | pMe->uLastError = (uint8_t)ConvertInt64(pItem, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4242 | /* |
| 4243 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4244 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4245 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4246 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4247 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4248 | |
| 4249 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4250 | |
| 4251 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small. |
| 4252 | |
| 4253 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4254 | static QCBORError Int64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4255 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4256 | switch(pItem->uDataType) { |
| 4257 | |
| 4258 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4259 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4260 | return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4261 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4262 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4263 | } |
| 4264 | break; |
| 4265 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4266 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4267 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4268 | return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4269 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4270 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4271 | } |
| 4272 | break; |
| 4273 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4274 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4275 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4276 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4277 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4278 | pItem->val.expAndMantissa.nExponent, |
| 4279 | pnValue, |
| 4280 | &Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4281 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4282 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4283 | } |
| 4284 | break; |
| 4285 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4286 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4287 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4288 | return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4289 | pItem->val.expAndMantissa.nExponent, |
| 4290 | pnValue, |
| 4291 | Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4292 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4293 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4294 | } |
| 4295 | break; |
| 4296 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4297 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4298 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4299 | int64_t nMantissa; |
| 4300 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4301 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4302 | if(uErr) { |
| 4303 | return uErr; |
| 4304 | } |
| 4305 | return ExponentiateNN(nMantissa, |
| 4306 | pItem->val.expAndMantissa.nExponent, |
| 4307 | pnValue, |
| 4308 | Exponentitate10); |
| 4309 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4310 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4311 | } |
| 4312 | break; |
| 4313 | |
| 4314 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4315 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4316 | int64_t nMantissa; |
| 4317 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4318 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4319 | if(uErr) { |
| 4320 | return uErr; |
| 4321 | } |
| 4322 | return ExponentiateNN(nMantissa, |
| 4323 | pItem->val.expAndMantissa.nExponent, |
| 4324 | pnValue, |
| 4325 | Exponentitate10); |
| 4326 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4327 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4328 | } |
| 4329 | break; |
| 4330 | |
| 4331 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4332 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4333 | int64_t nMantissa; |
| 4334 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4335 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4336 | if(uErr) { |
| 4337 | return uErr; |
| 4338 | } |
| 4339 | return ExponentiateNN(nMantissa, |
| 4340 | pItem->val.expAndMantissa.nExponent, |
| 4341 | pnValue, |
| 4342 | Exponentitate2); |
| 4343 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4344 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4345 | } |
| 4346 | break; |
| 4347 | |
| 4348 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4349 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4350 | int64_t nMantissa; |
| 4351 | QCBORError uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4352 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 4353 | if(uErr) { |
| 4354 | return uErr; |
| 4355 | } |
| 4356 | return ExponentiateNN(nMantissa, |
| 4357 | pItem->val.expAndMantissa.nExponent, |
| 4358 | pnValue, |
| 4359 | Exponentitate2); |
| 4360 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4361 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4362 | } |
| 4363 | break; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4364 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4365 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4366 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4367 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4368 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4369 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4370 | |
| 4371 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4372 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4373 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4374 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4375 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4376 | { |
| 4377 | QCBORItem Item; |
| 4378 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4379 | QCBORDecode_GetInt64ConvertInternal(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4380 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4381 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4382 | // The above conversion succeeded |
| 4383 | return; |
| 4384 | } |
| 4385 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4386 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4387 | // 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] | 4388 | return; |
| 4389 | } |
| 4390 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4391 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4394 | |
| 4395 | /* |
| 4396 | Public function, see header qcbor/qcbor_decode.h file |
| 4397 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4398 | void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4399 | int64_t nLabel, |
| 4400 | uint32_t uConvertTypes, |
| 4401 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4402 | { |
| 4403 | QCBORItem Item; |
| 4404 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4405 | QCBORDecode_GetInt64ConvertInternalInMapN(pMe, nLabel, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4406 | |
| 4407 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4408 | // The above conversion succeeded |
| 4409 | return; |
| 4410 | } |
| 4411 | |
| 4412 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4413 | // The above conversion failed in a way that code below can't correct |
| 4414 | return; |
| 4415 | } |
| 4416 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4417 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4421 | /* |
| 4422 | Public function, see header qcbor/qcbor_decode.h file |
| 4423 | */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4424 | void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4425 | const char *szLabel, |
| 4426 | uint32_t uConvertTypes, |
| 4427 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4428 | { |
| 4429 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4430 | QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4431 | |
| 4432 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4433 | // The above conversion succeeded |
| 4434 | return; |
| 4435 | } |
| 4436 | |
| 4437 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4438 | // The above conversion failed in a way that code below can't correct |
| 4439 | return; |
| 4440 | } |
| 4441 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4442 | pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uConvertTypes, pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4446 | static QCBORError ConvertUint64(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4447 | { |
| 4448 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4449 | case QCBOR_TYPE_DOUBLE: |
| 4450 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4451 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4452 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4453 | // TODO: this code needs work |
| 4454 | feclearexcept(FE_ALL_EXCEPT); |
| 4455 | double dRounded = round(pItem->val.dfnum); |
| 4456 | // TODO: over/underflow |
| 4457 | if(fetestexcept(FE_INVALID)) { |
| 4458 | // TODO: better error code |
| 4459 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4460 | } else if(isnan(dRounded)) { |
| 4461 | // TODO: better error code |
| 4462 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4463 | } else if(dRounded >= 0) { |
| 4464 | *puValue = (uint64_t)dRounded; |
| 4465 | } else { |
| 4466 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4467 | } |
| 4468 | } else { |
| 4469 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4470 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4471 | #else |
| 4472 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4473 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4474 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4475 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4476 | case QCBOR_TYPE_INT64: |
| 4477 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4478 | if(pItem->val.int64 >= 0) { |
| 4479 | *puValue = (uint64_t)pItem->val.int64; |
| 4480 | } else { |
| 4481 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4482 | } |
| 4483 | } else { |
| 4484 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4485 | } |
| 4486 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4487 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4488 | case QCBOR_TYPE_UINT64: |
| 4489 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 4490 | *puValue = pItem->val.uint64; |
| 4491 | } else { |
| 4492 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4493 | } |
| 4494 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4495 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4496 | default: |
| 4497 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4498 | } |
| 4499 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4500 | return QCBOR_SUCCESS; |
| 4501 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4502 | |
| 4503 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4504 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4505 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4506 | uint64_t *puValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4507 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4508 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4509 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4510 | return; |
| 4511 | } |
| 4512 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4513 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4514 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4515 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4516 | if(uError) { |
| 4517 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4518 | return; |
| 4519 | } |
| 4520 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 4521 | if(pItem) { |
| 4522 | *pItem = Item; |
| 4523 | } |
| 4524 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4525 | pMe->uLastError = (uint8_t)ConvertUint64(&Item, uConvertTypes, puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4526 | } |
| 4527 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4528 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4529 | void QCBORDecode_GetUint64ConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4530 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4531 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4532 | uint64_t *puValue, |
| 4533 | QCBORItem *pItem) |
| 4534 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4535 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4536 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4537 | return; |
| 4538 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4539 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4540 | pMe->uLastError = (uint8_t)ConvertUint64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 4544 | void QCBORDecode_GetUInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4545 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4546 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4547 | uint64_t *puValue, |
| 4548 | QCBORItem *pItem) |
| 4549 | { |
| 4550 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4551 | return; |
| 4552 | } |
| 4553 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4554 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4555 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4556 | return; |
| 4557 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4558 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4559 | pMe->uLastError = (uint8_t)ConvertUint64(pItem, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4560 | } |
| 4561 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4562 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4563 | /* |
| 4564 | Public function, see header qcbor/qcbor_decode.h file |
| 4565 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4566 | static QCBORError Uint64ConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4567 | { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4568 | switch(pItem->uDataType) { |
| 4569 | |
| 4570 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4571 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4572 | return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
| 4573 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4574 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4575 | } |
| 4576 | break; |
| 4577 | |
| 4578 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4579 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4580 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4581 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4582 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4583 | } |
| 4584 | break; |
| 4585 | |
| 4586 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 4587 | |
| 4588 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4589 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4590 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4591 | pItem->val.expAndMantissa.nExponent, |
| 4592 | puValue, |
| 4593 | Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4594 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4595 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4596 | } |
| 4597 | break; |
| 4598 | |
| 4599 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4600 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4601 | return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
| 4602 | pItem->val.expAndMantissa.nExponent, |
| 4603 | puValue, |
| 4604 | Exponentitate2); |
| 4605 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4606 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4607 | } |
| 4608 | break; |
| 4609 | |
| 4610 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4611 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4612 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4613 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4614 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4615 | if(uErr != QCBOR_SUCCESS) { |
| 4616 | return uErr; |
| 4617 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4618 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4619 | pItem->val.expAndMantissa.nExponent, |
| 4620 | puValue, |
| 4621 | Exponentitate10); |
| 4622 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4623 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4624 | } |
| 4625 | break; |
| 4626 | |
| 4627 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4628 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4629 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4630 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4631 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4632 | } |
| 4633 | break; |
| 4634 | |
| 4635 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4636 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4637 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4638 | QCBORError uErr; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4639 | uErr = ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4640 | if(uErr != QCBOR_SUCCESS) { |
| 4641 | return uErr; |
| 4642 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4643 | return ExponentitateUU(uMantissa, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4644 | pItem->val.expAndMantissa.nExponent, |
| 4645 | puValue, |
| 4646 | Exponentitate2); |
| 4647 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4648 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4649 | } |
| 4650 | break; |
| 4651 | |
| 4652 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4653 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4654 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4655 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4656 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4657 | } |
| 4658 | break; |
| 4659 | #endif |
| 4660 | default: |
| 4661 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4662 | } |
| 4663 | } |
| 4664 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4665 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4666 | /* |
| 4667 | Public function, see header qcbor/qcbor_decode.h file |
| 4668 | */ |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4669 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uConvertTypes, uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4670 | { |
| 4671 | QCBORItem Item; |
| 4672 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4673 | QCBORDecode_GetUInt64ConvertInternal(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4674 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 4675 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4676 | // The above conversion succeeded |
| 4677 | return; |
| 4678 | } |
| 4679 | |
| 4680 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4681 | // 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] | 4682 | return; |
| 4683 | } |
| 4684 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4685 | pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4686 | } |
| 4687 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4688 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4689 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4690 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4691 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 4692 | void QCBORDecode_GetUint64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 4693 | int64_t nLabel, |
| 4694 | uint32_t uConvertTypes, |
| 4695 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4696 | { |
| 4697 | QCBORItem Item; |
| 4698 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4699 | QCBORDecode_GetUint64ConvertInternalInMapN(pMe, nLabel, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4700 | |
| 4701 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4702 | // The above conversion succeeded |
| 4703 | return; |
| 4704 | } |
| 4705 | |
| 4706 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4707 | // The above conversion failed in a way that code below can't correct |
| 4708 | return; |
| 4709 | } |
| 4710 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4711 | pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | |
| 4715 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4716 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4717 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 4718 | void QCBORDecode_GetUint64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 4719 | const char *szLabel, |
| 4720 | uint32_t uConvertTypes, |
| 4721 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4722 | { |
| 4723 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4724 | QCBORDecode_GetUInt64ConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4725 | |
| 4726 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4727 | // The above conversion succeeded |
| 4728 | return; |
| 4729 | } |
| 4730 | |
| 4731 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 4732 | // The above conversion failed in a way that code below can't correct |
| 4733 | return; |
| 4734 | } |
| 4735 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4736 | pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uConvertTypes, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 4740 | static QCBORError ConvertDouble(const QCBORItem *pItem, |
| 4741 | uint32_t uConvertTypes, |
| 4742 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4743 | { |
| 4744 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4745 | case QCBOR_TYPE_FLOAT: |
| 4746 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 4747 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4748 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4749 | *pdValue = (double)pItem->val.fnum; |
| 4750 | } else { |
| 4751 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4752 | } |
| 4753 | } |
| 4754 | #else |
| 4755 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4756 | #endif |
| 4757 | break; |
| 4758 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4759 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4760 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 4761 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4762 | *pdValue = pItem->val.dfnum; |
| 4763 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4764 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | break; |
| 4768 | |
| 4769 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4770 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4771 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4772 | // TODO: how does this work? |
| 4773 | *pdValue = (double)pItem->val.int64; |
| 4774 | |
| 4775 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4776 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4777 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4778 | #else |
| 4779 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4780 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4781 | break; |
| 4782 | |
| 4783 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4784 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4785 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4786 | *pdValue = (double)pItem->val.uint64; |
| 4787 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4788 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4789 | } |
| 4790 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4791 | #else |
| 4792 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4793 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4794 | |
| 4795 | default: |
| 4796 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4797 | } |
| 4798 | |
| 4799 | return QCBOR_SUCCESS; |
| 4800 | } |
| 4801 | |
| 4802 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4803 | void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4804 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4805 | double *pdValue, |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4806 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4807 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4808 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4809 | return; |
| 4810 | } |
| 4811 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4812 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4813 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4814 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4815 | if(uError) { |
| 4816 | pMe->uLastError = (uint8_t)uError; |
| 4817 | return; |
| 4818 | } |
| 4819 | |
| 4820 | if(pItem) { |
| 4821 | *pItem = Item; |
| 4822 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4823 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4824 | pMe->uLastError = (uint8_t)ConvertDouble(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4825 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4826 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4827 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4828 | void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe, |
| 4829 | int64_t nLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4830 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4831 | double *pdValue, |
| 4832 | QCBORItem *pItem) |
| 4833 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4834 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4835 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4836 | return; |
| 4837 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4838 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4839 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4840 | } |
| 4841 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4842 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4843 | void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe, |
| 4844 | const char * szLabel, |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4845 | uint32_t uConvertTypes, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4846 | double *pdValue, |
| 4847 | QCBORItem *pItem) |
| 4848 | { |
| 4849 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4850 | return; |
| 4851 | } |
| 4852 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4853 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4854 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4855 | return; |
| 4856 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4857 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4858 | pMe->uLastError = (uint8_t)ConvertDouble(pItem, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4862 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4863 | static double ConvertBigNumToDouble(const UsefulBufC BigNum) |
| 4864 | { |
| 4865 | double dResult; |
| 4866 | |
| 4867 | dResult = 0.0; |
| 4868 | const uint8_t *pByte = BigNum.ptr; |
| 4869 | size_t uLen = BigNum.len; |
| 4870 | /* This will overflow and become the float value INFINITY if the number |
| 4871 | is too large to fit. No error will be logged. |
| 4872 | TODO: should an error be logged? */ |
| 4873 | while(uLen--) { |
| 4874 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 4875 | } |
| 4876 | |
| 4877 | return dResult; |
| 4878 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4879 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4880 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4881 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4882 | static QCBORError DoubleConvertAll(const QCBORItem *pItem, uint32_t uConvertTypes, double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4883 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4884 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4885 | /* |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4886 | https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 4887 | |
| 4888 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4889 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4890 | |
| 4891 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4892 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4893 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4894 | // TODO: rounding and overflow errors |
| 4895 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4896 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 4897 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4898 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4899 | } |
| 4900 | break; |
| 4901 | |
| 4902 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4903 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4904 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 4905 | exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4906 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4907 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4908 | } |
| 4909 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4910 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4911 | |
| 4912 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4913 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4914 | *pdValue = ConvertBigNumToDouble(pItem->val.bigNum); |
| 4915 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4916 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4917 | } |
| 4918 | break; |
| 4919 | |
| 4920 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4921 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4922 | *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4923 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4924 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4925 | } |
| 4926 | break; |
| 4927 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4928 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4929 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4930 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4931 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4932 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4933 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4934 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4935 | } |
| 4936 | break; |
| 4937 | |
| 4938 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4939 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4940 | double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4941 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 4942 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4943 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4944 | } |
| 4945 | break; |
| 4946 | |
| 4947 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4948 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4949 | double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 4950 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4951 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4952 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4953 | } |
| 4954 | break; |
| 4955 | |
| 4956 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4957 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 4958 | double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4959 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 4960 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4961 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4962 | } |
| 4963 | break; |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 4964 | #endif /* ndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 4965 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4966 | |
| 4967 | default: |
| 4968 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4969 | } |
| 4970 | |
| 4971 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4972 | |
| 4973 | #else |
| 4974 | (void)pItem; |
| 4975 | (void)uConvertTypes; |
| 4976 | (void)pdValue; |
| 4977 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4978 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 4979 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | |
| 4983 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4984 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4985 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 4986 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 4987 | uint32_t uConvertTypes, |
| 4988 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4989 | { |
| 4990 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4991 | QCBORItem Item; |
| 4992 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4993 | QCBORDecode_GetDoubleConvertInternal(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4994 | |
| 4995 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4996 | // The above conversion succeeded |
| 4997 | return; |
| 4998 | } |
| 4999 | |
| 5000 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5001 | // The above conversion failed in a way that code below can't correct |
| 5002 | return; |
| 5003 | } |
| 5004 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5005 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5006 | } |
| 5007 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5008 | |
| 5009 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5010 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5011 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 5012 | void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5013 | int64_t nLabel, |
| 5014 | uint32_t uConvertTypes, |
| 5015 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5016 | { |
| 5017 | QCBORItem Item; |
| 5018 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5019 | QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5020 | |
| 5021 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5022 | // The above conversion succeeded |
| 5023 | return; |
| 5024 | } |
| 5025 | |
| 5026 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5027 | // The above conversion failed in a way that code below can't correct |
| 5028 | return; |
| 5029 | } |
| 5030 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5031 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | |
| 5035 | /* |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5036 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5037 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 5038 | void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5039 | const char *szLabel, |
| 5040 | uint32_t uConvertTypes, |
| 5041 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5042 | { |
| 5043 | QCBORItem Item; |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5044 | QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5045 | |
| 5046 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5047 | // The above conversion succeeded |
| 5048 | return; |
| 5049 | } |
| 5050 | |
| 5051 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5052 | // The above conversion failed in a way that code below can't correct |
| 5053 | return; |
| 5054 | } |
| 5055 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5056 | pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5057 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5058 | |
| 5059 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5060 | |
| 5061 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5062 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5063 | static inline UsefulBufC ConvertIntToBigNum(uint64_t uInt, UsefulBuf Buffer) |
| 5064 | { |
| 5065 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5066 | uInt = uInt << 8; |
| 5067 | }; |
| 5068 | |
| 5069 | UsefulOutBuf UOB; |
| 5070 | |
| 5071 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5072 | |
| 5073 | while(uInt) { |
| 5074 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5075 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5076 | uInt = uInt << 8; |
| 5077 | (void)xx; |
| 5078 | } |
| 5079 | |
| 5080 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5081 | } |
| 5082 | |
| 5083 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5084 | static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe, |
| 5085 | TagSpecification TagSpec, |
| 5086 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5087 | { |
| 5088 | QCBORError uErr; |
| 5089 | // 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] | 5090 | while(1) { |
| 5091 | uErr = CheckTagRequirement(TagSpec, pItem->uDataType); |
| 5092 | if(uErr != QCBOR_SUCCESS) { |
| 5093 | goto Done; |
| 5094 | } |
| 5095 | |
| 5096 | if(pItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 5097 | break; // Successful exit. Moving on to finish decoding. |
| 5098 | } |
| 5099 | |
| 5100 | // The item is an array, which means an undecoded |
| 5101 | // mantissa and exponent, so decode it. It will then |
| 5102 | // have a different type and exit the loop if. |
| 5103 | uErr = QCBORDecode_MantissaAndExponent(pMe, pItem); |
| 5104 | if(uErr != QCBOR_SUCCESS) { |
| 5105 | goto Done; |
| 5106 | } |
| 5107 | |
| 5108 | // Second time around, the type must match. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 5109 | TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5110 | } |
| 5111 | Done: |
| 5112 | return uErr; |
| 5113 | } |
| 5114 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5115 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5116 | static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5117 | TagSpecification TagSpec, |
| 5118 | QCBORItem *pItem, |
| 5119 | int64_t *pnMantissa, |
| 5120 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5121 | { |
| 5122 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5123 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5124 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5125 | if(uErr != QCBOR_SUCCESS) { |
| 5126 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5127 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5128 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 5129 | switch (pItem->uDataType) { |
| 5130 | |
| 5131 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 5132 | case QCBOR_TYPE_BIGFLOAT: |
| 5133 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
| 5134 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5135 | break; |
| 5136 | |
| 5137 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 5138 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 5139 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5140 | uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5141 | break; |
| 5142 | |
| 5143 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 5144 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 5145 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5146 | uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
| 5147 | break; |
| 5148 | |
| 5149 | default: |
| 5150 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5151 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5152 | |
| 5153 | Done: |
| 5154 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5155 | } |
| 5156 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5157 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5158 | static void ProcessMantissaAndExponentBig(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5159 | TagSpecification TagSpec, |
| 5160 | QCBORItem *pItem, |
| 5161 | UsefulBuf BufferForMantissa, |
| 5162 | UsefulBufC *pMantissa, |
| 5163 | bool *pbIsNegative, |
| 5164 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5165 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5166 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5167 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5168 | uErr = MantissaAndExponentTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5169 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5170 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | uint64_t uMantissa; |
| 5174 | |
| 5175 | switch (pItem->uDataType) { |
| 5176 | |
| 5177 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5178 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5179 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 5180 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 5181 | *pbIsNegative = false; |
| 5182 | } else { |
| 5183 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 5184 | *pbIsNegative = true; |
| 5185 | } |
| 5186 | *pMantissa = ConvertIntToBigNum(uMantissa, BufferForMantissa); |
| 5187 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5188 | break; |
| 5189 | |
| 5190 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5191 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5192 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5193 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5194 | *pbIsNegative = false; |
| 5195 | break; |
| 5196 | |
| 5197 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5198 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5199 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 5200 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 5201 | *pbIsNegative = true; |
| 5202 | break; |
| 5203 | |
| 5204 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5205 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5206 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5207 | |
| 5208 | Done: |
| 5209 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5213 | /* |
| 5214 | Public function, see header qcbor/qcbor_decode.h file |
| 5215 | */ |
| 5216 | void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 5217 | uint8_t uTagRequirement, |
| 5218 | int64_t *pnMantissa, |
| 5219 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5220 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5221 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5222 | return; |
| 5223 | } |
| 5224 | |
| 5225 | QCBORItem Item; |
| 5226 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5227 | if(uError) { |
| 5228 | pMe->uLastError = (uint8_t)uError; |
| 5229 | return; |
| 5230 | } |
| 5231 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5232 | const TagSpecification TagSpec = |
| 5233 | { |
| 5234 | uTagRequirement, |
| 5235 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM}, |
| 5236 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5237 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5238 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5239 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5242 | |
| 5243 | /* |
| 5244 | Public function, see header qcbor/qcbor_decode.h file |
| 5245 | */ |
| 5246 | void QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5247 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5248 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5249 | int64_t *pnMantissa, |
| 5250 | int64_t *pnExponent) |
| 5251 | { |
| 5252 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5253 | return; |
| 5254 | } |
| 5255 | |
| 5256 | QCBORItem Item; |
| 5257 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5258 | |
| 5259 | const TagSpecification TagSpec = |
| 5260 | { |
| 5261 | uTagRequirement, |
| 5262 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM}, |
| 5263 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5264 | }; |
| 5265 | |
| 5266 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5267 | } |
| 5268 | |
| 5269 | |
| 5270 | /* |
| 5271 | Public function, see header qcbor/qcbor_decode.h file |
| 5272 | */ |
| 5273 | void QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5274 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5275 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5276 | int64_t *pnMantissa, |
| 5277 | int64_t *pnExponent) |
| 5278 | { |
| 5279 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5280 | return; |
| 5281 | } |
| 5282 | |
| 5283 | QCBORItem Item; |
| 5284 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5285 | |
| 5286 | const TagSpecification TagSpec = |
| 5287 | { |
| 5288 | uTagRequirement, |
| 5289 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM}, |
| 5290 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5291 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame^] | 5292 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5293 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5294 | } |
| 5295 | |
| 5296 | |
| 5297 | /* |
| 5298 | Public function, see header qcbor/qcbor_decode.h file |
| 5299 | */ |
| 5300 | void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 5301 | uint8_t uTagRequirement, |
| 5302 | UsefulBuf MantissaBuffer, |
| 5303 | UsefulBufC *pMantissa, |
| 5304 | bool *pbMantissaIsNegative, |
| 5305 | int64_t *pnExponent) |
| 5306 | { |
| 5307 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5308 | return; |
| 5309 | } |
| 5310 | |
| 5311 | QCBORItem Item; |
| 5312 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5313 | if(uError) { |
| 5314 | pMe->uLastError = (uint8_t)uError; |
| 5315 | return; |
| 5316 | } |
| 5317 | |
| 5318 | const TagSpecification TagSpec = |
| 5319 | { |
| 5320 | uTagRequirement, |
| 5321 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM}, |
| 5322 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5323 | }; |
| 5324 | |
| 5325 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
| 5326 | } |
| 5327 | |
| 5328 | |
| 5329 | /* |
| 5330 | Public function, see header qcbor/qcbor_decode.h file |
| 5331 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5332 | void QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5333 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5334 | uint8_t uTagRequirement, |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5335 | UsefulBuf BufferForMantissa, |
| 5336 | UsefulBufC *pMantissa, |
| 5337 | bool *pbIsNegative, |
| 5338 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5339 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5340 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5341 | return; |
| 5342 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5343 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5344 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5345 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5346 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5347 | return; |
| 5348 | } |
| 5349 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5350 | const TagSpecification TagSpec = |
| 5351 | { |
| 5352 | uTagRequirement, |
| 5353 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM}, |
| 5354 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5355 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5356 | |
| 5357 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5358 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5359 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5360 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5361 | /* |
| 5362 | Public function, see header qcbor/qcbor_decode.h file |
| 5363 | */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5364 | void QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5365 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5366 | uint8_t uTagRequirement, |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5367 | UsefulBuf BufferForMantissa, |
| 5368 | UsefulBufC *pMantissa, |
| 5369 | bool *pbIsNegative, |
| 5370 | int64_t *pnExponent) |
| 5371 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5372 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5373 | return; |
| 5374 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5375 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5376 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5377 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5378 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5379 | return; |
| 5380 | } |
| 5381 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5382 | const TagSpecification TagSpec = |
| 5383 | { |
| 5384 | uTagRequirement, |
| 5385 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM}, |
| 5386 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5387 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5388 | |
| 5389 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5390 | } |
| 5391 | |
| 5392 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5393 | /* |
| 5394 | Public function, see header qcbor/qcbor_decode.h file |
| 5395 | */ |
| 5396 | void QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 5397 | uint8_t uTagRequirement, |
| 5398 | int64_t *pnMantissa, |
| 5399 | int64_t *pnExponent) |
| 5400 | { |
| 5401 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5402 | return; |
| 5403 | } |
| 5404 | |
| 5405 | QCBORItem Item; |
| 5406 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5407 | if(uError) { |
| 5408 | pMe->uLastError = (uint8_t)uError; |
| 5409 | return; |
| 5410 | } |
| 5411 | const TagSpecification TagSpec = |
| 5412 | { |
| 5413 | uTagRequirement, |
| 5414 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM}, |
| 5415 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5416 | }; |
| 5417 | |
| 5418 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5419 | } |
| 5420 | |
| 5421 | |
| 5422 | /* |
| 5423 | Public function, see header qcbor/qcbor_decode.h file |
| 5424 | */ |
| 5425 | void QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5426 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5427 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5428 | int64_t *pnMantissa, |
| 5429 | int64_t *pnExponent) |
| 5430 | { |
| 5431 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5432 | return; |
| 5433 | } |
| 5434 | |
| 5435 | QCBORItem Item; |
| 5436 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5437 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5438 | return; |
| 5439 | } |
| 5440 | |
| 5441 | const TagSpecification TagSpec = |
| 5442 | { |
| 5443 | uTagRequirement, |
| 5444 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM}, |
| 5445 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5446 | }; |
| 5447 | |
| 5448 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5449 | } |
| 5450 | |
| 5451 | |
| 5452 | /* |
| 5453 | Public function, see header qcbor/qcbor_decode.h file |
| 5454 | */ |
| 5455 | void QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5456 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5457 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5458 | int64_t *pnMantissa, |
| 5459 | int64_t *pnExponent) |
| 5460 | { |
| 5461 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5462 | return; |
| 5463 | } |
| 5464 | |
| 5465 | QCBORItem Item; |
| 5466 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5467 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5468 | return; |
| 5469 | } |
| 5470 | |
| 5471 | const TagSpecification TagSpec = |
| 5472 | { |
| 5473 | uTagRequirement, |
| 5474 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM}, |
| 5475 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5476 | }; |
| 5477 | |
| 5478 | ProcessMantissaAndExponent(pMe, TagSpec, &Item, pnMantissa, pnExponent); |
| 5479 | } |
| 5480 | |
| 5481 | |
| 5482 | /* |
| 5483 | Public function, see header qcbor/qcbor_decode.h file |
| 5484 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5485 | void QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 5486 | uint8_t uTagRequirement, |
| 5487 | UsefulBuf MantissaBuffer, |
| 5488 | UsefulBufC *pMantissa, |
| 5489 | bool *pbMantissaIsNegative, |
| 5490 | int64_t *pnExponent) |
| 5491 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5492 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5493 | return; |
| 5494 | } |
| 5495 | |
| 5496 | QCBORItem Item; |
| 5497 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5498 | if(uError) { |
| 5499 | pMe->uLastError = (uint8_t)uError; |
| 5500 | return; |
| 5501 | } |
| 5502 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5503 | const TagSpecification TagSpec = |
| 5504 | { |
| 5505 | uTagRequirement, |
| 5506 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM}, |
| 5507 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5508 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5509 | |
| 5510 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5511 | } |
| 5512 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5513 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5514 | /* |
| 5515 | Public function, see header qcbor/qcbor_decode.h file |
| 5516 | */ |
| 5517 | void QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5518 | int64_t nLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5519 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5520 | UsefulBuf BufferForMantissa, |
| 5521 | UsefulBufC *pMantissa, |
| 5522 | bool *pbIsNegative, |
| 5523 | int64_t *pnExponent) |
| 5524 | { |
| 5525 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5526 | return; |
| 5527 | } |
| 5528 | |
| 5529 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5530 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 5531 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5532 | return; |
| 5533 | } |
| 5534 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5535 | const TagSpecification TagSpec = |
| 5536 | { |
| 5537 | uTagRequirement, |
| 5538 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM}, |
| 5539 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5540 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5541 | |
| 5542 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
| 5543 | } |
| 5544 | |
| 5545 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5546 | /* |
| 5547 | Public function, see header qcbor/qcbor_decode.h file |
| 5548 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5549 | void QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5550 | const char *szLabel, |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 5551 | uint8_t uTagRequirement, |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5552 | UsefulBuf BufferForMantissa, |
| 5553 | UsefulBufC *pMantissa, |
| 5554 | bool *pbIsNegative, |
| 5555 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5556 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5557 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5558 | return; |
| 5559 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5560 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5561 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5562 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 5563 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5564 | return; |
| 5565 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5566 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5567 | const TagSpecification TagSpec = |
| 5568 | { |
| 5569 | uTagRequirement, |
| 5570 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM}, |
| 5571 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 5572 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5573 | |
| 5574 | ProcessMantissaAndExponentBig(pMe, TagSpec, &Item, BufferForMantissa, pMantissa, pbIsNegative, pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5575 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 5576 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5577 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |