Source release 19.6.0

GitOrigin-RevId: 13a33e34413c19da1bfe76abcc66be519c9ac9d1
This commit is contained in:
Googler
2025-05-30 14:47:25 -07:00
committed by mattfedd
parent f7ec4fdeff
commit 6d36a0c93d
59 changed files with 3327 additions and 1491 deletions

View File

@@ -1,12 +1,17 @@
LibCppBor: A Modern C++ CBOR Parser and Generator
==============================================
TODO(b/254108623):
This is a modified version of LibCppBor and is C++-14 compliant. The released
version can be found at
https://android.googlesource.com/platform/external/libcppbor, which requires
C++-17. This is a reminder of refreshing the library with the latest source
above once we officially move to C++-17.
This is a modified version of LibCppBor, based on commit
`61d9bff9605ad2ffd877bd99a3bde414e21f01a2` from the upstream source at
https://android.googlesource.com/platform/external/libcppbor.
It's worth noting that while the latest LibCppBor release requires C++20,
CE CDM currently operates on C++17. This serves as a reminder to refresh our
local copy of the library with the latest upstream source it officially
transitions to C++20.
Below is copied from the latest README.md of LibCppBor.
==============================================
LibCppBor provides a natural and easy-to-use syntax for constructing and
parsing CBOR messages. It does not (yet) support all features of

File diff suppressed because it is too large Load Diff

View File

