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