blob: f5e8fcf487a578ef3f13c1add9aaf7907a211029 [file] [log] [blame]
Max Bires44c78812020-04-10 09:38:23 -07001/*
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 Bires44c78812020-04-10 09:38:23 -070019#include <stack>
20
Bram Bonnéf04333a2020-10-20 16:40:52 +020021#ifndef __TRUSTY__
Shawn Willden6ad57322020-11-20 00:31:53 -070022#include <android-base/logging.h>
23#define LOG_TAG "CppBor"
Bram Bonnéf04333a2020-10-20 16:40:52 +020024#else
Shawn Willden6ad57322020-11-20 00:31:53 -070025#define CHECK(x) (void)(x)
Bram Bonnéf04333a2020-10-20 16:40:52 +020026#endif
Max Bires44c78812020-04-10 09:38:23 -070027
28namespace cppbor {
29
30namespace {
31
32std::string insufficientLengthString(size_t bytesNeeded, size_t bytesAvail,
33 const std::string& type) {
Bram Bonnéf04333a2020-10-20 16:40:52 +020034 char buf[1024];
Shawn Willden0f9cd2d2020-11-20 00:35:10 -070035 snprintf(buf, sizeof(buf), "Need %zu byte(s) for %s, have %zu.", bytesNeeded, type.c_str(),
Shawn Willden6ad57322020-11-20 00:31:53 -070036 bytesAvail);
Bram Bonnéf04333a2020-10-20 16:40:52 +020037 return std::string(buf);
Max Bires44c78812020-04-10 09:38:23 -070038}
39
40template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
41std::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
56std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end,
Andrei Homescu4d171a72021-02-12 22:47:05 -080057 bool emitViews, ParseClient* parseClient);
Max Bires44c78812020-04-10 09:38:23 -070058
59std::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
67std::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 Homescua7ca25a2021-04-02 23:14:02 -070074 std::unique_ptr<Item> item = std::make_unique<Nint>(-1 - static_cast<int64_t>(value));
Max Bires44c78812020-04-10 09:38:23 -070075 return {hdrEnd,
76 parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)};
77}
78
79std::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
87std::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
94template <typename T>
95std::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) {
99 if (end - valueBegin < static_cast<ssize_t>(length)) {
100 parseClient->error(hdrBegin, insufficientLengthString(length, end - valueBegin, errLabel));
101 return {hdrBegin, nullptr /* end parsing */};
102 }
103
104 std::unique_ptr<Item> item = std::make_unique<T>(valueBegin, valueBegin + length);
105 return {valueBegin + length,
106 parseClient->item(item, hdrBegin, valueBegin, valueBegin + length)};
107}
108
109class IncompleteItem {
110 public:
111 virtual ~IncompleteItem() {}
112 virtual void add(std::unique_ptr<Item> item) = 0;
113};
114
115class IncompleteArray : public Array, public IncompleteItem {
116 public:
Shawn Willden03990c22020-11-24 19:05:09 -0700117 explicit IncompleteArray(size_t size) : mSize(size) {}
Max Bires44c78812020-04-10 09:38:23 -0700118
119 // We return the "complete" size, rather than the actual size.
120 size_t size() const override { return mSize; }
121
122 void add(std::unique_ptr<Item> item) override {
123 mEntries.reserve(mSize);
124 mEntries.push_back(std::move(item));
125 }
126
127 private:
128 size_t mSize;
129};
130
131class IncompleteMap : public Map, public IncompleteItem {
132 public:
Shawn Willden03990c22020-11-24 19:05:09 -0700133 explicit IncompleteMap(size_t size) : mSize(size) {}
Max Bires44c78812020-04-10 09:38:23 -0700134
135 // We return the "complete" size, rather than the actual size.
136 size_t size() const override { return mSize; }
137
138 void add(std::unique_ptr<Item> item) override {
Shawn Willden03990c22020-11-24 19:05:09 -0700139 if (mKeyHeldForAdding) {
140 mEntries.reserve(mSize);
141 mEntries.push_back({std::move(mKeyHeldForAdding), std::move(item)});
142 } else {
143 mKeyHeldForAdding = std::move(item);
144 }
Max Bires44c78812020-04-10 09:38:23 -0700145 }
146
147 private:
Shawn Willden03990c22020-11-24 19:05:09 -0700148 std::unique_ptr<Item> mKeyHeldForAdding;
Max Bires44c78812020-04-10 09:38:23 -0700149 size_t mSize;
150};
151
Shawn Willden315d8592020-11-25 15:46:34 -0700152class IncompleteSemanticTag : public SemanticTag, public IncompleteItem {
Max Bires44c78812020-04-10 09:38:23 -0700153 public:
Shawn Willden315d8592020-11-25 15:46:34 -0700154 explicit IncompleteSemanticTag(uint64_t value) : SemanticTag(value) {}
Max Bires44c78812020-04-10 09:38:23 -0700155
156 // We return the "complete" size, rather than the actual size.
157 size_t size() const override { return 1; }
158
Shawn Willden315d8592020-11-25 15:46:34 -0700159 void add(std::unique_ptr<Item> item) override { mTaggedItem = std::move(item); }
Max Bires44c78812020-04-10 09:38:23 -0700160};
161
162std::tuple<const uint8_t*, ParseClient*> handleEntries(size_t entryCount, const uint8_t* hdrBegin,
163 const uint8_t* pos, const uint8_t* end,
164 const std::string& typeName,
Andrei Homescu4d171a72021-02-12 22:47:05 -0800165 bool emitViews,
Max Bires44c78812020-04-10 09:38:23 -0700166 ParseClient* parseClient) {
167 while (entryCount > 0) {
168 --entryCount;
169 if (pos == end) {
170 parseClient->error(hdrBegin, "Not enough entries for " + typeName + ".");
171 return {hdrBegin, nullptr /* end parsing */};
172 }
Andrei Homescu4d171a72021-02-12 22:47:05 -0800173 std::tie(pos, parseClient) = parseRecursively(pos, end, emitViews, parseClient);
Max Bires44c78812020-04-10 09:38:23 -0700174 if (!parseClient) return {hdrBegin, nullptr};
175 }
176 return {pos, parseClient};
177}
178
179std::tuple<const uint8_t*, ParseClient*> handleCompound(
180 std::unique_ptr<Item> item, uint64_t entryCount, const uint8_t* hdrBegin,
181 const uint8_t* valueBegin, const uint8_t* end, const std::string& typeName,
Andrei Homescu4d171a72021-02-12 22:47:05 -0800182 bool emitViews, ParseClient* parseClient) {
Max Bires44c78812020-04-10 09:38:23 -0700183 parseClient =
184 parseClient->item(item, hdrBegin, valueBegin, valueBegin /* don't know the end yet */);
185 if (!parseClient) return {hdrBegin, nullptr};
186
187 const uint8_t* pos;
188 std::tie(pos, parseClient) =
Andrei Homescu4d171a72021-02-12 22:47:05 -0800189 handleEntries(entryCount, hdrBegin, valueBegin, end, typeName, emitViews, parseClient);
Max Bires44c78812020-04-10 09:38:23 -0700190 if (!parseClient) return {hdrBegin, nullptr};
191
192 return {pos, parseClient->itemEnd(item, hdrBegin, valueBegin, pos)};
193}
194
195std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end,
Andrei Homescu4d171a72021-02-12 22:47:05 -0800196 bool emitViews, ParseClient* parseClient) {
Max Bires44c78812020-04-10 09:38:23 -0700197 const uint8_t* pos = begin;
198
199 MajorType type = static_cast<MajorType>(*pos & 0xE0);
200 uint8_t tagInt = *pos & 0x1F;
201 ++pos;
202
203 bool success = true;
204 uint64_t addlData;
Andrew Scull42a7aa82021-03-30 12:48:04 +0000205 if (tagInt < ONE_BYTE_LENGTH) {
Max Bires44c78812020-04-10 09:38:23 -0700206 addlData = tagInt;
Andrew Scull42a7aa82021-03-30 12:48:04 +0000207 } else if (tagInt > EIGHT_BYTE_LENGTH) {
208 parseClient->error(
209 begin,
210 "Reserved additional information value or unsupported indefinite length item.");
211 return {begin, nullptr};
Max Bires44c78812020-04-10 09:38:23 -0700212 } else {
213 switch (tagInt) {
214 case ONE_BYTE_LENGTH:
215 std::tie(success, addlData, pos) = parseLength<uint8_t>(pos, end, parseClient);
216 break;
217
218 case TWO_BYTE_LENGTH:
219 std::tie(success, addlData, pos) = parseLength<uint16_t>(pos, end, parseClient);
220 break;
221
222 case FOUR_BYTE_LENGTH:
223 std::tie(success, addlData, pos) = parseLength<uint32_t>(pos, end, parseClient);
224 break;
225
226 case EIGHT_BYTE_LENGTH:
227 std::tie(success, addlData, pos) = parseLength<uint64_t>(pos, end, parseClient);
228 break;
229
230 default:
231 CHECK(false); // It's impossible to get here
232 break;
233 }
234 }
235
236 if (!success) return {begin, nullptr};
237
238 switch (type) {
239 case UINT:
240 return handleUint(addlData, begin, pos, parseClient);
241
242 case NINT:
243 return handleNint(addlData, begin, pos, parseClient);
244
245 case BSTR:
Andrei Homescu4d171a72021-02-12 22:47:05 -0800246 if (emitViews) {
247 return handleString<ViewBstr>(addlData, begin, pos, end, "byte string", parseClient);
248 } else {
249 return handleString<Bstr>(addlData, begin, pos, end, "byte string", parseClient);
250 }
Max Bires44c78812020-04-10 09:38:23 -0700251
252 case TSTR:
Andrei Homescu4d171a72021-02-12 22:47:05 -0800253 if (emitViews) {
254 return handleString<ViewTstr>(addlData, begin, pos, end, "text string", parseClient);
255 } else {
256 return handleString<Tstr>(addlData, begin, pos, end, "text string", parseClient);
257 }
Max Bires44c78812020-04-10 09:38:23 -0700258
259 case ARRAY:
260 return handleCompound(std::make_unique<IncompleteArray>(addlData), addlData, begin, pos,
Andrei Homescu4d171a72021-02-12 22:47:05 -0800261 end, "array", emitViews, parseClient);
Max Bires44c78812020-04-10 09:38:23 -0700262
263 case MAP:
264 return handleCompound(std::make_unique<IncompleteMap>(addlData), addlData * 2, begin,
Andrei Homescu4d171a72021-02-12 22:47:05 -0800265 pos, end, "map", emitViews, parseClient);
Max Bires44c78812020-04-10 09:38:23 -0700266
267 case SEMANTIC:
Shawn Willden315d8592020-11-25 15:46:34 -0700268 return handleCompound(std::make_unique<IncompleteSemanticTag>(addlData), 1, begin, pos,
Andrei Homescu4d171a72021-02-12 22:47:05 -0800269 end, "semantic", emitViews, parseClient);
Max Bires44c78812020-04-10 09:38:23 -0700270
271 case SIMPLE:
272 switch (addlData) {
273 case TRUE:
274 case FALSE:
275 return handleBool(addlData, begin, pos, parseClient);
276 case NULL_V:
277 return handleNull(begin, pos, parseClient);
Andrew Scullbdc577b2021-03-30 12:53:04 +0000278 default:
279 parseClient->error(begin, "Unsupported floating-point or simple value.");
280 return {begin, nullptr};
Max Bires44c78812020-04-10 09:38:23 -0700281 }
282 }
283 CHECK(false); // Impossible to get here.
284 return {};
285}
286
287class FullParseClient : public ParseClient {
288 public:
289 virtual ParseClient* item(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
290 const uint8_t* end) override {
291 if (mParentStack.empty() && !item->isCompound()) {
292 // This is the first and only item.
293 mTheItem = std::move(item);
294 mPosition = end;
295 return nullptr; // We're done.
296 }
297
298 if (item->isCompound()) {
299 // Starting a new compound data item, i.e. a new parent. Save it on the parent stack.
300 // It's safe to save a raw pointer because the unique_ptr is guaranteed to stay in
301 // existence until the corresponding itemEnd() call.
Shawn Willdenc5a4a3f2020-12-01 08:14:39 -0700302 mParentStack.push(item.get());
Max Bires44c78812020-04-10 09:38:23 -0700303 return this;
304 } else {
305 appendToLastParent(std::move(item));
306 return this;
307 }
308 }
309
310 virtual ParseClient* itemEnd(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
311 const uint8_t* end) override {
312 CHECK(item->isCompound() && item.get() == mParentStack.top());
313 mParentStack.pop();
314
315 if (mParentStack.empty()) {
316 mTheItem = std::move(item);
317 mPosition = end;
318 return nullptr; // We're done
319 } else {
320 appendToLastParent(std::move(item));
321 return this;
322 }
323 }
324
325 virtual void error(const uint8_t* position, const std::string& errorMessage) override {
326 mPosition = position;
327 mErrorMessage = errorMessage;
328 }
329
330 std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
331 std::string /* errMsg */>
332 parseResult() {
333 std::unique_ptr<Item> p = std::move(mTheItem);
334 return {std::move(p), mPosition, std::move(mErrorMessage)};
335 }
336
337 private:
338 void appendToLastParent(std::unique_ptr<Item> item) {
339 auto parent = mParentStack.top();
Shawn Willden6ad57322020-11-20 00:31:53 -0700340#if __has_feature(cxx_rtti)
341 assert(dynamic_cast<IncompleteItem*>(parent));
342#endif
Shawn Willden315d8592020-11-25 15:46:34 -0700343
344 IncompleteItem* parentItem{};
Max Bires44c78812020-04-10 09:38:23 -0700345 if (parent->type() == ARRAY) {
Shawn Willden315d8592020-11-25 15:46:34 -0700346 parentItem = static_cast<IncompleteArray*>(parent);
Max Bires44c78812020-04-10 09:38:23 -0700347 } else if (parent->type() == MAP) {
Shawn Willden315d8592020-11-25 15:46:34 -0700348 parentItem = static_cast<IncompleteMap*>(parent);
349 } else if (parent->asSemanticTag()) {
350 parentItem = static_cast<IncompleteSemanticTag*>(parent);
Max Bires44c78812020-04-10 09:38:23 -0700351 } else {
352 CHECK(false); // Impossible to get here.
353 }
Shawn Willden315d8592020-11-25 15:46:34 -0700354 parentItem->add(std::move(item));
Max Bires44c78812020-04-10 09:38:23 -0700355 }
356
357 std::unique_ptr<Item> mTheItem;
Shawn Willdenc5a4a3f2020-12-01 08:14:39 -0700358 std::stack<Item*> mParentStack;
Max Bires44c78812020-04-10 09:38:23 -0700359 const uint8_t* mPosition = nullptr;
360 std::string mErrorMessage;
361};
362
363} // anonymous namespace
364
365void parse(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) {
Andrei Homescu4d171a72021-02-12 22:47:05 -0800366 parseRecursively(begin, end, false, parseClient);
Max Bires44c78812020-04-10 09:38:23 -0700367}
368
369std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
370 std::string /* errMsg */>
371parse(const uint8_t* begin, const uint8_t* end) {
372 FullParseClient parseClient;
373 parse(begin, end, &parseClient);
374 return parseClient.parseResult();
375}
376
Andrei Homescu4d171a72021-02-12 22:47:05 -0800377void parseWithViews(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) {
378 parseRecursively(begin, end, true, parseClient);
379}
380
381std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
382 std::string /* errMsg */>
383parseWithViews(const uint8_t* begin, const uint8_t* end) {
384 FullParseClient parseClient;
385 parseWithViews(begin, end, &parseClient);
386 return parseClient.parseResult();
387}
388
Max Bires44c78812020-04-10 09:38:23 -0700389} // namespace cppbor