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

@@ -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