@@ -36,6 +36,24 @@ using ParseResult = std::tuple<std::unique_ptr<Item> /* result */, const uint8_t
*/
ParseResult parse(const uint8_t* begin, const uint8_t* end);
/**
* Parse the first CBOR data item (possibly compound) from the range [begin, end).
*
* Returns a tuple of Item pointer, buffer pointer and error message. If parsing is successful, the
* Item pointer is non-null, the buffer pointer points to the first byte after the
* successfully-parsed item and the error message string is empty. If parsing fails, the Item
* pointer is null, the buffer pointer points to the first byte that was unparseable (the first byte
* of a data item header that is malformed in some way, e.g. an invalid value, or a length that is
* too large for the remaining buffer, etc.) and the string contains an error message describing the
* problem encountered.
*
* The returned CBOR data item will contain View* items backed by
* std::string_view types over the input range.
* WARNING! If the input range changes underneath, the corresponding views will
* carry the same change.
*/
ParseResult parseWithViews(const uint8_t* begin, const uint8_t* end);
/**
* Parse the first CBOR data item (possibly compound) from the byte vector.
*
@@ -66,6 +84,26 @@ inline ParseResult parse(const uint8_t* begin, size_t size) {
return parse(begin, begin + size);
}
/**
* Parse the first CBOR data item (possibly compound) from the range [begin, begin + size).
*
* Returns a tuple of Item pointer, buffer pointer and error message. If parsing is successful, the
* Item pointer is non-null, the buffer pointer points to the first byte after the
* successfully-parsed item and the error message string is empty. If parsing fails, the Item
* pointer is null, the buffer pointer points to the first byte that was unparseable (the first byte
* of a data item header that is malformed in some way, e.g. an invalid value, or a length that is
* too large for the remaining buffer, etc.) and the string contains an error message describing the
* problem encountered.
*
* The returned CBOR data item will contain View* items backed by
* std::string_view types over the input range.
* WARNING! If the input range changes underneath, the corresponding views will
* carry the same change.
*/
inline ParseResult parseWithViews(const uint8_t* begin, size_t size) {
return parseWithViews(begin, begin + size);
}
/**
* Parse the first CBOR data item (possibly compound) from the value contained in a Bstr.
*
@@ -91,6 +129,13 @@ class ParseClient;
*/
void parse(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient);
/**
* Parse the CBOR data in the range [begin, end) in streaming fashion, calling methods on the
* provided ParseClient when elements are found. Uses the View* item types
* instead of the copying ones.
*/
void parseWithViews(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient);
/**
* Parse the CBOR data in the vector in streaming fashion, calling methods on the
* provided ParseClient when elements are found.

View File

@@ -17,9 +17,12 @@
#include "cppbor.h"
#include <inttypes.h>
#include <cstdint>
#include <limits>
#include "cppbor_parse.h"
using std::string;
using std::vector;
@@ -29,539 +32,570 @@ namespace cppbor {
namespace {
template <typename T, typename Iterator,
typename = std::enable_if<std::is_unsigned<T>::value>>
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;
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);
}
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;
}
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& [keyEntry, valueEntry] : *map) {
switch (keyEntry->type()) {
case ARRAY:
case MAP:
return false;
default:
break;
}
switch (valueEntry->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;
return true;
}
bool prettyPrintInternal(const Item* item, string& out, size_t indent,
size_t maxBStrSize,
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) {
if (!item) {
out.append("<NULL>");
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]);
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);
}
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;
switch (item->type()) {
case SEMANTIC:
// Handled above.
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 UINT:
snprintf(buf, sizeof(buf), "%" PRIu64, item->asUint()->unsignedValue());
out.append(buf);
break;
case MAP: {
const Map* map = item->asMap();
case NINT:
snprintf(buf, sizeof(buf), "%" PRId64, item->asNint()->value());
out.append(buf);
break;
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;
case BSTR: {
const uint8_t* valueData;
size_t valueSize;
const Bstr* bstr = item->asBstr();
if (bstr != nullptr) {
const vector<uint8_t>& value = bstr->value();
valueData = value.data();
valueSize = value.size();
} else {
const ViewBstr* viewBstr = item->asViewBstr();
assert(viewBstr != nullptr);
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;
valueData = viewBstr->view().data();
valueSize = viewBstr->view().size();
}
}
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;
}
if (valueSize > maxBStrSize) {
snprintf(buf, sizeof(buf), "<bstr size=%zd>", valueSize);
out.append(buf);
} else {
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;
return true;
case TSTR:
out.append("'");
{
// TODO: escape "'" characters
if (item->asTstr() != nullptr) {
out.append(item->asTstr()->value().c_str());
} else {
const ViewTstr* viewTstr = item->asViewTstr();
assert(viewTstr != nullptr);
out.append(viewTstr->view());
}
}
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& [map_key, map_value] : *map) {
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;
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;
}
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.
}
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;
}
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());
}
if (asViewBstr() != nullptr && other.asViewBstr() != nullptr) {
return *asViewBstr() == *(other.asViewBstr());
}
// 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());
}
if (asViewTstr() != nullptr && other.asViewTstr() != nullptr) {
return *asViewTstr() == *(other.asViewTstr());
}
// 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); }
Nint::Nint(int64_t v) : mValue(v) {
CHECK(v < 0);
}
bool Simple::operator==(const Simple& other) const& {
if (simpleType() != other.simpleType()) return false;
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;
}
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);
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);
}
for (auto c : mValue) {
encodeCallback(c);
}
}
uint8_t* ViewBstr::encode(uint8_t* pos, const uint8_t* end) const {
pos = encodeHeader(mView.size(), pos, end);
if (!pos || end - pos < static_cast<ptrdiff_t>(mView.size())) return nullptr;
return std::copy(mView.begin(), mView.end(), pos);
}
void ViewBstr::encodeValue(EncodeCallback encodeCallback) const {
for (auto c : mView) {
encodeCallback(static_cast<uint8_t>(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);
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));
}
for (auto c : mValue) {
encodeCallback(static_cast<uint8_t>(c));
}
}
uint8_t* ViewTstr::encode(uint8_t* pos, const uint8_t* end) const {
pos = encodeHeader(mView.size(), pos, end);
if (!pos || end - pos < static_cast<ptrdiff_t>(mView.size())) return nullptr;
return std::copy(mView.begin(), mView.end(), pos);
}
void ViewTstr::encodeValue(EncodeCallback encodeCallback) const {
for (auto c : mView) {
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;
});
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(),
[](auto& a, auto& 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);
pos = encodeHeader(size(), pos, end);
if (!pos) return nullptr;
}
return pos;
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);
}
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;
auto res = std::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;
});
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(), [](auto& a, auto& 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);
pos = encodeHeader(size(), pos, end);
if (!pos) return nullptr;
pos = entry.second->encode(pos, end);
if (!pos) return nullptr;
}
return pos;
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);
}
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:
// 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;
// 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();
// 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());
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;
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 ARRAY:
std::for_each(item->asArray()->begin(), item->asArray()->end(),
recursivelyCanonicalize);
return;
case MAP:
item->asMap()->canonicalize(true /* recurse */);
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;
}
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 (recurse) {
for (auto& entry : mEntries) {
recursivelyCanonicalize(entry.first);
recursivelyCanonicalize(entry.second);
}
}
}
if (size() < 2 || mCanonicalized) {
// Trivially or already canonical; do nothing.
if (size() < 2 || mCanonicalized) {
// Trivially or already canonical; do nothing.
return *this;
}
std::sort(begin(), end(),
[](auto& a, auto& b) { return keyLess(a.first.get(), b.first.get()); });
mCanonicalized = true;
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;
auto res = std::make_unique<Map>();
for (auto& [key, value] : *this) {
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());
return std::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);
// 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));
// 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(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;
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;
// 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();
levelCount -= nesting;
const SemanticTag* cur = this;
while (--levelCount > 0) cur = cur->mTaggedItem->asSemanticTag();
return cur->mValue;
return cur->mValue;
}
string prettyPrint(const Item* item, size_t maxBStrSize,
string prettyPrint(const Item* item, size_t maxBStrSize, const vector<string>& mapKeysToNotPrint) {
string out;
prettyPrintInternal(item, out, 0, maxBStrSize, mapKeysToNotPrint);
return out;
}
string prettyPrint(const vector<uint8_t>& encodedCbor, size_t maxBStrSize,
const vector<string>& mapKeysToNotPrint) {
string out;
prettyPrintInternal(item, out, 0, maxBStrSize, mapKeysToNotPrint);
return out;
auto [item, _, message] = parse(encodedCbor);
if (item == nullptr) {
return "";
}
return prettyPrint(item.get(), maxBStrSize, mapKeysToNotPrint);
}
} // namespace cppbor

