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