22 #ifndef NDN_ENCODING_TLV_HPP 23 #define NDN_ENCODING_TLV_HPP 30 #include <type_traits> 33 #include <boost/endian/conversion.hpp> 52 class Error :
public std::runtime_error
55 using std::runtime_error::runtime_error;
57 Error(
const char* expectedType, uint32_t actualType);
176 return type <= 31 || (type & 0x01);
190 template<
typename Iterator>
192 readVarNumber(Iterator& begin, Iterator end, uint64_t& number) noexcept;
207 template<
typename Iterator>
209 readType(Iterator& begin, Iterator end, uint32_t& type) noexcept;
221 template<
typename Iterator>
237 template<
typename Iterator>
239 readType(Iterator& begin, Iterator end);
267 template<
typename Iterator>
298 template<
typename Iterator>
303 operator()(
size_t size, Iterator& begin, Iterator end, uint64_t& number)
const noexcept
307 for (; begin != end && count < size; ++begin, ++count) {
308 number = (number << 8) | *begin;
310 return count == size;
316 template<
typename Iterator>
321 operator()(
size_t size, Iterator& begin, Iterator end, uint64_t& number)
const noexcept
323 if (begin + size > end) {
335 std::memcpy(&value, &*begin, 2);
337 number = boost::endian::big_to_native(value);
342 std::memcpy(&value, &*begin, 4);
344 number = boost::endian::big_to_native(value);
349 std::memcpy(&value, &*begin, 8);
351 number = boost::endian::big_to_native(value);
367 template<
typename Iterator,
368 typename DecayedIterator = std::decay_t<Iterator>,
369 typename ValueType =
typename std::iterator_traits<DecayedIterator>::value_type>
373 return (std::is_convertible<DecayedIterator, const ValueType*>::value ||
374 std::is_convertible<DecayedIterator,
typename std::basic_string<ValueType>::const_iterator>::value ||
375 std::is_convertible<DecayedIterator,
typename std::vector<ValueType>::const_iterator>::value) &&
376 sizeof(ValueType) == 1 &&
377 !std::is_same<ValueType, bool>::value;
380 template<
typename Iterator>
381 class ReadNumber :
public std::conditional_t<shouldSelectContiguousReadNumber<Iterator>(),
382 ReadNumberFast<Iterator>, ReadNumberSlow<Iterator>>
388 template<
typename Iterator>
395 uint8_t firstOctet = *begin;
397 if (firstOctet < 253) {
402 size_t size = firstOctet == 253 ? 2 :
403 firstOctet == 254 ? 4 : 8;
407 template<
typename Iterator>
409 readType(Iterator& begin, Iterator end, uint32_t& type) noexcept
413 if (!isOk || number ==
Invalid || number > std::numeric_limits<uint32_t>::max()) {
417 type =
static_cast<uint32_t
>(number);
421 template<
typename Iterator>
438 template<
typename Iterator>
443 if (type ==
Invalid || type > std::numeric_limits<uint32_t>::max()) {
447 return static_cast<uint32_t
>(type);
453 return number < 253 ? 1 :
454 number <= std::numeric_limits<uint16_t>::max() ? 3 :
455 number <= std::numeric_limits<uint32_t>::max() ? 5 : 9;
462 os.put(static_cast<char>(number));
465 else if (number <= std::numeric_limits<uint16_t>::max()) {
466 os.put(static_cast<char>(253));
467 uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(number));
468 os.write(reinterpret_cast<const char*>(&value), 2);
471 else if (number <= std::numeric_limits<uint32_t>::max()) {
472 os.put(static_cast<char>(254));
473 uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(number));
474 os.write(reinterpret_cast<const char*>(&value), 4);
478 os.put(static_cast<char>(255));
479 uint64_t value = boost::endian::native_to_big(number);
480 os.write(reinterpret_cast<const char*>(&value), 8);
485 template<
typename Iterator>
489 if (size != 1 && size != 2 && size != 4 && size != 8) {
496 NDN_THROW(
Error(
"Insufficient data during nonNegativeInteger parsing"));
505 return integer <= std::numeric_limits<uint8_t>::max() ? 1 :
506 integer <= std::numeric_limits<uint16_t>::max() ? 2 :
507 integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8;
513 if (integer <= std::numeric_limits<uint8_t>::max()) {
514 os.put(static_cast<char>(integer));
517 else if (integer <= std::numeric_limits<uint16_t>::max()) {
518 uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(integer));
519 os.write(reinterpret_cast<const char*>(&value), 2);
522 else if (integer <= std::numeric_limits<uint32_t>::max()) {
523 uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(integer));
524 os.write(reinterpret_cast<const char*>(&value), 4);
528 uint64_t value = boost::endian::native_to_big(integer);
529 os.write(reinterpret_cast<const char*>(&value), 8);
537 #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 TLV element.
std::ostream & operator<<(std::ostream &os, SignatureTypeValue st)
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.
import common constructs for ndn-cxx library internal use
Abstraction implementing Interest selectors.
constexpr bool shouldSelectContiguousReadNumber()
Determine whether to select ReadNumber implementation for ContiguousIterator.
Represents an absolute name.
ContentTypeValue
ContentType values.
SignatureTypeValue
SignatureType values.
constexpr int NameComponent
Function object to read a number from InputIterator.
std::string to_string(const V &v)
Represents a Data packet.
util::scheduler::EventId deprecated
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.
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.