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