blob: fcf0dac573bc2cf6e47d5d68ef5d1ee4622f4b24 [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 }
74 std::unique_ptr<Item> item = std::make_unique<Nint>(-1 - static_cast<uint64_t>(value));
75 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);
278 }
279 }
280 CHECK(false); // Impossible to get here.
281 return {};
282}
283
284class FullParseClient : public ParseClient {
285 public:
286 virtual ParseClient* item(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
287 const uint8_t* end) override {
288 if (mParentStack.empty() && !item->isCompound()) {
289 // This is the first and only item.
290 mTheItem = std::move(item);
291 mPosition = end;
292 return nullptr; // We're done.
293 }
294
295 if (item->isCompound()) {
296 // Starting a new compound data item, i.e. a new parent. Save it on the parent stack.
297 // It's safe to save a raw pointer because the unique_ptr is guaranteed to stay in
298 // existence until the corresponding itemEnd() call.
Shawn Willdenc5a4a3f2020-12-01 08:14:39 -0700299 mParentStack.push(item.get());
Max Bires44c78812020-04-10 09:38:23 -0700300 return this;
301 } else {
302 appendToLastParent(std::move(item));
303 return this;
304 }
305 }
306
307 virtual ParseClient* itemEnd(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
308 const uint8_t* end) override {
309 CHECK(item->isCompound() && item.get() == mParentStack.top());
310 mParentStack.pop();
311
312 if (mParentStack.empty()) {
313 mTheItem = std::move(item);
314 mPosition = end;
315 return nullptr; // We're done
316 } else {
317 appendToLastParent(std::move(item));
318 return this;
319 }
320 }
321
322 virtual void error(const uint8_t* position, const std::string& errorMessage) override {
323 mPosition = position;
324 mErrorMessage = errorMessage;
325 }
326
327 std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
328 std::string /* errMsg */>
329 parseResult() {
330 std::unique_ptr<Item> p = std::move(mTheItem);
331 return {std::move(p), mPosition, std::move(mErrorMessage)};
332 }
333
334 private:
335 void appendToLastParent(std::unique_ptr<Item> item) {
336 auto parent = mParentStack.top();
Shawn Willden6ad57322020-11-20 00:31:53 -0700337#if __has_feature(cxx_rtti)
338 assert(dynamic_cast<IncompleteItem*>(parent));
339#endif
Shawn Willden315d8592020-11-25 15:46:34 -0700340
341 IncompleteItem* parentItem{};
Max Bires44c78812020-04-10 09:38:23 -0700342 if (parent->type() == ARRAY) {
Shawn Willden315d8592020-11-25 15:46:34 -0700343 parentItem = static_cast<IncompleteArray*>(parent);
Max Bires44c78812020-04-10 09:38:23 -0700344 } else if (parent->type() == MAP) {
Shawn Willden315d8592020-11-25 15:46:34 -0700345 parentItem = static_cast<IncompleteMap*>(parent);
346 } else if (parent->asSemanticTag()) {
347 parentItem = static_cast<IncompleteSemanticTag*>(parent);
Max Bires44c78812020-04-10 09:38:23 -0700348 } else {
349 CHECK(false); // Impossible to get here.
350 }
Shawn Willden315d8592020-11-25 15:46:34 -0700351 parentItem->add(std::move(item));
Max Bires44c78812020-04-10 09:38:23 -0700352 }
353
354 std::unique_ptr<Item> mTheItem;
Shawn Willdenc5a4a3f2020-12-01 08:14:39 -0700355 std::stack<Item*> mParentStack;
Max Bires44c78812020-04-10 09:38:23 -0700356 const uint8_t* mPosition = nullptr;
357 std::string mErrorMessage;
358};
359
360} // anonymous namespace
361
362void parse(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) {
Andrei Homescu4d171a72021-02-12 22:47:05 -0800363 parseRecursively(begin, end, false, parseClient);
Max Bires44c78812020-04-10 09:38:23 -0700364}
365
366std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
367 std::string /* errMsg */>
368parse(const uint8_t* begin, const uint8_t* end) {
369 FullParseClient parseClient;
370 parse(begin, end, &parseClient);
371 return parseClient.parseResult();
372}
373
Andrei Homescu4d171a72021-02-12 22:47:05 -0800374void parseWithViews(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) {
375 parseRecursively(begin, end, true, parseClient);
376}
377
378std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
379 std::string /* errMsg */>
380parseWithViews(const uint8_t* begin, const uint8_t* end) {
381 FullParseClient parseClient;
382 parseWithViews(begin, end, &parseClient);
383 return parseClient.parseResult();
384}
385
Max Bires44c78812020-04-10 09:38:23 -0700386} // namespace cppbor