Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 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" |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 23 | #include "../../util/concepts.hpp" |
| 24 | #include "../../encoding/block-helpers.hpp" |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 28 | namespace v2 { |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 29 | |
| 30 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<AdditionalDescription>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<AdditionalDescription>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<AdditionalDescription>)); |
| 33 | BOOST_CONCEPT_ASSERT((WireDecodable<AdditionalDescription>)); |
| 34 | static_assert(std::is_base_of<tlv::Error, AdditionalDescription::Error>::value, |
| 35 | "AdditionalDescription::Error must inherit from tlv::Error"); |
| 36 | |
| 37 | static const size_t KEY_OFFSET = 0; |
| 38 | static const size_t VALUE_OFFSET = 1; |
| 39 | |
| 40 | AdditionalDescription::AdditionalDescription(const Block& block) |
| 41 | { |
| 42 | wireDecode(block); |
| 43 | } |
| 44 | |
| 45 | const std::string& |
| 46 | AdditionalDescription::get(const std::string& key) const |
| 47 | { |
| 48 | auto it = m_info.find(key); |
| 49 | if (it == m_info.end()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 50 | BOOST_THROW_EXCEPTION(Error("Entry does not exist for key (" + key + ")")); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 51 | |
| 52 | return it->second; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | AdditionalDescription::set(const std::string& key, const std::string& value) |
| 57 | { |
| 58 | m_info[key] = value; |
| 59 | } |
| 60 | |
| 61 | bool |
| 62 | AdditionalDescription::has(const std::string& key) const |
| 63 | { |
| 64 | return (m_info.find(key) != m_info.end()); |
| 65 | } |
| 66 | |
| 67 | AdditionalDescription::iterator |
| 68 | AdditionalDescription::begin() |
| 69 | { |
| 70 | return m_info.begin(); |
| 71 | } |
| 72 | |
| 73 | AdditionalDescription::iterator |
| 74 | AdditionalDescription::end() |
| 75 | { |
| 76 | return m_info.end(); |
| 77 | } |
| 78 | |
| 79 | AdditionalDescription::const_iterator |
| 80 | AdditionalDescription::begin() const |
| 81 | { |
| 82 | return m_info.begin(); |
| 83 | } |
| 84 | |
| 85 | AdditionalDescription::const_iterator |
| 86 | AdditionalDescription::end() const |
| 87 | { |
| 88 | return m_info.end(); |
| 89 | } |
| 90 | |
| 91 | template<encoding::Tag TAG> |
| 92 | size_t |
| 93 | AdditionalDescription::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 | |
| 112 | template size_t |
| 113 | AdditionalDescription::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
| 114 | |
| 115 | template size_t |
| 116 | AdditionalDescription::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
| 117 | |
| 118 | const Block& |
| 119 | AdditionalDescription::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 | |
| 136 | void |
| 137 | AdditionalDescription::wireDecode(const Block& wire) |
| 138 | { |
| 139 | if (!wire.hasWire()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 140 | BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format")); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | m_wire = wire; |
| 144 | m_wire.parse(); |
| 145 | |
| 146 | if (m_wire.type() != tlv::AdditionalDescription) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 147 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding AdditionalDescription")); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 148 | |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 155 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding DescriptionEntry")); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 156 | |
| 157 | if (entry.elements_size() != 2) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 158 | BOOST_THROW_EXCEPTION(Error("DescriptionEntry does not have two sub-TLVs")); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 159 | |
| 160 | if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey || |
| 161 | entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 162 | BOOST_THROW_EXCEPTION(Error("Invalid DescriptionKey or DescriptionValue field")); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 163 | |
| 164 | m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]); |
| 165 | it++; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | bool |
| 170 | AdditionalDescription::operator==(const AdditionalDescription& other) const |
| 171 | { |
| 172 | return (m_info == other.m_info); |
| 173 | } |
| 174 | |
| 175 | bool |
| 176 | AdditionalDescription::operator!=(const AdditionalDescription& other) const |
| 177 | { |
| 178 | return !(*this == other); |
| 179 | } |
| 180 | |
| 181 | std::ostream& |
| 182 | operator<<(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 Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 197 | } // namespace v2 |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 198 | } // namespace security |
| 199 | } // namespace ndn |