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)))
134 :
Block(make_span(buf, bufSize))
145 : m_buffer(std::move(value))
146 , m_begin(m_buffer->end())
147 , m_end(m_buffer->end())
148 , m_valueBegin(m_buffer->begin())
149 , m_valueEnd(m_buffer->end())
156 : m_buffer(value.m_buffer)
157 , m_begin(m_buffer->end())
158 , m_end(m_buffer->end())
159 , m_valueBegin(value.begin())
160 , m_valueEnd(value.end())
166 std::tuple<bool, Block>
169 auto begin = std::next(buffer->begin(), offset);
171 const auto end = buffer->end();
176 return std::make_tuple(
false,
Block());
182 return std::make_tuple(
false,
Block());
186 BOOST_ASSERT(pos <=
end);
187 if (length >
static_cast<uint64_t
>(std::distance(pos,
end))) {
188 return std::make_tuple(
false,
Block());
191 return std::make_tuple(
true,
Block(std::move(buffer),
type,
begin, pos + length, pos, pos + length));
194 std::tuple<bool, Block>
197 auto pos = buffer.begin();
198 const auto end = buffer.end();
203 return std::make_tuple(
false,
Block());
208 return std::make_tuple(
false,
Block());
212 BOOST_ASSERT(pos <=
end);
213 if (length >
static_cast<uint64_t
>(std::distance(pos,
end))) {
214 return std::make_tuple(
false,
Block());
216 std::advance(pos, length);
219 auto b = std::make_shared<Buffer>(buffer.begin(), pos);
220 return std::make_tuple(
true,
Block(b,
type, b->begin(), b->end(),
221 std::prev(b->end(), length), b->end()));
224 std::tuple<bool, Block>
233 std::istream_iterator<uint8_t>
begin(is >> std::noskipws);
234 std::istream_iterator<uint8_t>
end;
248 uint8_t* valueBuf = eb.data();
249 is.read(
reinterpret_cast<char*
>(valueBuf), length);
250 if (length !=
static_cast<uint64_t
>(is.gcount())) {
251 NDN_THROW(
Error(
"Not enough bytes from stream to fully parse TLV"));
254 eb.prependVarNumber(length);
255 eb.prependVarNumber(
type);
259 return Block(eb.getBuffer());
277 Buffer::const_iterator
286 Buffer::const_iterator
353 if (length >
static_cast<uint64_t
>(
end - pos)) {
356 " exceeds TLV-VALUE boundary of parent block"));
360 auto subEnd = std::next(pos, length);
374 size_t estimatedSize =
encode(estimator);
387 size_t len = encodeValue(estimator);
388 len += estimator.prependVarNumber(len);
389 len += estimator.prependVarNumber(
m_type);
398 len += element.encode(estimator);
407 m_end = encoder.begin();
413 len += element.encode(encoder);
419 len += encoder.prependVarNumber(len);
420 len += encoder.prependVarNumber(
m_type);
431 auto it = this->
find(type);
444 [
type] (
const Block& subBlock) { return subBlock.type() == type; });
453 [
type] (
const Block& subBlock) { return subBlock.type() == type; });
487 Block::operator boost::asio::const_buffer()
const
489 return {wire(), size()};
504 auto oldFmt = os.flags(std::ios_base::dec);
511 size_t tlvLength = block.encodeValue(estimator);
512 os << block.
type() <<
'[' << tlvLength <<
"]={";
521 os << block.
type() <<
"[empty]";
529 operator "" _block(
const char* input, std::size_t len)
531 namespace t = security::transform;
536 for (
const char* end = input + len; input != end; ++input) {
537 if (std::strchr(
"0123456789ABCDEF", *input) !=
nullptr) {
538 ss.write({
reinterpret_cast<const uint8_t*
>(input), 1});
545 catch (
const t::Error&) {
546 NDN_THROW(std::invalid_argument(
"Input has odd number of hexadecimal digits"));
Represents a TLV element of the NDN packet format.
element_container::const_iterator element_const_iterator
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
uint32_t type() const
Return the TLV-TYPE of the Block.
Block blockFromValue() const
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
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.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Buffer::const_iterator end() const
Get end iterator of encoded wire.
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
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
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.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
element_const_iterator elements_end() const
Equivalent to elements().end()
element_const_iterator elements_begin() const
Equivalent to elements().begin()
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 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, aka 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.
implements an output stream that constructs ndn::Buffer
shared_ptr< Buffer > buf()
Flush written data to the stream and return 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.