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