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)
153 template<
typename Iterator>
155 readVarNumber(Iterator& begin,
const Iterator& end, uint64_t& number);
171 template<
typename Iterator>
173 readType(Iterator& begin,
const Iterator& end, uint32_t& type);
185 template<
typename Iterator>
201 template<
typename Iterator>
203 readType(Iterator& begin,
const Iterator& end);
231 template<
typename Iterator>
262 template<
typename Iterator>
267 operator()(
size_t size, Iterator& begin,
const Iterator& end, uint64_t& number)
const 271 for (; begin != end && count < size; ++begin, ++count) {
272 number = (number << 8) | *begin;
274 return count == size;
280 template<
typename Iterator>
285 operator()(
size_t size, Iterator& begin,
const Iterator& end, uint64_t& number)
const 287 if (begin + size > end) {
299 std::memcpy(&value, &*begin, 2);
301 number = be16toh(value);
306 std::memcpy(&value, &*begin, 4);
308 number = be32toh(value);
313 std::memcpy(&value, &*begin, 8);
315 number = be64toh(value);
331 template<
typename Iterator,
332 typename DecayedIterator =
typename std::decay<Iterator>::type,
333 typename ValueType =
typename std::iterator_traits<DecayedIterator>::value_type>
337 return (std::is_convertible<DecayedIterator, const ValueType*>::value ||
338 std::is_convertible<DecayedIterator,
typename std::basic_string<ValueType>::const_iterator>::value ||
339 std::is_convertible<DecayedIterator,
typename std::vector<ValueType>::const_iterator>::value) &&
340 sizeof(ValueType) == 1 &&
341 !std::is_same<ValueType, bool>::value;
344 template<
typename Iterator>
345 class ReadNumber :
public std::conditional<shouldSelectContiguousReadNumber<Iterator>(),
346 ReadNumberFast<Iterator>, ReadNumberSlow<Iterator>>::type
352 template<
typename Iterator>
359 uint8_t firstOctet = *begin;
361 if (firstOctet < 253) {
366 size_t size = firstOctet == 253 ? 2 :
367 firstOctet == 254 ? 4 : 8;
371 template<
typename Iterator>
373 readType(Iterator& begin,
const Iterator& end, uint32_t& type)
377 if (!isOk || number > std::numeric_limits<uint32_t>::max()) {
381 type =
static_cast<uint32_t
>(number);
385 template<
typename Iterator>
390 BOOST_THROW_EXCEPTION(
Error(
"Empty buffer during TLV processing"));
395 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
401 template<
typename Iterator>
406 if (type > std::numeric_limits<uint32_t>::max()) {
407 BOOST_THROW_EXCEPTION(
Error(
"TLV-TYPE number exceeds allowed maximum"));
410 return static_cast<uint32_t
>(type);
416 return number < 253 ? 1 :
417 number <= std::numeric_limits<uint16_t>::max() ? 3 :
418 number <= std::numeric_limits<uint32_t>::max() ? 5 : 9;
425 os.put(static_cast<char>(number));
428 else if (number <= std::numeric_limits<uint16_t>::max()) {
429 os.put(static_cast<char>(253));
430 uint16_t value = htobe16(static_cast<uint16_t>(number));
431 os.write(reinterpret_cast<const char*>(&value), 2);
434 else if (number <= std::numeric_limits<uint32_t>::max()) {
435 os.put(static_cast<char>(254));
436 uint32_t value = htobe32(static_cast<uint32_t>(number));
437 os.write(reinterpret_cast<const char*>(&value), 4);
441 os.put(static_cast<char>(255));
442 uint64_t value = htobe64(number);
443 os.write(reinterpret_cast<const char*>(&value), 8);
448 template<
typename Iterator>
452 if (size != 1 && size != 2 && size != 4 && size != 8) {
453 BOOST_THROW_EXCEPTION(
Error(
"Invalid length for nonNegativeInteger " 454 "(only 1, 2, 4, and 8 are allowed)"));
460 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
469 return integer <= std::numeric_limits<uint8_t>::max() ? 1 :
470 integer <= std::numeric_limits<uint16_t>::max() ? 2 :
471 integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8;
477 if (integer <= std::numeric_limits<uint8_t>::max()) {
478 os.put(static_cast<char>(integer));
481 else if (integer <= std::numeric_limits<uint16_t>::max()) {
482 uint16_t value = htobe16(static_cast<uint16_t>(integer));
483 os.write(reinterpret_cast<const char*>(&value), 2);
486 else if (integer <= std::numeric_limits<uint32_t>::max()) {
487 uint32_t value = htobe32(static_cast<uint32_t>(integer));
488 os.write(reinterpret_cast<const char*>(&value), 4);
492 uint64_t value = htobe64(integer);
493 os.write(reinterpret_cast<const char*>(&value), 8);
502 #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
Represents a signature of DigestSha256 type.
represents an Interest packet
std::ostream & operator<<(std::ostream &os, SignatureTypeValue signatureType)
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.