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