ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
additional-description.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2025 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
25
26namespace ndn::security {
27
28constexpr size_t KEY_OFFSET = 0;
29constexpr size_t VALUE_OFFSET = 1;
30
35
36const std::string&
37AdditionalDescription::get(const std::string& key) const
38{
39 if (auto it = m_info.find(key); it != m_info.end()) {
40 return it->second;
41 }
42
43 NDN_THROW(Error("Entry does not exist for key '" + key + "'"));
44}
45
46void
47AdditionalDescription::set(const std::string& key, const std::string& value)
48{
49 m_info[key] = value;
50}
51
52bool
53AdditionalDescription::has(const std::string& key) const
54{
55 return m_info.find(key) != m_info.end();
56}
57
60{
61 return m_info.begin();
62}
63
66{
67 return m_info.end();
68}
69
72{
73 return m_info.begin();
74}
75
78{
79 return m_info.end();
80}
81
82template<encoding::Tag TAG>
83size_t
85{
86 size_t totalLength = 0;
87
88 for (auto it = m_info.rbegin(); it != m_info.rend(); it++) {
89 size_t entryLength = 0;
90 entryLength += prependStringBlock(encoder, tlv::DescriptionValue, it->second);
91 entryLength += prependStringBlock(encoder, tlv::DescriptionKey, it->first);
92 entryLength += encoder.prependVarNumber(entryLength);
93 entryLength += encoder.prependVarNumber(tlv::DescriptionEntry);
94
95 totalLength += entryLength;
96 }
97
98 totalLength += encoder.prependVarNumber(totalLength);
99 totalLength += encoder.prependVarNumber(tlv::AdditionalDescription);
100 return totalLength;
101}
102
104
105const Block&
107{
108 if (m_wire.hasWire())
109 return m_wire;
110
111 EncodingEstimator estimator;
112 size_t estimatedSize = wireEncode(estimator);
113
114 EncodingBuffer buffer(estimatedSize, 0);
115 wireEncode(buffer);
116
117 m_wire = buffer.block();
118 return m_wire;
119}
120
121void
123{
124 if (wire.type() != tlv::AdditionalDescription) {
125 NDN_THROW(Error("AdditionalDescription", wire.type()));
126 }
127 m_wire = wire;
128 m_wire.parse();
129
130 m_info.clear();
131
132 for (const auto& e : m_wire.elements()) {
133 switch (e.type()) {
135 e.parse();
136 if (e.elements_size() != 2) {
137 NDN_THROW(Error("DescriptionEntry does not have two sub-elements"));
138 }
139 if (e.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
140 e.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue) {
141 NDN_THROW(Error("Missing DescriptionKey or DescriptionValue field"));
142 }
143 m_info.insert_or_assign(readString(e.elements()[KEY_OFFSET]),
144 readString(e.elements()[VALUE_OFFSET]));
145 break;
146 default: // unrecognized element
147 // if the TLV-TYPE is critical, abort decoding
148 if (tlv::isCriticalType(e.type())) {
149 NDN_THROW(Error("Unrecognized element of critical type " + std::to_string(e.type())));
150 }
151 // otherwise, ignore it
152 break;
153 }
154 }
155}
156
157std::ostream&
158operator<<(std::ostream& os, const AdditionalDescription& desc)
159{
160 os << "[";
161
162 auto join = make_ostream_joiner(os, ", ");
163 for (const auto& entry : desc) {
164 join = "(" + entry.first + ":" + entry.second + ")";
165 }
166
167 return os << "]";
168}
169
170} // namespace ndn::security
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
const element_container & elements() const noexcept
Get container of sub-elements.
Definition block.hpp:424
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition block.hpp:205
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
void parse() const
Parse TLV-VALUE into sub-elements.
Definition block.cpp:326
Represents an AdditionalDescription TLV element.
std::map< std::string, std::string >::const_iterator const_iterator
bool has(const std::string &key) const
std::map< std::string, std::string >::iterator iterator
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
const std::string & get(const std::string &key) const
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
AdditionalDescription()=default
Create an empty AdditionalDescription.
void set(const std::string &key, const std::string &value)
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition exception.hpp:56
Contains the ndn-cxx security framework.
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &desc)
constexpr size_t VALUE_OFFSET
constexpr size_t KEY_OFFSET
@ DescriptionValue
Definition tlv.hpp:113
@ DescriptionKey
Definition tlv.hpp:112
@ AdditionalDescription
Definition tlv.hpp:110
@ DescriptionEntry
Definition tlv.hpp:111
constexpr bool isCriticalType(uint32_t type) noexcept
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
Definition tlv.hpp:162
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
Backport of ostream_joiner from the Library Fundamentals v2 TS.