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