| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| /* |
| * Copyright (c) 2013, Regents of the University of California |
| * |
| * BSD license, See the LICENSE file for more information |
| * |
| * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| */ |
| |
| #ifndef NDN_TLV_HPP |
| #define NDN_TLV_HPP |
| |
| #include <stdexcept> |
| #include <iterator> |
| #include "buffer.hpp" |
| #include "endian.h" |
| |
| namespace ndn { |
| |
| /** |
| * @brief Namespace defining NDN-TLV related constants and procedures |
| */ |
| namespace Tlv { |
| |
| struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| |
| enum { |
| Interest = 1, |
| Data = 2, |
| Name = 3, |
| NameComponent = 4, |
| Selectors = 5, |
| Nonce = 6, |
| Scope = 7, |
| InterestLifetime = 8, |
| MinSuffixComponents = 9, |
| MaxSuffixComponents = 10, |
| PublisherPublicKeyLocator = 11, |
| Exclude = 12, |
| ChildSelector = 13, |
| MustBeFresh = 14, |
| Any = 15, |
| MetaInfo = 16, |
| Content = 17, |
| SignatureInfo = 18, |
| SignatureValue = 19, |
| ContentType = 20, |
| FreshnessPeriod = 21, |
| SignatureType = 22, |
| KeyLocator = 23, |
| KeyLocatorDigest = 24, |
| |
| AppPrivateBlock1 = 128, |
| AppPrivateBlock2 = 32767 |
| }; |
| |
| enum SignatureType { |
| DigestSha256 = 0, |
| SignatureSha256WithRsa = 1, |
| }; |
| |
| enum ConentType { |
| ContentType_Default = 0, |
| ContentType_Link = 1, |
| ContentType_Key = 2, |
| }; |
| |
| /** |
| * @brief Read VAR-NUMBER in NDN-TLV encoding |
| * |
| * This call will throw ndn::Tlv::Error (aka std::runtime_error) if number cannot be read |
| * |
| * Note that after call finished, begin will point to the first byte after the read VAR-NUMBER |
| */ |
| template<class InputIterator> |
| inline uint64_t |
| readVarNumber(InputIterator &begin, const InputIterator &end); |
| |
| /** |
| * @brief Read TLV Type |
| * |
| * This call is largely equivalent to tlv::readVarNumber, but exception will be thrown if type |
| * is larger than 2^32-1 (type in this library is implemented as uint32_t) |
| */ |
| template<class InputIterator> |
| inline uint32_t |
| readType(InputIterator &begin, const InputIterator &end); |
| |
| /** |
| * @brief Get number of bytes necessary to hold value of VAR-NUMBER |
| */ |
| inline size_t |
| sizeOfVarNumber(uint64_t varNumber); |
| |
| /** |
| * @brief Write VAR-NUMBER to the specified stream |
| */ |
| inline size_t |
| writeVarNumber(std::ostream &os, uint64_t varNumber); |
| |
| /** |
| * @brief Read nonNegativeInteger in NDN-TLV encoding |
| * |
| * This call will throw ndn::Tlv::Error (aka std::runtime_error) if number cannot be read |
| * |
| * Note that after call finished, begin will point to the first byte after the read VAR-NUMBER |
| * |
| * How many bytes will be read is directly controlled by the size parameter, which can be either |
| * 1, 2, 4, or 8. If the value of size is different, then an exception will be thrown. |
| */ |
| template<class InputIterator> |
| inline uint64_t |
| readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end); |
| |
| /** |
| * @brief Get number of bytes necessary to hold value of nonNegativeInteger |
| */ |
| inline size_t |
| sizeOfNonNegativeInteger(uint64_t varNumber); |
| |
| /** |
| * @brief Write nonNegativeInteger to the specified stream |
| */ |
| inline size_t |
| writeNonNegativeInteger(std::ostream &os, uint64_t varNumber); |
| |
| ///////////////////////////////////////////////////////////////////////////////// |
| ///////////////////////////////////////////////////////////////////////////////// |
| ///////////////////////////////////////////////////////////////////////////////// |
| |
| // Inline implementations |
| |
| ///////////////////////////////////////////////////////////////////////////////// |
| ///////////////////////////////////////////////////////////////////////////////// |
| ///////////////////////////////////////////////////////////////////////////////// |
| |
| template<class InputIterator> |
| inline uint64_t |
| readVarNumber(InputIterator &begin, const InputIterator &end) |
| { |
| if (begin == end) |
| throw Error("Empty buffer during TLV processing"); |
| |
| uint8_t value = *begin; |
| ++begin; |
| if (value < 253) |
| { |
| return value; |
| } |
| else if (value == 253) |
| { |
| if (end - begin < 2) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint16_t value = *reinterpret_cast<const uint16_t*>(&*begin); // kind of dangerous... but should be efficient |
| begin += 2; |
| return be16toh(value); |
| } |
| else if (value == 254) |
| { |
| if (end - begin < 4) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint32_t value = *reinterpret_cast<const uint32_t*>(&*begin); // kind of dangerous... but should be efficient |
| begin += 4; |
| return be32toh(value); |
| } |
| else // if (value == 255) |
| { |
| if (end - begin < 8) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint64_t value = *reinterpret_cast<const uint64_t*>(&*begin); |
| begin += 8; |
| |
| return be64toh(value); |
| } |
| } |
| |
| template<> |
| inline uint64_t |
| readVarNumber<std::istream_iterator<uint8_t> >(std::istream_iterator<uint8_t> &begin, |
| const std::istream_iterator<uint8_t> &end) |
| { |
| if (begin == end) |
| throw Error("Empty buffer during TLV processing"); |
| |
| uint8_t value = *begin; |
| ++begin; |
| if (value < 253) |
| { |
| return value; |
| } |
| else if (value == 253) |
| { |
| uint8_t buffer[2]; |
| int count = 0; |
| |
| while(begin != end && count < 2){ |
| buffer[count] = *begin; |
| begin++; |
| count++; |
| } |
| |
| if (count < 2) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint16_t value = *reinterpret_cast<const uint16_t*>(buffer); |
| return be16toh(value); |
| } |
| else if (value == 254) |
| { |
| uint8_t buffer[4]; |
| int count = 0; |
| |
| while(begin != end && count < 4){ |
| buffer[count] = *begin; |
| begin++; |
| count++; |
| } |
| |
| if (count < 4) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint32_t value = *reinterpret_cast<const uint32_t*>(buffer); |
| return be32toh(value); |
| } |
| else // if (value == 255) |
| { |
| uint8_t buffer[8]; |
| int count = 0; |
| |
| while(begin != end && count < 8){ |
| buffer[count] = *begin; |
| begin++; |
| count++; |
| } |
| |
| if (count < 8) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint64_t value = *reinterpret_cast<const uint64_t*>(buffer); |
| return be64toh(value); |
| } |
| } |
| |
| template<class InputIterator> |
| inline uint32_t |
| readType(InputIterator &begin, const InputIterator &end) |
| { |
| uint64_t type = readVarNumber(begin, end); |
| if (type > std::numeric_limits<uint32_t>::max()) |
| { |
| throw Error("TLV type code exceeds allowed maximum"); |
| } |
| |
| return static_cast<uint32_t> (type); |
| } |
| |
| size_t |
| sizeOfVarNumber(uint64_t varNumber) |
| { |
| if (varNumber < 253) { |
| return 1; |
| } |
| else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| return 3; |
| } |
| else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| return 5; |
| } |
| else { |
| return 9; |
| } |
| } |
| |
| inline size_t |
| writeVarNumber(std::ostream &os, uint64_t varNumber) |
| { |
| if (varNumber < 253) { |
| os.put(static_cast<uint8_t> (varNumber)); |
| return 1; |
| } |
| else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| os.put(253); |
| uint16_t value = htobe16(static_cast<uint16_t> (varNumber)); |
| os.write(reinterpret_cast<const char*> (&value), 2); |
| return 3; |
| } |
| else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| os.put(254); |
| uint32_t value = htobe32(static_cast<uint32_t> (varNumber)); |
| os.write(reinterpret_cast<const char*> (&value), 4); |
| return 5; |
| } |
| else { |
| os.put(255); |
| uint64_t value = htobe64(varNumber); |
| os.write(reinterpret_cast<const char*> (&value), 8); |
| return 9; |
| } |
| } |
| |
| template<class InputIterator> |
| inline uint64_t |
| readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end) |
| { |
| switch (size) { |
| case 1: |
| { |
| if (end - begin < 1) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint8_t value = *begin; |
| begin++; |
| return value; |
| } |
| case 2: |
| { |
| if (end - begin < 2) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint16_t value = *reinterpret_cast<const uint16_t*>(&*begin); // kind of dangerous... but should be efficient |
| begin += 2; |
| return be16toh(value); |
| } |
| case 4: |
| { |
| if (end - begin < 4) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint32_t value = *reinterpret_cast<const uint32_t*>(&*begin); // kind of dangerous... but should be efficient |
| begin += 4; |
| return be32toh(value); |
| } |
| case 8: |
| { |
| if (end - begin < 8) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint64_t value = *reinterpret_cast<const uint64_t*>(&*begin); |
| begin += 8; |
| return be64toh(value); |
| } |
| } |
| throw Error("Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"); |
| } |
| |
| template<> |
| inline uint64_t |
| readNonNegativeInteger<std::istream_iterator<uint8_t> >(size_t size, |
| std::istream_iterator<uint8_t> &begin, |
| const std::istream_iterator<uint8_t> &end) |
| { |
| switch (size) { |
| case 1: |
| { |
| if(begin == end) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint8_t value = *begin; |
| begin++; |
| return value; |
| } |
| case 2: |
| { |
| uint8_t buffer[2]; |
| int count = 0; |
| |
| while(begin != end && count < 2){ |
| buffer[count] = *begin; |
| begin++; |
| count++; |
| } |
| |
| if (count < 2) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint16_t value = *reinterpret_cast<const uint16_t*>(buffer); |
| return be16toh(value); |
| } |
| case 4: |
| { |
| uint8_t buffer[4]; |
| int count = 0; |
| |
| while(begin != end && count < 4){ |
| buffer[count] = *begin; |
| begin++; |
| count++; |
| } |
| |
| if (count < 4) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint32_t value = *reinterpret_cast<const uint32_t*>(buffer); |
| return be32toh(value); |
| } |
| case 8: |
| { |
| uint8_t buffer[8]; |
| int count = 0; |
| |
| while(begin != end && count < 8){ |
| buffer[count] = *begin; |
| begin++; |
| count++; |
| } |
| |
| if (count < 8) |
| throw Error("Insufficient data during TLV processing"); |
| |
| uint64_t value = *reinterpret_cast<const uint64_t*>(buffer); |
| return be64toh(value); |
| } |
| } |
| throw Error("Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"); |
| } |
| |
| inline size_t |
| sizeOfNonNegativeInteger(uint64_t varNumber) |
| { |
| if (varNumber < 253) { |
| return 1; |
| } |
| else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| return 2; |
| } |
| else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| return 4; |
| } |
| else { |
| return 8; |
| } |
| } |
| |
| |
| inline size_t |
| writeNonNegativeInteger(std::ostream &os, uint64_t varNumber) |
| { |
| if (varNumber < 253) { |
| os.put(static_cast<uint8_t> (varNumber)); |
| return 1; |
| } |
| else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| uint16_t value = htobe16(static_cast<uint16_t> (varNumber)); |
| os.write(reinterpret_cast<const char*> (&value), 2); |
| return 2; |
| } |
| else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| uint32_t value = htobe32(static_cast<uint32_t> (varNumber)); |
| os.write(reinterpret_cast<const char*> (&value), 4); |
| return 4; |
| } |
| else { |
| uint64_t value = htobe64(varNumber); |
| os.write(reinterpret_cast<const char*> (&value), 8); |
| return 8; |
| } |
| } |
| |
| |
| } // namespace tlv |
| } // namespace ndn |
| |
| #endif // NDN_TLV_HPP |