29 #include <boost/asio/buffer.hpp> 30 #include <boost/range/adaptor/reversed.hpp> 34 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
35 #if NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE 37 "Block must be MoveConstructible with noexcept");
38 #endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE 40 #if NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE 42 "Block must be MoveAssignable with noexcept");
43 #endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE 50 : m_type(
std::numeric_limits<uint32_t>::max())
75 BOOST_THROW_EXCEPTION(std::invalid_argument(
"buffer is empty"));
78 const uint8_t* bufferBegin = &
m_buffer->front();
79 const uint8_t* bufferEnd = bufferBegin +
m_buffer->size();
80 if (&*begin < bufferBegin || &*begin > bufferEnd ||
81 &*end < bufferBegin || &*end > bufferEnd) {
82 BOOST_THROW_EXCEPTION(std::invalid_argument(
"begin/end iterators points out of the buffer"));
90 BOOST_THROW_EXCEPTION(
Error(
"TLV-LENGTH doesn't match buffer size"));
101 Buffer::const_iterator
begin, Buffer::const_iterator
end,
102 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
115 const uint8_t* pos = buf;
116 const uint8_t*
const end = buf + bufSize;
122 if (length > static_cast<uint64_t>(end - pos)) {
123 BOOST_THROW_EXCEPTION(
Error(
"Not enough bytes in the buffer to fully parse TLV"));
125 size_t typeLengthSize = pos - buf;
126 m_size = typeLengthSize + length;
165 std::istream_iterator<uint8_t>
begin(is >> std::noskipws);
166 std::istream_iterator<uint8_t>
end;
175 if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) {
176 BOOST_THROW_EXCEPTION(
Error(
"TLV-LENGTH from stream exceeds limit"));
180 uint8_t* valueBuf = eb.buf();
181 is.read(reinterpret_cast<char*>(valueBuf), length);
182 if (length != static_cast<uint64_t>(is.gcount())) {
183 BOOST_THROW_EXCEPTION(
Error(
"Not enough bytes from stream to fully parse TLV"));
186 eb.prependVarNumber(length);
187 eb.prependVarNumber(type);
191 return Block(eb.getBuffer());
194 std::tuple<bool, Block>
197 const Buffer::const_iterator
begin = buffer->begin() + offset;
198 Buffer::const_iterator pos =
begin;
203 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(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()));
260 m_type = std::numeric_limits<uint32_t>::max();
271 Buffer::const_iterator
275 BOOST_THROW_EXCEPTION(
Error(
"Underlying wire buffer is empty"));
280 Buffer::const_iterator
284 BOOST_THROW_EXCEPTION(
Error(
"Underlying wire buffer is empty"));
293 BOOST_THROW_EXCEPTION(
Error(
"Underlying wire buffer is empty"));
302 BOOST_THROW_EXCEPTION(
Error(
"Block size cannot be determined (undefined block size)"));
326 BOOST_THROW_EXCEPTION(
Error(
"Block has no TLV-VALUE"));
342 while (begin != end) {
343 Buffer::const_iterator pos =
begin;
347 if (length > static_cast<uint64_t>(end - pos)) {
349 BOOST_THROW_EXCEPTION(
Error(
"TLV-LENGTH of sub-element of type " +
to_string(type) +
350 " exceeds TLV-VALUE boundary of parent block"));
354 Buffer::const_iterator subEnd = pos + length;
368 size_t estimatedSize =
encode(estimator);
383 len += element.encode(estimator);
385 len += estimator.prependVarNumber(len);
386 len += estimator.prependVarNumber(
m_type);
394 m_end = encoder.begin();
400 len += element.encode(encoder);
406 len += encoder.prependVarNumber(len);
407 len += encoder.prependVarNumber(
m_type);
418 auto it = this->
find(type);
423 BOOST_THROW_EXCEPTION(
Error(
"No sub-element of type " +
to_string(type) +
431 [
type] (
const Block& subBlock) {
return subBlock.type() ==
type; });
440 [
type] (
const Block& subBlock) {
return subBlock.type() ==
type; });
449 #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR 453 std::advance(it, std::distance(
m_elements.cbegin(), position));
463 #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR 468 std::advance(itStart, std::distance(
m_elements.cbegin(), first));
469 std::advance(itEnd, std::distance(
m_elements.cbegin(), last));
486 #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR 490 std::advance(it, std::distance(
m_elements.cbegin(), pos));
497 Block::operator boost::asio::const_buffer()
const 499 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 empty() const
Check if the Block is empty.
static Block fromStream(std::istream &is)
Parse Block from an input stream.
Copyright (c) 2011-2015 Regents of the University of California.
bool hasValue() const
Get begin iterator of TLV-VALUE.
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 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 specified TLV-TYPE.
element_container::const_iterator element_const_iterator
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.
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.
void resetWire()
Reset wire buffer but keep TLV-TYPE and sub elements (if any)
uint32_t m_size
total size including Type-Length-Value
bool readVarNumber(Iterator &begin, const Iterator &end, uint64_t &number)
Read VAR-NUMBER in NDN-TLV encoding.
Buffer::const_iterator m_valueEnd
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Block blockFromValue() const
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
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.
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
void parse() const
Parse TLV-VALUE into sub elements.
uint32_t type() const
Get TLV-TYPE.
void encode()
Encode sub elements into TLV-VALUE.
const uint8_t * wire() const
Get pointer to encoded wire.
Buffer::const_iterator m_end
bool hasWire() const
Check if the Block has fully encoded wire.
const uint8_t * value() const
Get pointer to TLV-VALUE.
bool operator==(const Data &lhs, const Data &rhs)
shared_ptr< const Buffer > getBuffer() const
Get underlying buffer.
std::string to_string(const V &v)
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
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
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