29 #include <boost/asio/buffer.hpp> 30 #include <boost/range/adaptor/reversed.hpp> 35 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
36 #if NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE 37 static_assert(std::is_nothrow_move_constructible<Block>::value,
38 "Block must be MoveConstructible with noexcept");
39 #endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE 41 #if NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE 42 static_assert(std::is_nothrow_move_assignable<Block>::value,
43 "Block must be MoveAssignable with noexcept");
44 #endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE 51 : m_type(
std::numeric_limits<uint32_t>::max())
57 :
Block(const_cast<
EncodingBuffer&>(buffer).getBuffer(), buffer.begin(), buffer.end(), true)
62 :
Block(buffer, buffer->begin(), buffer->end(), true)
68 : m_buffer(
std::move(buffer))
71 , m_valueBegin(m_begin)
73 , m_size(m_end - m_begin)
76 BOOST_THROW_EXCEPTION(std::invalid_argument(
"buffer is empty"));
79 const uint8_t* bufferBegin = &
m_buffer->front();
80 const uint8_t* bufferEnd = bufferBegin +
m_buffer->size();
81 if (&*begin < bufferBegin || &*begin > bufferEnd ||
82 &*end < bufferBegin || &*end > bufferEnd) {
83 BOOST_THROW_EXCEPTION(std::invalid_argument(
"begin/end iterators points out of the buffer"));
91 BOOST_THROW_EXCEPTION(
Error(
"TLV-LENGTH doesn't match buffer size"));
97 :
Block(block.m_buffer, begin, end, verifyLength)
102 Buffer::const_iterator begin, Buffer::const_iterator end,
103 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
104 : m_buffer(
std::move(buffer))
107 , m_valueBegin(valueBegin)
108 , m_valueEnd(valueEnd)
110 , m_size(m_end - m_begin)
116 const uint8_t* pos = buf;
117 const uint8_t*
const end = buf + bufSize;
123 if (length > static_cast<uint64_t>(
end - pos)) {
124 BOOST_THROW_EXCEPTION(
Error(
"Not enough bytes in the buffer to fully parse TLV"));
126 size_t typeLengthSize = pos - buf;
127 m_size = typeLengthSize + length;
142 : m_buffer(
std::move(value))
143 , m_begin(m_buffer->end())
144 , m_end(m_buffer->end())
145 , m_valueBegin(m_buffer->begin())
146 , m_valueEnd(m_buffer->end())
153 : m_buffer(value.m_buffer)
154 , m_begin(m_buffer->end())
155 , m_end(m_buffer->end())
156 , m_valueBegin(value.begin())
157 , m_valueEnd(value.end())
166 std::istream_iterator<uint8_t>
begin(is >> std::noskipws);
167 std::istream_iterator<uint8_t>
end;
177 BOOST_THROW_EXCEPTION(
Error(
"TLV-LENGTH from stream exceeds limit"));
181 uint8_t* valueBuf = eb.buf();
182 is.read(reinterpret_cast<char*>(valueBuf), length);
183 if (length != static_cast<uint64_t>(is.gcount())) {
184 BOOST_THROW_EXCEPTION(
Error(
"Not enough bytes from stream to fully parse TLV"));
187 eb.prependVarNumber(length);
188 eb.prependVarNumber(
type);
192 return Block(eb.getBuffer());
195 std::tuple<bool, Block>
198 const Buffer::const_iterator
begin = buffer->begin() + offset;
199 Buffer::const_iterator pos =
begin;
204 return std::make_tuple(
false,
Block());
209 return std::make_tuple(
false,
Block());
213 if (length > static_cast<uint64_t>(buffer->end() - pos)) {
214 return std::make_tuple(
false,
Block());
217 return std::make_tuple(
true,
Block(buffer,
type,
begin, pos + length, pos, pos + length));
220 std::tuple<bool, Block>
223 const uint8_t* pos = buf;
224 const uint8_t*
const end = buf + bufSize;
229 return std::make_tuple(
false,
Block());
234 return std::make_tuple(
false,
Block());
238 if (length > static_cast<uint64_t>(
end - pos)) {
239 return std::make_tuple(
false,
Block());
242 size_t typeLengthSize = pos - buf;
243 auto b = make_shared<Buffer>(buf, pos + length);
244 return std::make_tuple(
true,
Block(b,
type, b->begin(), b->end(),
245 b->begin() + typeLengthSize, b->end()));
261 m_type = std::numeric_limits<uint32_t>::max();
272 Buffer::const_iterator
276 BOOST_THROW_EXCEPTION(
Error(
"Underlying wire buffer is empty"));
281 Buffer::const_iterator
285 BOOST_THROW_EXCEPTION(
Error(
"Underlying wire buffer is empty"));
294 BOOST_THROW_EXCEPTION(
Error(
"Underlying wire buffer is empty"));
303 BOOST_THROW_EXCEPTION(
Error(
"Block size cannot be determined (undefined block size)"));
327 BOOST_THROW_EXCEPTION(
Error(
"Block has no TLV-VALUE"));
344 Buffer::const_iterator pos =
begin;
348 if (length > static_cast<uint64_t>(
end - pos)) {
351 " exceeds TLV-VALUE boundary of parent block"));
355 Buffer::const_iterator subEnd = pos + length;
369 size_t estimatedSize =
encode(estimator);
384 len += element.encode(estimator);
386 len += estimator.prependVarNumber(len);
387 len += estimator.prependVarNumber(
m_type);
395 m_end = encoder.begin();
401 len += element.encode(encoder);
407 len += encoder.prependVarNumber(len);
408 len += encoder.prependVarNumber(
m_type);
419 auto it = this->
find(type);
432 [
type] (
const Block& subBlock) {
return subBlock.type() ==
type; });
441 [
type] (
const Block& subBlock) {
return subBlock.type() ==
type; });
450 #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR 454 std::advance(it, std::distance(
m_elements.cbegin(), position));
464 #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR 469 std::advance(itStart, std::distance(
m_elements.cbegin(), first));
470 std::advance(itEnd, std::distance(
m_elements.cbegin(), last));
487 #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR 491 std::advance(it, std::distance(
m_elements.cbegin(), pos));
498 Block::operator boost::asio::const_buffer()
const 500 return boost::asio::const_buffer(wire(), size());
shared_ptr< const Buffer > m_buffer
underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields
bool hasWire() const
Check if the Block has fully encoded wire.
static Block fromStream(std::istream &is)
Parse Block from an input stream.
Copyright (c) 2011-2015 Regents of the University of California.
bool readType(Iterator &begin, const Iterator &end, uint32_t &type)
Read TLV-TYPE.
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 m_begin
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
element_container::const_iterator element_const_iterator
const uint8_t * wire() const
Get pointer to encoded wire.
void parse() const
Parse TLV-VALUE into sub elements.
element_container m_elements
sub elements
Represents a TLV element of NDN packet format.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub element.
const uint8_t * value() const
Get pointer to TLV-VALUE.
element_iterator erase(element_const_iterator position)
Erase a sub element.
void resetWire()
Reset wire buffer but keep TLV-TYPE and sub elements (if any)
uint32_t m_size
total size including Type-Length-Value
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
bool readVarNumber(Iterator &begin, const Iterator &end, uint64_t &number)
Read VAR-NUMBER in NDN-TLV encoding.
Buffer::const_iterator m_valueEnd
bool empty() const
Check if the Block is empty.
size_t size() const
Get size of encoded wire, including Type-Length-Value.
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
element_const_iterator find(uint32_t type) const
Find the first sub element of specified TLV-TYPE.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
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 specified TLV-TYPE.
void reset()
Reset wire buffer of the element.
void push_back(const Block &element)
Append a sub element.
Buffer::const_iterator end() const
Get end iterator of encoded wire.
void encode()
Encode sub elements into TLV-VALUE.
Block blockFromValue() const
Buffer::const_iterator m_end
bool operator==(const Data &lhs, const Data &rhs)
std::string to_string(const V &v)
bool hasValue() const
Get begin iterator of TLV-VALUE.
constexpr size_t sizeOfVarNumber(uint64_t number)
Get number of bytes necessary to hold value of VAR-NUMBER.
Block()
Create an empty Block.
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
EncodingImpl< EstimatorTag > EncodingEstimator
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