22 #ifndef NDN_CXX_ENCODING_TLV_HPP
23 #define NDN_CXX_ENCODING_TLV_HPP
32 #include <boost/endian/conversion.hpp>
53 class Error :
public std::runtime_error
56 using std::runtime_error::runtime_error;
58 Error(
const char* expectedType, uint32_t actualType);
164 return type <= 31 || (type & 0x01);
178 template<
typename Iterator>
179 [[nodiscard]] constexpr
bool
180 readVarNumber(Iterator& begin, Iterator end, uint64_t& number) noexcept;
195 template<
typename Iterator>
196 [[nodiscard]] constexpr
bool
197 readType(Iterator& begin, Iterator end, uint32_t& type) noexcept;
209 template<
typename Iterator>
225 template<
typename Iterator>
227 readType(Iterator& begin, Iterator end);
232 [[nodiscard]] constexpr
size_t
255 template<
typename Iterator>
262 [[nodiscard]] constexpr
size_t
284 template<
typename Iterator,
285 typename DecayedIterator = std::decay_t<Iterator>,
286 typename ValueType =
typename std::iterator_traits<DecayedIterator>::value_type>
288 (std::is_convertible_v<DecayedIterator, const ValueType*> ||
289 std::is_convertible_v<DecayedIterator, typename std::basic_string<ValueType>::const_iterator> ||
290 std::is_convertible_v<DecayedIterator, typename std::vector<ValueType>::const_iterator>) &&
291 sizeof(ValueType) == 1 && !std::is_same_v<ValueType, bool>;
293 template<
typename Iterator>
295 readNumber(
size_t size, Iterator& begin, Iterator end, uint64_t& number) noexcept
297 if constexpr (IsContiguousIterator<Iterator>) {
299 if (begin + size > end) {
310 std::memcpy(&value, &*begin, 2);
312 number = boost::endian::big_to_native(value);
317 std::memcpy(&value, &*begin, 4);
319 number = boost::endian::big_to_native(value);
324 std::memcpy(&value, &*begin, 8);
326 number = boost::endian::big_to_native(value);
338 for (; begin != end && count < size; ++begin, ++count) {
339 number = (number << 8) | *begin;
341 return count == size;
347 template<
typename Iterator>
354 uint8_t firstOctet = *begin;
356 if (firstOctet < 253) {
361 size_t len = 1U << (firstOctet & 0b11);
365 template<
typename Iterator>
367 readType(Iterator& begin, Iterator end, uint32_t& type) noexcept
371 if (!isOk || number ==
Invalid || number > std::numeric_limits<uint32_t>::max()) {
375 type =
static_cast<uint32_t
>(number);
379 template<
typename Iterator>
392 template<
typename Iterator>
397 if (type ==
Invalid || type > std::numeric_limits<uint32_t>::max()) {
401 return static_cast<uint32_t
>(type);
407 return number < 253 ? 1 :
408 number <= std::numeric_limits<uint16_t>::max() ? 3 :
409 number <= std::numeric_limits<uint32_t>::max() ? 5 : 9;
412 template<
typename Iterator>
416 if (len != 1 && len != 2 && len != 4 && len != 8) {
423 NDN_THROW(
Error(
"Insufficient data during NonNegativeInteger parsing"));
432 return integer <= std::numeric_limits<uint8_t>::max() ? 1 :
433 integer <= std::numeric_limits<uint16_t>::max() ? 2 :
434 integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8;
#define NDN_CXX_UNREACHABLE
Represents a Data packet.
Represents an Interest packet.
Represents an absolute name.
Represents a SignatureInfo or InterestSignatureInfo TLV element.
Represents an error in TLV encoding or decoding.
Error(const char *expectedType, uint32_t actualType)
Common includes and macros used throughout the library.
std::string to_string(const errinfo_stacktrace &x)
constexpr bool readNumber(size_t size, Iterator &begin, Iterator end, uint64_t &number) noexcept
constexpr bool IsContiguousIterator
Determine whether to select the readNumber() implementation for ContiguousIterator.
@ ByteOffsetNameComponent
@ SequenceNumNameComponent
@ ParametersSha256DigestComponent
@ ImplicitSha256DigestComponent
constexpr uint64_t readNonNegativeInteger(size_t size, Iterator &begin, Iterator end)
Read a NonNegativeInteger in NDN-TLV encoding.
constexpr size_t sizeOfVarNumber(uint64_t number) noexcept
Get the number of bytes necessary to hold the value of number encoded as VAR-NUMBER.
constexpr bool readVarNumber(Iterator &begin, Iterator end, uint64_t &number) noexcept
Read VAR-NUMBER in NDN-TLV encoding.
constexpr bool readType(Iterator &begin, Iterator end, uint32_t &type) noexcept
Read TLV-TYPE.
constexpr bool isCriticalType(uint32_t type) noexcept
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
ContentTypeValue
ContentType values.
@ ContentType_Key
public key, certificate
@ ContentType_Link
another name that identifies the actual data content
@ ContentType_Blob
payload
@ ContentType_Flic
File-Like ICN Collection.
@ ContentType_PrefixAnn
prefix announcement
@ ContentType_Nack
application-level nack
size_t writeNonNegativeInteger(std::ostream &os, uint64_t integer)
Write a NonNegativeInteger to the specified stream.
std::ostream & operator<<(std::ostream &os, SignatureTypeValue st)
size_t writeVarNumber(std::ostream &os, uint64_t number)
Write VAR-NUMBER to the specified stream.
constexpr size_t sizeOfNonNegativeInteger(uint64_t integer) noexcept
Get the number of bytes necessary to hold the value of integer encoded as NonNegativeInteger.
SignatureTypeValue
SignatureType values.
@ SignatureHmacWithSha256
@ SignatureSha256WithEcdsa
constexpr size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.