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 | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 35 | #include "ieee754.h" |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 36 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 37 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 38 | /* |
| 39 | This casts away the const-ness of a pointer, usually so it can be |
| 40 | freed or realloced. |
| 41 | */ |
| 42 | #define UNCONST_POINTER(ptr) ((void *)(ptr)) |
| 43 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 44 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 45 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 46 | /*=========================================================================== |
| 47 | DecodeNesting -- Functions for tracking array/map nesting when decoding |
| 48 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 49 | See qcbor/qcbor_decode.h for definition of the object |
| 50 | used here: QCBORDecodeNesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 51 | ===========================================================================*/ |
| 52 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 53 | |
| 54 | |
| 55 | /* |
| 56 | The main mode of decoding is a pre-order travesal of the tree of leaves (numbers, strings...) |
| 57 | formed by intermediate nodes (arrays and maps). The cursor for the traversal |
| 58 | is the byte offset in the encoded input and a leaf counter for definite |
| 59 | length maps and arrays. Indefinite length maps and arrays are handled |
| 60 | by look ahead for the break. |
| 61 | |
| 62 | The view presented to the caller has tags, labels and the chunks of |
| 63 | indefinite length strings aggregated into one decorated data item. |
| 64 | |
| 65 | The caller understands the nesting level in pre-order traversal by |
| 66 | the fact that a data item that is a map or array is presented to |
| 67 | the caller when it is first encountered in the pre-order traversal and that all data items are presented with its nesting level |
| 68 | and the nesting level of the next item. |
| 69 | |
| 70 | The caller traverse maps and arrays in a special mode that often more convenient |
| 71 | that tracking by nesting level. When an array or map is expected or encountered |
| 72 | the EnterMap or EnteryArray can be called. |
| 73 | |
| 74 | When entering a map or array like this, the cursor points to the first |
| 75 | item in the map or array. When exiting, it points to the item after |
| 76 | the map or array, regardless of whether the items in the map or array were |
| 77 | all traversed. |
| 78 | |
| 79 | When in a map or array, the cursor functions as normal, but traversal |
| 80 | cannot go past the end of the map or array that was entered. If this |
| 81 | is attempted the QCBOR_ERR_NO_MORE_ITEMS error is returned. To |
| 82 | go past the end of the map or array ExitMap() or ExitArray() must |
| 83 | be called. It can be called any time regardless of the position |
| 84 | of the cursor. |
| 85 | |
| 86 | When a map is entered, a special function allows fetching data items |
| 87 | by label. This call will traversal the whole map looking for the |
| 88 | labeled item. The whole map is traversed so as to detect duplicates. |
| 89 | This type of fetching items does not affect the normal traversal |
| 90 | cursor. |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | When a data item is presented to the caller, the nesting level of the data |
| 102 | item is presented along with the nesting level of the item that would be |
| 103 | next consumed. |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | */ |
| 114 | |
Laurence Lundblade | 6b24930 | 2020-04-30 12:38:12 -0700 | [diff] [blame^] | 115 | inline static bool |
| 116 | // TODO: test Map as array better? |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 117 | IsMapOrArray(uint8_t uDataType) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 118 | { |
Laurence Lundblade | 6b24930 | 2020-04-30 12:38:12 -0700 | [diff] [blame^] | 119 | return uDataType == QCBOR_TYPE_MAP || |
| 120 | uDataType == QCBOR_TYPE_ARRAY || |
| 121 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 124 | inline static bool |
| 125 | DecodeNesting_IsAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 126 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 127 | if(pNesting->pCurrent == &(pNesting->pMapsAndArrays[0])) { |
| 128 | return true; |
| 129 | } else { |
| 130 | return false; |
| 131 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 134 | // Determine if at the end of a map or array, taking into |
| 135 | // account map mode. If this returns true, it is OK |
| 136 | // to get another item. |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 137 | inline static bool |
| 138 | DecodeNesting_AtEnd(const QCBORDecodeNesting *pNesting) |
| 139 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 140 | if(DecodeNesting_IsAtTop(pNesting)){ |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 141 | // Always at end if at the top level of nesting |
| 142 | return true; |
| 143 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 144 | |
| 145 | if(pNesting->pCurrent->uMapMode) { |
| 146 | if(pNesting->pCurrent->uCount == 0) { |
| 147 | // In map mode and consumed all items, so it is the end |
| 148 | return true; |
| 149 | } else { |
| 150 | // In map mode, all items not consumed, so it is NOT the end |
| 151 | return false; |
| 152 | } |
| 153 | } else { |
| 154 | // Not in map mode, and not at top level so it NOT the end. |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 155 | return false; |
| 156 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 160 | inline static int |
| 161 | DecodeNesting_IsIndefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 162 | { |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 163 | return pNesting->pCurrent->uCount == UINT16_MAX; |
| 164 | } |
| 165 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 166 | inline static uint8_t |
| 167 | DecodeNesting_GetLevel(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 168 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 169 | // Check in DecodeNesting_Descend and never having |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 170 | // QCBOR_MAX_ARRAY_NESTING > 255 gaurantees cast is safe |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 171 | return (uint8_t)(pNesting->pCurrent - &(pNesting->pMapsAndArrays[0])); |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 174 | inline static int |
| 175 | DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 176 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 177 | if(DecodeNesting_IsAtTop(pNesting)) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 178 | return 0; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 179 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 180 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 181 | return CBOR_MAJOR_TYPE_MAP == pNesting->pCurrent->uMajorType; |
| 182 | } |
| 183 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 184 | // Process a break. This will either ascend the nesting or error out |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 185 | inline static QCBORError |
| 186 | DecodeNesting_BreakAscend(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 187 | { |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 188 | // breaks must always occur when there is nesting |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 189 | if(DecodeNesting_IsAtTop(pNesting)) { |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 190 | return QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 191 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 192 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 193 | // breaks can only occur when the map/array is indefinite length |
| 194 | if(!DecodeNesting_IsIndefiniteLength(pNesting)) { |
| 195 | return QCBOR_ERR_BAD_BREAK; |
| 196 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 197 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 198 | // if all OK, the break reduces the level of nesting |
| 199 | pNesting->pCurrent--; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 200 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 201 | return QCBOR_SUCCESS; |
| 202 | } |
| 203 | |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 204 | // Called on every single item except breaks including decode of a map/array |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 205 | /* Decrements the map/array counter if possible. If decrement |
| 206 | closed out a map or array, then level up in nesting and decrement |
| 207 | again, until, the top is reached or the end of a map mode is reached |
| 208 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 209 | inline static void |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 210 | DecodeNesting_DecrementCount(QCBORDecodeNesting *pNesting, bool bExitingMapMode) |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 211 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 212 | while(!DecodeNesting_IsAtTop(pNesting)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 213 | // Not at the top level, so there is decrementing to be done. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 214 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 215 | if(!DecodeNesting_IsIndefiniteLength(pNesting)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 216 | // Decrement the current nesting level if it is not indefinite. |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 217 | pNesting->pCurrent->uCount--; |
| 218 | } |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 219 | |
| 220 | if(pNesting->pCurrent->uCount != 0) { |
| 221 | // Did not close out an array or map, so nothing further |
| 222 | break; |
| 223 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 224 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 225 | if(pNesting->pCurrent->uMapMode && !bExitingMapMode) { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 226 | // In map mode the level-up must be done explicitly |
| 227 | break; |
| 228 | } |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 229 | |
| 230 | // Closed out an array or map so level up |
| 231 | pNesting->pCurrent--; |
| 232 | |
| 233 | // Continue with loop to see if closing out this doesn't close out more |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 237 | // Called on every map/array |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 238 | inline static QCBORError |
| 239 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, QCBORItem *pItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 240 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 241 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 242 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 243 | if(pItem->val.uCount == 0) { |
| 244 | // Nothing to do for empty definite lenth arrays. They are just are |
| 245 | // effectively the same as an item that is not a map or array |
| 246 | goto Done; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 247 | // Empty indefinite length maps and arrays are handled elsewhere |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 248 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 249 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 250 | // Error out if arrays is too long to handle |
| 251 | if(pItem->val.uCount != UINT16_MAX && pItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 252 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 253 | goto Done; |
| 254 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 255 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 256 | // Error out if nesting is too deep |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 257 | if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) { |
| 258 | nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP; |
| 259 | goto Done; |
| 260 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 261 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 262 | // The actual descend |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 263 | pNesting->pCurrent++; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 264 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 265 | // Record a few details for this nesting level |
| 266 | pNesting->pCurrent->uMajorType = pItem->uDataType; |
| 267 | pNesting->pCurrent->uCount = pItem->val.uCount; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 268 | pNesting->pCurrent->uMapMode = 0; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 269 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 270 | Done: |
| 271 | return nReturn;; |
| 272 | } |
| 273 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 274 | inline static void |
| 275 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 276 | { |
| 277 | pNesting->pCurrent = &(pNesting->pMapsAndArrays[0]); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 282 | /* |
| 283 | This list of built-in tags. Only add tags here that are |
| 284 | clearly established and useful. Once a tag is added here |
| 285 | it can't be taken out as that would break backwards compatibility. |
| 286 | There are only 48 slots available forever. |
| 287 | */ |
| 288 | static const uint16_t spBuiltInTagMap[] = { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 289 | CBOR_TAG_DATE_STRING, // See TAG_MAPPER_FIRST_SIX |
| 290 | CBOR_TAG_DATE_EPOCH, // See TAG_MAPPER_FIRST_SIX |
| 291 | CBOR_TAG_POS_BIGNUM, // See TAG_MAPPER_FIRST_SIX |
| 292 | CBOR_TAG_NEG_BIGNUM, // See TAG_MAPPER_FIRST_SIX |
| 293 | CBOR_TAG_DECIMAL_FRACTION, // See TAG_MAPPER_FIRST_SIX |
| 294 | CBOR_TAG_BIGFLOAT, // See TAG_MAPPER_FIRST_SIX |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 295 | CBOR_TAG_COSE_ENCRYPTO, |
| 296 | CBOR_TAG_COSE_MAC0, |
| 297 | CBOR_TAG_COSE_SIGN1, |
| 298 | CBOR_TAG_ENC_AS_B64URL, |
| 299 | CBOR_TAG_ENC_AS_B64, |
| 300 | CBOR_TAG_ENC_AS_B16, |
| 301 | CBOR_TAG_CBOR, |
| 302 | CBOR_TAG_URI, |
| 303 | CBOR_TAG_B64URL, |
| 304 | CBOR_TAG_B64, |
| 305 | CBOR_TAG_REGEX, |
| 306 | CBOR_TAG_MIME, |
| 307 | CBOR_TAG_BIN_UUID, |
| 308 | CBOR_TAG_CWT, |
| 309 | CBOR_TAG_ENCRYPT, |
| 310 | CBOR_TAG_MAC, |
| 311 | CBOR_TAG_SIGN, |
| 312 | CBOR_TAG_GEO_COORD, |
| 313 | CBOR_TAG_CBOR_MAGIC |
| 314 | }; |
| 315 | |
| 316 | // This is used in a bit of cleverness in GetNext_TaggedItem() to |
| 317 | // keep code size down and switch for the internal processing of |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 318 | // these types. This will break if the first six items in |
| 319 | // spBuiltInTagMap don't have values 0,1,2,3,4,5. That is the |
| 320 | // mapping is 0 to 0, 1 to 1, 2 to 2 and 3 to 3.... |
| 321 | #define QCBOR_TAGFLAG_DATE_STRING (0x01LL << CBOR_TAG_DATE_STRING) |
| 322 | #define QCBOR_TAGFLAG_DATE_EPOCH (0x01LL << CBOR_TAG_DATE_EPOCH) |
| 323 | #define QCBOR_TAGFLAG_POS_BIGNUM (0x01LL << CBOR_TAG_POS_BIGNUM) |
| 324 | #define QCBOR_TAGFLAG_NEG_BIGNUM (0x01LL << CBOR_TAG_NEG_BIGNUM) |
| 325 | #define QCBOR_TAGFLAG_DECIMAL_FRACTION (0x01LL << CBOR_TAG_DECIMAL_FRACTION) |
| 326 | #define QCBOR_TAGFLAG_BIGFLOAT (0x01LL << CBOR_TAG_BIGFLOAT) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 327 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 328 | #define TAG_MAPPER_FIRST_SIX (QCBOR_TAGFLAG_DATE_STRING |\ |
| 329 | QCBOR_TAGFLAG_DATE_EPOCH |\ |
| 330 | QCBOR_TAGFLAG_POS_BIGNUM |\ |
| 331 | QCBOR_TAGFLAG_NEG_BIGNUM |\ |
| 332 | QCBOR_TAGFLAG_DECIMAL_FRACTION |\ |
| 333 | QCBOR_TAGFLAG_BIGFLOAT) |
| 334 | |
| 335 | #define TAG_MAPPER_FIRST_FOUR (QCBOR_TAGFLAG_DATE_STRING |\ |
| 336 | QCBOR_TAGFLAG_DATE_EPOCH |\ |
| 337 | QCBOR_TAGFLAG_POS_BIGNUM |\ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 338 | QCBOR_TAGFLAG_NEG_BIGNUM) |
| 339 | |
| 340 | #define TAG_MAPPER_TOTAL_TAG_BITS 64 // Number of bits in a uint64_t |
| 341 | #define TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS) // 48 |
| 342 | #define TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS ) // 48 |
| 343 | |
| 344 | static inline int TagMapper_LookupBuiltIn(uint64_t uTag) |
| 345 | { |
| 346 | if(sizeof(spBuiltInTagMap)/sizeof(uint16_t) > TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 347 | /* |
| 348 | This is a cross-check to make sure the above array doesn't |
| 349 | accidentally get made too big. In normal conditions the above |
| 350 | test should optimize out as all the values are known at compile |
| 351 | time. |
| 352 | */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 353 | return -1; |
| 354 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 355 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 356 | if(uTag > UINT16_MAX) { |
| 357 | // This tag map works only on 16-bit tags |
| 358 | return -1; |
| 359 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 360 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 361 | for(int nTagBitIndex = 0; nTagBitIndex < (int)(sizeof(spBuiltInTagMap)/sizeof(uint16_t)); nTagBitIndex++) { |
| 362 | if(spBuiltInTagMap[nTagBitIndex] == uTag) { |
| 363 | return nTagBitIndex; |
| 364 | } |
| 365 | } |
| 366 | return -1; // Indicates no match |
| 367 | } |
| 368 | |
| 369 | static inline int TagMapper_LookupCallerConfigured(const QCBORTagListIn *pCallerConfiguredTagMap, uint64_t uTag) |
| 370 | { |
| 371 | for(int nTagBitIndex = 0; nTagBitIndex < pCallerConfiguredTagMap->uNumTags; nTagBitIndex++) { |
| 372 | if(pCallerConfiguredTagMap->puTags[nTagBitIndex] == uTag) { |
| 373 | return nTagBitIndex + TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX; |
| 374 | } |
| 375 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 376 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 377 | return -1; // Indicates no match |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | Find the tag bit index for a given tag value, or error out |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 382 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 383 | This and the above functions could probably be optimized and made |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 384 | clearer and neater. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 385 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 386 | static QCBORError |
| 387 | TagMapper_Lookup(const QCBORTagListIn *pCallerConfiguredTagMap, |
| 388 | uint64_t uTag, |
| 389 | uint8_t *puTagBitIndex) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 390 | { |
| 391 | int nTagBitIndex = TagMapper_LookupBuiltIn(uTag); |
| 392 | if(nTagBitIndex >= 0) { |
| 393 | // Cast is safe because TagMapper_LookupBuiltIn never returns > 47 |
| 394 | *puTagBitIndex = (uint8_t)nTagBitIndex; |
| 395 | return QCBOR_SUCCESS; |
| 396 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 397 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 398 | if(pCallerConfiguredTagMap) { |
| 399 | if(pCallerConfiguredTagMap->uNumTags > QCBOR_MAX_CUSTOM_TAGS) { |
| 400 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 401 | } |
| 402 | nTagBitIndex = TagMapper_LookupCallerConfigured(pCallerConfiguredTagMap, uTag); |
| 403 | if(nTagBitIndex >= 0) { |
| 404 | // Cast is safe because TagMapper_LookupBuiltIn never returns > 63 |
| 405 | |
| 406 | *puTagBitIndex = (uint8_t)nTagBitIndex; |
| 407 | return QCBOR_SUCCESS; |
| 408 | } |
| 409 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 410 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 411 | return QCBOR_ERR_BAD_OPT_TAG; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 416 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 417 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 418 | |
| 419 | The following four functions are pretty wrappers for invocation of |
| 420 | the string allocator supplied by the caller. |
| 421 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 422 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 423 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 424 | static inline void |
| 425 | StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 426 | { |
| 427 | (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0); |
| 428 | } |
| 429 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 430 | // StringAllocator_Reallocate called with pMem NULL is |
| 431 | // equal to StringAllocator_Allocate() |
| 432 | static inline UsefulBuf |
| 433 | StringAllocator_Reallocate(const QCORInternalAllocator *pMe, |
| 434 | void *pMem, |
| 435 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 436 | { |
| 437 | return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize); |
| 438 | } |
| 439 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 440 | static inline UsefulBuf |
| 441 | StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 442 | { |
| 443 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 444 | } |
| 445 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 446 | static inline void |
| 447 | StringAllocator_Destruct(const QCORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 448 | { |
| 449 | if(pMe->pfAllocator) { |
| 450 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | |
| 455 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 456 | /*=========================================================================== |
| 457 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 458 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 459 | See qcbor/qcbor_decode.h for definition of the object |
| 460 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 461 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 462 | /* |
| 463 | Public function, see header file |
| 464 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 465 | void QCBORDecode_Init(QCBORDecodeContext *me, |
| 466 | UsefulBufC EncodedCBOR, |
| 467 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 468 | { |
| 469 | memset(me, 0, sizeof(QCBORDecodeContext)); |
| 470 | UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 471 | // Don't bother with error check on decode mode. If a bad value is |
| 472 | // 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] | 473 | me->uDecodeMode = (uint8_t)nDecodeMode; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 474 | DecodeNesting_Init(&(me->nesting)); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /* |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 479 | Public function, see header file |
| 480 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 481 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 482 | QCBORStringAllocate pfAllocateFunction, |
| 483 | void *pAllocateContext, |
| 484 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 485 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 486 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 487 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 488 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | Public function, see header file |
| 494 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 495 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me, |
| 496 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 497 | { |
| 498 | me->pCallerConfiguredTagList = pTagList; |
| 499 | } |
| 500 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 501 | |
| 502 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 503 | This decodes the fundamental part of a CBOR data item, the type and |
| 504 | number |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 505 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 506 | This is the Counterpart to InsertEncodedTypeAndNumber(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 507 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 508 | This does the network->host byte order conversion. The conversion |
| 509 | here also results in the conversion for floats in addition to that |
| 510 | for lengths, tags and integer values. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 511 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 512 | This returns: |
| 513 | pnMajorType -- the major type for the item |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 514 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 515 | puArgument -- the "number" which is used a the value for integers, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 516 | tags and floats and length for strings and arrays |
| 517 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 518 | pnAdditionalInfo -- Pass this along to know what kind of float or |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 519 | if length is indefinite |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 520 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 521 | The int type is preferred to uint8_t for some variables as this |
| 522 | avoids integer promotions, can reduce code size and makes |
| 523 | static analyzers happier. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 524 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 525 | inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, |
| 526 | int *pnMajorType, |
| 527 | uint64_t *puArgument, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 528 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 529 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 530 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 531 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 532 | // Get the initial byte that every CBOR data item has |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 533 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 534 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 535 | // Break down the initial byte |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 536 | const int nTmpMajorType = nInitialByte >> 5; |
| 537 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 538 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 539 | // Where the number or argument accumulates |
| 540 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 541 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 542 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 543 | // Need to get 1,2,4 or 8 additional argument bytes Map |
| 544 | // LEN_IS_ONE_BYTE.. LEN_IS_EIGHT_BYTES to actual length |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 545 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 546 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 547 | // Loop getting all the bytes in the argument |
| 548 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 549 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 550 | // This shift and add gives the endian conversion |
| 551 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 552 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 553 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 554 | // The reserved and thus-far unused additional info values |
| 555 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 556 | goto Done; |
| 557 | } else { |
| 558 | // Less than 24, additional info is argument or 31, an indefinite length |
| 559 | // No more bytes to get |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 560 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 561 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 562 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 563 | if(UsefulInputBuf_GetError(pUInBuf)) { |
| 564 | nReturn = QCBOR_ERR_HIT_END; |
| 565 | goto Done; |
| 566 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 567 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 568 | // All successful if we got here. |
| 569 | nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 570 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 571 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 572 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 573 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 574 | Done: |
| 575 | return nReturn; |
| 576 | } |
| 577 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 578 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 579 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 580 | CBOR doesn't explicitly specify two's compliment for integers but all |
| 581 | CPUs use it these days and the test vectors in the RFC are so. All |
| 582 | integers in the CBOR structure are positive and the major type |
| 583 | indicates positive or negative. CBOR can express positive integers |
| 584 | up to 2^x - 1 where x is the number of bits and negative integers |
| 585 | down to 2^x. Note that negative numbers can be one more away from |
| 586 | zero than positive. Stdint, as far as I can tell, uses two's |
| 587 | compliment to represent negative integers. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 588 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 589 | See http://www.unix.org/whitepapers/64bit.html for reasons int isn't |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 590 | used carefully here, and in particular why it isn't used in the interface. |
| 591 | Also see |
| 592 | https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 593 | |
| 594 | Int is used for values that need less than 16-bits and would be subject |
| 595 | to integer promotion and complaining by static analyzers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 596 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 597 | inline static QCBORError |
| 598 | DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 599 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 600 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 601 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 602 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
| 603 | if (uNumber <= INT64_MAX) { |
| 604 | pDecodedItem->val.int64 = (int64_t)uNumber; |
| 605 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 606 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 607 | } else { |
| 608 | pDecodedItem->val.uint64 = uNumber; |
| 609 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 610 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 611 | } |
| 612 | } else { |
| 613 | if(uNumber <= INT64_MAX) { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 614 | // CBOR's representation of negative numbers lines up with the |
| 615 | // two-compliment representation. A negative integer has one |
| 616 | // more in range than a positive integer. INT64_MIN is |
| 617 | // equal to (-INT64_MAX) - 1. |
| 618 | pDecodedItem->val.int64 = (-(int64_t)uNumber) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 619 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 620 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 621 | } else { |
| 622 | // C can't represent a negative integer in this range |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 623 | // so it is an error. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 624 | nReturn = QCBOR_ERR_INT_OVERFLOW; |
| 625 | } |
| 626 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 627 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 628 | return nReturn; |
| 629 | } |
| 630 | |
| 631 | // Make sure #define value line up as DecodeSimple counts on this. |
| 632 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 633 | #error QCBOR_TYPE_FALSE macro value wrong |
| 634 | #endif |
| 635 | |
| 636 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 637 | #error QCBOR_TYPE_TRUE macro value wrong |
| 638 | #endif |
| 639 | |
| 640 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 641 | #error QCBOR_TYPE_NULL macro value wrong |
| 642 | #endif |
| 643 | |
| 644 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 645 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 646 | #endif |
| 647 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 648 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 649 | #error QCBOR_TYPE_BREAK macro value wrong |
| 650 | #endif |
| 651 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 652 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 653 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 654 | #endif |
| 655 | |
| 656 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 657 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 658 | #endif |
| 659 | |
| 660 | /* |
| 661 | Decode true, false, floats, break... |
| 662 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 663 | inline static QCBORError |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 664 | DecodeSimple(int nAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 665 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 666 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 667 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 668 | // uAdditionalInfo is 5 bits from the initial byte compile time checks |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 669 | // above make sure uAdditionalInfo values line up with uDataType values. |
| 670 | // DecodeTypeAndNumber never returns a major type > 1f so cast is safe |
| 671 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 672 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 673 | switch(nAdditionalInfo) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 674 | // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are |
| 675 | // caught before this is called. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 676 | |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 677 | case HALF_PREC_FLOAT: |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 678 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber); |
| 679 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 680 | break; |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 681 | case SINGLE_PREC_FLOAT: |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 682 | pDecodedItem->val.dfnum = (double)UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber); |
| 683 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 684 | break; |
| 685 | case DOUBLE_PREC_FLOAT: |
| 686 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 687 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 688 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 689 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 690 | case CBOR_SIMPLEV_FALSE: // 20 |
| 691 | case CBOR_SIMPLEV_TRUE: // 21 |
| 692 | case CBOR_SIMPLEV_NULL: // 22 |
| 693 | case CBOR_SIMPLEV_UNDEF: // 23 |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 694 | case CBOR_SIMPLE_BREAK: // 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 695 | break; // nothing to do |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 696 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 697 | case CBOR_SIMPLEV_ONEBYTE: // 24 |
| 698 | if(uNumber <= CBOR_SIMPLE_BREAK) { |
| 699 | // 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] | 700 | nReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 701 | goto Done; |
| 702 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 703 | /* FALLTHROUGH */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 704 | // fall through intentionally |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 705 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 706 | default: // 0-19 |
| 707 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 708 | /* |
| 709 | DecodeTypeAndNumber will make uNumber equal to |
| 710 | uAdditionalInfo when uAdditionalInfo is < 24 This cast is |
| 711 | safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 712 | the double/float cases above |
| 713 | */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 714 | pDecodedItem->val.uSimple = (uint8_t)uNumber; |
| 715 | break; |
| 716 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 717 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 718 | Done: |
| 719 | return nReturn; |
| 720 | } |
| 721 | |
| 722 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 723 | /* |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 724 | Decode text and byte strings. Call the string allocator if asked to. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 725 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 726 | inline static QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator, |
| 727 | int nMajorType, |
| 728 | uint64_t uStrLen, |
| 729 | UsefulInputBuf *pUInBuf, |
| 730 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 731 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 732 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 733 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 734 | // CBOR lengths can be 64 bits, but size_t is not 64 bits on all CPUs. |
| 735 | // This check makes the casts to size_t below safe. |
| 736 | |
| 737 | // 4 bytes less than the largest sizeof() so this can be tested by |
| 738 | // putting a SIZE_MAX length in the CBOR test input (no one will |
| 739 | // care the limit on strings is 4 bytes shorter). |
| 740 | if(uStrLen > SIZE_MAX-4) { |
| 741 | nReturn = QCBOR_ERR_STRING_TOO_LONG; |
| 742 | goto Done; |
| 743 | } |
| 744 | |
| 745 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 746 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 747 | // Failed to get the bytes for this string item |
| 748 | nReturn = QCBOR_ERR_HIT_END; |
| 749 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 750 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 751 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 752 | if(pAllocator) { |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 753 | // We are asked to use string allocator to make a copy |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 754 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 755 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 756 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 757 | goto Done; |
| 758 | } |
| 759 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 760 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 761 | } else { |
| 762 | // Normal case with no string allocator |
| 763 | pDecodedItem->val.string = Bytes; |
| 764 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 765 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 766 | // Cast because ternary operator causes promotion to integer |
| 767 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 768 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 769 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 770 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 771 | return nReturn; |
| 772 | } |
| 773 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 774 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 775 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 776 | |
| 777 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 778 | |
| 779 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 780 | // Make sure the constants align as this is assumed by |
| 781 | // the GetAnItem() implementation |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 782 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 783 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 784 | #endif |
| 785 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 786 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 787 | #endif |
| 788 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 789 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 790 | This gets a single data item and decodes it including preceding |
| 791 | optional tagging. This does not deal with arrays and maps and nesting |
| 792 | except to decode the data item introducing them. Arrays and maps are |
| 793 | handled at the next level up in GetNext(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 794 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 795 | Errors detected here include: an array that is too long to decode, |
| 796 | hit end of buffer unexpectedly, a few forms of invalid encoded CBOR |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 797 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 798 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, |
| 799 | QCBORItem *pDecodedItem, |
| 800 | const QCORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 801 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 802 | QCBORError nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 803 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 804 | /* |
| 805 | Get the major type and the number. Number could be length of more |
| 806 | bytes or the value depending on the major type nAdditionalInfo is |
| 807 | an encoding of the length of the uNumber and is needed to decode |
| 808 | floats and doubles |
| 809 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 810 | int nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 811 | uint64_t uNumber; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 812 | int nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 813 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 814 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 815 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 816 | nReturn = DecodeTypeAndNumber(pUInBuf, &nMajorType, &uNumber, &nAdditionalInfo); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 817 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 818 | // Error out here if we got into trouble on the type and number. The |
| 819 | // 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] | 820 | if(nReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 821 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 822 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 823 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 824 | // At this point the major type and the value are valid. We've got |
| 825 | // the type and the number that starts every CBOR data item. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 826 | switch (nMajorType) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 827 | case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0 |
| 828 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 829 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 830 | nReturn = QCBOR_ERR_BAD_INT; |
| 831 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 832 | nReturn = DecodeInteger(nMajorType, uNumber, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 833 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 834 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 835 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 836 | case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2 |
| 837 | case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 838 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 839 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
| 840 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 841 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 842 | pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 843 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 844 | nReturn = DecodeBytes(pAllocator, nMajorType, uNumber, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 845 | } |
| 846 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 847 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 848 | case CBOR_MAJOR_TYPE_ARRAY: // Major type 4 |
| 849 | case CBOR_MAJOR_TYPE_MAP: // Major type 5 |
| 850 | // Record the number of items in the array or map |
| 851 | if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 852 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 853 | goto Done; |
| 854 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 855 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 856 | pDecodedItem->val.uCount = UINT16_MAX; // Indicate indefinite length |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 857 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 858 | // type conversion OK because of check above |
| 859 | pDecodedItem->val.uCount = (uint16_t)uNumber; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 860 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 861 | // C preproc #if above makes sure constants for major types align |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 862 | // DecodeTypeAndNumber never returns a major type > 7 so cast is safe |
| 863 | pDecodedItem->uDataType = (uint8_t)nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 864 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 865 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 866 | case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 867 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 868 | nReturn = QCBOR_ERR_BAD_INT; |
| 869 | } else { |
| 870 | pDecodedItem->val.uTagV = uNumber; |
| 871 | pDecodedItem->uDataType = QCBOR_TYPE_OPTTAG; |
| 872 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 873 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 874 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 875 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 876 | // Major type 7, float, double, true, false, null... |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 877 | nReturn = DecodeSimple(nAdditionalInfo, uNumber, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 878 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 879 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 880 | default: |
| 881 | // Never happens because DecodeTypeAndNumber() should never return > 7 |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 882 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 883 | break; |
| 884 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 885 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 886 | Done: |
| 887 | return nReturn; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | |
| 892 | /* |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 893 | This layer deals with indefinite length strings. It pulls all the |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 894 | individual chunk items together into one QCBORItem using the string |
| 895 | allocator. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 896 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 897 | Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 898 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 899 | static inline QCBORError |
| 900 | GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 901 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 902 | // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96 |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 903 | |
| 904 | // Get pointer to string allocator. First use is to pass it to |
| 905 | // GetNext_Item() when option is set to allocate for *every* string. |
| 906 | // Second use here is to allocate space to coallese indefinite |
| 907 | // length string items into one. |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 908 | const QCORInternalAllocator *pAllocator = me->StringAllocator.pfAllocator ? |
| 909 | &(me->StringAllocator) : |
| 910 | NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 911 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 912 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 913 | nReturn = GetNext_Item(&(me->InBuf), |
| 914 | pDecodedItem, |
| 915 | me->bStringAllocateAll ? pAllocator: NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 916 | if(nReturn) { |
| 917 | goto Done; |
| 918 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 919 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 920 | // To reduce code size by removing support for indefinite length strings, the |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 921 | // code in this function from here down can be eliminated. Run tests, except |
| 922 | // 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] | 923 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 924 | // Only do indefinite length processing on strings |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 925 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 926 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 927 | goto Done; // no need to do any work here on non-string types |
| 928 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 929 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 930 | // Is this a string with an indefinite length? |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 931 | if(pDecodedItem->val.string.len != SIZE_MAX) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 932 | goto Done; // length is not indefinite, so no work to do here |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 933 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 934 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 935 | // Can't do indefinite length strings without a string allocator |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 936 | if(pAllocator == NULL) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 937 | nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 938 | goto Done; |
| 939 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 940 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 941 | // Loop getting chunk of indefinite string |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 942 | UsefulBufC FullString = NULLUsefulBufC; |
| 943 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 944 | for(;;) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 945 | // Get item for next chunk |
| 946 | QCBORItem StringChunkItem; |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 947 | // NULL string allocator passed here. Do not need to allocate |
| 948 | // chunks even if bStringAllocateAll is set. |
Laurence Lundblade | fae26bf | 2019-02-18 11:15:43 -0800 | [diff] [blame] | 949 | nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 950 | if(nReturn) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 951 | break; // Error getting the next chunk |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 952 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 953 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 954 | // See if it is a marker at end of indefinite length string |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 955 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 956 | // String is complete |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 957 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 958 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 959 | break; |
| 960 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 961 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 962 | // Match data type of chunk to type at beginning. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 963 | // Also catches error of other non-string types that don't belong. |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 964 | // Also catches indefinite length strings inside indefinite length strings |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 965 | if(StringChunkItem.uDataType != uStringType || |
| 966 | StringChunkItem.val.string.len == SIZE_MAX) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 967 | nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 968 | break; |
| 969 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 970 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 971 | // Alloc new buffer or expand previously allocated buffer so it can fit |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 972 | // The first time throurgh FullString.ptr is NULL and this is |
| 973 | // equivalent to StringAllocator_Allocate() |
| 974 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 975 | UNCONST_POINTER(FullString.ptr), |
| 976 | FullString.len + StringChunkItem.val.string.len); |
| 977 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 978 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 979 | // Allocation of memory for the string failed |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 980 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 981 | break; |
| 982 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 983 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 984 | // Copy new string chunk at the end of string so far. |
| 985 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 986 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 987 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 988 | if(nReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 989 | // Getting the item failed, clean up the allocated memory |
| 990 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 991 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 992 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 993 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 994 | return nReturn; |
| 995 | } |
| 996 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 997 | |
| 998 | /* |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 999 | Gets all optional tag data items preceding a data item that is not an |
| 1000 | optional tag and records them as bits in the tag map. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1001 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1002 | static QCBORError |
| 1003 | GetNext_TaggedItem(QCBORDecodeContext *me, |
| 1004 | QCBORItem *pDecodedItem, |
| 1005 | QCBORTagListOut *pTags) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1006 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1007 | // Stack usage: int/ptr: 3 -- 24 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1008 | QCBORError nReturn; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1009 | uint64_t uTagBits = 0; |
| 1010 | if(pTags) { |
| 1011 | pTags->uNumUsed = 0; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1012 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1013 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1014 | // Loop fetching items until the item fetched is not a tag |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1015 | for(;;) { |
| 1016 | nReturn = GetNext_FullItem(me, pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1017 | if(nReturn) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1018 | goto Done; // Error out of the loop |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1019 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1020 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1021 | if(pDecodedItem->uDataType != QCBOR_TYPE_OPTTAG) { |
| 1022 | // Successful exit from loop; maybe got some tags, maybe not |
| 1023 | pDecodedItem->uTagBits = uTagBits; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1024 | break; |
| 1025 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1026 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1027 | uint8_t uTagBitIndex; |
| 1028 | // Tag was mapped, tag was not mapped, error with tag list |
| 1029 | switch(TagMapper_Lookup(me->pCallerConfiguredTagList, pDecodedItem->val.uTagV, &uTagBitIndex)) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1030 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1031 | case QCBOR_SUCCESS: |
| 1032 | // Successfully mapped the tag |
| 1033 | uTagBits |= 0x01ULL << uTagBitIndex; |
| 1034 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1035 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1036 | case QCBOR_ERR_BAD_OPT_TAG: |
| 1037 | // Tag is not recognized. Do nothing |
| 1038 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1039 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1040 | default: |
| 1041 | // Error Condition |
| 1042 | goto Done; |
| 1043 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1044 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1045 | if(pTags) { |
| 1046 | // Caller wants all tags recorded in the provided buffer |
| 1047 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 1048 | nReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 1049 | goto Done; |
| 1050 | } |
| 1051 | pTags->puTags[pTags->uNumUsed] = pDecodedItem->val.uTagV; |
| 1052 | pTags->uNumUsed++; |
| 1053 | } |
| 1054 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1055 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1056 | Done: |
| 1057 | return nReturn; |
| 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1062 | This layer takes care of map entries. It combines the label and data |
| 1063 | items into one QCBORItem. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1064 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1065 | static inline QCBORError |
| 1066 | GetNext_MapEntry(QCBORDecodeContext *me, |
| 1067 | QCBORItem *pDecodedItem, |
| 1068 | QCBORTagListOut *pTags) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1069 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1070 | // Stack use: int/ptr 1, QCBORItem -- 56 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1071 | QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1072 | if(nReturn) |
| 1073 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1074 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1075 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1076 | // Break can't be a map entry |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1077 | goto Done; |
| 1078 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1079 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1080 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1081 | // In a map and caller wants maps decoded, not treated as arrays |
| 1082 | |
| 1083 | if(DecodeNesting_TypeIsMap(&(me->nesting))) { |
| 1084 | // If in a map and the right decoding mode, get the label |
| 1085 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1086 | // Save label in pDecodedItem and get the next which will |
| 1087 | // be the real data |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1088 | QCBORItem LabelItem = *pDecodedItem; |
| 1089 | nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags); |
| 1090 | if(nReturn) |
| 1091 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1092 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1093 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1094 | |
| 1095 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 1096 | // strings are always good labels |
| 1097 | pDecodedItem->label.string = LabelItem.val.string; |
| 1098 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 1099 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1100 | // It's not a string and we only want strings |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1101 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1102 | goto Done; |
| 1103 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1104 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1105 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1106 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1107 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1108 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1109 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1110 | pDecodedItem->label.string = LabelItem.val.string; |
| 1111 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1112 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1113 | } else { |
| 1114 | // label is not an int or a string. It is an arrray |
| 1115 | // or a float or such and this implementation doesn't handle that. |
| 1116 | // Also, tags on labels are ignored. |
| 1117 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1118 | goto Done; |
| 1119 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } else { |
| 1122 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1123 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
| 1124 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 1125 | goto Done; |
| 1126 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1127 | // Decoding a map as an array |
| 1128 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1129 | // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2 |
| 1130 | // Cast is needed because of integer promotion |
| 1131 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1134 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1135 | Done: |
| 1136 | return nReturn; |
| 1137 | } |
| 1138 | |
| 1139 | |
| 1140 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1141 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1142 | TODO: correct this comment |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1143 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1144 | QCBORError QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, |
| 1145 | QCBORItem *pDecodedItem, |
| 1146 | QCBORTagListOut *pTags) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1147 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1148 | // Stack ptr/int: 2, QCBORItem : 64 |
| 1149 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1150 | QCBORError nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1151 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1152 | // Check if there are an TODO: incomplete comment |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1153 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0 && DecodeNesting_IsAtTop(&(me->nesting))) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1154 | nReturn = QCBOR_ERR_NO_MORE_ITEMS; |
| 1155 | goto Done; |
| 1156 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1157 | |
| 1158 | // This is to handle map and array mode |
| 1159 | if(UsefulInputBuf_Tell(&(me->InBuf)) != 0 && DecodeNesting_AtEnd(&(me->nesting))) { |
| 1160 | nReturn = QCBOR_ERR_NO_MORE_ITEMS; |
| 1161 | goto Done; |
| 1162 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1163 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1164 | nReturn = GetNext_MapEntry(me, pDecodedItem, pTags); |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1165 | if(nReturn) { |
| 1166 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1167 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1168 | |
| 1169 | // Break ending arrays/maps are always processed at the end of this function. |
| 1170 | // They should never show up here. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1171 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1172 | nReturn = QCBOR_ERR_BAD_BREAK; |
| 1173 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1174 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1175 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1176 | // Record the nesting level for this data item before processing any of |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1177 | // decrementing and descending. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1178 | pDecodedItem->uNestingLevel = DecodeNesting_GetLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1179 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1180 | // Process the item just received for descent or decrement, and |
| 1181 | // ascent if decrements are enough to close out a definite length array/map |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 1182 | if(IsMapOrArray(pDecodedItem->uDataType)) { |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1183 | // If the new item is array or map, the nesting level descends |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 1184 | nReturn = DecodeNesting_Descend(&(me->nesting), pDecodedItem); |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1185 | // Maps and arrays do count in as items in the map/array that encloses |
| 1186 | // them so a decrement needs to be done for them too, but that is done |
| 1187 | // only when all the items in them have been processed, not when they |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 1188 | // are opened with the exception of an empty map or array. |
| 1189 | if(pDecodedItem->val.uCount == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1190 | DecodeNesting_DecrementCount(&(me->nesting), false); |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 1191 | } |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1192 | } else { |
| 1193 | // Decrement the count of items in the enclosing map/array |
| 1194 | // If the count in the enclosing map/array goes to zero, that |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1195 | // triggers a decrement in the map/array above that and |
| 1196 | // an ascend in nesting level. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1197 | DecodeNesting_DecrementCount(&(me->nesting), false); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1198 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1199 | if(nReturn) { |
| 1200 | goto Done; |
| 1201 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1202 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1203 | // For indefinite length maps/arrays, looking at any and |
| 1204 | // all breaks that might terminate them. The equivalent |
| 1205 | // for definite length maps/arrays happens in |
| 1206 | // DecodeNesting_DecrementCount(). |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1207 | if(!DecodeNesting_IsAtTop(&(me->nesting)) && DecodeNesting_IsIndefiniteLength(&(me->nesting))) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1208 | while(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
| 1209 | // Peek forward one item to see if it is a break. |
| 1210 | QCBORItem Peek; |
| 1211 | size_t uPeek = UsefulInputBuf_Tell(&(me->InBuf)); |
| 1212 | nReturn = GetNext_Item(&(me->InBuf), &Peek, NULL); |
| 1213 | if(nReturn) { |
| 1214 | goto Done; |
| 1215 | } |
| 1216 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
| 1217 | // It is not a break, rewind so it can be processed normally. |
| 1218 | UsefulInputBuf_Seek(&(me->InBuf), uPeek); |
| 1219 | break; |
| 1220 | } |
| 1221 | // It is a break. Ascend one nesting level. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1222 | // The break is consumed. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1223 | nReturn = DecodeNesting_BreakAscend(&(me->nesting)); |
| 1224 | if(nReturn) { |
| 1225 | // break occured outside of an indefinite length array/map |
| 1226 | goto Done; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1230 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1231 | // Tell the caller what level is next. This tells them what maps/arrays |
| 1232 | // were closed out and makes it possible for them to reconstruct |
| 1233 | // the tree with just the information returned by GetNext |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1234 | if(me->nesting.pCurrent->uMapMode && me->nesting.pCurrent->uCount == 0) { |
| 1235 | // At end of a map / array in map mode, so next nest is 0 to |
| 1236 | // indicate this end. |
| 1237 | pDecodedItem->uNextNestLevel = 0; |
| 1238 | } else { |
| 1239 | pDecodedItem->uNextNestLevel = DecodeNesting_GetLevel(&(me->nesting)); |
| 1240 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1241 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1242 | Done: |
Laurence Lundblade | e9482dd | 2019-10-11 12:58:46 -0700 | [diff] [blame] | 1243 | if(nReturn != QCBOR_SUCCESS) { |
| 1244 | // Make sure uDataType and uLabelType are QCBOR_TYPE_NONE |
| 1245 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1246 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1247 | return nReturn; |
| 1248 | } |
| 1249 | |
| 1250 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1251 | /* |
| 1252 | Mostly just assign the right data type for the date string. |
| 1253 | */ |
| 1254 | inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem) |
| 1255 | { |
| 1256 | // Stack Use: UsefulBuf 1 16 |
| 1257 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1258 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1259 | } |
| 1260 | |
| 1261 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1262 | pDecodedItem->val.dateString = Temp; |
| 1263 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING; |
| 1264 | return QCBOR_SUCCESS; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | /* |
| 1269 | Mostly just assign the right data type for the bignum. |
| 1270 | */ |
| 1271 | inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem) |
| 1272 | { |
| 1273 | // Stack Use: UsefulBuf 1 -- 16 |
| 1274 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1275 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1276 | } |
| 1277 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1278 | pDecodedItem->val.bigNum = Temp; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1279 | const bool bIsPosBigNum = (bool)(pDecodedItem->uTagBits & QCBOR_TAGFLAG_POS_BIGNUM); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1280 | pDecodedItem->uDataType = (uint8_t)(bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM |
| 1281 | : QCBOR_TYPE_NEGBIGNUM); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1282 | return QCBOR_SUCCESS; |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1287 | The epoch formatted date. Turns lots of different forms of encoding |
| 1288 | date into uniform one |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1289 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1290 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1291 | { |
| 1292 | // Stack usage: 1 |
| 1293 | QCBORError nReturn = QCBOR_SUCCESS; |
| 1294 | |
| 1295 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1296 | |
| 1297 | switch (pDecodedItem->uDataType) { |
| 1298 | |
| 1299 | case QCBOR_TYPE_INT64: |
| 1300 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1301 | break; |
| 1302 | |
| 1303 | case QCBOR_TYPE_UINT64: |
| 1304 | if(pDecodedItem->val.uint64 > INT64_MAX) { |
| 1305 | nReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1306 | goto Done; |
| 1307 | } |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1308 | pDecodedItem->val.epochDate.nSeconds = (int64_t)pDecodedItem->val.uint64; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1309 | break; |
| 1310 | |
| 1311 | case QCBOR_TYPE_DOUBLE: |
| 1312 | { |
| 1313 | // This comparison needs to be done as a float before |
| 1314 | // conversion to an int64_t to be able to detect doubles |
| 1315 | // that are too large to fit into an int64_t. A double |
| 1316 | // has 52 bits of preceision. An int64_t has 63. Casting |
| 1317 | // INT64_MAX to a double actually causes a round up which |
| 1318 | // is bad and wrong for the comparison because it will |
| 1319 | // allow conversion of doubles that can't fit into a |
| 1320 | // uint64_t. To remedy this INT64_MAX - 0x7ff is used as |
| 1321 | // the cutoff point as if that rounds up in conversion to |
| 1322 | // double it will still be less than INT64_MAX. 0x7ff is |
| 1323 | // picked because it has 11 bits set. |
| 1324 | // |
| 1325 | // INT64_MAX seconds is on the order of 10 billion years, |
| 1326 | // and the earth is less than 5 billion years old, so for |
| 1327 | // most uses this conversion error won't occur even though |
| 1328 | // doubles can go much larger. |
| 1329 | // |
| 1330 | // Without the 0x7ff there is a ~30 minute range of time |
| 1331 | // values 10 billion years in the past and in the future |
| 1332 | // where this this code would go wrong. |
| 1333 | const double d = pDecodedItem->val.dfnum; |
| 1334 | if(d > (double)(INT64_MAX - 0x7ff)) { |
| 1335 | nReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1336 | goto Done; |
| 1337 | } |
| 1338 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
| 1339 | pDecodedItem->val.epochDate.fSecondsFraction = d - (double)pDecodedItem->val.epochDate.nSeconds; |
| 1340 | } |
| 1341 | break; |
| 1342 | |
| 1343 | default: |
| 1344 | nReturn = QCBOR_ERR_BAD_OPT_TAG; |
| 1345 | goto Done; |
| 1346 | } |
| 1347 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1348 | |
| 1349 | Done: |
| 1350 | return nReturn; |
| 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1355 | /* |
| 1356 | Decode decimal fractions and big floats. |
| 1357 | |
| 1358 | When called pDecodedItem must be the array that is tagged as a big |
| 1359 | float or decimal fraction, the array that has the two members, the |
| 1360 | exponent and mantissa. |
| 1361 | |
| 1362 | This will fetch and decode the exponent and mantissa and put the |
| 1363 | result back into pDecodedItem. |
| 1364 | */ |
| 1365 | inline static QCBORError |
| 1366 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
| 1367 | { |
| 1368 | QCBORError nReturn; |
| 1369 | |
| 1370 | // --- Make sure it is an array; track nesting level of members --- |
| 1371 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 1372 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1373 | goto Done; |
| 1374 | } |
| 1375 | |
| 1376 | // A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1377 | // definite length arrays, but not for indefnite. Instead remember |
| 1378 | // the nesting level the two integers must be at, which is one |
| 1379 | // deeper than that of the array. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1380 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 1381 | |
| 1382 | // --- Is it a decimal fraction or a bigfloat? --- |
| 1383 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
| 1384 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 1385 | |
| 1386 | // --- Get the exponent --- |
| 1387 | QCBORItem exponentItem; |
| 1388 | nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem, NULL); |
| 1389 | if(nReturn != QCBOR_SUCCESS) { |
| 1390 | goto Done; |
| 1391 | } |
| 1392 | if(exponentItem.uNestingLevel != nNestLevel) { |
| 1393 | // Array is empty or a map/array encountered when expecting an int |
| 1394 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1395 | goto Done; |
| 1396 | } |
| 1397 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
| 1398 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1399 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1400 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1401 | // will be too large for this to handle and thus an error that will |
| 1402 | // get handled in the next else. |
| 1403 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 1404 | } else { |
| 1405 | // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1406 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1407 | goto Done; |
| 1408 | } |
| 1409 | |
| 1410 | // --- Get the mantissa --- |
| 1411 | QCBORItem mantissaItem; |
| 1412 | nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL); |
| 1413 | if(nReturn != QCBOR_SUCCESS) { |
| 1414 | goto Done; |
| 1415 | } |
| 1416 | if(mantissaItem.uNestingLevel != nNestLevel) { |
| 1417 | // Mantissa missing or map/array encountered when expecting number |
| 1418 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1419 | goto Done; |
| 1420 | } |
| 1421 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
| 1422 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1423 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1424 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1425 | // will be too large for this to handle and thus an error that |
| 1426 | // will get handled in an else below. |
| 1427 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
| 1428 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 1429 | // Got a good big num mantissa |
| 1430 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
| 1431 | // Depends on numbering of QCBOR_TYPE_XXX |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1432 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 1433 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 1434 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1435 | } else { |
| 1436 | // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1437 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1438 | goto Done; |
| 1439 | } |
| 1440 | |
| 1441 | // --- Check that array only has the two numbers --- |
| 1442 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
| 1443 | // Extra items in the decimal fraction / big num |
| 1444 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1445 | goto Done; |
| 1446 | } |
| 1447 | |
| 1448 | Done: |
| 1449 | |
| 1450 | return nReturn; |
| 1451 | } |
| 1452 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1453 | |
| 1454 | |
| 1455 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1456 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1457 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1458 | QCBORError |
| 1459 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, |
| 1460 | QCBORItem *pDecodedItem, |
| 1461 | QCBORTagListOut *pTags) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1462 | { |
| 1463 | QCBORError nReturn; |
| 1464 | |
| 1465 | nReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem, pTags); |
| 1466 | if(nReturn != QCBOR_SUCCESS) { |
| 1467 | goto Done; |
| 1468 | } |
| 1469 | |
| 1470 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1471 | #define TAG_MAPPER_FIRST_XXX TAG_MAPPER_FIRST_SIX |
| 1472 | #else |
| 1473 | #define TAG_MAPPER_FIRST_XXX TAG_MAPPER_FIRST_FOUR |
| 1474 | #endif |
| 1475 | |
| 1476 | // Only pay attention to tags this code knows how to decode. |
| 1477 | switch(pDecodedItem->uTagBits & TAG_MAPPER_FIRST_XXX) { |
| 1478 | case 0: |
| 1479 | // No tags at all or none we know about. Nothing to do. |
| 1480 | // This is the pass-through path of this function |
| 1481 | // that will mostly be taken when decoding any item. |
| 1482 | break; |
| 1483 | |
| 1484 | case QCBOR_TAGFLAG_DATE_STRING: |
| 1485 | nReturn = DecodeDateString(pDecodedItem); |
| 1486 | break; |
| 1487 | |
| 1488 | case QCBOR_TAGFLAG_DATE_EPOCH: |
| 1489 | nReturn = DecodeDateEpoch(pDecodedItem); |
| 1490 | break; |
| 1491 | |
| 1492 | case QCBOR_TAGFLAG_POS_BIGNUM: |
| 1493 | case QCBOR_TAGFLAG_NEG_BIGNUM: |
| 1494 | nReturn = DecodeBigNum(pDecodedItem); |
| 1495 | break; |
| 1496 | |
| 1497 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1498 | case QCBOR_TAGFLAG_DECIMAL_FRACTION: |
| 1499 | case QCBOR_TAGFLAG_BIGFLOAT: |
| 1500 | // For aggregate tagged types, what goes into pTags is only collected |
| 1501 | // from the surrounding data item, not the contents, so pTags is not |
| 1502 | // passed on here. |
| 1503 | |
| 1504 | nReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem); |
| 1505 | break; |
| 1506 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1507 | |
| 1508 | default: |
| 1509 | // Encountering some mixed-up CBOR like something that |
| 1510 | // is tagged as both a string and integer date. |
| 1511 | nReturn = QCBOR_ERR_BAD_OPT_TAG; |
| 1512 | } |
| 1513 | |
| 1514 | Done: |
| 1515 | if(nReturn != QCBOR_SUCCESS) { |
| 1516 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 1517 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 1518 | } |
| 1519 | return nReturn; |
| 1520 | } |
| 1521 | |
| 1522 | |
| 1523 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1524 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1525 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1526 | QCBORError QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1527 | { |
| 1528 | return QCBORDecode_GetNextWithTags(me, pDecodedItem, NULL); |
| 1529 | } |
| 1530 | |
| 1531 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1532 | /* |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1533 | Decoding items is done in 5 layered functions, one calling the |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1534 | next one down. If a layer has no work to do for a particular item |
| 1535 | it returns quickly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1536 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1537 | - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes |
| 1538 | tagged data items, turning them into the local C representation. |
| 1539 | For the most simple it is just associating a QCBOR_TYPE with the data. For |
| 1540 | the complex ones that an aggregate of data items, there is some further |
| 1541 | decoding and a little bit of recursion. |
| 1542 | |
| 1543 | - QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1544 | ends of maps and arrays. It tracks descending into and ascending |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1545 | out of maps/arrays. It processes all breaks that terminate |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1546 | indefinite length maps and arrays. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1547 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1548 | - GetNext_MapEntry -- This handles the combining of two |
| 1549 | items, the label and the data, that make up a map entry. |
| 1550 | It only does work on maps. It combines the label and data |
| 1551 | items into one labeled item. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1552 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1553 | - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the |
| 1554 | tags into bit flags associated with the data item. No actual decoding |
| 1555 | of the contents of the tagged item is performed here. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1556 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1557 | - GetNext_FullItem -- This assembles the sub-items that make up |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1558 | an indefinte length string into one string item. It uses the |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1559 | string allocater to create contiguous space for the item. It |
| 1560 | processes all breaks that are part of indefinite length strings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1561 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1562 | - GetNext_Item -- This decodes the atomic data items in CBOR. Each |
| 1563 | atomic data item has a "major type", an integer "argument" and optionally |
| 1564 | some content. For text and byte strings, the content is the bytes |
| 1565 | that make up the string. These are the smallest data items that are |
| 1566 | considered to be well-formed. The content may also be other data items in |
| 1567 | the case of aggregate types. They are not handled in this layer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1568 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1569 | Roughly this takes 300 bytes of stack for vars. Need to |
| 1570 | evaluate this more carefully and correctly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1571 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1572 | */ |
| 1573 | |
| 1574 | |
| 1575 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1576 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1577 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1578 | int QCBORDecode_IsTagged(QCBORDecodeContext *me, |
| 1579 | const QCBORItem *pItem, |
| 1580 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1581 | { |
| 1582 | const QCBORTagListIn *pCallerConfiguredTagMap = me->pCallerConfiguredTagList; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1583 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1584 | uint8_t uTagBitIndex; |
| 1585 | // Do not care about errors in pCallerConfiguredTagMap here. They are |
| 1586 | // caught during GetNext() before this is called. |
| 1587 | if(TagMapper_Lookup(pCallerConfiguredTagMap, uTag, &uTagBitIndex)) { |
| 1588 | return 0; |
| 1589 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1590 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1591 | const uint64_t uTagBit = 0x01ULL << uTagBitIndex; |
| 1592 | return (uTagBit & pItem->uTagBits) != 0; |
| 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1597 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1598 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1599 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1600 | { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1601 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1602 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1603 | // Error out if all the maps/arrays are not closed out |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1604 | if(!DecodeNesting_IsAtTop(&(me->nesting))) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1605 | nReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN; |
| 1606 | goto Done; |
| 1607 | } |
| 1608 | |
| 1609 | // Error out if not all the bytes are consumed |
| 1610 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
| 1611 | nReturn = QCBOR_ERR_EXTRA_BYTES; |
| 1612 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1613 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1614 | Done: |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1615 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1616 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1617 | StringAllocator_Destruct(&(me->StringAllocator)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1618 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1619 | return nReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | |
| 1623 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1624 | /* |
| 1625 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1626 | Decoder errors handled in this file |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1627 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1628 | - Hit end of input before it was expected while decoding type and |
| 1629 | number QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1630 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1631 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1632 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1633 | - Hit end of input while decoding a text or byte string |
| 1634 | QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1635 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1636 | - Encountered conflicting tags -- e.g., an item is tagged both a date |
| 1637 | string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1638 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1639 | - Encontered an array or mapp that has too many items |
| 1640 | QCBOR_ERR_ARRAY_TOO_LONG |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1641 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1642 | - Encountered array/map nesting that is too deep |
| 1643 | QCBOR_ERR_ARRAY_NESTING_TOO_DEEP |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1644 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1645 | - An epoch date > INT64_MAX or < INT64_MIN was encountered |
| 1646 | QCBOR_ERR_DATE_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1647 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1648 | - The type of a map label is not a string or int |
| 1649 | QCBOR_ERR_MAP_LABEL_TYPE |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1650 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1651 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1652 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1653 | */ |
| 1654 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1655 | |
| 1656 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1657 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1658 | /* =========================================================================== |
| 1659 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1660 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1661 | This implements a simple sting allocator for indefinite length |
| 1662 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 1663 | implements the function type QCBORStringAllocate and allows easy |
| 1664 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1665 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1666 | This particular allocator is built-in for convenience. The caller |
| 1667 | can implement their own. All of this following code will get |
| 1668 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 1669 | |
| 1670 | This is a very primitive memory allocator. It does not track |
| 1671 | individual allocations, only a high-water mark. A free or |
| 1672 | reallocation must be of the last chunk allocated. |
| 1673 | |
| 1674 | The size of the pool and offset to free memory are packed into the |
| 1675 | first 8 bytes of the memory pool so we don't have to keep them in |
| 1676 | the decode context. Since the address of the pool may not be |
| 1677 | aligned, they have to be packed and unpacked as if they were |
| 1678 | serialized data of the wire or such. |
| 1679 | |
| 1680 | The sizes packed in are uint32_t to be the same on all CPU types |
| 1681 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1682 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1683 | |
| 1684 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1685 | static inline int |
| 1686 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1687 | { |
| 1688 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 1689 | UsefulInputBuf UIB; |
| 1690 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1691 | // Just assume the size here. It was checked during SetUp so |
| 1692 | // the assumption is safe. |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1693 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem, QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
| 1694 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 1695 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 1696 | return UsefulInputBuf_GetError(&UIB); |
| 1697 | } |
| 1698 | |
| 1699 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1700 | static inline int |
| 1701 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1702 | { |
| 1703 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 1704 | // length check performed here is useful. |
| 1705 | UsefulOutBuf UOB; |
| 1706 | |
| 1707 | UsefulOutBuf_Init(&UOB, Pool); |
| 1708 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 1709 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 1710 | return UsefulOutBuf_GetError(&UOB); |
| 1711 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1712 | |
| 1713 | |
| 1714 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1715 | Internal function for an allocation, reallocation free and destuct. |
| 1716 | |
| 1717 | Having only one function rather than one each per mode saves space in |
| 1718 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1719 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1720 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 1721 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1722 | static UsefulBuf |
| 1723 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1724 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1725 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1726 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1727 | uint32_t uPoolSize; |
| 1728 | uint32_t uFreeOffset; |
| 1729 | |
| 1730 | if(uNewSize > UINT32_MAX) { |
| 1731 | // This allocator is only good up to 4GB. This check should |
| 1732 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 1733 | goto Done; |
| 1734 | } |
| 1735 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 1736 | |
| 1737 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 1738 | goto Done; |
| 1739 | } |
| 1740 | |
| 1741 | if(uNewSize) { |
| 1742 | if(pMem) { |
| 1743 | // REALLOCATION MODE |
| 1744 | // Calculate pointer to the end of the memory pool. It is |
| 1745 | // assumed that pPool + uPoolSize won't wrap around by |
| 1746 | // assuming the caller won't pass a pool buffer in that is |
| 1747 | // not in legitimate memory space. |
| 1748 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 1749 | |
| 1750 | // Check that the pointer for reallocation is in the range of the |
| 1751 | // pool. This also makes sure that pointer math further down |
| 1752 | // doesn't wrap under or over. |
| 1753 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 1754 | // Offset to start of chunk for reallocation. This won't |
| 1755 | // wrap under because of check that pMem >= pPool. Cast |
| 1756 | // is safe because the pool is always less than UINT32_MAX |
| 1757 | // because of check in QCBORDecode_SetMemPool(). |
| 1758 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 1759 | |
| 1760 | // Check to see if the allocation will fit. uPoolSize - |
| 1761 | // uMemOffset will not wrap under because of check that |
| 1762 | // pMem is in the range of the uPoolSize by check above. |
| 1763 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 1764 | ReturnValue.ptr = pMem; |
| 1765 | ReturnValue.len = uNewSize; |
| 1766 | |
| 1767 | // Addition won't wrap around over because uNewSize was |
| 1768 | // checked to be sure it is less than the pool size. |
| 1769 | uFreeOffset = uMemOffset + uNewSize32; |
| 1770 | } |
| 1771 | } |
| 1772 | } else { |
| 1773 | // ALLOCATION MODE |
| 1774 | // uPoolSize - uFreeOffset will not underflow because this |
| 1775 | // pool implementation makes sure uFreeOffset is always |
| 1776 | // smaller than uPoolSize through this check here and |
| 1777 | // reallocation case. |
| 1778 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 1779 | ReturnValue.len = uNewSize; |
| 1780 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1781 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1782 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1783 | } |
| 1784 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1785 | if(pMem) { |
| 1786 | // FREE MODE |
| 1787 | // Cast is safe because of limit on pool size in |
| 1788 | // QCBORDecode_SetMemPool() |
| 1789 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 1790 | } else { |
| 1791 | // DESTRUCT MODE |
| 1792 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1793 | } |
| 1794 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1795 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1796 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 1797 | MemPool_Pack(Pool, uFreeOffset); |
| 1798 | |
| 1799 | Done: |
| 1800 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1803 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1804 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1805 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1806 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1807 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 1808 | UsefulBuf Pool, |
| 1809 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1810 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1811 | // The pool size and free mem offset are packed into the beginning |
| 1812 | // of the pool memory. This compile time check make sure the |
| 1813 | // constant in the header is correct. This check should optimize |
| 1814 | // down to nothing. |
| 1815 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1816 | return QCBOR_ERR_BUFFER_TOO_SMALL; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1817 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1818 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1819 | // The pool size and free offset packed in to the beginning of pool |
| 1820 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 1821 | // machines. |
| 1822 | if(Pool.len > UINT32_MAX) { |
| 1823 | return QCBOR_ERR_BUFFER_TOO_LARGE; |
| 1824 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1825 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1826 | // This checks that the pool buffer given is big enough. |
| 1827 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
| 1828 | return QCBOR_ERR_BUFFER_TOO_SMALL; |
| 1829 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1830 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1831 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 1832 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 1833 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1834 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1835 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1836 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1837 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1838 | #include <stdio.h> |
| 1839 | void printdecode(QCBORDecodeContext *pMe, const char *szName) |
| 1840 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1841 | printf("---%s--%d--%d--\nLevel Count Type Offset SaveCount MapMode\n", |
| 1842 | szName, |
| 1843 | (uint32_t)pMe->InBuf.cursor, |
| 1844 | (uint32_t)pMe->InBuf.UB.len); |
| 1845 | /* for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) { |
| 1846 | if(&(pMe->nesting.pMapsAndArrays[i]) > pMe->nesting.pCurrent) { |
| 1847 | break; |
| 1848 | } |
| 1849 | printf(" %2d %5d %s %6u %2d %d\n", |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1850 | i, |
| 1851 | pMe->nesting.pMapsAndArrays[i].uCount, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1852 | pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_MAP ? " map" : |
| 1853 | (pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_ARRAY ? "array" : |
| 1854 | (pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_NONE ? " none" : "?????")), |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1855 | pMe->nesting.pMapsAndArrays[i].uOffset, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1856 | pMe->nesting.pMapsAndArrays[i].uSaveCount, |
| 1857 | pMe->nesting.pMapsAndArrays[i].uMapMode |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1858 | ); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1859 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1860 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1861 | printf("\n"); */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1862 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1863 | |
| 1864 | |
| 1865 | /* |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1866 | * |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1867 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1868 | static inline QCBORError |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1869 | ConsumeItem(QCBORDecodeContext *pMe, |
| 1870 | const QCBORItem *pItemToConsume, |
| 1871 | uint_fast8_t *puNextNestLevel) |
| 1872 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1873 | QCBORError nReturn; |
| 1874 | QCBORItem Item; |
| 1875 | |
| 1876 | printdecode(pMe, "ConsumeItem"); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1877 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1878 | if(IsMapOrArray(pItemToConsume->uDataType)) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1879 | /* There is only real work to do for maps and arrays */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1880 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1881 | /* This works for definite and indefinite length |
| 1882 | * maps and arrays by using the nesting level |
| 1883 | */ |
| 1884 | do { |
| 1885 | nReturn = QCBORDecode_GetNext(pMe, &Item); |
| 1886 | if(nReturn != QCBOR_SUCCESS) { |
| 1887 | goto Done; |
| 1888 | } |
| 1889 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1890 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1891 | if(puNextNestLevel != NULL) { |
| 1892 | *puNextNestLevel = Item.uNextNestLevel; |
| 1893 | } |
| 1894 | nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1895 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1896 | } else { |
| 1897 | /* item_to_consume is not a map or array */ |
| 1898 | if(puNextNestLevel != NULL) { |
| 1899 | /* Just pass the nesting level through */ |
| 1900 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 1901 | } |
| 1902 | nReturn = QCBOR_SUCCESS; |
| 1903 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1904 | |
| 1905 | Done: |
| 1906 | return nReturn; |
| 1907 | } |
| 1908 | |
| 1909 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1910 | /* Return true if the labels in Item1 and Item2 are the same. |
| 1911 | Works only for integer and string labels. Returns false |
| 1912 | for any other type. */ |
| 1913 | static inline bool |
| 1914 | MatchLabel(QCBORItem Item1, QCBORItem Item2) |
| 1915 | { |
| 1916 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 1917 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 1918 | return true; |
| 1919 | } |
| 1920 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
| 1921 | if(Item2.uLabelType == QCBOR_TYPE_TEXT_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 1922 | return true; |
| 1923 | } |
| 1924 | } |
| 1925 | /* Other label types are never matched */ |
| 1926 | return false; |
| 1927 | } |
| 1928 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1929 | /* |
| 1930 | * Public function. qcbor_util.h |
| 1931 | */ |
| 1932 | QCBORError |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1933 | GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemArray, size_t *puOffset, size_t *puEndOffset) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1934 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1935 | QCBORItem *pIterator; |
| 1936 | QCBORError nReturn; |
| 1937 | |
| 1938 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1939 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1940 | printdecode(pMe, "GetItemsInMapStart"); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1941 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1942 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1943 | // TODO: check we are in map mode |
| 1944 | |
| 1945 | /* Clear structure holding the items found */ |
| 1946 | for(pIterator = pItemArray; pIterator->uLabelType != 0; pIterator++) { |
| 1947 | pIterator->uDataType = QCBOR_TYPE_NONE; |
| 1948 | } |
| 1949 | |
| 1950 | // Save the cursor and such used for pre-order traversal |
| 1951 | /* const size_t uSave = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 1952 | const uint16_t uSaveCount = pMe->nesting.pCurrent->uCount; |
| 1953 | struct nesting_decode_level *pSaveCurrent = pMe->nesting.pCurrent; |
| 1954 | */ |
| 1955 | QCBORDecodeNesting N = pMe->nesting; |
| 1956 | |
| 1957 | if(pMe->nesting.pCurrent->uCount != UINT16_MAX) { |
| 1958 | pMe->nesting.pCurrent->uCount = pMe->nesting.pCurrent->uSaveCount; |
| 1959 | } |
| 1960 | |
| 1961 | UsefulInputBuf_Seek(&(pMe->InBuf), pMe->nesting.pCurrent->uOffset); |
| 1962 | |
| 1963 | /* Loop over all the items in the map. They could be |
| 1964 | * deeply nested and this should handle both definite |
| 1965 | * and indefinite length maps and arrays, so this |
| 1966 | * adds some complexity. */ |
| 1967 | const uint8_t uMapNestLevel = DecodeNesting_GetLevel(&(pMe->nesting)); |
| 1968 | |
| 1969 | while(1) { |
| 1970 | QCBORItem Item; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1971 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1972 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1973 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1974 | printdecode(pMe, "GetItemsInMapMid1"); |
| 1975 | |
| 1976 | if((nReturn = QCBORDecode_GetNext(pMe, &Item)) != QCBOR_SUCCESS) { |
| 1977 | /* Got non-well-formed CBOR */ |
| 1978 | goto Done; |
| 1979 | } |
| 1980 | |
| 1981 | printdecode(pMe, "GetItemsInMapMid2"); |
| 1982 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1983 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1984 | // Loop over all the items to check this item against |
| 1985 | for(pIterator = pItemArray; pIterator->uLabelType != 0; pIterator++) { |
| 1986 | if(MatchLabel(Item, *pIterator)) { |
| 1987 | // A label match has been found |
| 1988 | if(pIterator->uDataType != QCBOR_TYPE_NONE) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1989 | nReturn = QCBOR_ERR_DUPLICATE_LABEL; |
| 1990 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* Successful match. Return the item. */ |
| 1994 | *pIterator = Item; |
| 1995 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 1996 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | /* Still have to consume the item that did or didn't match. |
| 2002 | The item could be a deeply nested array or map. */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2003 | |
| 2004 | /* Only looking at top-level data items, so just consume any |
| 2005 | * map or array encountered.*/ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2006 | uint_fast8_t uNextNestLevel; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2007 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2008 | nReturn = ConsumeItem(pMe, &Item, &uNextNestLevel); |
| 2009 | if(nReturn) { |
| 2010 | goto Done; |
| 2011 | } |
| 2012 | if(uNextNestLevel < uMapNestLevel) { |
| 2013 | nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2014 | /* Got all the items in the map. This is the non-error exit |
| 2015 | * from the loop. */ |
| 2016 | // Cast OK because encoded CBOR is limited to UINT32_MAX |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2017 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2018 | pMe->uMapEndOffset = (uint32_t)uEndOffset; |
| 2019 | // TODO: is zero *puOffset OK? |
| 2020 | if(puEndOffset) { |
| 2021 | *puEndOffset = uEndOffset; |
| 2022 | } |
| 2023 | // TODO: record the offset here for exit to save CPU time |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2024 | break; |
| 2025 | } |
| 2026 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2027 | |
| 2028 | Done: |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2029 | printdecode(pMe, "GetItemsInMapBeforeDone"); |
| 2030 | |
| 2031 | |
| 2032 | // Restore cursor for pre-order traversal |
| 2033 | /* |
| 2034 | pMe->nesting.pCurrent = pSaveCurrent; |
| 2035 | pMe->nesting.pCurrent->uCount = uSaveCount; |
| 2036 | UsefulInputBuf_Seek(&(pMe->InBuf), uSave); |
| 2037 | */ |
| 2038 | pMe->nesting = N; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2039 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2040 | printdecode(pMe, "GetItemsInMapEnd"); |
| 2041 | |
| 2042 | return nReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | void QCBORDecode_ExitMap(QCBORDecodeContext *pMe) |
| 2046 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2047 | size_t uEndOffset; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2048 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2049 | /* |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2050 | if(pMe->uMapEndOffset) { |
| 2051 | uEndOffset = pMe->uMapEndOffset; |
| 2052 | // It is only valid once. |
| 2053 | pMe->uMapEndOffset = 0; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2054 | } else { */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2055 | QCBORItem Dummy; |
| 2056 | |
| 2057 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 2058 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2059 | QCBORError nReturn = GetItemsInMap(pMe, &Dummy, NULL, &uEndOffset); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2060 | |
| 2061 | (void)nReturn; // TODO: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2062 | // } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2063 | |
| 2064 | printdecode(pMe, "start exit"); |
| 2065 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 2066 | |
| 2067 | if(pMe->nesting.pCurrent->uCount != UINT16_MAX) { |
| 2068 | pMe->nesting.pCurrent->uCount = 1; |
| 2069 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2070 | pMe->nesting.pCurrent->uMapMode = 0; |
| 2071 | |
| 2072 | DecodeNesting_DecrementCount(&(pMe->nesting), true); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2073 | printdecode(pMe, "end exit"); |
| 2074 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | |
| 2078 | QCBORError QCBORDecode_GetItemInMap(QCBORDecodeContext *pMe, |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2079 | int64_t nLabel, |
| 2080 | uint8_t uQcborType, |
| 2081 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2082 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2083 | QCBORItem One[2]; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2084 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2085 | One[0].uLabelType = QCBOR_TYPE_INT64; |
| 2086 | One[0].label.int64 = nLabel; |
| 2087 | One[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
| 2088 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2089 | QCBORError nReturn = GetItemsInMap(pMe, One, NULL, NULL); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2090 | if(nReturn) { |
| 2091 | return nReturn; |
| 2092 | } |
| 2093 | |
| 2094 | if(One[0].uDataType == QCBOR_TYPE_NONE) { |
| 2095 | return QCBOR_ERR_NOT_FOUND; |
| 2096 | } |
| 2097 | |
| 2098 | if(One[0].uDataType != uQcborType) { |
| 2099 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2100 | } |
| 2101 | |
| 2102 | *pItem = One[0]; |
| 2103 | |
| 2104 | return QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | QCBORError QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2109 | const char *szLabel, |
| 2110 | uint8_t uQcborType, |
| 2111 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2112 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2113 | QCBORItem One[2]; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2114 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2115 | One[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2116 | One[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2117 | One[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
| 2118 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2119 | QCBORError nReturn = GetItemsInMap(pMe, One, NULL, NULL); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2120 | if(nReturn) { |
| 2121 | return nReturn; |
| 2122 | } |
| 2123 | |
| 2124 | if(One[0].uDataType == QCBOR_TYPE_NONE) { |
| 2125 | return QCBOR_ERR_NOT_FOUND; |
| 2126 | } |
| 2127 | |
| 2128 | if(One[0].uDataType != uQcborType) { |
| 2129 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2130 | } |
| 2131 | |
| 2132 | *pItem = One[0]; |
| 2133 | |
| 2134 | return QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | |
| 2138 | void QCBORDecode_GetBstrInMap(QCBORDecodeContext *pMe, int64_t nLabel, UsefulBufC *pBstr) |
| 2139 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2140 | // TODO: error handling |
| 2141 | QCBORItem Item; |
| 2142 | QCBORDecode_GetItemInMap(pMe, nLabel, QCBOR_TYPE_BYTE_STRING, &Item); |
| 2143 | *pBstr = Item.val.string; |
| 2144 | } |
| 2145 | |
| 2146 | void QCBORDecode_GetBstrInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, UsefulBufC *pBstr) |
| 2147 | { |
| 2148 | // TODO: error handling |
| 2149 | QCBORItem Item; |
| 2150 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_BYTE_STRING, &Item); |
| 2151 | *pBstr = Item.val.string; |
| 2152 | } |
| 2153 | |
| 2154 | void QCBORDecode_GetTextInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, UsefulBufC *pBstr) |
| 2155 | { |
| 2156 | // TODO: error handling |
| 2157 | QCBORItem Item; |
| 2158 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_TEXT_STRING, &Item); |
| 2159 | *pBstr = Item.val.string; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | |
| 2163 | QCBORError QCBORDecode_EnterMapInMap(QCBORDecodeContext *pMe, int64_t nLabel) |
| 2164 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2165 | QCBORItem One[2]; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2166 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2167 | One[0].uLabelType = QCBOR_TYPE_INT64; |
| 2168 | One[0].label.int64 = nLabel; |
| 2169 | One[1].uLabelType = QCBOR_TYPE_NONE; |
| 2170 | |
| 2171 | size_t uOffset; |
| 2172 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2173 | QCBORError nReturn = GetItemsInMap(pMe, One, &uOffset, NULL); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2174 | |
| 2175 | if(nReturn) { |
| 2176 | return nReturn; |
| 2177 | } |
| 2178 | |
| 2179 | if(One[0].uDataType != QCBOR_TYPE_MAP) { |
| 2180 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2181 | } |
| 2182 | |
| 2183 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
| 2184 | |
| 2185 | DecodeNesting_Descend(&(pMe->nesting), &One[1]); |
| 2186 | |
| 2187 | pMe->nesting.pCurrent->uOffset = (uint32_t)UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2188 | pMe->nesting.pCurrent->uMapMode = 1; |
| 2189 | |
| 2190 | return 0; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | |
| 2194 | QCBORError QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 2195 | { |
| 2196 | QCBORItem One[2]; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2197 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2198 | One[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2199 | One[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2200 | One[1].uLabelType = QCBOR_TYPE_NONE; |
| 2201 | |
| 2202 | size_t uOffset; |
| 2203 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2204 | QCBORError nReturn = GetItemsInMap(pMe, One, &uOffset, NULL); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2205 | |
| 2206 | if(nReturn) { |
| 2207 | return nReturn; |
| 2208 | } |
| 2209 | |
| 2210 | if(One[0].uDataType != QCBOR_TYPE_MAP) { |
| 2211 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2212 | } |
| 2213 | |
| 2214 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
| 2215 | |
| 2216 | QCBORItem MapToEnter; |
| 2217 | QCBORDecode_GetNext(pMe, &MapToEnter); |
| 2218 | |
| 2219 | |
| 2220 | // DecodeNesting_Descend(&(pMe->nesting), &One[1]); |
| 2221 | |
| 2222 | pMe->nesting.pCurrent->uOffset = (uint32_t)UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2223 | pMe->nesting.pCurrent->uMapMode = 1; |
| 2224 | pMe->nesting.pCurrent->uSaveCount = pMe->nesting.pCurrent->uCount; |
| 2225 | |
| 2226 | printdecode(pMe, "Entered Map in Map"); |
| 2227 | |
| 2228 | return 0; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2232 | QCBORError QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t uLabel) |
| 2233 | { |
| 2234 | (void)pMe; (void)uLabel; |
| 2235 | return 0; |
| 2236 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2237 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2238 | |
| 2239 | QCBORError QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 2240 | { |
| 2241 | QCBORItem One[2]; |
| 2242 | |
| 2243 | One[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 2244 | One[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 2245 | One[1].uLabelType = QCBOR_TYPE_NONE; |
| 2246 | |
| 2247 | size_t uOffset; |
| 2248 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2249 | QCBORError nReturn = GetItemsInMap(pMe, One, &uOffset, NULL); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2250 | |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 2251 | if(nReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2252 | return nReturn; |
| 2253 | } |
| 2254 | |
| 2255 | if(One[0].uDataType != QCBOR_TYPE_ARRAY) { |
| 2256 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2257 | } |
| 2258 | |
| 2259 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
| 2260 | |
| 2261 | QCBORItem ArrayToEnter; |
| 2262 | QCBORDecode_GetNext(pMe, &ArrayToEnter); |
| 2263 | |
| 2264 | |
| 2265 | // DecodeNesting_Descend(&(pMe->nesting), &One[1]); |
| 2266 | |
| 2267 | pMe->nesting.pCurrent->uOffset = (uint32_t)UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2268 | pMe->nesting.pCurrent->uMapMode = 1; |
| 2269 | pMe->nesting.pCurrent->uSaveCount = pMe->nesting.pCurrent->uCount; |
| 2270 | |
| 2271 | printdecode(pMe, "Entered Array in Map"); |
| 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | |
| 2277 | |
| 2278 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2279 | /* Next item must be map or this generates an error */ |
| 2280 | QCBORError QCBORDecode_EnterMap(QCBORDecodeContext *pMe) |
| 2281 | { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 2282 | QCBORItem Item; |
| 2283 | QCBORError nReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2284 | |
| 2285 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 2286 | nReturn = QCBORDecode_GetNext(pMe, &Item); |
| 2287 | if(nReturn != QCBOR_SUCCESS) { |
| 2288 | return nReturn; |
| 2289 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2290 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 2291 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2292 | } |
| 2293 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2294 | |
| 2295 | // Cast to uint32_t is safe because QCBOR onl works on data < UINT32_MAX |
| 2296 | pMe->nesting.pCurrent->uOffset = (uint32_t)UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2297 | pMe->nesting.pCurrent->uMapMode = 1; |
| 2298 | pMe->nesting.pCurrent->uSaveCount = pMe->nesting.pCurrent->uCount; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2299 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2300 | printdecode(pMe, "EnterMapDone"); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2301 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2302 | return QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | |
| 2306 | |
| 2307 | QCBORError QCBORDecode_GetItemsInMap(QCBORDecodeContext *pCtx, QCBORItem *pItemList) |
| 2308 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2309 | return GetItemsInMap(pCtx, pItemList, NULL, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | |
| 2313 | void QCBORDecode_GetIntInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, int64_t *pInt) |
| 2314 | { |
| 2315 | // TODO: error handling |
| 2316 | QCBORItem Item; |
| 2317 | QCBORDecode_GetItemInMapSZ(pMe,szLabel, QCBOR_TYPE_INT64, &Item); |
| 2318 | *pInt = Item.val.int64; |
| 2319 | } |
| 2320 | |
| 2321 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2322 | void QCBORDecode_RewindMap(QCBORDecodeContext *pMe) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2323 | { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2324 | // TODO: check for map mode |
| 2325 | pMe->nesting.pCurrent->uCount = pMe->nesting.pCurrent->uSaveCount; |
| 2326 | UsefulInputBuf_Seek(&(pMe->InBuf), pMe->nesting.pCurrent->uOffset); |
| 2327 | } |
| 2328 | |
| 2329 | |
| 2330 | QCBORError QCBORDecode_EnterArray(QCBORDecodeContext *pMe) |
| 2331 | { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 2332 | QCBORItem Item; |
| 2333 | QCBORError nReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2334 | |
| 2335 | /* Get the data item that is the map that is being searched */ |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 2336 | nReturn = QCBORDecode_GetNext(pMe, &Item); |
| 2337 | if(nReturn != QCBOR_SUCCESS) { |
| 2338 | return nReturn; |
| 2339 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2340 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
| 2341 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 2342 | } |
| 2343 | |
| 2344 | printdecode(pMe, "EnterArray"); |
| 2345 | |
| 2346 | // Cast to uint32_t is safe because QCBOR onl works on data < UINT32_MAX |
| 2347 | pMe->nesting.pCurrent->uOffset = (uint32_t)UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2348 | pMe->nesting.pCurrent->uMapMode = 1; |
| 2349 | pMe->nesting.pCurrent->uSaveCount = pMe->nesting.pCurrent->uCount; |
| 2350 | |
| 2351 | |
| 2352 | return QCBOR_SUCCESS; |
| 2353 | } |
| 2354 | |
| 2355 | |
| 2356 | void QCBORDecode_ExitArray(QCBORDecodeContext *pMe) |
| 2357 | { |
| 2358 | // TODO: make sure we have entered an array |
| 2359 | // TODO: combine with code for map? It is the same so far. |
| 2360 | size_t uEndOffset; |
| 2361 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2362 | /* if(pMe->uMapEndOffset) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2363 | uEndOffset = pMe->uMapEndOffset; |
| 2364 | // It is only valid once. |
| 2365 | pMe->uMapEndOffset = 0; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2366 | } else {*/ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2367 | QCBORItem Dummy; |
| 2368 | |
| 2369 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
| 2370 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2371 | QCBORError nReturn = GetItemsInMap(pMe, &Dummy, NULL, &uEndOffset); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2372 | |
| 2373 | (void)nReturn; // TODO: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2374 | //} |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2375 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2376 | printdecode(pMe, "start exit"); |
| 2377 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 2378 | |
| 2379 | if(pMe->nesting.pCurrent->uCount != UINT16_MAX) { |
| 2380 | pMe->nesting.pCurrent->uCount = 1; |
| 2381 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2382 | pMe->nesting.pCurrent->uMapMode = 0; |
| 2383 | DecodeNesting_DecrementCount(&(pMe->nesting), true); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 2384 | printdecode(pMe, "end exit"); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2385 | } |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2386 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2387 | |
| 2388 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2389 | void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2390 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2391 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2392 | // Already in error state, do nothing |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2393 | return; |
| 2394 | } |
| 2395 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2396 | QCBORError nError; |
| 2397 | QCBORItem Item; |
| 2398 | |
| 2399 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 2400 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2401 | pMe->uLastError = nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2402 | return; |
| 2403 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2404 | |
| 2405 | switch(Item.uDataType) { |
| 2406 | case QCBOR_TYPE_TRUE: |
| 2407 | *pValue = true; |
| 2408 | break; |
| 2409 | |
| 2410 | case QCBOR_TYPE_FALSE: |
| 2411 | *pValue = false; |
| 2412 | break; |
| 2413 | |
| 2414 | default: |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2415 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2416 | break; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2420 | #if 0 |
| 2421 | // TODO: fix this |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2422 | /* Types of text strings |
| 2423 | * Plain, b64, b64url, URI, regex, MIME Text |
| 2424 | * One function for each with options to expect plain? |
| 2425 | * One function for all so you can say what you want? |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2426 | * |
| 2427 | * A label is expected if pLabel is not NULL. |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2428 | */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2429 | void QCBORDecode_GetTextFoo(QCBORDecodeContext *pMe, QCBORLabel *pLabel, UsefulBufC *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2430 | { |
| 2431 | QCBORItem Item; |
| 2432 | QCBORError nError; |
| 2433 | |
| 2434 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 2435 | if(nError) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2436 | pMe->uLastError = nError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2437 | return; |
| 2438 | } |
| 2439 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2440 | if(pLabel != NULL) { |
| 2441 | if(Item.uLabelType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2442 | pMe->uLastError = 9; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2443 | return; |
| 2444 | } else { |
| 2445 | // TODO: what about label allocation? |
| 2446 | pLabel->uLabelType = Item.uLabelType; |
| 2447 | pLabel->label.xx = Item.label.int64; // TOOD: figure out assignment |
| 2448 | } |
| 2449 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2450 | |
| 2451 | switch(Item.uDataType) { |
| 2452 | case QCBOR_TYPE_TEXT_STRING: |
| 2453 | *pValue = Item.val.string; |
| 2454 | break; |
| 2455 | |
| 2456 | default: |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2457 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2458 | } |
| 2459 | } |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2460 | #endif |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2461 | |
| 2462 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2463 | /* |
| 2464 | Options for MIME data, CBOR, positive big num, negative big num ?? |
| 2465 | */ |
| 2466 | void QCBORDecode_GetStringInternal(QCBORDecodeContext *pMe, UsefulBufC *pValue, uint8_t uType) |
| 2467 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2468 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2469 | // Already in error state, do nothing |
| 2470 | return; |
| 2471 | } |
| 2472 | |
| 2473 | QCBORError nError; |
| 2474 | QCBORItem Item; |
| 2475 | |
| 2476 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 2477 | if(nError != QCBOR_SUCCESS) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2478 | pMe->uLastError = nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2479 | return; |
| 2480 | } |
| 2481 | |
| 2482 | if(Item.uDataType == uType) { |
| 2483 | *pValue = Item.val.string; |
| 2484 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2485 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2489 | void QCBORDecode_GetBytes(QCBORDecodeContext *pMe, UsefulBufC *pValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2490 | { |
| 2491 | QCBORDecode_GetStringInternal(pMe, pValue, QCBOR_TYPE_BYTE_STRING); |
| 2492 | } |
| 2493 | |
| 2494 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2495 | void QCBORDecode_GetText(QCBORDecodeContext *pMe, UsefulBufC *pValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2496 | { |
| 2497 | QCBORDecode_GetStringInternal(pMe, pValue, QCBOR_TYPE_TEXT_STRING); |
| 2498 | } |
| 2499 | |
| 2500 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2501 | void QCBORDecode_GetPosBignum(QCBORDecodeContext *pMe, UsefulBufC *pValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2502 | { |
| 2503 | // TODO: do these have to be tagged? |
| 2504 | // Probably should allow tagged or untagged, but not wrong-tagged |
| 2505 | QCBORDecode_GetStringInternal(pMe, pValue, QCBOR_TYPE_POSBIGNUM); |
| 2506 | } |
| 2507 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2508 | void QCBORDecode_GetNegBignum(QCBORDecodeContext *pMe, UsefulBufC *pValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2509 | { |
| 2510 | QCBORDecode_GetStringInternal(pMe, pValue, QCBOR_TYPE_NEGBIGNUM); |
| 2511 | } |
| 2512 | |
| 2513 | |
| 2514 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2515 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2516 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2517 | |
| 2518 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2519 | // The main exponentiator that works on only positive numbers |
| 2520 | static QCBORError Exponentitate10UU(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2521 | { |
| 2522 | uint64_t uResult; |
| 2523 | |
| 2524 | uResult = uMantissa; |
| 2525 | |
| 2526 | /* This loop will run a maximum of 19 times because |
| 2527 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 2528 | * exit with the overflow error |
| 2529 | */ |
| 2530 | while(nExponent > 0) { |
| 2531 | if(uResult > UINT64_MAX / 10) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2532 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2533 | } |
| 2534 | uResult = uResult * 10; |
| 2535 | nExponent--; |
| 2536 | } |
| 2537 | |
| 2538 | while(nExponent < 0 ) { |
| 2539 | if(uResult == 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2540 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2541 | } |
| 2542 | uResult = uResult / 10; |
| 2543 | nExponent--; |
| 2544 | } |
| 2545 | |
| 2546 | *puResult = uResult; |
| 2547 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2548 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2552 | /* Convert a decimal fraction to an int64_t without using |
| 2553 | floating point or math libraries. Most decimal fractions |
| 2554 | will not fit in an int64_t and this will error out with |
| 2555 | under or overflow |
| 2556 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2557 | static QCBORError Exponentitate2UU(uint64_t nMantissa, int64_t nExponent, uint64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2558 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2559 | uint64_t nResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2560 | |
| 2561 | nResult = nMantissa; |
| 2562 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2563 | /* This loop will run a maximum of 64 times because |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2564 | * INT64_MAX < 2^31. More than that will cause |
| 2565 | * exist with the overflow error |
| 2566 | */ |
| 2567 | while(nExponent > 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2568 | if(nResult > UINT64_MAX >> 1) { |
| 2569 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2570 | } |
| 2571 | nResult = nResult << 1; |
| 2572 | nExponent--; |
| 2573 | } |
| 2574 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2575 | while(nExponent < 0 ) { |
| 2576 | if(nResult == 0) { |
| 2577 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error |
| 2578 | } |
| 2579 | nResult = nResult >> 1; |
| 2580 | nExponent--; |
| 2581 | } |
| 2582 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2583 | *pnResult = nResult; |
| 2584 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2585 | return QCBOR_SUCCESS; |
| 2586 | } |
| 2587 | |
| 2588 | |
| 2589 | static inline QCBORError ExponentiateNN(int64_t nMantissa, int64_t nExponent, int64_t *pnResult, fExponentiator pfExp) |
| 2590 | { |
| 2591 | uint64_t uResult; |
| 2592 | |
| 2593 | // Take the absolute value of the mantissa |
| 2594 | uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa; |
| 2595 | |
| 2596 | // Do the exponentiation of the positive mantissa |
| 2597 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 2598 | if(uReturn) { |
| 2599 | return uReturn; |
| 2600 | } |
| 2601 | |
| 2602 | // Error out if too large on the plus side for an int64_t |
| 2603 | if(uResult > INT64_MAX) { |
| 2604 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 2605 | } |
| 2606 | |
| 2607 | // Error out if too large on the negative side for an int64_t |
| 2608 | if(uResult < (uint64_t)INT64_MAX+1) { |
| 2609 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 2610 | of INT64_MIN. This assumes two's compliment representation where |
| 2611 | INT64_MIN is one increment farther from 0 than INT64_MAX. |
| 2612 | Trying to write -INT64_MIN doesn't work to get this because the |
| 2613 | compiler tries to work with an int64_t which can't represent |
| 2614 | -INT64_MIN. |
| 2615 | */ |
| 2616 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 2617 | } |
| 2618 | |
| 2619 | // Casts are safe because of checks above |
| 2620 | *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult; |
| 2621 | |
| 2622 | return QCBOR_SUCCESS; |
| 2623 | } |
| 2624 | |
| 2625 | |
| 2626 | static inline QCBORError ExponentitateNU(int64_t nMantissa, int64_t nExponent, uint64_t *puResult, fExponentiator pfExp) |
| 2627 | { |
| 2628 | if(nMantissa < 0) { |
| 2629 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 2630 | } |
| 2631 | |
| 2632 | // Cast to unsigned is OK because of check for negative |
| 2633 | // Cast to unsigned is OK because UINT64_MAX > INT64_MAX |
| 2634 | // Exponentiation is straight forward |
| 2635 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 2636 | } |
| 2637 | |
| 2638 | |
| 2639 | // TODO: use this or get rid of it |
| 2640 | QCBORError ExponentitateUN(uint64_t uMantissa, int64_t nExponent, int64_t *pnResult, fExponentiator pfExp) |
| 2641 | { |
| 2642 | uint64_t uResult; |
| 2643 | |
| 2644 | QCBORError uR; |
| 2645 | |
| 2646 | uR = (*pfExp)(uMantissa, nExponent, &uResult); |
| 2647 | if(uR) { |
| 2648 | return uR; |
| 2649 | } |
| 2650 | |
| 2651 | if(uResult > INT64_MAX) { |
| 2652 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 2653 | } |
| 2654 | |
| 2655 | // Cast is OK because of check above |
| 2656 | *pnResult = (int64_t)uResult; |
| 2657 | |
| 2658 | return QCBOR_SUCCESS; |
| 2659 | } |
| 2660 | |
| 2661 | |
| 2662 | |
| 2663 | |
| 2664 | #include <math.h> |
| 2665 | /* |
| 2666 | static inline uint8_t Exponentitate10F(uint64_t uMantissa, int64_t nExponent, double *pfResult) |
| 2667 | { |
| 2668 | // TODO: checkout exceptions; what is HUGE_VAL? |
| 2669 | *pfResult = pow((double)10, (double)nExponent) * (double)uMantissa; |
| 2670 | |
| 2671 | //if(*pfResult == HUGE_VAL) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2672 | return 0; |
| 2673 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2674 | */ |
| 2675 | |
| 2676 | |
| 2677 | |
| 2678 | |
| 2679 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2680 | |
| 2681 | /* |
| 2682 | A) bignum is positive |
| 2683 | A1) output is signed INT64_MAX |
| 2684 | A2) output is unsigned UINT64_MAX |
| 2685 | B) bignum is negative |
| 2686 | B1) output is signed INT64_MAX |
| 2687 | B2) output is unsigned error |
| 2688 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2689 | static inline QCBORError ConvertBigNum(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2690 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2691 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2692 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2693 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2694 | const uint8_t *pByte = BigNum.ptr; |
| 2695 | size_t uLen = BigNum.len; |
| 2696 | while(uLen--) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2697 | if(uResult > uMax >> 8) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2698 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2699 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2700 | uResult = (uResult << 8) + *pByte; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2703 | *pResult = uResult; |
| 2704 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2707 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2708 | #if 0 |
| 2709 | static inline QCBORError ConvertBigNumToDouble(const UsefulBufC BigNum, uint64_t uMax, double *pResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2710 | { |
| 2711 | double nResult; |
| 2712 | |
| 2713 | nResult = 0; |
| 2714 | const uint8_t *pByte = BigNum.ptr; |
| 2715 | size_t uLen = BigNum.len; |
| 2716 | /* This will overflow and become the float value INFINITY if the number |
| 2717 | is too large to fit. No error will be logged. |
| 2718 | TODO: should an error be logged? */ |
| 2719 | while(uLen--) { |
| 2720 | nResult = (nResult * 256) + *pByte; |
| 2721 | } |
| 2722 | |
| 2723 | *pResult = nResult; |
| 2724 | return 0; |
| 2725 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2726 | #endif |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2727 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2728 | static inline QCBORError ConvertPositiveBigNumToUnSigned(const UsefulBufC BigNum, uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2729 | { |
| 2730 | return ConvertBigNum(BigNum, UINT64_MAX, pResult); |
| 2731 | } |
| 2732 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2733 | static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2734 | { |
| 2735 | uint64_t uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2736 | QCBORError n = ConvertBigNum(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2737 | if(n) { |
| 2738 | return n; |
| 2739 | } |
| 2740 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 2741 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2742 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2746 | static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2747 | { |
| 2748 | uint64_t uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2749 | QCBORError n = ConvertBigNum(BigNum, INT64_MAX-1, &uResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2750 | if(n) { |
| 2751 | return n; |
| 2752 | } |
| 2753 | /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */ |
| 2754 | *pResult = -(int64_t)uResult; |
| 2755 | return 0; |
| 2756 | } |
| 2757 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2758 | // No function to convert a negative bignum to unsigned; it is an error |
| 2759 | |
| 2760 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2761 | #if 0 |
| 2762 | static inline int ConvertXYZ(const UsefulBufC Mantissa, int64_t nExponent, int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2763 | { |
| 2764 | int64_t nMantissa; |
| 2765 | |
| 2766 | int xx = ConvertPositiveBigNumToSigned(Mantissa, &nMantissa); |
| 2767 | if(xx) { |
| 2768 | return xx; |
| 2769 | } |
| 2770 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2771 | return ExponentiateNN(nMantissa, nExponent, pResult, &Exponentitate10UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2772 | } |
| 2773 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2774 | #endif |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2775 | |
| 2776 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2777 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2778 | |
| 2779 | /* |
| 2780 | Get the next item as an int64_t. The CBOR type can be unsigned, negative, float |
| 2781 | a big float, a decimal fraction or a big num. Conversion will be dones as |
| 2782 | expected. Some cases will error out with under or over flow. |
| 2783 | */ |
| 2784 | void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pValue, QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2785 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2786 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2787 | return; |
| 2788 | } |
| 2789 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2790 | QCBORItem Item; |
| 2791 | QCBORError nError; |
| 2792 | |
| 2793 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 2794 | if(nError) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2795 | pMe->uLastError = nError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2796 | return; |
| 2797 | } |
| 2798 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2799 | if(pItem) { |
| 2800 | *pItem = Item; |
| 2801 | } |
| 2802 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2803 | switch(Item.uDataType) { |
| 2804 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2805 | if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2806 | // TODO: what about under/overflow here? |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2807 | // Invokes the floating-point HW and/or compiler-added libraries |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2808 | *pValue = (int64_t)Item.val.dfnum; |
| 2809 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2810 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2811 | } |
| 2812 | break; |
| 2813 | |
| 2814 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2815 | if(uOptions & QCBOR_CONVERT_TYPE_INT64) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2816 | *pValue = Item.val.int64; |
| 2817 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2818 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2819 | } |
| 2820 | break; |
| 2821 | |
| 2822 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2823 | if(uOptions & QCBOR_CONVERT_TYPE_UINT64) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2824 | if(Item.val.uint64 < INT64_MAX) { |
| 2825 | *pValue = Item.val.int64; |
| 2826 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2827 | pMe->uLastError = QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2828 | } |
| 2829 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2830 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2831 | } |
| 2832 | break; |
| 2833 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2834 | default: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2835 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | /* This works for signed, unsigned and float */ |
| 2840 | void QCBORDecode_GetInt64Convert(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pValue) |
| 2841 | { |
| 2842 | QCBORItem Item; |
| 2843 | QCBORDecode_GetInt64ConvertInternal(pMe, uOptions, pValue, &Item); |
| 2844 | } |
| 2845 | |
| 2846 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2847 | // TODO make this inline |
| 2848 | void QCBORDecode_GetInt64(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pValue) |
| 2849 | { |
| 2850 | QCBORDecode_GetInt64Convert(pMe, QCBOR_TYPE_INT64, pValue); |
| 2851 | } |
| 2852 | |
| 2853 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2854 | |
| 2855 | /* |
| 2856 | Get the next item as an int64_t. The CBOR type can be unsigned, negative, float |
| 2857 | a big float, a decimal fraction or a big num. Conversion will be dones as |
| 2858 | expected. Some cases will error out with under or over flow. |
| 2859 | */ |
| 2860 | void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pValue) |
| 2861 | { |
| 2862 | QCBORItem Item; |
| 2863 | |
| 2864 | QCBORDecode_GetInt64ConvertInternal(pMe, uOptions, pValue, &Item); |
| 2865 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2866 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 2867 | // The above conversion succeeded |
| 2868 | return; |
| 2869 | } |
| 2870 | |
| 2871 | if(pMe->uLastError != QCBOR_SUCCESS && pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 2872 | // 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] | 2873 | return; |
| 2874 | } |
| 2875 | |
| 2876 | switch(Item.uDataType) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2877 | |
| 2878 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2879 | if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2880 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.bigNum, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2881 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2882 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2883 | } |
| 2884 | break; |
| 2885 | |
| 2886 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2887 | if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2888 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.bigNum, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2889 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2890 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2891 | } |
| 2892 | break; |
| 2893 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2894 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 2895 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 2896 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
| 2897 | pMe->uLastError = ExponentiateNN(Item.val.expAndMantissa.Mantissa.nInt, |
| 2898 | Item.val.expAndMantissa.nExponent, |
| 2899 | pValue, |
| 2900 | &Exponentitate10UU); |
| 2901 | } else { |
| 2902 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
| 2903 | } |
| 2904 | break; |
| 2905 | |
| 2906 | case QCBOR_TYPE_BIGFLOAT: |
| 2907 | if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
| 2908 | pMe->uLastError = ExponentiateNN(Item.val.expAndMantissa.Mantissa.nInt, |
| 2909 | Item.val.expAndMantissa.nExponent, |
| 2910 | pValue, |
| 2911 | &Exponentitate2UU); |
| 2912 | } else { |
| 2913 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
| 2914 | } |
| 2915 | break; |
| 2916 | |
| 2917 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2918 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2919 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2920 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2921 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 2922 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2923 | pMe->uLastError = ExponentiateNN(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2924 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2925 | pValue, |
| 2926 | &Exponentitate10UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2927 | } |
| 2928 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2929 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2930 | } |
| 2931 | break; |
| 2932 | |
| 2933 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2934 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2935 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2936 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 2937 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2938 | pMe->uLastError = ExponentiateNN(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2939 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2940 | pValue, |
| 2941 | Exponentitate10UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2942 | } |
| 2943 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2944 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2945 | } |
| 2946 | break; |
| 2947 | |
| 2948 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2949 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2950 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2951 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 2952 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2953 | pMe->uLastError = ExponentiateNN(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2954 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2955 | pValue, |
| 2956 | Exponentitate2UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2957 | } |
| 2958 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2959 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2960 | } |
| 2961 | break; |
| 2962 | |
| 2963 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2964 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2965 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2966 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 2967 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2968 | pMe->uLastError = ExponentiateNN(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2969 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2970 | pValue, |
| 2971 | Exponentitate2UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2972 | } |
| 2973 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2974 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2975 | } |
| 2976 | break; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2977 | |
| 2978 | default: |
| 2979 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 2980 | #endif |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2984 | |
| 2985 | |
| 2986 | void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe, uint32_t uOptions, uint64_t *pValue, QCBORItem *pItem) |
| 2987 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2988 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2989 | return; |
| 2990 | } |
| 2991 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2992 | QCBORItem Item; |
| 2993 | QCBORError nError; |
| 2994 | |
| 2995 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 2996 | if(nError) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 2997 | pMe->uLastError = nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 2998 | return; |
| 2999 | } |
| 3000 | |
| 3001 | switch(Item.uDataType) { |
| 3002 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3003 | if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3004 | if(Item.val.dfnum >= 0) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3005 | // TODO: over/underflow |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3006 | *pValue = (uint64_t)Item.val.dfnum; |
| 3007 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3008 | pMe->uLastError = QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3009 | } |
| 3010 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3011 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3012 | } |
| 3013 | break; |
| 3014 | |
| 3015 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3016 | if(uOptions & QCBOR_CONVERT_TYPE_INT64) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3017 | if(Item.val.int64 >= 0) { |
| 3018 | *pValue = (uint64_t)Item.val.int64; |
| 3019 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3020 | pMe->uLastError = QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3021 | } |
| 3022 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3023 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3024 | } |
| 3025 | break; |
| 3026 | |
| 3027 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3028 | if(uOptions & QCBOR_CONVERT_TYPE_UINT64) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3029 | *pValue = Item.val.uint64; |
| 3030 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3031 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3032 | } |
| 3033 | break; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3034 | |
| 3035 | default: |
| 3036 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3037 | } |
| 3038 | } |
| 3039 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3040 | |
| 3041 | /* This works for signed, unsigned and float */ |
| 3042 | void QCBORDecode_GetUInt64Convert(QCBORDecodeContext *pMe, uint32_t uOptions, uint64_t *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3043 | { |
| 3044 | QCBORItem Item; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3045 | QCBORDecode_GetUInt64ConvertInternal(pMe, uOptions, pValue, &Item); |
| 3046 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3047 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3048 | |
| 3049 | // TODO make this inline |
| 3050 | void QCBORDecode_GetUInt64(QCBORDecodeContext *pMe, uint32_t uOptions, uint64_t *pValue) |
| 3051 | { |
| 3052 | QCBORDecode_GetUInt64Convert(pMe, QCBOR_TYPE_UINT64, pValue); |
| 3053 | } |
| 3054 | |
| 3055 | |
| 3056 | |
| 3057 | |
| 3058 | void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, uint64_t *pValue) |
| 3059 | { |
| 3060 | QCBORItem Item; |
| 3061 | |
| 3062 | QCBORDecode_GetUInt64ConvertInternal(pMe, uOptions, pValue, &Item); |
| 3063 | |
| 3064 | if(pMe->uLastError != QCBOR_SUCCESS && pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3065 | return; |
| 3066 | } |
| 3067 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3068 | switch(Item.uDataType) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3069 | |
| 3070 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3071 | if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) { |
| 3072 | pMe->uLastError = ConvertPositiveBigNumToUnSigned(Item.val.bigNum, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3073 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3074 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3075 | } |
| 3076 | break; |
| 3077 | |
| 3078 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3079 | if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) { |
| 3080 | pMe->uLastError = ConvertPositiveBigNumToUnSigned(Item.val.bigNum, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3081 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3082 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3083 | } |
| 3084 | break; |
| 3085 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3086 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 3087 | |
| 3088 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 3089 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
| 3090 | pMe->uLastError = ExponentitateNU(Item.val.expAndMantissa.Mantissa.nInt, |
| 3091 | Item.val.expAndMantissa.nExponent, |
| 3092 | pValue, |
| 3093 | Exponentitate10UU); |
| 3094 | } else { |
| 3095 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; // TODO: error code |
| 3096 | } |
| 3097 | break; |
| 3098 | |
| 3099 | case QCBOR_TYPE_BIGFLOAT: |
| 3100 | if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
| 3101 | pMe->uLastError = ExponentitateNU(Item.val.expAndMantissa.Mantissa.nInt, |
| 3102 | Item.val.expAndMantissa.nExponent, |
| 3103 | pValue, |
| 3104 | Exponentitate2UU); |
| 3105 | } else { |
| 3106 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; // TODO: error code |
| 3107 | } |
| 3108 | break; |
| 3109 | |
| 3110 | |
| 3111 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3112 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3113 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3114 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3115 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3116 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3117 | pMe->uLastError = ExponentitateNU(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3118 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3119 | pValue, |
| 3120 | Exponentitate10UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3121 | } |
| 3122 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3123 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; // TODO: error code |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3124 | } |
| 3125 | break; |
| 3126 | |
| 3127 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3128 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3129 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3130 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3131 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3132 | pMe->uLastError = ExponentitateNU(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3133 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3134 | pValue, |
| 3135 | Exponentitate10UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3138 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; // TODO: error code |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3139 | } |
| 3140 | break; |
| 3141 | |
| 3142 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3143 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3144 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3145 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3146 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3147 | pMe->uLastError = ExponentitateNU(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3148 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3149 | pValue, |
| 3150 | Exponentitate2UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3151 | } |
| 3152 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3153 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; // TODO: error code |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3154 | } |
| 3155 | break; |
| 3156 | |
| 3157 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3158 | if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3159 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3160 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3161 | if(!pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3162 | pMe->uLastError = ExponentitateNU(nMantissa, |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3163 | Item.val.expAndMantissa.nExponent, |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3164 | pValue, |
| 3165 | Exponentitate2UU); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3166 | } |
| 3167 | } else { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3168 | pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3169 | } |
| 3170 | break; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3171 | #endif |
| 3172 | default: |
| 3173 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3177 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3178 | |
| 3179 | #if 0 |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3180 | /* |
| 3181 | |
| 3182 | Convert from bignums, |
| 3183 | |
| 3184 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 3185 | void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, double *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3186 | { |
| 3187 | /* the same range of conversions */ |
| 3188 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3189 | /* Conversion from bignums, decimal fractions and such will be interesting */ |
| 3190 | |
| 3191 | QCBORItem Item; |
| 3192 | QCBORError nError; |
| 3193 | |
| 3194 | nError = QCBORDecode_GetNext(pMe, &Item); |
| 3195 | if(nError) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3196 | pMe->uLastError = nError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3197 | return; |
| 3198 | } |
| 3199 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3200 | |
| 3201 | switch(Item.uDataType) { |
| 3202 | case QCBOR_TYPE_FLOAT: |
| 3203 | if(uOptions & QCBOR_DECODE_TYPE_FLOAT) { |
| 3204 | *pValue = Item.val.dfnum; |
| 3205 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3206 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3207 | } |
| 3208 | break; |
| 3209 | |
| 3210 | case QCBOR_TYPE_INT64: |
| 3211 | if(uOptions & QCBOR_DECODE_TYPE_INT64) { |
| 3212 | // TODO: how does this work? |
| 3213 | *pValue = (double)Item.val.int64; |
| 3214 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3215 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3216 | } |
| 3217 | break; |
| 3218 | |
| 3219 | case QCBOR_TYPE_UINT64: |
| 3220 | if(uOptions & QCBOR_DECODE_TYPE_UINT64) { |
| 3221 | *pValue = (double)Item.val.uint64; |
| 3222 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3223 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3224 | } |
| 3225 | break; |
| 3226 | |
| 3227 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 3228 | if(uOptions & QCBOR_DECODE_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3229 | pMe->uLastError = Exponentitate10(Item.val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3230 | Item.val.expAndMantissa.nExponent, |
| 3231 | pValue); |
| 3232 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3233 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3234 | } |
| 3235 | break; |
| 3236 | |
| 3237 | case QCBOR_TYPE_BIGFLOAT: |
| 3238 | if(uOptions & QCBOR_DECODE_TYPE_BIGFLOAT) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3239 | pMe->uLastError = Exponentitate2(Item.val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3240 | Item.val.expAndMantissa.nExponent, |
| 3241 | pValue); |
| 3242 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3243 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3244 | } |
| 3245 | break; |
| 3246 | |
| 3247 | case QCBOR_TYPE_POSBIGNUM: |
| 3248 | if(uOptions & QCBOR_DECODE_TYPE_BIG_NUM) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3249 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.bigNum, pValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3250 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3251 | pMe->uLastError = 99; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3252 | } |
| 3253 | break; |
| 3254 | |
| 3255 | case QCBOR_TYPE_NEGBIGNUM: |
| 3256 | if(uOptions & QCBOR_DECODE_TYPE_BIG_NUM) { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3257 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.bigNum, pValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3258 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3259 | pMe->uLastError = 99; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3260 | } |
| 3261 | break; |
| 3262 | |
| 3263 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 3264 | if(uOptions & QCBOR_DECODE_TYPE_DECIMAL_FRACTION) { |
| 3265 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3266 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3267 | if(!pMe->uLastError) { |
| 3268 | pMe->uLastError = Exponentitate10(nMantissa, |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3269 | Item.val.expAndMantissa.nExponent, |
| 3270 | pValue); |
| 3271 | } |
| 3272 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3273 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3274 | } |
| 3275 | break; |
| 3276 | |
| 3277 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 3278 | if(uOptions & QCBOR_DECODE_TYPE_DECIMAL_FRACTION) { |
| 3279 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3280 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3281 | if(!pMe->uLastError) { |
| 3282 | pMe->uLastError = Exponentitate10(nMantissa, |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3283 | Item.val.expAndMantissa.nExponent, |
| 3284 | pValue); |
| 3285 | } |
| 3286 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3287 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3288 | } |
| 3289 | break; |
| 3290 | |
| 3291 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 3292 | if(uOptions & QCBOR_DECODE_TYPE_DECIMAL_FRACTION) { |
| 3293 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3294 | pMe->uLastError = ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3295 | if(!pMe->uLastError) { |
| 3296 | pMe->uLastError = Exponentitate2(nMantissa, |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3297 | Item.val.expAndMantissa.nExponent, |
| 3298 | pValue); |
| 3299 | } |
| 3300 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3301 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3302 | } |
| 3303 | break; |
| 3304 | |
| 3305 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 3306 | if(uOptions & QCBOR_DECODE_TYPE_DECIMAL_FRACTION) { |
| 3307 | int64_t nMantissa; |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3308 | pMe->uLastError = ConvertNegativeBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
| 3309 | if(!pMe->uLastError) { |
| 3310 | pMe->uLastError = Exponentitate2(nMantissa, |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3311 | Item.val.expAndMantissa.nExponent, |
| 3312 | pValue); |
| 3313 | } |
| 3314 | } else { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3315 | pMe->uLastError = 99; // TODO: error code |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 3316 | } |
| 3317 | break; |
| 3318 | } |
| 3319 | |
| 3320 | |
| 3321 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 3324 | #endif |