diff options
author | Shawn Willden <swillden@google.com> | 2020-11-25 15:46:34 -0700 |
---|---|---|
committer | Shawn Willden <swillden@google.com> | 2020-12-14 10:02:02 -0700 |
commit | 315d859ec47ec349348cc400973ab4d5f5973332 (patch) | |
tree | 57e62d291329e2b3fea02b07897a98e6a0a3e9b3 /src/cppbor_parse.cpp | |
parent | f79067125cdf2977226a264180fa193895df0ef6 (diff) | |
download | libcppbor-315d859ec47ec349348cc400973ab4d5f5973332.tar.gz |
Change semantic tagging.
Semantic tagging in libcppbor was a bit cumbersome to use, with tags
treated as separate items, requiring code that analyzes tagged data to
pay attention to the tags. Among other issues, that violates the
intention of semantic tagging in CBOR, which, per the RFC, does not
require decoders to understand tags. This CL changes that behavior so
that code that walks a parsed Item tree will not "see" the tags unless
it looks for them, by calling "Item::semanticTagCount()" and then
"Item::semanticTag()". Nested tags are supported.
Test: cppbor_test_external
Change-Id: Ifa99475fd0d9f369f3e379251979446a2ec262b5
Diffstat (limited to 'src/cppbor_parse.cpp')
-rw-r--r-- | src/cppbor_parse.cpp | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/src/cppbor_parse.cpp b/src/cppbor_parse.cpp index 2736b71..42d74fb 100644 --- a/src/cppbor_parse.cpp +++ b/src/cppbor_parse.cpp @@ -149,17 +149,14 @@ class IncompleteMap : public Map, public IncompleteItem { size_t mSize; }; -class IncompleteSemantic : public Semantic, public IncompleteItem { +class IncompleteSemanticTag : public SemanticTag, public IncompleteItem { public: - explicit IncompleteSemantic(uint64_t value) : Semantic(value) {} + 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 { - mEntries.reserve(1); - mEntries.push_back(std::move(item)); - } + 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, @@ -254,7 +251,7 @@ std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, pos, end, "map", parseClient); case SEMANTIC: - return handleCompound(std::make_unique<IncompleteSemantic>(addlData), 1, begin, pos, + return handleCompound(std::make_unique<IncompleteSemanticTag>(addlData), 1, begin, pos, end, "semantic", parseClient); case SIMPLE: @@ -326,15 +323,18 @@ class FullParseClient : public ParseClient { #if __has_feature(cxx_rtti) assert(dynamic_cast<IncompleteItem*>(parent)); #endif + + IncompleteItem* parentItem{}; if (parent->type() == ARRAY) { - static_cast<IncompleteArray*>(parent)->add(std::move(item)); + parentItem = static_cast<IncompleteArray*>(parent); } else if (parent->type() == MAP) { - static_cast<IncompleteMap*>(parent)->add(std::move(item)); - } else if (parent->type() == SEMANTIC) { - static_cast<IncompleteSemantic*>(parent)->add(std::move(item)); + parentItem = static_cast<IncompleteMap*>(parent); + } else if (parent->asSemanticTag()) { + parentItem = static_cast<IncompleteSemanticTag*>(parent); } else { CHECK(false); // Impossible to get here. } + parentItem->add(std::move(item)); } std::unique_ptr<Item> mTheItem; |