blob: 6fdfd53cecd629f81bd7dbf1aa4f4e62adcf6e22 [file] [log] [blame]
Yingdi Yuea382942015-07-17 23:20:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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
112template size_t
113AdditionalDescription::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
114
115template size_t
116AdditionalDescription::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
117
118const Block&
119AdditionalDescription::wireEncode() const
120{
121 if (m_wire.hasWire())
122 return m_wire;
123
124 EncodingEstimator estimator;
125 size_t estimatedSize = wireEncode(estimator);
126
127 EncodingBuffer buffer(estimatedSize, 0);
128 wireEncode(buffer);
129
130 m_wire = buffer.block();
131 m_wire.parse();
132
133 return m_wire;
134}
135
136void
137AdditionalDescription::wireDecode(const Block& wire)
138{
139 if (!wire.hasWire()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700140 BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
Yingdi Yuea382942015-07-17 23:20:44 -0700141 }
142
143 m_wire = wire;
144 m_wire.parse();
145
146 if (m_wire.type() != tlv::AdditionalDescription)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700147 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding AdditionalDescription"));
Yingdi Yuea382942015-07-17 23:20:44 -0700148
149 Block::element_const_iterator it = m_wire.elements_begin();
150 while (it != m_wire.elements_end()) {
151 const Block& entry = *it;
152 entry.parse();
153
154 if (entry.type() != tlv::DescriptionEntry)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700155 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding DescriptionEntry"));
Yingdi Yuea382942015-07-17 23:20:44 -0700156
157 if (entry.elements_size() != 2)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700158 BOOST_THROW_EXCEPTION(Error("DescriptionEntry does not have two sub-TLVs"));
Yingdi Yuea382942015-07-17 23:20:44 -0700159
160 if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
161 entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700162 BOOST_THROW_EXCEPTION(Error("Invalid DescriptionKey or DescriptionValue field"));
Yingdi Yuea382942015-07-17 23:20:44 -0700163
164 m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
165 it++;
166 }
167}
168
169bool
170AdditionalDescription::operator==(const AdditionalDescription& other) const
171{
172 return (m_info == other.m_info);
173}
174
175bool
176AdditionalDescription::operator!=(const AdditionalDescription& other) const
177{
178 return !(*this == other);
179}
180
181std::ostream&
182operator<<(std::ostream& os, const AdditionalDescription& other)
183{
184 size_t count = 0;
185 os << "(";
186 for (const auto& entry : other) {
187 if (count > 0)
188 os << ", ";
189 os << "(" << entry.first << ":" << entry.second << ")";
190 count++;
191 }
192 os << ")";
193
194 return os;
195}
196
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700197} // namespace v2
Yingdi Yuea382942015-07-17 23:20:44 -0700198} // namespace security
199} // namespace ndn