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