Source release 19.1.0
This commit is contained in:
567
third_party/libcppbor/src/cppbor.cpp
vendored
Normal file
567
third_party/libcppbor/src/cppbor.cpp
vendored
Normal file
@@ -0,0 +1,567 @@
|
||||
/*
|
||||
* Copyright 2019 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "cppbor.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <limits>
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
#define CHECK(x) (void)(x)
|
||||
|
||||
namespace cppbor {
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename T, typename Iterator,
|
||||
typename = std::enable_if<std::is_unsigned<T>::value>>
|
||||
Iterator writeBigEndian(T value, Iterator pos) {
|
||||
for (unsigned i = 0; i < sizeof(value); ++i) {
|
||||
*pos++ = static_cast<uint8_t>(value >> (8 * (sizeof(value) - 1)));
|
||||
value = static_cast<T>(value << 8);
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
template <typename T, typename = std::enable_if<std::is_unsigned<T>::value>>
|
||||
void writeBigEndian(T value, std::function<void(uint8_t)>& cb) {
|
||||
for (unsigned i = 0; i < sizeof(value); ++i) {
|
||||
cb(static_cast<uint8_t>(value >> (8 * (sizeof(value) - 1))));
|
||||
value = static_cast<T>(value << 8);
|
||||
}
|
||||
}
|
||||
|
||||
bool cborAreAllElementsNonCompound(const Item* compoundItem) {
|
||||
if (compoundItem->type() == ARRAY) {
|
||||
const Array* array = compoundItem->asArray();
|
||||
for (size_t n = 0; n < array->size(); n++) {
|
||||
const Item* entry = (*array)[n].get();
|
||||
switch (entry->type()) {
|
||||
case ARRAY:
|
||||
case MAP:
|
||||
return false;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const Map* map = compoundItem->asMap();
|
||||
for (auto& entry : *map) {
|
||||
auto& keyEntry = entry.first;
|
||||
auto& valueEntry = entry.second;
|
||||
|
||||
switch (keyEntry->type()) {
|
||||
case ARRAY:
|
||||
case MAP:
|
||||
return false;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (valueEntry->type()) {
|
||||
case ARRAY:
|
||||
case MAP:
|
||||
return false;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool prettyPrintInternal(const Item* item, string& out, size_t indent,
|
||||
size_t maxBStrSize,
|
||||
const vector<string>& mapKeysToNotPrint) {
|
||||
if (!item) {
|
||||
out.append("<NULL>");
|
||||
return false;
|
||||
}
|
||||
|
||||
char buf[80];
|
||||
|
||||
string indentString(indent, ' ');
|
||||
|
||||
size_t tagCount = item->semanticTagCount();
|
||||
while (tagCount > 0) {
|
||||
--tagCount;
|
||||
snprintf(buf, sizeof(buf), "tag %" PRIu64 " ", item->semanticTag(tagCount));
|
||||
out.append(buf);
|
||||
}
|
||||
|
||||
switch (item->type()) {
|
||||
case SEMANTIC:
|
||||
// Handled above.
|
||||
break;
|
||||
|
||||
case UINT:
|
||||
snprintf(buf, sizeof(buf), "%" PRIu64, item->asUint()->unsignedValue());
|
||||
out.append(buf);
|
||||
break;
|
||||
|
||||
case NINT:
|
||||
snprintf(buf, sizeof(buf), "%" PRId64, item->asNint()->value());
|
||||
out.append(buf);
|
||||
break;
|
||||
|
||||
case BSTR: {
|
||||
const uint8_t* valueData;
|
||||
size_t valueSize;
|
||||
const Bstr* bstr = item->asBstr();
|
||||
if (bstr == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const vector<uint8_t>& value = bstr->value();
|
||||
valueData = value.data();
|
||||
valueSize = value.size();
|
||||
|
||||
out.append("{");
|
||||
for (size_t n = 0; n < valueSize; n++) {
|
||||
if (n > 0) {
|
||||
out.append(", ");
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "0x%02x", valueData[n]);
|
||||
out.append(buf);
|
||||
}
|
||||
out.append("}");
|
||||
} break;
|
||||
|
||||
case TSTR:
|
||||
out.append("'");
|
||||
{
|
||||
// TODO: escape "'" characters
|
||||
if (item->asTstr() != nullptr) {
|
||||
out.append(item->asTstr()->value().c_str());
|
||||
} else {
|
||||
}
|
||||
}
|
||||
out.append("'");
|
||||
break;
|
||||
|
||||
case ARRAY: {
|
||||
const Array* array = item->asArray();
|
||||
if (array->size() == 0) {
|
||||
out.append("[]");
|
||||
} else if (cborAreAllElementsNonCompound(array)) {
|
||||
out.append("[");
|
||||
for (size_t n = 0; n < array->size(); n++) {
|
||||
if (!prettyPrintInternal((*array)[n].get(), out, indent + 2,
|
||||
maxBStrSize, mapKeysToNotPrint)) {
|
||||
return false;
|
||||
}
|
||||
out.append(", ");
|
||||
}
|
||||
out.append("]");
|
||||
} else {
|
||||
out.append("[\n" + indentString);
|
||||
for (size_t n = 0; n < array->size(); n++) {
|
||||
out.append(" ");
|
||||
if (!prettyPrintInternal((*array)[n].get(), out, indent + 2,
|
||||
maxBStrSize, mapKeysToNotPrint)) {
|
||||
return false;
|
||||
}
|
||||
out.append(",\n" + indentString);
|
||||
}
|
||||
out.append("]");
|
||||
}
|
||||
} break;
|
||||
|
||||
case MAP: {
|
||||
const Map* map = item->asMap();
|
||||
|
||||
if (map->size() == 0) {
|
||||
out.append("{}");
|
||||
} else {
|
||||
out.append("{\n" + indentString);
|
||||
for (auto& entry : *map) {
|
||||
auto& map_key = entry.first;
|
||||
auto& map_value = entry.second;
|
||||
|
||||
out.append(" ");
|
||||
|
||||
if (!prettyPrintInternal(map_key.get(), out, indent + 2, maxBStrSize,
|
||||
mapKeysToNotPrint)) {
|
||||
return false;
|
||||
}
|
||||
out.append(" : ");
|
||||
if (map_key->type() == TSTR &&
|
||||
std::find(mapKeysToNotPrint.begin(), mapKeysToNotPrint.end(),
|
||||
map_key->asTstr()->value()) !=
|
||||
mapKeysToNotPrint.end()) {
|
||||
out.append("<not printed>");
|
||||
} else {
|
||||
if (!prettyPrintInternal(map_value.get(), out, indent + 2,
|
||||
maxBStrSize, mapKeysToNotPrint)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out.append(",\n" + indentString);
|
||||
}
|
||||
out.append("}");
|
||||
}
|
||||
} break;
|
||||
|
||||
case SIMPLE:
|
||||
const Bool* asBool = item->asSimple()->asBool();
|
||||
const Null* asNull = item->asSimple()->asNull();
|
||||
if (asBool != nullptr) {
|
||||
out.append(asBool->value() ? "true" : "false");
|
||||
} else if (asNull != nullptr) {
|
||||
out.append("null");
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
size_t headerSize(uint64_t addlInfo) {
|
||||
if (addlInfo < ONE_BYTE_LENGTH) return 1;
|
||||
if (addlInfo <= std::numeric_limits<uint8_t>::max()) return 2;
|
||||
if (addlInfo <= std::numeric_limits<uint16_t>::max()) return 3;
|
||||
if (addlInfo <= std::numeric_limits<uint32_t>::max()) return 5;
|
||||
return 9;
|
||||
}
|
||||
|
||||
uint8_t* encodeHeader(MajorType type, uint64_t addlInfo, uint8_t* pos,
|
||||
const uint8_t* end) {
|
||||
size_t sz = headerSize(addlInfo);
|
||||
if (end - pos < static_cast<ssize_t>(sz)) return nullptr;
|
||||
switch (sz) {
|
||||
case 1:
|
||||
*pos++ = type | static_cast<uint8_t>(addlInfo);
|
||||
return pos;
|
||||
case 2:
|
||||
*pos++ = type | ONE_BYTE_LENGTH;
|
||||
*pos++ = static_cast<uint8_t>(addlInfo);
|
||||
return pos;
|
||||
case 3:
|
||||
*pos++ = type | TWO_BYTE_LENGTH;
|
||||
return writeBigEndian(static_cast<uint16_t>(addlInfo), pos);
|
||||
case 5:
|
||||
*pos++ = type | FOUR_BYTE_LENGTH;
|
||||
return writeBigEndian(static_cast<uint32_t>(addlInfo), pos);
|
||||
case 9:
|
||||
*pos++ = type | EIGHT_BYTE_LENGTH;
|
||||
return writeBigEndian(addlInfo, pos);
|
||||
default:
|
||||
CHECK(false); // Impossible to get here.
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void encodeHeader(MajorType type, uint64_t addlInfo,
|
||||
EncodeCallback encodeCallback) {
|
||||
size_t sz = headerSize(addlInfo);
|
||||
switch (sz) {
|
||||
case 1:
|
||||
encodeCallback(type | static_cast<uint8_t>(addlInfo));
|
||||
break;
|
||||
case 2:
|
||||
encodeCallback(type | ONE_BYTE_LENGTH);
|
||||
encodeCallback(static_cast<uint8_t>(addlInfo));
|
||||
break;
|
||||
case 3:
|
||||
encodeCallback(type | TWO_BYTE_LENGTH);
|
||||
writeBigEndian(static_cast<uint16_t>(addlInfo), encodeCallback);
|
||||
break;
|
||||
case 5:
|
||||
encodeCallback(type | FOUR_BYTE_LENGTH);
|
||||
writeBigEndian(static_cast<uint32_t>(addlInfo), encodeCallback);
|
||||
break;
|
||||
case 9:
|
||||
encodeCallback(type | EIGHT_BYTE_LENGTH);
|
||||
writeBigEndian(addlInfo, encodeCallback);
|
||||
break;
|
||||
default:
|
||||
CHECK(false); // Impossible to get here.
|
||||
}
|
||||
}
|
||||
|
||||
bool Item::operator==(const Item& other) const& {
|
||||
if (type() != other.type()) return false;
|
||||
switch (type()) {
|
||||
case UINT:
|
||||
return *asUint() == *(other.asUint());
|
||||
case NINT:
|
||||
return *asNint() == *(other.asNint());
|
||||
case BSTR:
|
||||
if (asBstr() != nullptr && other.asBstr() != nullptr) {
|
||||
return *asBstr() == *(other.asBstr());
|
||||
}
|
||||
// Interesting corner case: comparing a Bstr and ViewBstr with
|
||||
// identical contents. The function currently returns false for
|
||||
// this case.
|
||||
// TODO: if it should return true, this needs a deep comparison
|
||||
return false;
|
||||
case TSTR:
|
||||
if (asTstr() != nullptr && other.asTstr() != nullptr) {
|
||||
return *asTstr() == *(other.asTstr());
|
||||
}
|
||||
// Same corner case as Bstr
|
||||
return false;
|
||||
case ARRAY:
|
||||
return *asArray() == *(other.asArray());
|
||||
case MAP:
|
||||
return *asMap() == *(other.asMap());
|
||||
case SIMPLE:
|
||||
return *asSimple() == *(other.asSimple());
|
||||
case SEMANTIC:
|
||||
return *asSemanticTag() == *(other.asSemanticTag());
|
||||
default:
|
||||
CHECK(false); // Impossible to get here.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Nint::Nint(int64_t v) : mValue(v) { CHECK(v < 0); }
|
||||
|
||||
bool Simple::operator==(const Simple& other) const& {
|
||||
if (simpleType() != other.simpleType()) return false;
|
||||
|
||||
switch (simpleType()) {
|
||||
case BOOLEAN:
|
||||
return *asBool() == *(other.asBool());
|
||||
case NULL_T:
|
||||
return true;
|
||||
default:
|
||||
CHECK(false); // Impossible to get here.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t* Bstr::encode(uint8_t* pos, const uint8_t* end) const {
|
||||
pos = encodeHeader(mValue.size(), pos, end);
|
||||
if (!pos || end - pos < static_cast<ptrdiff_t>(mValue.size())) return nullptr;
|
||||
return std::copy(mValue.begin(), mValue.end(), pos);
|
||||
}
|
||||
|
||||
void Bstr::encodeValue(EncodeCallback encodeCallback) const {
|
||||
for (auto c : mValue) {
|
||||
encodeCallback(c);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t* Tstr::encode(uint8_t* pos, const uint8_t* end) const {
|
||||
pos = encodeHeader(mValue.size(), pos, end);
|
||||
if (!pos || end - pos < static_cast<ptrdiff_t>(mValue.size())) return nullptr;
|
||||
return std::copy(mValue.begin(), mValue.end(), pos);
|
||||
}
|
||||
|
||||
void Tstr::encodeValue(EncodeCallback encodeCallback) const {
|
||||
for (auto c : mValue) {
|
||||
encodeCallback(static_cast<uint8_t>(c));
|
||||
}
|
||||
}
|
||||
|
||||
bool Array::operator==(const Array& other) const& {
|
||||
return size() == other.size()
|
||||
// Can't use vector::operator== because the contents are pointers.
|
||||
// std::equal lets us provide a predicate that does the dereferencing.
|
||||
&& std::equal(mEntries.begin(), mEntries.end(), other.mEntries.begin(),
|
||||
[](const std::unique_ptr<cppbor::Item>& a,
|
||||
const std::unique_ptr<cppbor::Item>& b) -> bool {
|
||||
return *a == *b;
|
||||
});
|
||||
}
|
||||
|
||||
uint8_t* Array::encode(uint8_t* pos, const uint8_t* end) const {
|
||||
pos = encodeHeader(size(), pos, end);
|
||||
if (!pos) return nullptr;
|
||||
for (auto& entry : mEntries) {
|
||||
pos = entry->encode(pos, end);
|
||||
if (!pos) return nullptr;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
void Array::encode(EncodeCallback encodeCallback) const {
|
||||
encodeHeader(size(), encodeCallback);
|
||||
for (auto& entry : mEntries) {
|
||||
entry->encode(encodeCallback);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Item> Array::clone() const {
|
||||
auto res = make_unique<Array>();
|
||||
for (size_t i = 0; i < mEntries.size(); i++) {
|
||||
res->add(mEntries[i]->clone());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Map::operator==(const Map& other) const& {
|
||||
return size() == other.size()
|
||||
// Can't use vector::operator== because the contents are pairs of
|
||||
// pointers. std::equal lets us provide a predicate that does the
|
||||
// dereferencing.
|
||||
&& std::equal(begin(), end(), other.begin(),
|
||||
[](const entry_type& a, const entry_type& b) {
|
||||
return *a.first == *b.first && *a.second == *b.second;
|
||||
});
|
||||
}
|
||||
|
||||
uint8_t* Map::encode(uint8_t* pos, const uint8_t* end) const {
|
||||
pos = encodeHeader(size(), pos, end);
|
||||
if (!pos) return nullptr;
|
||||
for (auto& entry : mEntries) {
|
||||
pos = entry.first->encode(pos, end);
|
||||
if (!pos) return nullptr;
|
||||
pos = entry.second->encode(pos, end);
|
||||
if (!pos) return nullptr;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
void Map::encode(EncodeCallback encodeCallback) const {
|
||||
encodeHeader(size(), encodeCallback);
|
||||
for (auto& entry : mEntries) {
|
||||
entry.first->encode(encodeCallback);
|
||||
entry.second->encode(encodeCallback);
|
||||
}
|
||||
}
|
||||
|
||||
bool Map::keyLess(const Item* a, const Item* b) {
|
||||
// CBOR map canonicalization rules are:
|
||||
|
||||
// 1. If two keys have different lengths, the shorter one sorts earlier.
|
||||
if (a->encodedSize() < b->encodedSize()) return true;
|
||||
if (a->encodedSize() > b->encodedSize()) return false;
|
||||
|
||||
// 2. If two keys have the same length, the one with the lower value in
|
||||
// (byte-wise) lexical order sorts earlier. This requires encoding both
|
||||
// items.
|
||||
auto encodedA = a->encode();
|
||||
auto encodedB = b->encode();
|
||||
|
||||
return std::lexicographical_compare(encodedA.begin(), encodedA.end(), //
|
||||
encodedB.begin(), encodedB.end());
|
||||
}
|
||||
|
||||
void recursivelyCanonicalize(std::unique_ptr<Item>& item) {
|
||||
switch (item->type()) {
|
||||
case UINT:
|
||||
case NINT:
|
||||
case BSTR:
|
||||
case TSTR:
|
||||
case SIMPLE:
|
||||
return;
|
||||
|
||||
case ARRAY:
|
||||
std::for_each(item->asArray()->begin(), item->asArray()->end(),
|
||||
recursivelyCanonicalize);
|
||||
return;
|
||||
|
||||
case MAP:
|
||||
item->asMap()->canonicalize(true /* recurse */);
|
||||
return;
|
||||
|
||||
case SEMANTIC:
|
||||
// This can't happen. SemanticTags delegate their type() method to the
|
||||
// contained Item's type.
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Map& Map::canonicalize(bool recurse) & {
|
||||
if (recurse) {
|
||||
for (auto& entry : mEntries) {
|
||||
recursivelyCanonicalize(entry.first);
|
||||
recursivelyCanonicalize(entry.second);
|
||||
}
|
||||
}
|
||||
|
||||
if (size() < 2 || mCanonicalized) {
|
||||
// Trivially or already canonical; do nothing.
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::sort(begin(), end(), [](const entry_type& a, const entry_type& b) {
|
||||
return keyLess(a.first.get(), b.first.get());
|
||||
});
|
||||
mCanonicalized = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::unique_ptr<Item> Map::clone() const {
|
||||
auto res = make_unique<Map>();
|
||||
for (auto& entry : *this) {
|
||||
auto& key = entry.first;
|
||||
auto& value = entry.second;
|
||||
res->add(key->clone(), value->clone());
|
||||
}
|
||||
res->mCanonicalized = mCanonicalized;
|
||||
return res;
|
||||
}
|
||||
|
||||
std::unique_ptr<Item> SemanticTag::clone() const {
|
||||
return make_unique<SemanticTag>(mValue, mTaggedItem->clone());
|
||||
}
|
||||
|
||||
uint8_t* SemanticTag::encode(uint8_t* pos, const uint8_t* end) const {
|
||||
// Can't use the encodeHeader() method that calls type() to get the major
|
||||
// type, since that will return the tagged Item's type.
|
||||
pos = ::cppbor::encodeHeader(kMajorType, mValue, pos, end);
|
||||
if (!pos) return nullptr;
|
||||
return mTaggedItem->encode(pos, end);
|
||||
}
|
||||
|
||||
void SemanticTag::encode(EncodeCallback encodeCallback) const {
|
||||
// Can't use the encodeHeader() method that calls type() to get the major
|
||||
// type, since that will return the tagged Item's type.
|
||||
::cppbor::encodeHeader(kMajorType, mValue, encodeCallback);
|
||||
mTaggedItem->encode(std::move(encodeCallback));
|
||||
}
|
||||
|
||||
size_t SemanticTag::semanticTagCount() const {
|
||||
size_t levelCount = 1; // Count this level.
|
||||
const SemanticTag* cur = this;
|
||||
while (cur->mTaggedItem &&
|
||||
(cur = cur->mTaggedItem->asSemanticTag()) != nullptr)
|
||||
++levelCount;
|
||||
return levelCount;
|
||||
}
|
||||
|
||||
uint64_t SemanticTag::semanticTag(size_t nesting) const {
|
||||
// Getting the value of a specific nested tag is a bit tricky, because we
|
||||
// start with the outer tag and don't know how many are inside. We count the
|
||||
// number of nesting levels to find out how many there are in total, then to
|
||||
// get the one we want we have to walk down levelCount - nesting steps.
|
||||
size_t levelCount = semanticTagCount();
|
||||
if (nesting >= levelCount) return 0;
|
||||
|
||||
levelCount -= nesting;
|
||||
const SemanticTag* cur = this;
|
||||
while (--levelCount > 0) cur = cur->mTaggedItem->asSemanticTag();
|
||||
|
||||
return cur->mValue;
|
||||
}
|
||||
|
||||
string prettyPrint(const Item* item, size_t maxBStrSize,
|
||||
const vector<string>& mapKeysToNotPrint) {
|
||||
string out;
|
||||
prettyPrintInternal(item, out, 0, maxBStrSize, mapKeysToNotPrint);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace cppbor
|
||||
380
third_party/libcppbor/src/cppbor_parse.cpp
vendored
Normal file
380
third_party/libcppbor/src/cppbor_parse.cpp
vendored
Normal file
@@ -0,0 +1,380 @@
|
||||
/*
|
||||
* Copyright 2019 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "cppbor_parse.h"
|
||||
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <stack>
|
||||
|
||||
#ifndef __has_feature
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
|
||||
namespace cppbor {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string insufficientLengthString(size_t bytesNeeded, size_t bytesAvail,
|
||||
const std::string& type) {
|
||||
char buf[1024];
|
||||
snprintf(buf, sizeof(buf), "Need %zu byte(s) for %s, have %zu.", bytesNeeded, type.c_str(),
|
||||
bytesAvail);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
template <typename T, typename = std::enable_if_t<std::is_unsigned<T>::value>>
|
||||
std::tuple<bool, uint64_t, const uint8_t*> parseLength(const uint8_t* pos, const uint8_t* end,
|
||||
ParseClient* parseClient) {
|
||||
if (pos + sizeof(T) > end) {
|
||||
parseClient->error(pos - 1, insufficientLengthString(sizeof(T), end - pos, "length field"));
|
||||
return {false, 0, pos};
|
||||
}
|
||||
|
||||
const uint8_t* intEnd = pos + sizeof(T);
|
||||
T result = 0;
|
||||
do {
|
||||
result = static_cast<T>((result << 8) | *pos++);
|
||||
} while (pos < intEnd);
|
||||
return {true, result, pos};
|
||||
}
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end,
|
||||
bool emitViews, ParseClient* parseClient);
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> handleUint(uint64_t value, const uint8_t* hdrBegin,
|
||||
const uint8_t* hdrEnd,
|
||||
ParseClient* parseClient) {
|
||||
std::unique_ptr<Item> item = std::make_unique<Uint>(value);
|
||||
return {hdrEnd,
|
||||
parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)};
|
||||
}
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> handleNint(uint64_t value, const uint8_t* hdrBegin,
|
||||
const uint8_t* hdrEnd,
|
||||
ParseClient* parseClient) {
|
||||
if (value > std::numeric_limits<int64_t>::max()) {
|
||||
parseClient->error(hdrBegin, "NINT values that don't fit in int64_t are not supported.");
|
||||
return {hdrBegin, nullptr /* end parsing */};
|
||||
}
|
||||
std::unique_ptr<Item> item = std::make_unique<Nint>(-1 - static_cast<int64_t>(value));
|
||||
return {hdrEnd,
|
||||
parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)};
|
||||
}
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> handleBool(uint64_t value, const uint8_t* hdrBegin,
|
||||
const uint8_t* hdrEnd,
|
||||
ParseClient* parseClient) {
|
||||
std::unique_ptr<Item> item = std::make_unique<Bool>(value == TRUE);
|
||||
return {hdrEnd,
|
||||
parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)};
|
||||
}
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> handleNull(const uint8_t* hdrBegin, const uint8_t* hdrEnd,
|
||||
ParseClient* parseClient) {
|
||||
std::unique_ptr<Item> item = std::make_unique<Null>();
|
||||
return {hdrEnd,
|
||||
parseClient->item(item, hdrBegin, hdrEnd /* valueBegin */, hdrEnd /* itemEnd */)};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::tuple<const uint8_t*, ParseClient*> handleString(uint64_t length, const uint8_t* hdrBegin,
|
||||
const uint8_t* valueBegin, const uint8_t* end,
|
||||
const std::string& errLabel,
|
||||
ParseClient* parseClient) {
|
||||
ssize_t signed_length = static_cast<ssize_t>(length);
|
||||
if (end - valueBegin < signed_length || signed_length < 0) {
|
||||
parseClient->error(hdrBegin, insufficientLengthString(length, end - valueBegin, errLabel));
|
||||
return {hdrBegin, nullptr /* end parsing */};
|
||||
}
|
||||
|
||||
std::unique_ptr<Item> item = std::make_unique<T>(valueBegin, valueBegin + length);
|
||||
return {valueBegin + length,
|
||||
parseClient->item(item, hdrBegin, valueBegin, valueBegin + length)};
|
||||
}
|
||||
|
||||
class IncompleteItem {
|
||||
public:
|
||||
virtual ~IncompleteItem() {}
|
||||
virtual void add(std::unique_ptr<Item> item) = 0;
|
||||
};
|
||||
|
||||
class IncompleteArray : public Array, public IncompleteItem {
|
||||
public:
|
||||
explicit IncompleteArray(size_t size) : mSize(size) {}
|
||||
|
||||
// We return the "complete" size, rather than the actual size.
|
||||
size_t size() const override { return mSize; }
|
||||
|
||||
void add(std::unique_ptr<Item> item) override {
|
||||
mEntries.reserve(mSize);
|
||||
mEntries.push_back(std::move(item));
|
||||
}
|
||||
|
||||
private:
|
||||
size_t mSize;
|
||||
};
|
||||
|
||||
class IncompleteMap : public Map, public IncompleteItem {
|
||||
public:
|
||||
explicit IncompleteMap(size_t size) : mSize(size) {}
|
||||
|
||||
// We return the "complete" size, rather than the actual size.
|
||||
size_t size() const override { return mSize; }
|
||||
|
||||
void add(std::unique_ptr<Item> item) override {
|
||||
if (mKeyHeldForAdding) {
|
||||
mEntries.reserve(mSize);
|
||||
mEntries.push_back({std::move(mKeyHeldForAdding), std::move(item)});
|
||||
} else {
|
||||
mKeyHeldForAdding = std::move(item);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<Item> mKeyHeldForAdding;
|
||||
size_t mSize;
|
||||
};
|
||||
|
||||
class IncompleteSemanticTag : public SemanticTag, public IncompleteItem {
|
||||
public:
|
||||
explicit IncompleteSemanticTag(uint64_t value) : SemanticTag(value) {}
|
||||
|
||||
// We return the "complete" size, rather than the actual size.
|
||||
size_t size() const override { return 1; }
|
||||
|
||||
void add(std::unique_ptr<Item> item) override { mTaggedItem = std::move(item); }
|
||||
};
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> handleEntries(size_t entryCount, const uint8_t* hdrBegin,
|
||||
const uint8_t* pos, const uint8_t* end,
|
||||
const std::string& typeName,
|
||||
bool emitViews,
|
||||
ParseClient* parseClient) {
|
||||
while (entryCount > 0) {
|
||||
--entryCount;
|
||||
if (pos == end) {
|
||||
parseClient->error(hdrBegin, "Not enough entries for " + typeName + ".");
|
||||
return {hdrBegin, nullptr /* end parsing */};
|
||||
}
|
||||
std::tie(pos, parseClient) = parseRecursively(pos, end, emitViews, parseClient);
|
||||
if (!parseClient) return {hdrBegin, nullptr};
|
||||
}
|
||||
return {pos, parseClient};
|
||||
}
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> handleCompound(
|
||||
std::unique_ptr<Item> item, uint64_t entryCount, const uint8_t* hdrBegin,
|
||||
const uint8_t* valueBegin, const uint8_t* end, const std::string& typeName,
|
||||
bool emitViews, ParseClient* parseClient) {
|
||||
parseClient =
|
||||
parseClient->item(item, hdrBegin, valueBegin, valueBegin /* don't know the end yet */);
|
||||
if (!parseClient) return {hdrBegin, nullptr};
|
||||
|
||||
const uint8_t* pos;
|
||||
std::tie(pos, parseClient) =
|
||||
handleEntries(entryCount, hdrBegin, valueBegin, end, typeName, emitViews, parseClient);
|
||||
if (!parseClient) return {hdrBegin, nullptr};
|
||||
|
||||
return {pos, parseClient->itemEnd(item, hdrBegin, valueBegin, pos)};
|
||||
}
|
||||
|
||||
std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end,
|
||||
bool emitViews, ParseClient* parseClient) {
|
||||
if (begin == end) {
|
||||
parseClient->error(
|
||||
begin,
|
||||
"Input buffer is empty. Begin and end cannot point to the same location.");
|
||||
return {begin, nullptr};
|
||||
}
|
||||
|
||||
const uint8_t* pos = begin;
|
||||
|
||||
MajorType type = static_cast<MajorType>(*pos & 0xE0);
|
||||
uint8_t tagInt = *pos & 0x1F;
|
||||
++pos;
|
||||
|
||||
bool success = true;
|
||||
uint64_t addlData;
|
||||
if (tagInt < ONE_BYTE_LENGTH) {
|
||||
addlData = tagInt;
|
||||
} else if (tagInt > EIGHT_BYTE_LENGTH) {
|
||||
parseClient->error(
|
||||
begin,
|
||||
"Reserved additional information value or unsupported indefinite length item.");
|
||||
return {begin, nullptr};
|
||||
} else {
|
||||
switch (tagInt) {
|
||||
case ONE_BYTE_LENGTH:
|
||||
std::tie(success, addlData, pos) = parseLength<uint8_t>(pos, end, parseClient);
|
||||
break;
|
||||
|
||||
case TWO_BYTE_LENGTH:
|
||||
std::tie(success, addlData, pos) = parseLength<uint16_t>(pos, end, parseClient);
|
||||
break;
|
||||
|
||||
case FOUR_BYTE_LENGTH:
|
||||
std::tie(success, addlData, pos) = parseLength<uint32_t>(pos, end, parseClient);
|
||||
break;
|
||||
|
||||
case EIGHT_BYTE_LENGTH:
|
||||
std::tie(success, addlData, pos) = parseLength<uint64_t>(pos, end, parseClient);
|
||||
break;
|
||||
|
||||
default:
|
||||
// It's impossible to get here
|
||||
parseClient->error(begin, "Invalid tag.");
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) return {begin, nullptr};
|
||||
|
||||
switch (type) {
|
||||
case UINT:
|
||||
return handleUint(addlData, begin, pos, parseClient);
|
||||
|
||||
case NINT:
|
||||
return handleNint(addlData, begin, pos, parseClient);
|
||||
|
||||
case BSTR:
|
||||
return handleString<Bstr>(addlData, begin, pos, end, "byte string", parseClient);
|
||||
|
||||
case TSTR:
|
||||
return handleString<Tstr>(addlData, begin, pos, end, "text string", parseClient);
|
||||
|
||||
case ARRAY:
|
||||
return handleCompound(std::make_unique<IncompleteArray>(addlData), addlData, begin, pos,
|
||||
end, "array", emitViews, parseClient);
|
||||
|
||||
case MAP:
|
||||
return handleCompound(std::make_unique<IncompleteMap>(addlData), addlData * 2, begin,
|
||||
pos, end, "map", emitViews, parseClient);
|
||||
|
||||
case SEMANTIC:
|
||||
return handleCompound(std::make_unique<IncompleteSemanticTag>(addlData), 1, begin, pos,
|
||||
end, "semantic", emitViews, parseClient);
|
||||
|
||||
case SIMPLE:
|
||||
switch (addlData) {
|
||||
case TRUE:
|
||||
case FALSE:
|
||||
return handleBool(addlData, begin, pos, parseClient);
|
||||
case NULL_V:
|
||||
return handleNull(begin, pos, parseClient);
|
||||
default:
|
||||
parseClient->error(begin, "Unsupported floating-point or simple value.");
|
||||
return {begin, nullptr};
|
||||
}
|
||||
}
|
||||
// Impossible to get here.
|
||||
parseClient->error(begin, "Invalid type.");
|
||||
return {};
|
||||
}
|
||||
|
||||
class FullParseClient : public ParseClient {
|
||||
public:
|
||||
virtual ParseClient* item(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
|
||||
const uint8_t* end) override {
|
||||
if (mParentStack.empty() && !item->isCompound()) {
|
||||
// This is the first and only item.
|
||||
mTheItem = std::move(item);
|
||||
mPosition = end;
|
||||
return nullptr; // We're done.
|
||||
}
|
||||
|
||||
if (item->isCompound()) {
|
||||
// Starting a new compound data item, i.e. a new parent. Save it on the parent stack.
|
||||
// It's safe to save a raw pointer because the unique_ptr is guaranteed to stay in
|
||||
// existence until the corresponding itemEnd() call.
|
||||
mParentStack.push(item.get());
|
||||
return this;
|
||||
} else {
|
||||
appendToLastParent(std::move(item));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
virtual ParseClient* itemEnd(std::unique_ptr<Item>& item, const uint8_t*, const uint8_t*,
|
||||
const uint8_t* end) override {
|
||||
if (!item->isCompound() || item.get() != mParentStack.top()) {
|
||||
return nullptr;
|
||||
}
|
||||
mParentStack.pop();
|
||||
|
||||
if (mParentStack.empty()) {
|
||||
mTheItem = std::move(item);
|
||||
mPosition = end;
|
||||
return nullptr; // We're done
|
||||
} else {
|
||||
appendToLastParent(std::move(item));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void error(const uint8_t* position, const std::string& errorMessage) override {
|
||||
mPosition = position;
|
||||
mErrorMessage = errorMessage;
|
||||
}
|
||||
|
||||
std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
|
||||
std::string /* errMsg */>
|
||||
parseResult() {
|
||||
std::unique_ptr<Item> p = std::move(mTheItem);
|
||||
return {std::move(p), mPosition, std::move(mErrorMessage)};
|
||||
}
|
||||
|
||||
private:
|
||||
void appendToLastParent(std::unique_ptr<Item> item) {
|
||||
auto parent = mParentStack.top();
|
||||
#if __has_feature(cxx_rtti)
|
||||
assert(dynamic_cast<IncompleteItem*>(parent));
|
||||
#endif
|
||||
|
||||
IncompleteItem* parentItem{};
|
||||
if (parent->type() == ARRAY) {
|
||||
parentItem = static_cast<IncompleteArray*>(parent);
|
||||
} else if (parent->type() == MAP) {
|
||||
parentItem = static_cast<IncompleteMap*>(parent);
|
||||
} else if (parent->asSemanticTag()) {
|
||||
parentItem = static_cast<IncompleteSemanticTag*>(parent);
|
||||
} else {
|
||||
// Impossible to get here.
|
||||
}
|
||||
parentItem->add(std::move(item));
|
||||
}
|
||||
|
||||
std::unique_ptr<Item> mTheItem;
|
||||
std::stack<Item*> mParentStack;
|
||||
const uint8_t* mPosition = nullptr;
|
||||
std::string mErrorMessage;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void parse(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) {
|
||||
parseRecursively(begin, end, false, parseClient);
|
||||
}
|
||||
|
||||
std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
|
||||
std::string /* errMsg */>
|
||||
parse(const uint8_t* begin, const uint8_t* end) {
|
||||
FullParseClient parseClient;
|
||||
parse(begin, end, &parseClient);
|
||||
return parseClient.parseResult();
|
||||
}
|
||||
|
||||
} // namespace cppbor
|
||||
Reference in New Issue
Block a user