Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 22 | #ifndef NDN_ENCODING_TLV_HPP |
| 23 | #define NDN_ENCODING_TLV_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 54467af | 2014-01-06 15:45:32 -0800 | [diff] [blame] | 25 | #include <stdexcept> |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 26 | #include <iterator> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 27 | #include "buffer.hpp" |
Alexander Afanasyev | 200dd6f | 2014-01-28 19:04:25 -0800 | [diff] [blame] | 28 | #include "endian.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | |
| 32 | /** |
| 33 | * @brief Namespace defining NDN-TLV related constants and procedures |
| 34 | */ |
| 35 | namespace Tlv { |
| 36 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 37 | class Error : public std::runtime_error |
| 38 | { |
| 39 | public: |
| 40 | explicit |
| 41 | Error(const std::string& what) |
| 42 | : std::runtime_error(what) |
| 43 | { |
| 44 | } |
| 45 | }; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 46 | |
| 47 | enum { |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 48 | Interest = 5, |
| 49 | Data = 6, |
| 50 | Name = 7, |
| 51 | NameComponent = 8, |
| 52 | Selectors = 9, |
| 53 | Nonce = 10, |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 54 | Scope = 11, // deprecated |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 55 | InterestLifetime = 12, |
| 56 | MinSuffixComponents = 13, |
| 57 | MaxSuffixComponents = 14, |
| 58 | PublisherPublicKeyLocator = 15, |
| 59 | Exclude = 16, |
| 60 | ChildSelector = 17, |
| 61 | MustBeFresh = 18, |
| 62 | Any = 19, |
| 63 | MetaInfo = 20, |
| 64 | Content = 21, |
| 65 | SignatureInfo = 22, |
| 66 | SignatureValue = 23, |
| 67 | ContentType = 24, |
| 68 | FreshnessPeriod = 25, |
| 69 | FinalBlockId = 26, |
| 70 | SignatureType = 27, |
| 71 | KeyLocator = 28, |
| 72 | KeyLocatorDigest = 29, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 73 | |
| 74 | AppPrivateBlock1 = 128, |
| 75 | AppPrivateBlock2 = 32767 |
| 76 | }; |
| 77 | |
| 78 | enum SignatureType { |
| 79 | DigestSha256 = 0, |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 80 | SignatureSha256WithRsa = 1 |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | enum ConentType { |
| 84 | ContentType_Default = 0, |
| 85 | ContentType_Link = 1, |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 86 | ContentType_Key = 2 |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * @brief Read VAR-NUMBER in NDN-TLV encoding |
| 91 | * |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 92 | * @param [in] begin Begin (pointer or iterator) of the buffer |
| 93 | * @param [in] end End (pointer or iterator) of the buffer |
| 94 | * @param [out] number Read number |
| 95 | * |
| 96 | * @throws This call never throws exception |
| 97 | * |
| 98 | * @return true if number successfully read from input, false otherwise |
| 99 | */ |
| 100 | template<class InputIterator> |
| 101 | inline bool |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 102 | readVarNumber(InputIterator& begin, const InputIterator& end, uint64_t& number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * @brief Read TLV Type |
| 106 | * |
| 107 | * @param [in] begin Begin (pointer or iterator) of the buffer |
| 108 | * @param [in] end End (pointer or iterator) of the buffer |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 109 | * @param [out] type Read type number |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 110 | * |
| 111 | * @throws This call never throws exception |
| 112 | * |
| 113 | * This call is largely equivalent to tlv::readVarNumber, but exception will be thrown if type |
| 114 | * is larger than 2^32-1 (type in this library is implemented as uint32_t) |
| 115 | */ |
| 116 | template<class InputIterator> |
| 117 | inline bool |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 118 | readType(InputIterator& begin, const InputIterator& end, uint32_t& type); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | /** |
| 122 | * @brief Read VAR-NUMBER in NDN-TLV encoding |
| 123 | * |
| 124 | * @throws This call will throw ndn::Tlv::Error (aka std::runtime_error) if number cannot be read |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 125 | * |
| 126 | * Note that after call finished, begin will point to the first byte after the read VAR-NUMBER |
| 127 | */ |
| 128 | template<class InputIterator> |
| 129 | inline uint64_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 130 | readVarNumber(InputIterator& begin, const InputIterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @brief Read TLV Type |
| 134 | * |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 135 | * @throws This call will throw ndn::Tlv::Error (aka std::runtime_error) if number cannot be read |
| 136 | * |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 137 | * This call is largely equivalent to tlv::readVarNumber, but exception will be thrown if type |
| 138 | * is larger than 2^32-1 (type in this library is implemented as uint32_t) |
| 139 | */ |
| 140 | template<class InputIterator> |
| 141 | inline uint32_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 142 | readType(InputIterator& begin, const InputIterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * @brief Get number of bytes necessary to hold value of VAR-NUMBER |
| 146 | */ |
| 147 | inline size_t |
| 148 | sizeOfVarNumber(uint64_t varNumber); |
| 149 | |
| 150 | /** |
| 151 | * @brief Write VAR-NUMBER to the specified stream |
| 152 | */ |
| 153 | inline size_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 154 | writeVarNumber(std::ostream& os, uint64_t varNumber); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * @brief Read nonNegativeInteger in NDN-TLV encoding |
| 158 | * |
| 159 | * This call will throw ndn::Tlv::Error (aka std::runtime_error) if number cannot be read |
| 160 | * |
| 161 | * Note that after call finished, begin will point to the first byte after the read VAR-NUMBER |
| 162 | * |
| 163 | * How many bytes will be read is directly controlled by the size parameter, which can be either |
| 164 | * 1, 2, 4, or 8. If the value of size is different, then an exception will be thrown. |
| 165 | */ |
| 166 | template<class InputIterator> |
| 167 | inline uint64_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 168 | readNonNegativeInteger(size_t size, InputIterator& begin, const InputIterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * @brief Get number of bytes necessary to hold value of nonNegativeInteger |
| 172 | */ |
| 173 | inline size_t |
| 174 | sizeOfNonNegativeInteger(uint64_t varNumber); |
| 175 | |
| 176 | /** |
| 177 | * @brief Write nonNegativeInteger to the specified stream |
| 178 | */ |
| 179 | inline size_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 180 | writeNonNegativeInteger(std::ostream& os, uint64_t varNumber); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 181 | |
| 182 | ///////////////////////////////////////////////////////////////////////////////// |
| 183 | ///////////////////////////////////////////////////////////////////////////////// |
| 184 | ///////////////////////////////////////////////////////////////////////////////// |
| 185 | |
| 186 | // Inline implementations |
| 187 | |
| 188 | ///////////////////////////////////////////////////////////////////////////////// |
| 189 | ///////////////////////////////////////////////////////////////////////////////// |
| 190 | ///////////////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | template<class InputIterator> |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 193 | inline bool |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 194 | readVarNumber(InputIterator& begin, const InputIterator& end, uint64_t& number) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 195 | { |
| 196 | if (begin == end) |
| 197 | return false; |
| 198 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 199 | uint8_t firstOctet = *begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 200 | ++begin; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 201 | if (firstOctet < 253) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 202 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 203 | number = firstOctet; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 204 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 205 | else if (firstOctet == 253) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 206 | { |
| 207 | if (end - begin < 2) |
| 208 | return false; |
| 209 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 210 | uint16_t value = *reinterpret_cast<const uint16_t*>(&*begin); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 211 | begin += 2; |
| 212 | number = be16toh(value); |
| 213 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 214 | else if (firstOctet == 254) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 215 | { |
| 216 | if (end - begin < 4) |
| 217 | return false; |
| 218 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 219 | uint32_t value = *reinterpret_cast<const uint32_t*>(&*begin); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 220 | begin += 4; |
| 221 | number = be32toh(value); |
| 222 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 223 | else // if (firstOctet == 255) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 224 | { |
| 225 | if (end - begin < 8) |
| 226 | return false; |
| 227 | |
| 228 | uint64_t value = *reinterpret_cast<const uint64_t*>(&*begin); |
| 229 | begin += 8; |
| 230 | |
| 231 | number = be64toh(value); |
| 232 | } |
| 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | template<class InputIterator> |
| 238 | inline bool |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 239 | readType(InputIterator& begin, const InputIterator& end, uint32_t& type) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 240 | { |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 241 | uint64_t number = 0; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 242 | bool isOk = readVarNumber(begin, end, number); |
| 243 | if (!isOk || number > std::numeric_limits<uint32_t>::max()) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 244 | { |
| 245 | return false; |
| 246 | } |
| 247 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 248 | type = static_cast<uint64_t>(number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
| 252 | template<class InputIterator> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 253 | inline uint64_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 254 | readVarNumber(InputIterator& begin, const InputIterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 255 | { |
| 256 | if (begin == end) |
| 257 | throw Error("Empty buffer during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 258 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 259 | uint64_t value; |
| 260 | bool isOk = readVarNumber(begin, end, value); |
| 261 | if (!isOk) |
| 262 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 263 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 264 | return value; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 267 | template<> |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 268 | inline bool |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 269 | readVarNumber<std::istream_iterator<uint8_t> >(std::istream_iterator<uint8_t>& begin, |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 270 | const std::istream_iterator<uint8_t>& end, |
| 271 | uint64_t& value) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 272 | { |
| 273 | if (begin == end) |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 274 | return false; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 275 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 276 | uint8_t firstOctet = *begin; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 277 | ++begin; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 278 | if (firstOctet < 253) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 279 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 280 | value = firstOctet; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 281 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 282 | else if (firstOctet == 253) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 283 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 284 | value = 0; |
| 285 | size_t count = 0; |
| 286 | for (; begin != end && count < 2; ++count) |
| 287 | { |
| 288 | value = ((value << 8) | *begin); |
| 289 | begin++; |
| 290 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 291 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 292 | if (count != 2) |
| 293 | return false; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 294 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 295 | else if (firstOctet == 254) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 296 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 297 | value = 0; |
| 298 | size_t count = 0; |
| 299 | for (; begin != end && count < 4; ++count) |
| 300 | { |
| 301 | value = ((value << 8) | *begin); |
| 302 | begin++; |
| 303 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 304 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 305 | if (count != 4) |
| 306 | return false; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 307 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 308 | else // if (firstOctet == 255) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 309 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 310 | value = 0; |
| 311 | size_t count = 0; |
| 312 | for (; begin != end && count < 8; ++count) |
| 313 | { |
| 314 | value = ((value << 8) | *begin); |
| 315 | begin++; |
| 316 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 317 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 318 | if (count != 8) |
| 319 | return false; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 320 | } |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 321 | |
| 322 | return true; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 325 | template<class InputIterator> |
| 326 | inline uint32_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 327 | readType(InputIterator& begin, const InputIterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 328 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 329 | uint64_t type = readVarNumber(begin, end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 330 | if (type > std::numeric_limits<uint32_t>::max()) |
| 331 | { |
| 332 | throw Error("TLV type code exceeds allowed maximum"); |
| 333 | } |
| 334 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 335 | return static_cast<uint32_t>(type); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | size_t |
| 339 | sizeOfVarNumber(uint64_t varNumber) |
| 340 | { |
| 341 | if (varNumber < 253) { |
| 342 | return 1; |
| 343 | } |
| 344 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 345 | return 3; |
| 346 | } |
| 347 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 348 | return 5; |
| 349 | } |
| 350 | else { |
| 351 | return 9; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | inline size_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 356 | writeVarNumber(std::ostream& os, uint64_t varNumber) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 357 | { |
| 358 | if (varNumber < 253) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 359 | os.put(static_cast<char>(varNumber)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 360 | return 1; |
| 361 | } |
| 362 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 363 | os.put(static_cast<char>(253)); |
| 364 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 365 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 366 | return 3; |
| 367 | } |
| 368 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 369 | os.put(static_cast<char>(254)); |
| 370 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 371 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 372 | return 5; |
| 373 | } |
| 374 | else { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 375 | os.put(static_cast<char>(255)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 376 | uint64_t value = htobe64(varNumber); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 377 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 378 | return 9; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | template<class InputIterator> |
| 383 | inline uint64_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 384 | readNonNegativeInteger(size_t size, InputIterator& begin, const InputIterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 385 | { |
| 386 | switch (size) { |
| 387 | case 1: |
| 388 | { |
| 389 | if (end - begin < 1) |
| 390 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 391 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 392 | uint8_t value = *begin; |
| 393 | begin++; |
| 394 | return value; |
| 395 | } |
| 396 | case 2: |
| 397 | { |
| 398 | if (end - begin < 2) |
| 399 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 400 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 401 | uint16_t value = *reinterpret_cast<const uint16_t*>(&*begin); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 402 | begin += 2; |
| 403 | return be16toh(value); |
| 404 | } |
| 405 | case 4: |
| 406 | { |
| 407 | if (end - begin < 4) |
| 408 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 409 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 410 | uint32_t value = *reinterpret_cast<const uint32_t*>(&*begin); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 411 | begin += 4; |
| 412 | return be32toh(value); |
| 413 | } |
| 414 | case 8: |
| 415 | { |
| 416 | if (end - begin < 8) |
| 417 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 418 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 419 | uint64_t value = *reinterpret_cast<const uint64_t*>(&*begin); |
| 420 | begin += 8; |
| 421 | return be64toh(value); |
| 422 | } |
| 423 | } |
| 424 | throw Error("Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"); |
| 425 | } |
| 426 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 427 | template<> |
| 428 | inline uint64_t |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 429 | readNonNegativeInteger<std::istream_iterator<uint8_t> >(size_t size, |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 430 | std::istream_iterator<uint8_t>& begin, |
| 431 | const std::istream_iterator<uint8_t>& end) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 432 | { |
| 433 | switch (size) { |
| 434 | case 1: |
| 435 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 436 | if (begin == end) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 437 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 438 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 439 | uint64_t value = *begin; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 440 | begin++; |
| 441 | return value; |
| 442 | } |
| 443 | case 2: |
| 444 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 445 | uint64_t value = 0; |
| 446 | size_t count = 0; |
| 447 | for (; begin != end && count < 2; ++count) |
| 448 | { |
| 449 | value = ((value << 8) | *begin); |
| 450 | begin++; |
| 451 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 452 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 453 | if (count != 2) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 454 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 455 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 456 | return value; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 457 | } |
| 458 | case 4: |
| 459 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 460 | uint64_t value = 0; |
| 461 | size_t count = 0; |
| 462 | for (; begin != end && count < 4; ++count) |
| 463 | { |
| 464 | value = ((value << 8) | *begin); |
| 465 | begin++; |
| 466 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 467 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 468 | if (count != 4) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 469 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 470 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 471 | return value; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 472 | } |
| 473 | case 8: |
| 474 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 475 | uint64_t value = 0; |
| 476 | size_t count = 0; |
| 477 | for (; begin != end && count < 8; ++count) |
| 478 | { |
| 479 | value = ((value << 8) | *begin); |
| 480 | begin++; |
| 481 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 482 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 483 | if (count != 8) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 484 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 485 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 486 | return value; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | throw Error("Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"); |
| 490 | } |
| 491 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 492 | inline size_t |
| 493 | sizeOfNonNegativeInteger(uint64_t varNumber) |
| 494 | { |
| 495 | if (varNumber < 253) { |
| 496 | return 1; |
| 497 | } |
| 498 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 499 | return 2; |
| 500 | } |
| 501 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 502 | return 4; |
| 503 | } |
| 504 | else { |
| 505 | return 8; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | inline size_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 511 | writeNonNegativeInteger(std::ostream& os, uint64_t varNumber) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 512 | { |
| 513 | if (varNumber < 253) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 514 | os.put(static_cast<char>(varNumber)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 515 | return 1; |
| 516 | } |
| 517 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 518 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 519 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 520 | return 2; |
| 521 | } |
| 522 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 523 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 524 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 525 | return 4; |
| 526 | } |
| 527 | else { |
| 528 | uint64_t value = htobe64(varNumber); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 529 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 530 | return 8; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 535 | } // namespace Tlv |
| 536 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 537 | } // namespace ndn |
| 538 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 539 | #endif // NDN_ENCODING_TLV_HPP |