View File

@@ -19,10 +19,12 @@
#include <limits>
#include <sstream>
#include <stack>
#include <tuple>
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#define CHECK(x) (void)(x)
namespace cppbor {
@@ -36,7 +38,7 @@ std::string insufficientLengthString(size_t bytesNeeded, size_t bytesAvail,
return std::string(buf);
}
template <typename T, typename = std::enable_if_t<std::is_unsigned<T>::value>>
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
std::tuple<bool, uint64_t, const uint8_t*> parseLength(const uint8_t* pos, const uint8_t* end,
ParseClient* parseClient) {
if (pos + sizeof(T) > end) {
@@ -235,9 +237,8 @@ std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin,
break;
default:
// It's impossible to get here
parseClient->error(begin, "Invalid tag.");
return {};
CHECK(false); // It's impossible to get here
break;
}
}
@@ -251,10 +252,18 @@ std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin,
return handleNint(addlData, begin, pos, parseClient);
case BSTR:
return handleString<Bstr>(addlData, begin, pos, end, "byte string", parseClient);
if (emitViews) {
return handleString<ViewBstr>(addlData, begin, pos, end, "byte string", parseClient);
} else {
return handleString<Bstr>(addlData, begin, pos, end, "byte string", parseClient);
}
case TSTR:
return handleString<Tstr>(addlData, begin, pos, end, "text string", parseClient);
if (emitViews) {
return handleString<ViewTstr>(addlData, begin, pos, end, "text string", parseClient);
} else {
return handleString<Tstr>(addlData, begin, pos, end, "text string", parseClient);
}
case ARRAY:
return handleCompound(std::make_unique<IncompleteArray>(addlData), addlData, begin, pos,
@@ -280,8 +289,7 @@ std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin,
return {begin, nullptr};
}
}
// Impossible to get here.
parseClient->error(begin, "Invalid type.");
CHECK(false); // Impossible to get here.
return {};
}
@@ -310,9 +318,7 @@ class FullParseClient : public ParseClient {
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;
}
CHECK(item->isCompound() && item.get() == mParentStack.top());
mParentStack.pop();
if (mParentStack.empty()) {
@@ -340,10 +346,10 @@ class FullParseClient : public ParseClient {
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);
@@ -352,7 +358,7 @@ class FullParseClient : public ParseClient {
} else if (parent->asSemanticTag()) {
parentItem = static_cast<IncompleteSemanticTag*>(parent);
} else {
// Impossible to get here.
CHECK(false); // Impossible to get here.
}
parentItem->add(std::move(item));
}
@@ -377,4 +383,16 @@ parse(const uint8_t* begin, const uint8_t* end) {
return parseClient.parseResult();
}
void parseWithViews(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient) {
parseRecursively(begin, end, true, parseClient);
}
std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
std::string /* errMsg */>
parseWithViews(const uint8_t* begin, const uint8_t* end) {
FullParseClient parseClient;
parseWithViews(begin, end, &parseClient);
return parseClient.parseResult();
}
} // namespace cppbor

