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-2023 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 
26 namespace ndn::security {
27 
28 constexpr size_t KEY_OFFSET = 0;
29 constexpr size_t VALUE_OFFSET = 1;
30 
32 {
33  wireDecode(block);
34 }
35 
36 const std::string&
37 AdditionalDescription::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 
46 void
47 AdditionalDescription::set(const std::string& key, const std::string& value)
48 {
49  m_info[key] = value;
50 }
51 
52 bool
53 AdditionalDescription::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 
82 template<encoding::Tag TAG>
83 size_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 
105 const 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  m_wire.parse();
119 
120  return m_wire;
121 }
122 
123 void
125 {
126  if (!wire.hasWire()) {
127  NDN_THROW(Error("The supplied block does not contain wire format"));
128  }
129 
130  m_wire = wire;
131  m_wire.parse();
132 
133  if (m_wire.type() != tlv::AdditionalDescription)
134  NDN_THROW(Error("AdditionalDescription", m_wire.type()));
135 
136  auto it = m_wire.elements_begin();
137  while (it != m_wire.elements_end()) {
138  const Block& entry = *it;
139  entry.parse();
140 
141  if (entry.type() != tlv::DescriptionEntry)
142  NDN_THROW(Error("DescriptionEntry", entry.type()));
143 
144  if (entry.elements_size() != 2)
145  NDN_THROW(Error("DescriptionEntry does not have two sub-TLVs"));
146 
147  if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
148  entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
149  NDN_THROW(Error("Invalid DescriptionKey or DescriptionValue field"));
150 
151  m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
152  it++;
153  }
154 }
155 
156 std::ostream&
157 operator<<(std::ostream& os, const AdditionalDescription& desc)
158 {
159  os << "[";
160 
161  auto join = make_ostream_joiner(os, ", ");
162  for (const auto& entry : desc) {
163  join = "(" + entry.first + ":" + entry.second + ")";
164  }
165 
166  return os << "]";
167 }
168 
169 } // namespace ndn::security
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition: block.hpp:433
size_t elements_size() const noexcept
Equivalent to elements().size().
Definition: block.hpp:451
const element_container & elements() const noexcept
Get container of sub-elements.
Definition: block.hpp:424
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition: block.hpp:442
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
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, std::string_view value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read the TLV-VALUE of a TLV element as a string.
EncodingImpl< EncoderTag > EncodingBuffer
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
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.