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