Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "cppbor_parse.h" |
| 18 | |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 19 | #include <stack> |
| 20 | |
Bram Bonné | f04333a | 2020-10-20 16:40:52 +0200 | [diff] [blame] | 21 | #ifndef __TRUSTY__ |
Shawn Willden | 6ad5732 | 2020-11-20 00:31:53 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | #define LOG_TAG "CppBor" |
Bram Bonné | f04333a | 2020-10-20 16:40:52 +0200 | [diff] [blame] | 24 | #else |
Shawn Willden | 6ad5732 | 2020-11-20 00:31:53 -0700 | [diff] [blame] | 25 | #define CHECK(x) (void)(x) |
Bram Bonné | f04333a | 2020-10-20 16:40:52 +0200 | [diff] [blame] | 26 | #endif |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 27 | |
| 28 | namespace cppbor { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | std::string insufficientLengthString(size_t bytesNeeded, size_t bytesAvail, |
| 33 | const std::string& type) { |
Bram Bonné | f04333a | 2020-10-20 16:40:52 +0200 | [diff] [blame] | 34 | char buf[1024]; |
Shawn Willden | 0f9cd2d | 2020-11-20 00:35:10 -0700 | [diff] [blame] | 35 | snprintf(buf, sizeof(buf), "Need %zu byte(s) for %s, have %zu.", bytesNeeded, type.c_str(), |
Shawn Willden | 6ad5732 | 2020-11-20 00:31:53 -0700 | [diff] [blame] | 36 | bytesAvail); |
Bram Bonné | f04333a | 2020-10-20 16:40:52 +0200 | [diff] [blame] | 37 | return std::string(buf); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>> |
| 41 | std::tuple<bool, uint64_t, const uint8_t*> parseLength(const uint8_t* pos, const uint8_t* end, |
| 42 | ParseClient* parseClient) { |
| 43 | if (pos + sizeof(T) > end) { |
| 44 | parseClient->error(pos - 1, insufficientLengthString(sizeof(T), end - pos, "length field")); |
| 45 | return {false, 0, pos}; |
| 46 | } |
| 47 | |
| 48 | const uint8_t* intEnd = pos + sizeof(T); |
| 49 | T result = 0; |
| 50 | do { |
| 51 | result = static_cast<T>((result << 8) | *pos++); |
| 52 | } while (pos < intEnd); |
| 53 | return {true, result, pos}; |
| 54 | } |
| 55 | |
| 56 | std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end, |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 57 | bool emitViews, ParseClient* parseClient); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 58 | |
| 59 | std::tuple<const uint8_t*, ParseClient*> handleUint(uint64_t value, const uint8_t* hdrBegin, |
| 60 | const uint8_t* hdrEnd, |
| 61 | ParseClient* parseClient) { |
| 62 | std::unique_ptr<Item> item = std::make_unique<Uint>(value); |
| 63 | return {hdrEnd, |
| 64 | parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)}; |
| 65 | } |
| 66 | |
| 67 | std::tuple<const uint8_t*, ParseClient*> handleNint(uint64_t value, const uint8_t* hdrBegin, |
| 68 | const uint8_t* hdrEnd, |
| 69 | ParseClient* parseClient) { |
| 70 | if (value > std::numeric_limits<int64_t>::max()) { |
| 71 | parseClient->error(hdrBegin, "NINT values that don't fit in int64_t are not supported."); |
| 72 | return {hdrBegin, nullptr /* end parsing */}; |
| 73 | } |
Andrei Homescu | a7ca25a | 2021-04-02 23:14:02 -0700 | [diff] [blame] | 74 | std::unique_ptr<Item> item = std::make_unique<Nint>(-1 - static_cast<int64_t>(value)); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 75 | return {hdrEnd, |
| 76 | parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)}; |
| 77 | } |
| 78 | |
| 79 | std::tuple<const uint8_t*, ParseClient*> handleBool(uint64_t value, const uint8_t* hdrBegin, |
| 80 | const uint8_t* hdrEnd, |
| 81 | ParseClient* parseClient) { |
| 82 | std::unique_ptr<Item> item = std::make_unique<Bool>(value == TRUE); |
| 83 | return {hdrEnd, |
| 84 | parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)}; |
| 85 | } |
| 86 | |
| 87 | std::tuple<const uint8_t*, ParseClient*> handleNull(const uint8_t* hdrBegin, const uint8_t* hdrEnd, |
| 88 | ParseClient* parseClient) { |
| 89 | std::unique_ptr<Item> item = std::make_unique<Null>(); |
| 90 | return {hdrEnd, |
| 91 | parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)}; |
| 92 | } |
| 93 | |
| 94 | template <typename T> |
| 95 | std::tuple<const uint8_t*, ParseClient*> handleString(uint64_t length, const uint8_t* hdrBegin, |
| 96 | const uint8_t* valueBegin, const uint8_t* end, |
| 97 | const std::string& errLabel, |
| 98 | ParseClient* parseClient) { |
Shawn Willden | 4907264 | 2021-06-25 10:44:06 -0600 | [diff] [blame] | 99 | ssize_t signed_length = static_cast<ssize_t>(length); |
| 100 | if (end - valueBegin < signed_length || signed_length < 0) { |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 101 | parseClient->error(hdrBegin, insufficientLengthString(length, end - valueBegin, errLabel)); |
| 102 | return {hdrBegin, nullptr /* end parsing */}; |
| 103 | } |
| 104 | |
| 105 | std::unique_ptr<Item> item = std::make_unique<T>(valueBegin, valueBegin + length); |
| 106 | return {valueBegin + length, |
| 107 | parseClient->item(item, hdrBegin, valueBegin, valueBegin + length)}; |
| 108 | } |
| 109 | |
| 110 | class IncompleteItem { |
| 111 | public: |
| 112 | virtual ~IncompleteItem() {} |
| 113 | virtual void add(std::unique_ptr<Item> item) = 0; |
| 114 | }; |
| 115 | |
| 116 | class IncompleteArray : public Array, public IncompleteItem { |
| 117 | public: |
Shawn Willden | 03990c2 | 2020-11-24 19:05:09 -0700 | [diff] [blame] | 118 | explicit IncompleteArray(size_t size) : mSize(size) {} |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 119 | |
| 120 | // We return the "complete" size, rather than the actual size. |
| 121 | size_t size() const override { return mSize; } |
| 122 | |
| 123 | void add(std::unique_ptr<Item> item) override { |
| 124 | mEntries.reserve(mSize); |
| 125 | mEntries.push_back(std::move(item)); |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | size_t mSize; |
| 130 | }; |
| 131 | |
| 132 | class IncompleteMap : public Map, public IncompleteItem { |
| 133 | public: |
Shawn Willden | 03990c2 | 2020-11-24 19:05:09 -0700 | [diff] [blame] | 134 | explicit IncompleteMap(size_t size) : mSize(size) {} |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 135 | |
| 136 | // We return the "complete" size, rather than the actual size. |
| 137 | size_t size() const override { return mSize; } |
| 138 | |
| 139 | void add(std::unique_ptr<Item> item) override { |
Shawn Willden | 03990c2 | 2020-11-24 19:05:09 -0700 | [diff] [blame] | 140 | if (mKeyHeldForAdding) { |
| 141 | mEntries.reserve(mSize); |
| 142 | mEntries.push_back({std::move(mKeyHeldForAdding), std::move(item)}); |
| 143 | } else { |
| 144 | mKeyHeldForAdding = std::move(item); |
| 145 | } |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private: |
Shawn Willden | 03990c2 | 2020-11-24 19:05:09 -0700 | [diff] [blame] | 149 | std::unique_ptr<Item> mKeyHeldForAdding; |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 150 | size_t mSize; |
| 151 | }; |
| 152 | |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 153 | class IncompleteSemanticTag : public SemanticTag, public IncompleteItem { |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 154 | public: |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 155 | explicit IncompleteSemanticTag(uint64_t value) : SemanticTag(value) {} |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 156 | |
| 157 | // We return the "complete" size, rather than the actual size. |
| 158 | size_t size() const override { return 1; } |
| 159 | |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 160 | void add(std::unique_ptr<Item> item) override { mTaggedItem = std::move(item); } |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | std::tuple<const uint8_t*, ParseClient*> handleEntries(size_t entryCount, const uint8_t* hdrBegin, |
| 164 | const uint8_t* pos, const uint8_t* end, |
| 165 | const std::string& typeName, |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 166 | bool emitViews, |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 167 | ParseClient* parseClient) { |
| 168 | while (entryCount > 0) { |
| 169 | --entryCount; |
| 170 | if (pos == end) { |
| 171 | parseClient->error(hdrBegin, "Not enough entries for " + typeName + "."); |
| 172 | return {hdrBegin, nullptr /* end parsing */}; |
| 173 | } |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 174 | std::tie(pos, parseClient) = parseRecursively(pos, end, emitViews, parseClient); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 175 | if (!parseClient) return {hdrBegin, nullptr}; |
| 176 | } |
| 177 | return {pos, parseClient}; |
| 178 | } |
| 179 | |
| 180 | std::tuple<const uint8_t*, ParseClient*> handleCompound( |
| 181 | std::unique_ptr<Item> item, uint64_t entryCount, const uint8_t* hdrBegin, |
| 182 | const uint8_t* valueBegin, const uint8_t* end, const std::string& typeName, |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 183 | bool emitViews, ParseClient* parseClient) { |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 184 | parseClient = |
| 185 | parseClient->item(item, hdrBegin, valueBegin, valueBegin /* don't know the end yet */); |
| 186 | if (!parseClient) return {hdrBegin, nullptr}; |
| 187 | |
| 188 | const uint8_t* pos; |
| 189 | std::tie(pos, parseClient) = |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 190 | handleEntries(entryCount, hdrBegin, valueBegin, end, typeName, emitViews, parseClient); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 191 | if (!parseClient) return {hdrBegin, nullptr}; |
| 192 | |
| 193 | return {pos, parseClient->itemEnd(item, hdrBegin, valueBegin, pos)}; |
| 194 | } |
| 195 | |
| 196 | std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end, |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 197 | bool emitViews, ParseClient* parseClient) { |
Hasini Gunasinghe | 8f49088 | 2022-01-11 02:58:57 +0000 | [diff] [blame^] | 198 | if (begin == end) { |
| 199 | parseClient->error( |
| 200 | begin, |
| 201 | "Input buffer is empty. Begin and end cannot point to the same location."); |
| 202 | return {begin, nullptr}; |
| 203 | } |
| 204 | |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 205 | const uint8_t* pos = begin; |
| 206 | |
| 207 | MajorType type = static_cast<MajorType>(*pos & 0xE0); |
| 208 | uint8_t tagInt = *pos & 0x1F; |
| 209 | ++pos; |
| 210 | |
| 211 | bool success = true; |
| 212 | uint64_t addlData; |
Andrew Scull | 42a7aa8 | 2021-03-30 12:48:04 +0000 | [diff] [blame] | 213 | if (tagInt < ONE_BYTE_LENGTH) { |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 214 | addlData = tagInt; |
Andrew Scull | 42a7aa8 | 2021-03-30 12:48:04 +0000 | [diff] [blame] | 215 | } else if (tagInt > EIGHT_BYTE_LENGTH) { |
| 216 | parseClient->error( |
| 217 | begin, |
| 218 | "Reserved additional information value or unsupported indefinite length item."); |
| 219 | return {begin, nullptr}; |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 220 | } else { |
| 221 | switch (tagInt) { |
| 222 | case ONE_BYTE_LENGTH: |
| 223 | std::tie(success, addlData, pos) = parseLength<uint8_t>(pos, end, parseClient); |
| 224 | break; |
| 225 | |
| 226 | case TWO_BYTE_LENGTH: |
| 227 | std::tie(success, addlData, pos) = parseLength<uint16_t>(pos, end, parseClient); |
| 228 | break; |
| 229 | |
| 230 | case FOUR_BYTE_LENGTH: |
| 231 | std::tie(success, addlData, pos) = parseLength<uint32_t>(pos, end, parseClient); |
| 232 | break; |
| 233 | |
| 234 | case EIGHT_BYTE_LENGTH: |
| 235 | std::tie(success, addlData, pos) = parseLength<uint64_t>(pos, end, parseClient); |
| 236 | break; |
| 237 | |
| 238 | default: |
| 239 | CHECK(false); // It's impossible to get here |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (!success) return {begin, nullptr}; |
| 245 | |
| 246 | switch (type) { |
| 247 | case UINT: |
| 248 | return handleUint(addlData, begin, pos, parseClient); |
| 249 | |
| 250 | case NINT: |
| 251 | return handleNint(addlData, begin, pos, parseClient); |
| 252 | |
| 253 | case BSTR: |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 254 | if (emitViews) { |
| 255 | return handleString<ViewBstr>(addlData, begin, pos, end, "byte string", parseClient); |
| 256 | } else { |
| 257 | return handleString<Bstr>(addlData, begin, pos, end, "byte string", parseClient); |
| 258 | } |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 259 | |
| 260 | case TSTR: |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 261 | if (emitViews) { |
| 262 | return handleString<ViewTstr>(addlData, begin, pos, end, "text string", parseClient); |
| 263 | } else { |
| 264 | return handleString<Tstr>(addlData, begin, pos, end, "text string", parseClient); |
| 265 | } |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 266 | |
| 267 | case ARRAY: |
| 268 | return handleCompound(std::make_unique<IncompleteArray>(addlData), addlData, begin, pos, |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 269 | end, "array", emitViews, parseClient); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 270 | |
| 271 | case MAP: |
| 272 | return handleCompound(std::make_unique<IncompleteMap>(addlData), addlData * 2, begin, |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 273 | pos, end, "map", emitViews, parseClient); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 274 | |
| 275 | case SEMANTIC: |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 276 | return handleCompound(std::make_unique<IncompleteSemanticTag>(addlData), 1, begin, pos, |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 277 | end, "semantic", emitViews, parseClient); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 278 | |
| 279 | case SIMPLE: |
| 280 | switch (addlData) { |
| 281 | case TRUE: |
| 282 | case FALSE: |
| 283 | return handleBool(addlData, begin, pos, parseClient); |
| 284 | case NULL_V: |
| 285 | return handleNull(begin, pos, parseClient); |
Andrew Scull | bdc577b | 2021-03-30 12:53:04 +0000 | [diff] [blame] | 286 | default: |
| 287 | parseClient->error(begin, "Unsupported floating-point or simple value."); |
| 288 | return {begin, nullptr}; |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | CHECK(false); // Impossible to get here. |
| 292 | return {}; |
| 293 | } |
| 294 | |
| 295 | class FullParseClient : public ParseClient { |
| 296 | public: |
| 297 | virtual ParseClient* item(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*, |
| 298 | const uint8_t* end) override { |
| 299 | if (mParentStack.empty() && !item->isCompound()) { |
| 300 | // This is the first and only item. |
| 301 | mTheItem = std::move(item); |
| 302 | mPosition = end; |
| 303 | return nullptr; // We're done. |
| 304 | } |
| 305 | |
| 306 | if (item->isCompound()) { |
| 307 | // Starting a new compound data item, i.e. a new parent. Save it on the parent stack. |
| 308 | // It's safe to save a raw pointer because the unique_ptr is guaranteed to stay in |
| 309 | // existence until the corresponding itemEnd() call. |
Shawn Willden | c5a4a3f | 2020-12-01 08:14:39 -0700 | [diff] [blame] | 310 | mParentStack.push(item.get()); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 311 | return this; |
| 312 | } else { |
| 313 | appendToLastParent(std::move(item)); |
| 314 | return this; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | virtual ParseClient* itemEnd(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*, |
| 319 | const uint8_t* end) override { |
| 320 | CHECK(item->isCompound() && item.get() == mParentStack.top()); |
| 321 | mParentStack.pop(); |
| 322 | |
| 323 | if (mParentStack.empty()) { |
| 324 | mTheItem = std::move(item); |
| 325 | mPosition = end; |
| 326 | return nullptr; // We're done |
| 327 | } else { |
| 328 | appendToLastParent(std::move(item)); |
| 329 | return this; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | virtual void error(const uint8_t* position, const std::string& errorMessage) override { |
| 334 | mPosition = position; |
| 335 | mErrorMessage = errorMessage; |
| 336 | } |
| 337 | |
| 338 | std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */, |
| 339 | std::string /* errMsg */> |
| 340 | parseResult() { |
| 341 | std::unique_ptr<Item> p = std::move(mTheItem); |
| 342 | return {std::move(p), mPosition, std::move(mErrorMessage)}; |
| 343 | } |
| 344 | |
| 345 | private: |
| 346 | void appendToLastParent(std::unique_ptr<Item> item) { |
| 347 | auto parent = mParentStack.top(); |
Shawn Willden | 6ad5732 | 2020-11-20 00:31:53 -0700 | [diff] [blame] | 348 | #if __has_feature(cxx_rtti) |
| 349 | assert(dynamic_cast<IncompleteItem*>(parent)); |
| 350 | #endif |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 351 | |
| 352 | IncompleteItem* parentItem{}; |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 353 | if (parent->type() == ARRAY) { |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 354 | parentItem = static_cast<IncompleteArray*>(parent); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 355 | } else if (parent->type() == MAP) { |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 356 | parentItem = static_cast<IncompleteMap*>(parent); |
| 357 | } else if (parent->asSemanticTag()) { |
| 358 | parentItem = static_cast<IncompleteSemanticTag*>(parent); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 359 | } else { |
| 360 | CHECK(false); // Impossible to get here. |
| 361 | } |
Shawn Willden | 315d859 | 2020-11-25 15:46:34 -0700 | [diff] [blame] | 362 | parentItem->add(std::move(item)); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | std::unique_ptr<Item> mTheItem; |
Shawn Willden | c5a4a3f | 2020-12-01 08:14:39 -0700 | [diff] [blame] | 366 | std::stack<Item*> mParentStack; |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 367 | const uint8_t* mPosition = nullptr; |
| 368 | std::string mErrorMessage; |
| 369 | }; |
| 370 | |
| 371 | } // anonymous namespace |
| 372 | |
| 373 | void parse(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) { |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 374 | parseRecursively(begin, end, false, parseClient); |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */, |
| 378 | std::string /* errMsg */> |
| 379 | parse(const uint8_t* begin, const uint8_t* end) { |
| 380 | FullParseClient parseClient; |
| 381 | parse(begin, end, &parseClient); |
| 382 | return parseClient.parseResult(); |
| 383 | } |
| 384 | |
Andrei Homescu | 4d171a7 | 2021-02-12 22:47:05 -0800 | [diff] [blame] | 385 | void parseWithViews(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) { |
| 386 | parseRecursively(begin, end, true, parseClient); |
| 387 | } |
| 388 | |
| 389 | std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */, |
| 390 | std::string /* errMsg */> |
| 391 | parseWithViews(const uint8_t* begin, const uint8_t* end) { |
| 392 | FullParseClient parseClient; |
| 393 | parseWithViews(begin, end, &parseClient); |
| 394 | return parseClient.parseResult(); |
| 395 | } |
| 396 | |
Max Bires | 44c7881 | 2020-04-10 09:38:23 -0700 | [diff] [blame] | 397 | } // namespace cppbor |