blob: 42d74fb15c7b0bea8517ea526766d973cd063f7b [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,
57 ParseClient* parseClient);
58
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,
165 ParseClient* parseClient) {
166 while (entryCount > 0) {
167 --entryCount;
168 if (pos == end) {
169 parseClient->error(hdrBegin, "Not enough entries for " + typeName + ".");
170 return {hdrBegin, nullptr /* end parsing */};
171 }
172 std::tie(pos, parseClient) = parseRecursively(pos, end, parseClient);
173 if (!parseClient) return {hdrBegin, nullptr};
174 }
175 return {pos, parseClient};
176}
177
178std::tuple<const uint8_t*, ParseClient*> handleCompound(
179 std::unique_ptr<Item> item, uint64_t entryCount, const uint8_t* hdrBegin,
180 const uint8_t* valueBegin, const uint8_t* end, const std::string& typeName,
181 ParseClient* parseClient) {
182 parseClient =
183 parseClient->item(item, hdrBegin, valueBegin, valueBegin /* don't know the end yet */);
184 if (!parseClient) return {hdrBegin, nullptr};
185
186 const uint8_t* pos;
187 std::tie(pos, parseClient) =
188 handleEntries(entryCount, hdrBegin, valueBegin, end, typeName, parseClient);
189 if (!parseClient) return {hdrBegin, nullptr};
190
191 return {pos, parseClient->itemEnd(item, hdrBegin, valueBegin, pos)};
192}
193
194std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end,
195 ParseClient* parseClient) {
196 const uint8_t* pos = begin;
197
198 MajorType type = static_cast<MajorType>(*pos & 0xE0);
199 uint8_t tagInt = *pos & 0x1F;
200 ++pos;
201
202 bool success = true;
203 uint64_t addlData;
204 if (tagInt < ONE_BYTE_LENGTH || tagInt > EIGHT_BYTE_LENGTH) {
205 addlData = tagInt;
206 } else {
207 switch (tagInt) {
208 case ONE_BYTE_LENGTH:
209 std::tie(success, addlData, pos) = parseLength<uint8_t>(pos, end, parseClient);
210 break;
211
212 case TWO_BYTE_LENGTH:
213 std::tie(success, addlData, pos) = parseLength<uint16_t>(pos, end, parseClient);
214 break;
215
216 case FOUR_BYTE_LENGTH:
217 std::tie(success, addlData, pos) = parseLength<uint32_t>(pos, end, parseClient);
218 break;
219
220 case EIGHT_BYTE_LENGTH:
221 std::tie(success, addlData, pos) = parseLength<uint64_t>(pos, end, parseClient);
222 break;
223
224 default:
225 CHECK(false); // It's impossible to get here
226 break;
227 }
228 }
229
230 if (!success) return {begin, nullptr};
231
232 switch (type) {
233 case UINT:
234 return handleUint(addlData, begin, pos, parseClient);
235
236 case NINT:
237 return handleNint(addlData, begin, pos, parseClient);
238
239 case BSTR:
240 return handleString<Bstr>(addlData, begin, pos, end, "byte string", parseClient);
241
242 case TSTR:
243 return handleString<Tstr>(addlData, begin, pos, end, "text string", parseClient);
244
245 case ARRAY:
246 return handleCompound(std::make_unique<IncompleteArray>(addlData), addlData, begin, pos,
247 end, "array", parseClient);
248
249 case MAP:
250 return handleCompound(std::make_unique<IncompleteMap>(addlData), addlData * 2, begin,
251 pos, end, "map", parseClient);
252
253 case SEMANTIC:
Shawn Willden315d8592020-11-25 15:46:34 -0700254 return handleCompound(std::make_unique<IncompleteSemanticTag>(addlData), 1, begin, pos,
Max Bires44c78812020-04-10 09:38:23 -0700255 end, "semantic", parseClient);
256
257 case SIMPLE:
258 switch (addlData) {
259 case TRUE:
260 case FALSE:
261 return handleBool(addlData, begin, pos, parseClient);
262 case NULL_V:
263 return handleNull(begin, pos, parseClient);
264 }
265 }
266 CHECK(false); // Impossible to get here.
267 return {};
268}
269
270class FullParseClient : public ParseClient {
271 public:
272 virtual ParseClient* item(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
273 const uint8_t* end) override {
274 if (mParentStack.empty() && !item->isCompound()) {
275 // This is the first and only item.
276 mTheItem = std::move(item);
277 mPosition = end;
278 return nullptr; // We're done.
279 }
280
281 if (item->isCompound()) {
282 // Starting a new compound data item, i.e. a new parent. Save it on the parent stack.
283 // It's safe to save a raw pointer because the unique_ptr is guaranteed to stay in
284 // existence until the corresponding itemEnd() call.
Shawn Willdenc5a4a3f2020-12-01 08:14:39 -0700285 mParentStack.push(item.get());
Max Bires44c78812020-04-10 09:38:23 -0700286 return this;
287 } else {
288 appendToLastParent(std::move(item));
289 return this;
290 }
291 }
292
293 virtual ParseClient* itemEnd(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
294 const uint8_t* end) override {
295 CHECK(item->isCompound() && item.get() == mParentStack.top());
296 mParentStack.pop();
297
298 if (mParentStack.empty()) {
299 mTheItem = std::move(item);
300 mPosition = end;
301 return nullptr; // We're done
302 } else {
303 appendToLastParent(std::move(item));
304 return this;
305 }
306 }
307
308 virtual void error(const uint8_t* position, const std::string& errorMessage) override {
309 mPosition = position;
310 mErrorMessage = errorMessage;
311 }
312
313 std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
314 std::string /* errMsg */>
315 parseResult() {
316 std::unique_ptr<Item> p = std::move(mTheItem);
317 return {std::move(p), mPosition, std::move(mErrorMessage)};
318 }
319
320 private:
321 void appendToLastParent(std::unique_ptr<Item> item) {
322 auto parent = mParentStack.top();
Shawn Willden6ad57322020-11-20 00:31:53 -0700323#if __has_feature(cxx_rtti)
324 assert(dynamic_cast<IncompleteItem*>(parent));
325#endif
Shawn Willden315d8592020-11-25 15:46:34 -0700326
327 IncompleteItem* parentItem{};
Max Bires44c78812020-04-10 09:38:23 -0700328 if (parent->type() == ARRAY) {
Shawn Willden315d8592020-11-25 15:46:34 -0700329 parentItem = static_cast<IncompleteArray*>(parent);
Max Bires44c78812020-04-10 09:38:23 -0700330 } else if (parent->type() == MAP) {
Shawn Willden315d8592020-11-25 15:46:34 -0700331 parentItem = static_cast<IncompleteMap*>(parent);
332 } else if (parent->asSemanticTag()) {
333 parentItem = static_cast<IncompleteSemanticTag*>(parent);
Max Bires44c78812020-04-10 09:38:23 -0700334 } else {
335 CHECK(false); // Impossible to get here.
336 }
Shawn Willden315d8592020-11-25 15:46:34 -0700337 parentItem->add(std::move(item));
Max Bires44c78812020-04-10 09:38:23 -0700338 }
339
340 std::unique_ptr<Item> mTheItem;
Shawn Willdenc5a4a3f2020-12-01 08:14:39 -0700341 std::stack<Item*> mParentStack;
Max Bires44c78812020-04-10 09:38:23 -0700342 const uint8_t* mPosition = nullptr;
343 std::string mErrorMessage;
344};
345
346} // anonymous namespace
347
348void parse(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) {
349 parseRecursively(begin, end, parseClient);
350}
351
352std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
353 std::string /* errMsg */>
354parse(const uint8_t* begin, const uint8_t* end) {
355 FullParseClient parseClient;
356 parse(begin, end, &parseClient);
357 return parseClient.parseResult();
358}
359
360} // namespace cppbor