View File

@@ -152,6 +152,31 @@ TEST(SimpleValueTest, NestedSemanticTagEncoding) {
tripleTagged.toString());
}
TEST(SimpleValueTest, ViewByteStringEncodings) {
EXPECT_EQ("\x40", ViewBstr("").toString());
EXPECT_EQ("\x41\x61", ViewBstr("a").toString());
EXPECT_EQ("\x41\x41", ViewBstr("A").toString());
EXPECT_EQ("\x44\x49\x45\x54\x46", ViewBstr("IETF").toString());
EXPECT_EQ("\x42\x22\x5c", ViewBstr("\"\\").toString());
EXPECT_EQ("\x42\xc3\xbc", ViewBstr("\xc3\xbc").toString());
EXPECT_EQ("\x43\xe6\xb0\xb4", ViewBstr("\xe6\xb0\xb4").toString());
EXPECT_EQ("\x44\xf0\x90\x85\x91", ViewBstr("\xf0\x90\x85\x91").toString());
EXPECT_EQ("\x44\x01\x02\x03\x04", ViewBstr("\x01\x02\x03\x04").toString());
EXPECT_EQ("\x44\x40\x40\x40\x40", ViewBstr("@@@@").toString());
}
TEST(SimpleValueTest, ViewTextStringEncodings) {
EXPECT_EQ("\x60"s, ViewTstr("").toString());
EXPECT_EQ("\x61\x61"s, ViewTstr("a").toString());
EXPECT_EQ("\x61\x41"s, ViewTstr("A").toString());
EXPECT_EQ("\x64\x49\x45\x54\x46"s, ViewTstr("IETF").toString());
EXPECT_EQ("\x62\x22\x5c"s, ViewTstr("\"\\").toString());
EXPECT_EQ("\x62\xc3\xbc"s, ViewTstr("\xc3\xbc").toString());
EXPECT_EQ("\x63\xe6\xb0\xb4"s, ViewTstr("\xe6\xb0\xb4").toString());
EXPECT_EQ("\x64\xf0\x90\x85\x91"s, ViewTstr("\xf0\x90\x85\x91").toString());
EXPECT_EQ("\x64\x01\x02\x03\x04"s, ViewTstr("\x01\x02\x03\x04").toString());
}
TEST(IsIteratorPairOverTest, All) {
EXPECT_TRUE((
details::is_iterator_pair_over<pair<string::iterator, string::iterator>, char>::value));
@@ -230,6 +255,13 @@ TEST(MakeEntryTest, StdStrings) {
details::makeItem(std::move(s1))->toString()); // move string
}
TEST(MakeEntryTest, StdStringViews) {
string_view s1("hello");
const string_view s2("hello");
EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s1)->toString());
EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s2)->toString());
}
TEST(MakeEntryTest, CStrings) {
char s1[] = "hello";
const char s2[] = "hello";
@@ -496,6 +528,8 @@ TEST(EqualityTest, Uint) {
EXPECT_NE(val, Bool(false));
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("99"));
}
TEST(EqualityTest, Nint) {
@@ -509,6 +543,8 @@ TEST(EqualityTest, Nint) {
EXPECT_NE(val, Bool(false));
EXPECT_NE(val, Array(99));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("99"));
}
TEST(EqualityTest, Tstr) {
@@ -523,6 +559,8 @@ TEST(EqualityTest, Tstr) {
EXPECT_NE(val, Bool(false));
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("99"));
}
TEST(EqualityTest, Bstr) {
@@ -537,6 +575,8 @@ TEST(EqualityTest, Bstr) {
EXPECT_NE(val, Bool(false));
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("99"));
}
TEST(EqualityTest, Bool) {
@@ -551,6 +591,8 @@ TEST(EqualityTest, Bool) {
EXPECT_NE(val, Bool(true));
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("98"));
}
TEST(EqualityTest, Array) {
@@ -567,6 +609,8 @@ TEST(EqualityTest, Array) {
EXPECT_NE(val, Array(98, 1));
EXPECT_NE(val, Array(99, 1, 2));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("98"));
}
TEST(EqualityTest, Map) {
@@ -582,6 +626,8 @@ TEST(EqualityTest, Map) {
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 2));
EXPECT_NE(val, Map(99, 1, 99, 2));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("98"));
}
TEST(EqualityTest, Null) {
@@ -597,6 +643,8 @@ TEST(EqualityTest, Null) {
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 2));
EXPECT_NE(val, Map(99, 1, 99, 2));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("98"));
}
TEST(EqualityTest, SemanticTag) {
@@ -627,6 +675,40 @@ TEST(EqualityTest, NestedSemanticTag) {
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 2));
EXPECT_NE(val, Null());
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("98"));
}
TEST(EqualityTest, ViewTstr) {
ViewTstr val("99");
EXPECT_EQ(val, ViewTstr("99"));
EXPECT_NE(val, Uint(99));
EXPECT_NE(val, Nint(-1));
EXPECT_NE(val, Nint(-4));
EXPECT_NE(val, Tstr("99"));
EXPECT_NE(val, Bstr("99"));
EXPECT_NE(val, Bool(false));
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("98"));
EXPECT_NE(val, ViewBstr("99"));
}
TEST(EqualityTest, ViewBstr) {
ViewBstr val("99");
EXPECT_EQ(val, ViewBstr("99"));
EXPECT_NE(val, Uint(99));
EXPECT_NE(val, Nint(-1));
EXPECT_NE(val, Nint(-4));
EXPECT_NE(val, Tstr("99"));
EXPECT_NE(val, Bstr("99"));
EXPECT_NE(val, Bool(false));
EXPECT_NE(val, Array(99, 1));
EXPECT_NE(val, Map(99, 1));
EXPECT_NE(val, ViewTstr("99"));
EXPECT_NE(val, ViewBstr("98"));
}
TEST(ConvertTest, Uint) {
@@ -641,6 +723,8 @@ TEST(ConvertTest, Uint) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ(10, item->asInt()->value());
EXPECT_EQ(10, item->asUint()->value());
@@ -658,6 +742,8 @@ TEST(ConvertTest, Nint) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ(-10, item->asInt()->value());
EXPECT_EQ(-10, item->asNint()->value());
@@ -675,6 +761,8 @@ TEST(ConvertTest, Tstr) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ("hello"s, item->asTstr()->value());
}
@@ -692,6 +780,8 @@ TEST(ConvertTest, Bstr) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ(vec, item->asBstr()->value());
}
@@ -708,6 +798,8 @@ TEST(ConvertTest, Bool) {
EXPECT_NE(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ(cppbor::BOOLEAN, item->asSimple()->simpleType());
EXPECT_NE(nullptr, item->asSimple()->asBool());
@@ -728,6 +820,8 @@ TEST(ConvertTest, Map) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_NE(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ(0U, item->asMap()->size());
}
@@ -744,6 +838,8 @@ TEST(ConvertTest, Array) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_NE(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ(0U, item->asArray()->size());
}
@@ -759,6 +855,8 @@ TEST(ConvertTest, SemanticTag) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
// Both asTstr() (the contained type) and asSemanticTag() return non-null.
EXPECT_NE(nullptr, item->asTstr());
@@ -785,6 +883,8 @@ TEST(ConvertTest, NestedSemanticTag) {
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
// Both asTstr() (the contained type) and asSemanticTag() return non-null.
EXPECT_NE(nullptr, item->asTstr());
@@ -814,12 +914,52 @@ TEST(ConvertTest, Null) {
EXPECT_NE(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ(NULL_T, item->asSimple()->simpleType());
EXPECT_EQ(nullptr, item->asSimple()->asBool());
EXPECT_NE(nullptr, item->asSimple()->asNull());
}
TEST(ConvertTest, ViewTstr) {
unique_ptr<Item> item = details::makeItem(ViewTstr("hello"));
EXPECT_EQ(TSTR, item->type());
EXPECT_EQ(nullptr, item->asInt());
EXPECT_EQ(nullptr, item->asUint());
EXPECT_EQ(nullptr, item->asNint());
EXPECT_EQ(nullptr, item->asTstr());
EXPECT_EQ(nullptr, item->asBstr());
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_NE(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
EXPECT_EQ("hello"sv, item->asViewTstr()->view());
}
TEST(ConvertTest, ViewBstr) {
array<uint8_t, 3> vec{0x23, 0x24, 0x22};
basic_string_view<uint8_t> sv(vec.data(), vec.size());
unique_ptr<Item> item = details::makeItem(ViewBstr(sv));
EXPECT_EQ(BSTR, item->type());
EXPECT_EQ(nullptr, item->asInt());
EXPECT_EQ(nullptr, item->asUint());
EXPECT_EQ(nullptr, item->asNint());
EXPECT_EQ(nullptr, item->asTstr());
EXPECT_EQ(nullptr, item->asBstr());
EXPECT_EQ(nullptr, item->asSimple());
EXPECT_EQ(nullptr, item->asMap());
EXPECT_EQ(nullptr, item->asArray());
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_NE(nullptr, item->asViewBstr());
EXPECT_EQ(sv, item->asViewBstr()->view());
}
TEST(CloningTest, Uint) {
Uint item(10);
auto clone = item.clone();
@@ -928,6 +1068,26 @@ TEST(CloningTest, NestedSemanticTag) {
EXPECT_EQ(*clone->asSemanticTag(), copy);
}
TEST(CloningTest, ViewTstr) {
ViewTstr item("qwertyasdfgh");
auto clone = item.clone();
EXPECT_EQ(clone->type(), TSTR);
EXPECT_NE(clone->asViewTstr(), nullptr);
EXPECT_EQ(item, *clone->asViewTstr());
EXPECT_EQ(*clone->asViewTstr(), ViewTstr("qwertyasdfgh"));
}
TEST(CloningTest, ViewBstr) {
array<uint8_t, 5> vec{1, 2, 3, 255, 0};
basic_string_view<uint8_t> sv(vec.data(), vec.size());
ViewBstr item(sv);
auto clone = item.clone();
EXPECT_EQ(clone->type(), BSTR);
EXPECT_NE(clone->asViewBstr(), nullptr);
EXPECT_EQ(item, *clone->asViewBstr());
EXPECT_EQ(*clone->asViewBstr(), ViewBstr(sv));
}
TEST(PrettyPrintingTest, NestedSemanticTag) {
SemanticTag item(20, //
SemanticTag(30, //
@@ -1354,6 +1514,36 @@ TEST(StreamParseTest, Map) {
parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
}
TEST(StreamParseTest, ViewTstr) {
MockParseClient mpc;
ViewTstr val("Hello");
auto encoded = val.encode();
uint8_t* encBegin = encoded.data();
uint8_t* encEnd = encoded.data() + encoded.size();
EXPECT_CALL(mpc, item(MatchesItem(val), encBegin, encBegin + 1, encEnd)).WillOnce(Return(&mpc));
EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
EXPECT_CALL(mpc, error(_, _)).Times(0);
parseWithViews(encoded.data(), encoded.data() + encoded.size(), &mpc);
}
TEST(StreamParseTest, ViewBstr) {
MockParseClient mpc;
ViewBstr val("Hello");
auto encoded = val.encode();
uint8_t* encBegin = encoded.data();
uint8_t* encEnd = encoded.data() + encoded.size();
EXPECT_CALL(mpc, item(MatchesItem(val), encBegin, encBegin + 1, encEnd)).WillOnce(Return(&mpc));
EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
EXPECT_CALL(mpc, error(_, _)).Times(0);
parseWithViews(encoded.data(), encoded.data() + encoded.size(), &mpc);
}
TEST(FullParserTest, Uint) {
Uint val(10);
@@ -1506,6 +1696,25 @@ TEST(FullParserTest, MapWithTruncatedEntry) {
EXPECT_EQ("Need 4 byte(s) for length field, have 3.", message);
}
TEST(FullParserTest, ViewTstr) {
ViewTstr val("Hello");
auto enc = val.encode();
auto [item, pos, message] = parseWithViews(enc.data(), enc.size());
EXPECT_THAT(item, MatchesItem(val));
}
TEST(FullParserTest, ViewBstr) {
const std::string strVal = "\x00\x01\x02"s;
const ViewBstr val(strVal);
EXPECT_EQ(val.toString(), "\x43\x00\x01\x02"s);
auto enc = val.encode();
auto [item, pos, message] = parseWithViews(enc.data(), enc.size());
EXPECT_THAT(item, MatchesItem(val));
EXPECT_EQ(hexDump(item->toString()), hexDump(val.toString()));
}
TEST(FullParserTest, ReservedAdditionalInformation) {
vector<uint8_t> reservedVal = {0x1D};