31 #include <boost/asio/buffer.hpp> 32 #include <boost/range/adaptor/reversed.hpp> 37 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
51 :
Block(buffer.getBuffer(), buffer.begin(), buffer.end(), true)
70 NDN_THROW(std::invalid_argument(
"Buffer is empty"));
73 const uint8_t* bufferBegin = &
m_buffer->front();
74 const uint8_t* bufferEnd = bufferBegin +
m_buffer->size();
75 if (&*begin < bufferBegin || &*begin > bufferEnd ||
76 &*end < bufferBegin || &*end > bufferEnd) {
77 NDN_THROW(std::invalid_argument(
"Begin/end iterators point outside the buffer"));
96 Buffer::const_iterator
begin, Buffer::const_iterator
end,
97 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
110 const uint8_t* pos = buf;
111 const uint8_t*
const end = buf + bufSize;
117 BOOST_ASSERT(pos <= end);
118 if (length > static_cast<uint64_t>(end - pos)) {
119 NDN_THROW(
Error(
"Not enough bytes in the buffer to fully parse TLV"));
122 BOOST_ASSERT(pos > buf);
123 uint64_t typeLengthSize =
static_cast<uint64_t
>(pos - buf);
124 m_size = typeLengthSize + length;
163 std::istream_iterator<uint8_t>
begin(is >> std::noskipws);
164 std::istream_iterator<uint8_t>
end;
173 if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) {
178 uint8_t* valueBuf = eb.buf();
179 is.read(reinterpret_cast<char*>(valueBuf), length);
180 if (length != static_cast<uint64_t>(is.gcount())) {
181 NDN_THROW(
Error(
"Not enough bytes from stream to fully parse TLV"));
184 eb.prependVarNumber(length);
185 eb.prependVarNumber(type);
189 return Block(eb.getBuffer());
192 std::tuple<bool, Block>
195 auto begin = buffer->begin() + offset;
201 return std::make_tuple(
false,
Block());
207 return std::make_tuple(
false,
Block());
211 if (length > static_cast<uint64_t>(buffer->end() - pos)) {
212 return std::make_tuple(
false,
Block());
215 return std::make_tuple(
true,
Block(
std::move(buffer), type,
begin, pos + length, pos, pos + length));
218 std::tuple<bool, Block>
221 const uint8_t* pos = buf;
222 const uint8_t*
const end = buf + bufSize;
227 return std::make_tuple(
false,
Block());
232 return std::make_tuple(
false,
Block());
236 if (length > static_cast<uint64_t>(end - pos)) {
237 return std::make_tuple(
false,
Block());
240 size_t typeLengthSize = pos - buf;
241 auto b = make_shared<Buffer>(buf, pos + length);
242 return std::make_tuple(
true,
Block(b, type, b->begin(), b->end(),
243 b->begin() + typeLengthSize, b->end()));
261 Buffer::const_iterator
270 Buffer::const_iterator
332 while (begin != end) {
333 Buffer::const_iterator pos =
begin;
337 if (length > static_cast<uint64_t>(end - pos)) {
340 " exceeds TLV-VALUE boundary of parent block"));
344 Buffer::const_iterator subEnd = pos + length;
358 size_t estimatedSize =
encode(estimator);
371 size_t len = encodeValue(estimator);
372 len += estimator.prependVarNumber(len);
373 len += estimator.prependVarNumber(
m_type);
382 len += element.encode(estimator);
391 m_end = encoder.begin();
397 len += element.encode(encoder);
403 len += encoder.prependVarNumber(len);
404 len += encoder.prependVarNumber(
m_type);
415 auto it = this->
find(type);
428 [
type] (
const Block& subBlock) {
return subBlock.type() ==
type; });
437 [
type] (
const Block& subBlock) {
return subBlock.type() ==
type; });
471 Block::operator boost::asio::const_buffer()
const 473 return boost::asio::const_buffer(
wire(),
size());
488 auto oldFmt = os.flags(std::ios_base::dec);
495 size_t tlvLength = block.encodeValue(estimator);
496 os << block.
type() <<
'[' << tlvLength <<
"]={";
505 os << block.
type() <<
"[empty]";
513 operator "" _block(
const char* input, std::size_t len)
515 namespace t = security::transform;
520 for (
const char*
end = input + len; input !=
end; ++input) {
521 if (std::strchr(
"0123456789ABCDEF", *input) !=
nullptr) {
522 ss.write(reinterpret_cast<const uint8_t*>(input), 1);
529 catch (
const t::Error&) {
530 NDN_THROW(std::invalid_argument(
"Input has odd number of hexadecimal digits"));
size_t m_size
Total size including Type-Length-Value.
shared_ptr< const Buffer > m_buffer
Underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields.
static Block fromStream(std::istream &is)
Parse Block from an input stream.
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to parse Block from a wire buffer.
Buffer::const_iterator m_valueBegin
Buffer::const_iterator end() const
Get end iterator of encoded wire.
Buffer::const_iterator m_begin
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
element_container::const_iterator element_const_iterator
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
element_container m_elements
Contains the sub-elements.
friend std::ostream & operator<<(std::ostream &os, const Block &block)
Print block to os.
Represents a TLV element of NDN packet format.
void resetWire() noexcept
Reset wire buffer but keep TLV-TYPE and sub-elements (if any)
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Buffer::const_iterator m_valueEnd
size_t size() const
Return the size of the encoded wire, i.e.
constexpr size_t sizeOfVarNumber(uint64_t number) noexcept
Get the number of bytes necessary to hold the value of number encoded as VAR-NUMBER.
Block blockFromValue() const
void reset() noexcept
Reset the Block to a default-constructed state.
bool readType(Iterator &begin, Iterator end, uint32_t &type) noexcept
Read TLV-TYPE.
Block & operator=(const Block &)
Copy assignment operator.
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
element_container::iterator element_iterator
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
void push_back(const Block &element)
Append a sub-element.
void parse() const
Parse TLV-VALUE into sub-elements.
uint32_t type() const
Return the TLV-TYPE of the Block.
bool isValid() const noexcept
Check if the Block is valid.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
bool hasValue() const noexcept
Check if the Block has a non-empty TLV-VALUE.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
void encode()
Encode sub-elements into TLV-VALUE.
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Buffer::const_iterator m_end
bool operator==(const Data &lhs, const Data &rhs)
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
implements an output stream that constructs ndn::Buffer
std::string to_string(const V &v)
element_const_iterator elements_end() const
Equivalent to elements().end()
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Block()
Create an invalid Block.
EncodingImpl< EncoderTag > EncodingBuffer
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
EncodingImpl< EstimatorTag > EncodingEstimator
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
const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM
shared_ptr< const Buffer > ConstBufferPtr