22 #ifndef NDN_ENCODING_TLV_HPP 23 #define NDN_ENCODING_TLV_HPP 32 #include <boost/endian/conversion.hpp> 51 class Error :
public std::runtime_error
54 using std::runtime_error::runtime_error;
56 Error(
const char* expectedType, uint32_t actualType);
179 return type <= 31 || (type & 0x01);
193 template<
typename Iterator>
195 readVarNumber(Iterator& begin, Iterator end, uint64_t& number) noexcept;
210 template<
typename Iterator>
212 readType(Iterator& begin, Iterator end, uint32_t& type) noexcept;
224 template<
typename Iterator>
240 template<
typename Iterator>
242 readType(Iterator& begin, Iterator end);
270 template<
typename Iterator>
301 template<
typename Iterator>
306 operator()(
size_t size, Iterator& begin, Iterator end, uint64_t& number)
const noexcept
310 for (; begin != end && count < size; ++begin, ++count) {
311 number = (number << 8) | *begin;
313 return count == size;
319 template<
typename Iterator>
324 operator()(
size_t size, Iterator& begin, Iterator end, uint64_t& number)
const noexcept
326 if (begin + size > end) {
338 std::memcpy(&value, &*begin, 2);
340 number = boost::endian::big_to_native(value);
345 std::memcpy(&value, &*begin, 4);
347 number = boost::endian::big_to_native(value);
352 std::memcpy(&value, &*begin, 8);
354 number = boost::endian::big_to_native(value);
369 template<
typename Iterator,
370 typename DecayedIterator = std::decay_t<Iterator>,
371 typename ValueType =
typename std::iterator_traits<DecayedIterator>::value_type>
375 return (std::is_convertible<DecayedIterator, const ValueType*>::value ||
376 std::is_convertible<DecayedIterator,
typename std::basic_string<ValueType>::const_iterator>::value ||
377 std::is_convertible<DecayedIterator,
typename std::vector<ValueType>::const_iterator>::value) &&
378 sizeof(ValueType) == 1 &&
379 !std::is_same<ValueType, bool>::value;
382 template<
typename Iterator>
383 class ReadNumber :
public std::conditional_t<shouldSelectContiguousReadNumber<Iterator>(),
384 ReadNumberFast<Iterator>, ReadNumberSlow<Iterator>>
390 template<
typename Iterator>
397 uint8_t firstOctet = *begin;
399 if (firstOctet < 253) {
404 size_t size = firstOctet == 253 ? 2 :
405 firstOctet == 254 ? 4 : 8;
409 template<
typename Iterator>
411 readType(Iterator& begin, Iterator end, uint32_t& type) noexcept
415 if (!isOk || number ==
Invalid || number > std::numeric_limits<uint32_t>::max()) {
419 type =
static_cast<uint32_t
>(number);
423 template<
typename Iterator>
440 template<
typename Iterator>
445 if (type ==
Invalid || type > std::numeric_limits<uint32_t>::max()) {
449 return static_cast<uint32_t
>(type);
455 return number < 253 ? 1 :
456 number <= std::numeric_limits<uint16_t>::max() ? 3 :
457 number <= std::numeric_limits<uint32_t>::max() ? 5 : 9;
464 os.put(static_cast<char>(number));
467 else if (number <= std::numeric_limits<uint16_t>::max()) {
468 os.put(static_cast<char>(253));
469 uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(number));
470 os.write(reinterpret_cast<const char*>(&value), 2);
473 else if (number <= std::numeric_limits<uint32_t>::max()) {
474 os.put(static_cast<char>(254));
475 uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(number));
476 os.write(reinterpret_cast<const char*>(&value), 4);
480 os.put(static_cast<char>(255));
481 uint64_t value = boost::endian::native_to_big(number);
482 os.write(reinterpret_cast<const char*>(&value), 8);
487 template<
typename Iterator>
491 if (size != 1 && size != 2 && size != 4 && size != 8) {
498 NDN_THROW(
Error(
"Insufficient data during nonNegativeInteger parsing"));
507 return integer <= std::numeric_limits<uint8_t>::max() ? 1 :
508 integer <= std::numeric_limits<uint16_t>::max() ? 2 :
509 integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8;
515 if (integer <= std::numeric_limits<uint8_t>::max()) {
516 os.put(static_cast<char>(integer));
519 else if (integer <= std::numeric_limits<uint16_t>::max()) {
520 uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(integer));
521 os.write(reinterpret_cast<const char*>(&value), 2);
524 else if (integer <= std::numeric_limits<uint32_t>::max()) {
525 uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(integer));
526 os.write(reinterpret_cast<const char*>(&value), 4);
530 uint64_t value = boost::endian::native_to_big(integer);
531 os.write(reinterpret_cast<const char*>(&value), 8);
539 #endif // NDN_ENCODING_TLV_HPP Represents a signature of Sha256WithRsa type.
constexpr size_t sizeOfNonNegativeInteger(uint64_t integer) noexcept
Get the number of bytes necessary to hold the value of integer encoded as nonNegativeInteger.
Represents a SignatureInfo or InterestSignatureInfo TLV element.
std::ostream & operator<<(std::ostream &os, SignatureTypeValue st)
std::string to_string(const T &val)
constexpr bool operator()(size_t size, Iterator &begin, Iterator end, uint64_t &number) const noexcept
constexpr bool operator()(size_t size, Iterator &begin, Iterator end, uint64_t &number) const noexcept
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.
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, Iterator end)
Read nonNegativeInteger in NDN-TLV encoding.
File-Like ICN Collection.
another name that identifies the actual data content
size_t writeNonNegativeInteger(std::ostream &os, uint64_t integer)
Write nonNegativeInteger to the specified stream.
size_t writeVarNumber(std::ostream &os, uint64_t number)
Write VAR-NUMBER to the specified stream.
Represents a signature of Sha256WithEcdsa type.
constexpr size_t sizeOfVarNumber(uint64_t number) noexcept
Get the number of bytes necessary to hold the value of number encoded as VAR-NUMBER.
bool readType(Iterator &begin, Iterator end, uint32_t &type) noexcept
Read TLV-TYPE.
#define NDN_CXX_NODISCARD
Common includes and macros used throughout the library.
constexpr bool shouldSelectContiguousReadNumber()
Determine whether to select ReadNumber implementation for ContiguousIterator.
Represents an absolute name.
ContentTypeValue
ContentType values.
SignatureTypeValue
SignatureType values.
Function object to read a number from InputIterator.
#define NDN_CXX_UNREACHABLE
Represents a Data packet.
Error(const char *expectedType, uint32_t actualType)
represents an error in TLV encoding or decoding
bool readVarNumber(Iterator &begin, Iterator end, uint64_t &number) noexcept
Read VAR-NUMBER in NDN-TLV encoding.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Function object to read a number from ContiguousIterator.