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, |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 68 | SignatureSha256WithRsa = 1 |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | enum ConentType { |
| 72 | ContentType_Default = 0, |
| 73 | ContentType_Link = 1, |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 74 | ContentType_Key = 2 |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 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 | { |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 229 | uint64_t number = 0; |
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 | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 247 | uint64_t value; |
| 248 | bool isOk = readVarNumber(begin, end, value); |
| 249 | if (!isOk) |
| 250 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 251 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 252 | return value; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 255 | template<> |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 256 | inline bool |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 257 | readVarNumber<std::istream_iterator<uint8_t> >(std::istream_iterator<uint8_t>& begin, |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 258 | const std::istream_iterator<uint8_t>& end, |
| 259 | uint64_t& value) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 260 | { |
| 261 | if (begin == end) |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 262 | return false; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 263 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 264 | uint8_t firstOctet = *begin; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 265 | ++begin; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 266 | if (firstOctet < 253) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 267 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 268 | value = firstOctet; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 269 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 270 | else if (firstOctet == 253) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 271 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 272 | value = 0; |
| 273 | size_t count = 0; |
| 274 | for (; begin != end && count < 2; ++count) |
| 275 | { |
| 276 | value = ((value << 8) | *begin); |
| 277 | begin++; |
| 278 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 279 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 280 | if (count != 2) |
| 281 | return false; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 282 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 283 | else if (firstOctet == 254) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 284 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 285 | value = 0; |
| 286 | size_t count = 0; |
| 287 | for (; begin != end && count < 4; ++count) |
| 288 | { |
| 289 | value = ((value << 8) | *begin); |
| 290 | begin++; |
| 291 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 292 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 293 | if (count != 4) |
| 294 | return false; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 295 | } |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 296 | else // if (firstOctet == 255) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 297 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 298 | value = 0; |
| 299 | size_t count = 0; |
| 300 | for (; begin != end && count < 8; ++count) |
| 301 | { |
| 302 | value = ((value << 8) | *begin); |
| 303 | begin++; |
| 304 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 305 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 306 | if (count != 8) |
| 307 | return false; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 308 | } |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 309 | |
| 310 | return true; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 313 | template<class InputIterator> |
| 314 | inline uint32_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 315 | readType(InputIterator& begin, const InputIterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 316 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 317 | uint64_t type = readVarNumber(begin, end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 318 | if (type > std::numeric_limits<uint32_t>::max()) |
| 319 | { |
| 320 | throw Error("TLV type code exceeds allowed maximum"); |
| 321 | } |
| 322 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 323 | return static_cast<uint32_t>(type); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | size_t |
| 327 | sizeOfVarNumber(uint64_t varNumber) |
| 328 | { |
| 329 | if (varNumber < 253) { |
| 330 | return 1; |
| 331 | } |
| 332 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 333 | return 3; |
| 334 | } |
| 335 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 336 | return 5; |
| 337 | } |
| 338 | else { |
| 339 | return 9; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | inline size_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 344 | writeVarNumber(std::ostream& os, uint64_t varNumber) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 345 | { |
| 346 | if (varNumber < 253) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 347 | os.put(static_cast<char>(varNumber)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 348 | return 1; |
| 349 | } |
| 350 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 351 | os.put(static_cast<char>(253)); |
| 352 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 353 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 354 | return 3; |
| 355 | } |
| 356 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 357 | os.put(static_cast<char>(254)); |
| 358 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 359 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 360 | return 5; |
| 361 | } |
| 362 | else { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 363 | os.put(static_cast<char>(255)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 364 | uint64_t value = htobe64(varNumber); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 365 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 366 | return 9; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | template<class InputIterator> |
| 371 | inline uint64_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 372 | readNonNegativeInteger(size_t size, InputIterator& begin, const InputIterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 373 | { |
| 374 | switch (size) { |
| 375 | case 1: |
| 376 | { |
| 377 | if (end - begin < 1) |
| 378 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 379 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 380 | uint8_t value = *begin; |
| 381 | begin++; |
| 382 | return value; |
| 383 | } |
| 384 | case 2: |
| 385 | { |
| 386 | if (end - begin < 2) |
| 387 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 388 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 389 | uint16_t value = *reinterpret_cast<const uint16_t*>(&*begin); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 390 | begin += 2; |
| 391 | return be16toh(value); |
| 392 | } |
| 393 | case 4: |
| 394 | { |
| 395 | if (end - begin < 4) |
| 396 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 397 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 398 | uint32_t value = *reinterpret_cast<const uint32_t*>(&*begin); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 399 | begin += 4; |
| 400 | return be32toh(value); |
| 401 | } |
| 402 | case 8: |
| 403 | { |
| 404 | if (end - begin < 8) |
| 405 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 406 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 407 | uint64_t value = *reinterpret_cast<const uint64_t*>(&*begin); |
| 408 | begin += 8; |
| 409 | return be64toh(value); |
| 410 | } |
| 411 | } |
| 412 | throw Error("Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"); |
| 413 | } |
| 414 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 415 | template<> |
| 416 | inline uint64_t |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 417 | readNonNegativeInteger<std::istream_iterator<uint8_t> >(size_t size, |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 418 | std::istream_iterator<uint8_t>& begin, |
| 419 | const std::istream_iterator<uint8_t>& end) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 420 | { |
| 421 | switch (size) { |
| 422 | case 1: |
| 423 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 424 | if (begin == end) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 425 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 426 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 427 | uint64_t value = *begin; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 428 | begin++; |
| 429 | return value; |
| 430 | } |
| 431 | case 2: |
| 432 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 433 | uint64_t value = 0; |
| 434 | size_t count = 0; |
| 435 | for (; begin != end && count < 2; ++count) |
| 436 | { |
| 437 | value = ((value << 8) | *begin); |
| 438 | begin++; |
| 439 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 440 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 441 | if (count != 2) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 442 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 443 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 444 | return value; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 445 | } |
| 446 | case 4: |
| 447 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 448 | uint64_t value = 0; |
| 449 | size_t count = 0; |
| 450 | for (; begin != end && count < 4; ++count) |
| 451 | { |
| 452 | value = ((value << 8) | *begin); |
| 453 | begin++; |
| 454 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 455 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 456 | if (count != 4) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 457 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 458 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 459 | return value; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 460 | } |
| 461 | case 8: |
| 462 | { |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 463 | uint64_t value = 0; |
| 464 | size_t count = 0; |
| 465 | for (; begin != end && count < 8; ++count) |
| 466 | { |
| 467 | value = ((value << 8) | *begin); |
| 468 | begin++; |
| 469 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 470 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 471 | if (count != 8) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 472 | throw Error("Insufficient data during TLV processing"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 473 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 474 | return value; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | throw Error("Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"); |
| 478 | } |
| 479 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 480 | inline size_t |
| 481 | sizeOfNonNegativeInteger(uint64_t varNumber) |
| 482 | { |
| 483 | if (varNumber < 253) { |
| 484 | return 1; |
| 485 | } |
| 486 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 487 | return 2; |
| 488 | } |
| 489 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 490 | return 4; |
| 491 | } |
| 492 | else { |
| 493 | return 8; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | |
| 498 | inline size_t |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 499 | writeNonNegativeInteger(std::ostream& os, uint64_t varNumber) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 500 | { |
| 501 | if (varNumber < 253) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 502 | os.put(static_cast<char>(varNumber)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 503 | return 1; |
| 504 | } |
| 505 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 506 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 507 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 508 | return 2; |
| 509 | } |
| 510 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 511 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 512 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 513 | return 4; |
| 514 | } |
| 515 | else { |
| 516 | uint64_t value = htobe64(varNumber); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 517 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 518 | return 8; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 523 | } // namespace Tlv |
| 524 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 525 | } // namespace ndn |
| 526 | |
| 527 | #endif // NDN_TLV_HPP |