32 #include <boost/asio/buffer.hpp>
33 #include <boost/range/adaptor/reversed.hpp>
38 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
53 auto pos = buffer.begin();
54 const auto end = buffer.end();
60 BOOST_ASSERT(pos <=
end);
61 if (length >
static_cast<uint64_t
>(std::distance(pos,
end))) {
62 NDN_THROW(
Error(
"Not enough bytes in the buffer to fully parse TLV"));
64 std::advance(pos, length);
67 m_buffer = std::make_shared<Buffer>(buffer.begin(), pos);
76 :
Block(buffer.getBuffer(), buffer.begin(), buffer.end(), true)
81 :
Block(buffer, buffer->begin(), buffer->end(), true)
87 : m_buffer(std::move(buffer))
90 , m_valueBegin(m_begin)
92 , m_size(static_cast<size_t>(std::distance(m_begin, m_end)))
95 NDN_THROW(std::invalid_argument(
"Buffer is empty"));
98 const uint8_t* bufferBegin = &
m_buffer->front();
99 const uint8_t* bufferEnd = bufferBegin +
m_buffer->size();
100 if (&*begin < bufferBegin || &*begin > bufferEnd ||
101 &*end < bufferBegin || &*end > bufferEnd) {
102 NDN_THROW(std::invalid_argument(
"Begin/end iterators point outside the buffer"));
116 :
Block(block.m_buffer, begin, end, verifyLength)
121 Buffer::const_iterator begin, Buffer::const_iterator end,
122 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
123 : m_buffer(std::move(buffer))
126 , m_valueBegin(valueBegin)
127 , m_valueEnd(valueEnd)
129 , m_size(static_cast<size_t>(std::distance(m_begin, m_end)))
140 : m_buffer(std::move(value))
141 , m_begin(m_buffer->end())
142 , m_end(m_buffer->end())
143 , m_valueBegin(m_buffer->begin())
144 , m_valueEnd(m_buffer->end())
151 : m_buffer(value.m_buffer)
152 , m_begin(m_buffer->end())
153 , m_end(m_buffer->end())
154 , m_valueBegin(value.begin())
155 , m_valueEnd(value.end())
161 std::tuple<bool, Block>
164 auto begin = std::next(buffer->begin(), offset);
166 const auto end = buffer->end();
171 return std::make_tuple(
false,
Block());
177 return std::make_tuple(
false,
Block());
181 BOOST_ASSERT(pos <=
end);
182 if (length >
static_cast<uint64_t
>(std::distance(pos,
end))) {
183 return std::make_tuple(
false,
Block());
186 return std::make_tuple(
true,
Block(std::move(buffer),
type,
begin, pos + length, pos, pos + length));
189 std::tuple<bool, Block>
192 auto pos = buffer.begin();
193 const auto end = buffer.end();
198 return std::make_tuple(
false,
Block());
203 return std::make_tuple(
false,
Block());
207 BOOST_ASSERT(pos <=
end);
208 if (length >
static_cast<uint64_t
>(std::distance(pos,
end))) {
209 return std::make_tuple(
false,
Block());
211 std::advance(pos, length);
214 auto b = std::make_shared<Buffer>(buffer.begin(), pos);
215 return std::make_tuple(
true,
Block(b,
type, b->begin(), b->end(),
216 std::prev(b->end(), length), b->end()));
222 std::istream_iterator<uint8_t>
begin(is >> std::noskipws);
223 std::istream_iterator<uint8_t>
end;
237 uint8_t* valueBuf = eb.data();
238 is.read(
reinterpret_cast<char*
>(valueBuf), length);
239 if (length !=
static_cast<uint64_t
>(is.gcount())) {
240 NDN_THROW(
Error(
"Not enough bytes from stream to fully parse TLV"));
243 eb.prependVarNumber(length);
244 eb.prependVarNumber(
type);
248 return Block(eb.getBuffer());
336 if (length >
static_cast<uint64_t
>(
end - pos)) {
339 " exceeds TLV-VALUE boundary of parent block"));
343 auto subEnd = std::next(pos, length);
357 size_t estimatedSize =
encode(estimator);
370 size_t len = encodeValue(estimator);
371 len += estimator.prependVarNumber(len);
372 len += estimator.prependVarNumber(
m_type);
381 len += element.encode(estimator);
390 m_end = encoder.begin();
396 len += element.encode(encoder);
402 len += encoder.prependVarNumber(len);
403 len += encoder.prependVarNumber(
m_type);
414 auto it = this->
find(type);
427 [
type] (
const Block& subBlock) { return subBlock.type() == type; });
436 [
type] (
const Block& subBlock) { return subBlock.type() == type; });
477 Block::operator boost::asio::const_buffer()
const
479 return {data(), size()};
485 return lhs.type() == rhs.type() &&
486 lhs.value_size() == rhs.value_size() &&
487 (lhs.value_size() == 0 ||
488 std::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0);
494 auto oldFmt = os.flags(std::ios_base::dec);
501 size_t tlvLength = block.encodeValue(estimator);
502 os << block.
type() <<
'[' << tlvLength <<
"]={";
511 os << block.
type() <<
"[empty]";
519 operator ""_block(
const char* input, std::size_t len)
521 namespace t = security::transform;
526 for (
const char* end = input + len; input != end; ++input) {
527 if (std::strchr(
"0123456789ABCDEF", *input) !=
nullptr) {
528 ss.write({
reinterpret_cast<const uint8_t*
>(input), 1});
535 catch (
const t::Error&) {
536 NDN_THROW(std::invalid_argument(
"Input has odd number of hexadecimal digits"));
Represents a TLV element of the NDN packet format.
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
element_container::const_iterator element_const_iterator
const uint8_t * data() const
Return a raw pointer to the beginning of the encoded wire.
Buffer::const_iterator const_iterator
const_iterator value_end() const noexcept
Get end iterator of TLV-VALUE.
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
const_iterator begin() const
Get begin iterator of encoded wire.
Block blockFromValue() const
Return a new Block constructed from the TLV-VALUE of this Block.
element_iterator erase(element_const_iterator position)
Erase a sub-element.
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
size_t size() const
Return the size of the encoded wire, i.e., of the whole TLV.
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
size_t m_size
Total size including Type-Length-Value.
Buffer::const_iterator m_valueEnd
void resetWire() noexcept
Reset wire buffer but keep TLV-TYPE and sub-elements (if any)
element_container m_elements
Contains the sub-elements.
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset=0)
Try to parse Block from a wire buffer.
void push_back(const Block &element)
Append a sub-element.
Block & operator=(const Block &)
Copy assignment operator.
Buffer::const_iterator m_end
Buffer::const_iterator m_valueBegin
bool hasValue() const noexcept
Check if the Block has a non-empty TLV-VALUE.
bool isValid() const noexcept
Check if the Block is valid.
element_container::iterator element_iterator
void encode()
Encode sub-elements into TLV-VALUE.
span< const uint8_t > value_bytes() const noexcept
Return a read-only view of TLV-VALUE as a contiguous range of bytes.
const_iterator end() const
Get end iterator of encoded wire.
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
static Block fromStream(std::istream &is)
Parse Block from an input stream.
void reset() noexcept
Reset the Block to a default-constructed state.
Buffer::const_iterator m_begin
shared_ptr< const Buffer > m_buffer
Underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields.
void parse() const
Parse TLV-VALUE into sub-elements.
const_iterator value_begin() const noexcept
Get begin iterator of TLV-VALUE.
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
size_t value_size() const noexcept
Return the size of TLV-VALUE, i.e., the TLV-LENGTH.
Block()
Create an invalid Block.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
An output stream that writes to a Buffer.
shared_ptr< Buffer > buf()
Return a shared pointer to the underlying buffer.
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
std::string to_string(const errinfo_stacktrace &x)
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 readVarNumber(Iterator &begin, Iterator end, uint64_t &number) noexcept
Read VAR-NUMBER in NDN-TLV encoding.
bool readType(Iterator &begin, Iterator end, uint32_t &type) noexcept
Read TLV-TYPE.
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
shared_ptr< const Buffer > ConstBufferPtr
bool operator==(const Data &lhs, const Data &rhs)
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
std::ostream & operator<<(std::ostream &os, const Data &data)
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM
Backport of ostream_joiner from the Library Fundamentals v2 TS.