22 #ifndef NDN_ENCODING_TLV_HPP 23 #define NDN_ENCODING_TLV_HPP 31 #include <type_traits> 50 class Error :
public std::runtime_error
55 :
std::runtime_error(what)
162 return type <= 31 || (type & 0x01);
177 template<
typename Iterator>
179 readVarNumber(Iterator& begin,
const Iterator& end, uint64_t& number);
195 template<
typename Iterator>
197 readType(Iterator& begin,
const Iterator& end, uint32_t& type);
209 template<
typename Iterator>
225 template<
typename Iterator>
227 readType(Iterator& begin,
const Iterator& end);
255 template<
typename Iterator>
286 template<
typename Iterator>
291 operator()(
size_t size, Iterator& begin,
const Iterator& end, uint64_t& number)
const 295 for (; begin != end && count < size; ++begin, ++count) {
296 number = (number << 8) | *begin;
298 return count == size;
304 template<
typename Iterator>
309 operator()(
size_t size, Iterator& begin,
const Iterator& end, uint64_t& number)
const 311 if (begin + size > end) {
323 std::memcpy(&value, &*begin, 2);
325 number = be16toh(value);
330 std::memcpy(&value, &*begin, 4);
332 number = be32toh(value);
337 std::memcpy(&value, &*begin, 8);
339 number = be64toh(value);
355 template<
typename Iterator,
356 typename DecayedIterator =
typename std::decay<Iterator>::type,
357 typename ValueType =
typename std::iterator_traits<DecayedIterator>::value_type>
361 return (std::is_convertible<DecayedIterator, const ValueType*>::value ||
362 std::is_convertible<DecayedIterator,
typename std::basic_string<ValueType>::const_iterator>::value ||
363 std::is_convertible<DecayedIterator,
typename std::vector<ValueType>::const_iterator>::value) &&
364 sizeof(ValueType) == 1 &&
365 !std::is_same<ValueType, bool>::value;
368 template<
typename Iterator>
369 class ReadNumber :
public std::conditional<shouldSelectContiguousReadNumber<Iterator>(),
370 ReadNumberFast<Iterator>, ReadNumberSlow<Iterator>>::type
376 template<
typename Iterator>
383 uint8_t firstOctet = *begin;
385 if (firstOctet < 253) {
390 size_t size = firstOctet == 253 ? 2 :
391 firstOctet == 254 ? 4 : 8;
395 template<
typename Iterator>
397 readType(Iterator& begin,
const Iterator& end, uint32_t& type)
401 if (!isOk || number > std::numeric_limits<uint32_t>::max()) {
405 type =
static_cast<uint32_t
>(number);
409 template<
typename Iterator>
414 BOOST_THROW_EXCEPTION(
Error(
"Empty buffer during TLV processing"));
419 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
425 template<
typename Iterator>
430 if (type > std::numeric_limits<uint32_t>::max()) {
431 BOOST_THROW_EXCEPTION(
Error(
"TLV-TYPE number exceeds allowed maximum"));
434 return static_cast<uint32_t
>(type);
440 return number < 253 ? 1 :
441 number <= std::numeric_limits<uint16_t>::max() ? 3 :
442 number <= std::numeric_limits<uint32_t>::max() ? 5 : 9;
449 os.put(static_cast<char>(number));
452 else if (number <= std::numeric_limits<uint16_t>::max()) {
453 os.put(static_cast<char>(253));
454 uint16_t value = htobe16(static_cast<uint16_t>(number));
455 os.write(reinterpret_cast<const char*>(&value), 2);
458 else if (number <= std::numeric_limits<uint32_t>::max()) {
459 os.put(static_cast<char>(254));
460 uint32_t value = htobe32(static_cast<uint32_t>(number));
461 os.write(reinterpret_cast<const char*>(&value), 4);
465 os.put(static_cast<char>(255));
466 uint64_t value = htobe64(number);
467 os.write(reinterpret_cast<const char*>(&value), 8);
472 template<
typename Iterator>
476 if (size != 1 && size != 2 && size != 4 && size != 8) {
477 BOOST_THROW_EXCEPTION(
Error(
"Invalid length for nonNegativeInteger " 478 "(only 1, 2, 4, and 8 are allowed)"));
484 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
493 return integer <= std::numeric_limits<uint8_t>::max() ? 1 :
494 integer <= std::numeric_limits<uint16_t>::max() ? 2 :
495 integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8;
501 if (integer <= std::numeric_limits<uint8_t>::max()) {
502 os.put(static_cast<char>(integer));
505 else if (integer <= std::numeric_limits<uint16_t>::max()) {
506 uint16_t value = htobe16(static_cast<uint16_t>(integer));
507 os.write(reinterpret_cast<const char*>(&value), 2);
510 else if (integer <= std::numeric_limits<uint32_t>::max()) {
511 uint32_t value = htobe32(static_cast<uint32_t>(integer));
512 os.write(reinterpret_cast<const char*>(&value), 4);
516 uint64_t value = htobe64(integer);
517 os.write(reinterpret_cast<const char*>(&value), 8);
526 #endif // NDN_ENCODING_TLV_HPP Represents a signature of Sha256WithRsa type.
Copyright (c) 2011-2015 Regents of the University of California.
Error(const std::string &what)
bool readType(Iterator &begin, const Iterator &end, uint32_t &type)
Read TLV-TYPE.
Represents a SignatureInfo TLV element.
indicates a producer generated NACK
constexpr bool isCriticalType(uint32_t type)
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
Represents a signature of DigestSha256 type.
Represents an Interest packet.
std::ostream & operator<<(std::ostream &os, SignatureTypeValue signatureType)
constexpr int NameComponent NDN_CXX_DEPRECATED
size_t writeNonNegativeInteger(std::ostream &os, uint64_t integer)
Write nonNegativeInteger to the specified stream.
bool readVarNumber(Iterator &begin, const Iterator &end, uint64_t &number)
Read VAR-NUMBER in NDN-TLV encoding.
size_t writeVarNumber(std::ostream &os, uint64_t number)
Write VAR-NUMBER to the specified stream.
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, const Iterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
Represents a signature of Sha256WithEcdsa type.
constexpr size_t sizeOfNonNegativeInteger(uint64_t integer)
Get number of bytes necessary to hold value of nonNegativeInteger.
Abstraction implementing Interest selectors.
constexpr bool shouldSelectContiguousReadNumber()
Determine whether to select ReadNumber implementation for ContiguousIterator.
Represents an absolute name.
ContentTypeValue
indicates a possible value of ContentType field
indicates content is the actual data bits
bool operator()(size_t size, Iterator &begin, const Iterator &end, uint64_t &number) const
Function object to read a number from InputIterator.
indicates content is a public key
constexpr size_t sizeOfVarNumber(uint64_t number)
Get number of bytes necessary to hold value of VAR-NUMBER.
Represents a Data packet.
bool operator()(size_t size, Iterator &begin, const Iterator &end, uint64_t &number) const
indicates content is another name which identifies actual data content
represents an error in TLV encoding or decoding
Represents Exclude selector in NDN Interest.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Function object to read a number from ContiguousIterator.