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