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)
155 template<
typename Iterator>
157 readVarNumber(Iterator& begin,
const Iterator& end, uint64_t& number);
173 template<
typename Iterator>
175 readType(Iterator& begin,
const Iterator& end, uint32_t& type);
187 template<
typename Iterator>
203 template<
typename Iterator>
205 readType(Iterator& begin,
const Iterator& end);
233 template<
typename Iterator>
264 template<
typename Iterator>
269 operator()(
size_t size, Iterator& begin,
const Iterator& end, uint64_t& number)
const 273 for (; begin != end && count < size; ++begin, ++count) {
274 number = (number << 8) | *begin;
276 return count == size;
282 template<
typename Iterator>
287 operator()(
size_t size, Iterator& begin,
const Iterator& end, uint64_t& number)
const 289 if (begin + size > end) {
301 std::memcpy(&value, &*begin, 2);
303 number = be16toh(value);
308 std::memcpy(&value, &*begin, 4);
310 number = be32toh(value);
315 std::memcpy(&value, &*begin, 8);
317 number = be64toh(value);
333 template<
typename Iterator,
334 typename DecayedIterator =
typename std::decay<Iterator>::type,
335 typename ValueType =
typename std::iterator_traits<DecayedIterator>::value_type>
340 std::is_convertible<DecayedIterator,
typename std::basic_string<ValueType>::const_iterator>::
value ||
341 std::is_convertible<DecayedIterator,
typename std::vector<ValueType>::const_iterator>::
value) &&
342 sizeof(ValueType) == 1 &&
346 template<
typename Iterator>
347 class ReadNumber :
public std::conditional<shouldSelectContiguousReadNumber<Iterator>(),
348 ReadNumberFast<Iterator>, ReadNumberSlow<Iterator>>::type
354 template<
typename Iterator>
361 uint8_t firstOctet = *begin;
363 if (firstOctet < 253) {
368 size_t size = firstOctet == 253 ? 2 :
369 firstOctet == 254 ? 4 : 8;
373 template<
typename Iterator>
375 readType(Iterator& begin,
const Iterator& end, uint32_t& type)
379 if (!isOk || number > std::numeric_limits<uint32_t>::max()) {
383 type =
static_cast<uint32_t
>(number);
387 template<
typename Iterator>
392 BOOST_THROW_EXCEPTION(
Error(
"Empty buffer during TLV processing"));
397 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
403 template<
typename Iterator>
408 if (type > std::numeric_limits<uint32_t>::max()) {
409 BOOST_THROW_EXCEPTION(
Error(
"TLV-TYPE number exceeds allowed maximum"));
412 return static_cast<uint32_t
>(type);
418 return number < 253 ? 1 :
419 number <= std::numeric_limits<uint16_t>::max() ? 3 :
420 number <= std::numeric_limits<uint32_t>::max() ? 5 : 9;
427 os.put(static_cast<char>(number));
430 else if (number <= std::numeric_limits<uint16_t>::max()) {
431 os.put(static_cast<char>(253));
432 uint16_t
value = htobe16(static_cast<uint16_t>(number));
433 os.write(reinterpret_cast<const char*>(&value), 2);
436 else if (number <= std::numeric_limits<uint32_t>::max()) {
437 os.put(static_cast<char>(254));
438 uint32_t
value = htobe32(static_cast<uint32_t>(number));
439 os.write(reinterpret_cast<const char*>(&value), 4);
443 os.put(static_cast<char>(255));
444 uint64_t
value = htobe64(number);
445 os.write(reinterpret_cast<const char*>(&value), 8);
450 template<
typename Iterator>
454 if (size != 1 && size != 2 && size != 4 && size != 8) {
455 BOOST_THROW_EXCEPTION(
Error(
"Invalid length for nonNegativeInteger " 456 "(only 1, 2, 4, and 8 are allowed)"));
462 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
471 return integer <= std::numeric_limits<uint8_t>::max() ? 1 :
472 integer <= std::numeric_limits<uint16_t>::max() ? 2 :
473 integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8;
479 if (integer <= std::numeric_limits<uint8_t>::max()) {
480 os.put(static_cast<char>(integer));
483 else if (integer <= std::numeric_limits<uint16_t>::max()) {
484 uint16_t
value = htobe16(static_cast<uint16_t>(integer));
485 os.write(reinterpret_cast<const char*>(&value), 2);
488 else if (integer <= std::numeric_limits<uint32_t>::max()) {
489 uint32_t
value = htobe32(static_cast<uint32_t>(integer));
490 os.write(reinterpret_cast<const char*>(&value), 4);
494 uint64_t
value = htobe64(integer);
495 os.write(reinterpret_cast<const char*>(&value), 8);
504 #endif // NDN_ENCODING_TLV_HPP Represents a signature of Sha256WithRsa type.
bool operator()(size_t size, Iterator &begin, const Iterator &end, uint64_t &number) const
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
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
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
std::ostream & operator<<(std::ostream &os, const SignatureTypeValue &signatureType)
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.