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