blob: 63b5ee687800b0e7ce63e16b485642881e48045d [file] [log] [blame]
Yingdi Yuea382942015-07-17 23:20:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yuea382942015-07-17 23:20:44 -07004 *
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
22#include "additional-description.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070023#include "../../util/concepts.hpp"
24#include "../../encoding/block-helpers.hpp"
Yingdi Yuea382942015-07-17 23:20:44 -070025
26namespace ndn {
27namespace security {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070028namespace v2 {
Yingdi Yuea382942015-07-17 23:20:44 -070029
30BOOST_CONCEPT_ASSERT((boost::EqualityComparable<AdditionalDescription>));
31BOOST_CONCEPT_ASSERT((WireEncodable<AdditionalDescription>));
32BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<AdditionalDescription>));
33BOOST_CONCEPT_ASSERT((WireDecodable<AdditionalDescription>));
34static_assert(std::is_base_of<tlv::Error, AdditionalDescription::Error>::value,
35 "AdditionalDescription::Error must inherit from tlv::Error");
36
37static const size_t KEY_OFFSET = 0;
38static const size_t VALUE_OFFSET = 1;
39
40AdditionalDescription::AdditionalDescription(const Block& block)
41{
42 wireDecode(block);
43}
44
45const std::string&
46AdditionalDescription::get(const std::string& key) const
47{
48 auto it = m_info.find(key);
49 if (it == m_info.end())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070050 BOOST_THROW_EXCEPTION(Error("Entry does not exist for key (" + key + ")"));
Yingdi Yuea382942015-07-17 23:20:44 -070051
52 return it->second;
53}
54
55void
56AdditionalDescription::set(const std::string& key, const std::string& value)
57{
58 m_info[key] = value;
59}
60
61bool
62AdditionalDescription::has(const std::string& key) const
63{
64 return (m_info.find(key) != m_info.end());
65}
66
67AdditionalDescription::iterator
68AdditionalDescription::begin()
69{
70 return m_info.begin();
71}
72
73AdditionalDescription::iterator
74AdditionalDescription::end()
75{
76 return m_info.end();
77}
78
79AdditionalDescription::const_iterator
80AdditionalDescription::begin() const
81{
82 return m_info.begin();
83}
84
85AdditionalDescription::const_iterator
86AdditionalDescription::end() const
87{
88 return m_info.end();
89}
90
91template<encoding::Tag TAG>
92size_t
93AdditionalDescription::wireEncode(EncodingImpl<TAG>& encoder) const
94{
95 size_t totalLength = 0;
96
97 for (auto it = m_info.rbegin(); it != m_info.rend(); it++) {
98 size_t entryLength = 0;
99 entryLength += prependStringBlock(encoder, tlv::DescriptionValue, it->second);
100 entryLength += prependStringBlock(encoder, tlv::DescriptionKey, it->first);
101 entryLength += encoder.prependVarNumber(entryLength);
102 entryLength += encoder.prependVarNumber(tlv::DescriptionEntry);
103
104 totalLength += entryLength;
105 }
106
107 totalLength += encoder.prependVarNumber(totalLength);
108 totalLength += encoder.prependVarNumber(tlv::AdditionalDescription);
109 return totalLength;
110}
111
Davide Pesavento88a0d812017-08-19 21:31:42 -0400112NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdditionalDescription);
Yingdi Yuea382942015-07-17 23:20:44 -0700113
114const Block&
115AdditionalDescription::wireEncode() const
116{
117 if (m_wire.hasWire())
118 return m_wire;
119
120 EncodingEstimator estimator;
121 size_t estimatedSize = wireEncode(estimator);
122
123 EncodingBuffer buffer(estimatedSize, 0);
124 wireEncode(buffer);
125
126 m_wire = buffer.block();
127 m_wire.parse();
128
129 return m_wire;
130}
131
132void
133AdditionalDescription::wireDecode(const Block& wire)
134{
135 if (!wire.hasWire()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700136 BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
Yingdi Yuea382942015-07-17 23:20:44 -0700137 }
138
139 m_wire = wire;
140 m_wire.parse();
141
142 if (m_wire.type() != tlv::AdditionalDescription)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700143 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding AdditionalDescription"));
Yingdi Yuea382942015-07-17 23:20:44 -0700144
145 Block::element_const_iterator it = m_wire.elements_begin();
146 while (it != m_wire.elements_end()) {
147 const Block& entry = *it;
148 entry.parse();
149
150 if (entry.type() != tlv::DescriptionEntry)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700151 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding DescriptionEntry"));
Yingdi Yuea382942015-07-17 23:20:44 -0700152
153 if (entry.elements_size() != 2)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700154 BOOST_THROW_EXCEPTION(Error("DescriptionEntry does not have two sub-TLVs"));
Yingdi Yuea382942015-07-17 23:20:44 -0700155
156 if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
157 entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700158 BOOST_THROW_EXCEPTION(Error("Invalid DescriptionKey or DescriptionValue field"));
Yingdi Yuea382942015-07-17 23:20:44 -0700159
160 m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
161 it++;
162 }
163}
164
165bool
166AdditionalDescription::operator==(const AdditionalDescription& other) const
167{
168 return (m_info == other.m_info);
169}
170
171bool
172AdditionalDescription::operator!=(const AdditionalDescription& other) const
173{
174 return !(*this == other);
175}
176
177std::ostream&
178operator<<(std::ostream& os, const AdditionalDescription& other)
179{
180 size_t count = 0;
181 os << "(";
182 for (const auto& entry : other) {
183 if (count > 0)
184 os << ", ";
185 os << "(" << entry.first << ":" << entry.second << ")";
186 count++;
187 }
188 os << ")";
189
190 return os;
191}
192
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700193} // namespace v2
Yingdi Yuea382942015-07-17 23:20:44 -0700194} // namespace security
195} // namespace ndn