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