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