32 #include <boost/asio/buffer.hpp>    33 #include <boost/range/adaptor/reversed.hpp>    38 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
    52   : 
Block(buffer.getBuffer(), buffer.begin(), buffer.end(), true)
    71     NDN_THROW(std::invalid_argument(
"Buffer is empty"));
    74   const uint8_t* bufferBegin = &
m_buffer->front();
    75   const uint8_t* bufferEnd = bufferBegin + 
m_buffer->size();
    76   if (&*begin < bufferBegin || &*begin > bufferEnd ||
    77       &*end   < bufferBegin || &*end   > bufferEnd) {
    78     NDN_THROW(std::invalid_argument(
"Begin/end iterators point outside the buffer"));
    97              Buffer::const_iterator 
begin, Buffer::const_iterator 
end,
    98              Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
   111   const uint8_t* pos = 
buf;
   112   const uint8_t* 
const end = buf + bufSize;
   118   BOOST_ASSERT(pos <= end);
   119   if (length > static_cast<uint64_t>(end - pos)) {
   120     NDN_THROW(
Error(
"Not enough bytes in the buffer to fully parse TLV"));
   123   BOOST_ASSERT(pos > buf);
   124   uint64_t typeLengthSize = 
static_cast<uint64_t
>(pos - 
buf);
   125   m_size = typeLengthSize + length;
   164   std::istream_iterator<uint8_t> 
begin(is >> std::noskipws);
   165   std::istream_iterator<uint8_t> 
end;
   174   if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) {
   179   uint8_t* valueBuf = eb.buf();
   180   is.read(reinterpret_cast<char*>(valueBuf), length);
   181   if (length != static_cast<uint64_t>(is.gcount())) {
   182     NDN_THROW(
Error(
"Not enough bytes from stream to fully parse TLV"));
   185   eb.prependVarNumber(length);
   186   eb.prependVarNumber(type);
   190   return Block(eb.getBuffer());
   193 std::tuple<bool, Block>
   196   auto begin = buffer->begin() + offset;
   202     return std::make_tuple(
false, 
Block());
   208     return std::make_tuple(
false, 
Block());
   212   if (length > static_cast<uint64_t>(buffer->end() - pos)) {
   213     return std::make_tuple(
false, 
Block());
   216   return std::make_tuple(
true, 
Block(
std::move(buffer), type, 
begin, pos + length, pos, pos + length));
   219 std::tuple<bool, Block>
   222   const uint8_t* pos = 
buf;
   223   const uint8_t* 
const end = buf + bufSize;
   228     return std::make_tuple(
false, 
Block());
   233     return std::make_tuple(
false, 
Block());
   237   if (length > static_cast<uint64_t>(end - pos)) {
   238     return std::make_tuple(
false, 
Block());
   241   size_t typeLengthSize = pos - 
buf;
   242   auto b = make_shared<Buffer>(
buf, pos + length);
   243   return std::make_tuple(
true, 
Block(b, type, b->begin(), b->end(),
   244                                      b->begin() + typeLengthSize, b->end()));
   262 Buffer::const_iterator
   271 Buffer::const_iterator
   333   while (begin != end) {
   334     Buffer::const_iterator pos = 
begin;
   338     if (length > static_cast<uint64_t>(end - pos)) {
   341                       " exceeds TLV-VALUE boundary of parent block"));
   345     Buffer::const_iterator subEnd = pos + length;
   359   size_t estimatedSize = 
encode(estimator);
   372   size_t len = encodeValue(estimator);
   373   len += estimator.prependVarNumber(len);
   374   len += estimator.prependVarNumber(
m_type);
   383     len += element.encode(estimator);
   392   m_end = encoder.begin();
   398       len += element.encode(encoder);
   404   len += encoder.prependVarNumber(len);
   405   len += encoder.prependVarNumber(
m_type);
   416   auto it = this->
find(type);
   429                       [
type] (
const Block& subBlock) { 
return subBlock.type() == 
type; });
   438                            [
type] (
const Block& subBlock) { 
return subBlock.type() == 
type; });
   472 Block::operator boost::asio::const_buffer()
 const   474   return boost::asio::const_buffer(
wire(), 
size());
   489   auto oldFmt = os.flags(std::ios_base::dec);
   496     size_t tlvLength = block.encodeValue(estimator);
   497     os << block.
type() << 
'[' << tlvLength << 
"]={";
   506     os << block.
type() << 
"[empty]";
   514 operator "" _block(
const char* input, std::size_t len)
   516   namespace t = security::transform;
   521   for (
const char* 
end = input + len; input != 
end; ++input) {
   522     if (std::strchr(
"0123456789ABCDEF", *input) != 
nullptr) {
   523       ss.write(reinterpret_cast<const uint8_t*>(input), 1);
   530   catch (
const t::Error&) {
   531     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
std::string to_string(const T &val)
Buffer::const_iterator m_begin
element_container::const_iterator element_const_iterator
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire. 
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH. 
void parse() const
Parse TLV-VALUE into sub-elements. 
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_const_iterator elements_begin() const
Equivalent to elements().begin() 
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 m_valueEnd
size_t size() const
Return the size of the encoded wire, i.e. 
element_const_iterator elements_end() const
Equivalent to elements().end() 
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-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. 
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE. 
void reset() noexcept
Reset the Block to a default-constructed state. 
Backport of ostream_joiner from the Library Fundamentals v2 TS. 
bool readType(Iterator &begin, Iterator end, uint32_t &type) noexcept
Read TLV-TYPE. 
Block & operator=(const Block &)
Copy assignment operator. 
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE. 
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
Buffer::const_iterator begin() const
Get begin iterator of encoded wire. 
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. 
Buffer::const_iterator end() const
Get end iterator of encoded wire. 
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. 
Block blockFromValue() const
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 
Block()
Create an invalid Block. 
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Return the TLV-TYPE of the Block. 
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