blob: ed8f5c908043fca72fc550ec271e4cae936f1191 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shidb7464d2017-07-13 03:11:17 +00002/*
Junxiao Shi72c0c642018-04-20 15:41:09 +00003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080022 */
23
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080024#include "block.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070025#include "buffer-stream.hpp"
Junxiao Shidb7464d2017-07-13 03:11:17 +000026#include "encoding-buffer.hpp"
27#include "tlv.hpp"
Junxiao Shi72c0c642018-04-20 15:41:09 +000028#include "../util/string-helper.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070030#include <boost/asio/buffer.hpp>
Junxiao Shid2777fa2017-07-27 18:35:34 +000031#include <boost/range/adaptor/reversed.hpp>
Davide Pesaventoe245b052017-10-31 13:00:44 -040032#include <cstring>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080033
34namespace ndn {
35
Junxiao Shidc4277a2017-07-17 11:34:02 +000036BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
Junxiao Shi88681402015-06-30 09:58:53 -070037static_assert(std::is_nothrow_move_constructible<Block>::value,
38 "Block must be MoveConstructible with noexcept");
Junxiao Shi88681402015-06-30 09:58:53 -070039static_assert(std::is_nothrow_move_assignable<Block>::value,
40 "Block must be MoveAssignable with noexcept");
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070041
susmit8b156552016-01-12 13:10:55 -070042const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = MAX_NDN_PACKET_SIZE;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070043
Junxiao Shidb7464d2017-07-13 03:11:17 +000044// ---- constructor, creation, assignment ----
45
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080046Block::Block()
47 : m_type(std::numeric_limits<uint32_t>::max())
Junxiao Shidb7464d2017-07-13 03:11:17 +000048 , m_size(0)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080049{
50}
51
Alexander Afanasyev15151312014-02-16 00:53:51 -080052Block::Block(const EncodingBuffer& buffer)
Junxiao Shidb7464d2017-07-13 03:11:17 +000053 : Block(const_cast<EncodingBuffer&>(buffer).getBuffer(), buffer.begin(), buffer.end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080054{
55}
56
Alexander Afanasyeva465e972014-03-22 17:21:49 -070057Block::Block(const ConstBufferPtr& buffer)
Junxiao Shidb7464d2017-07-13 03:11:17 +000058 : Block(buffer, buffer->begin(), buffer->end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080059{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080060}
61
Junxiao Shidb7464d2017-07-13 03:11:17 +000062Block::Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
63 bool verifyLength)
64 : m_buffer(std::move(buffer))
Alexander Afanasyev187bc482014-02-06 15:04:04 -080065 , m_begin(begin)
66 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +000067 , m_valueBegin(m_begin)
68 , m_valueEnd(m_end)
Alexander Afanasyev380420b2014-02-09 20:52:29 -080069 , m_size(m_end - m_begin)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080070{
Junxiao Shidb7464d2017-07-13 03:11:17 +000071 if (m_buffer->size() == 0) {
72 BOOST_THROW_EXCEPTION(std::invalid_argument("buffer is empty"));
73 }
Alexander Afanasyev380420b2014-02-09 20:52:29 -080074
Junxiao Shidb7464d2017-07-13 03:11:17 +000075 const uint8_t* bufferBegin = &m_buffer->front();
76 const uint8_t* bufferEnd = bufferBegin + m_buffer->size();
77 if (&*begin < bufferBegin || &*begin > bufferEnd ||
78 &*end < bufferBegin || &*end > bufferEnd) {
79 BOOST_THROW_EXCEPTION(std::invalid_argument("begin/end iterators points out of the buffer"));
80 }
81
82 m_type = tlv::readType(m_valueBegin, m_valueEnd);
83 uint64_t length = tlv::readVarNumber(m_valueBegin, m_valueEnd);
84 // m_valueBegin now points to TLV-VALUE
85
86 if (verifyLength && length != static_cast<uint64_t>(m_valueEnd - m_valueBegin)) {
87 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH doesn't match buffer size"));
Alexander Afanasyev4448d292015-08-09 20:11:37 -070088 }
89}
90
Junxiao Shidb7464d2017-07-13 03:11:17 +000091Block::Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
92 bool verifyLength)
93 : Block(block.m_buffer, begin, end, verifyLength)
94{
95}
96
97Block::Block(ConstBufferPtr buffer, uint32_t type,
98 Buffer::const_iterator begin, Buffer::const_iterator end,
99 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
100 : m_buffer(std::move(buffer))
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700101 , m_begin(begin)
102 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000103 , m_valueBegin(valueBegin)
104 , m_valueEnd(valueEnd)
105 , m_type(type)
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700106 , m_size(m_end - m_begin)
107{
Alexander Afanasyev187bc482014-02-06 15:04:04 -0800108}
109
Junxiao Shidb7464d2017-07-13 03:11:17 +0000110Block::Block(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800111{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000112 const uint8_t* pos = buf;
113 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700114
Junxiao Shidb7464d2017-07-13 03:11:17 +0000115 m_type = tlv::readType(pos, end);
116 uint64_t length = tlv::readVarNumber(pos, end);
117 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700118
Junxiao Shidb7464d2017-07-13 03:11:17 +0000119 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000120 BOOST_THROW_EXCEPTION(Error("Not enough bytes in the buffer to fully parse TLV"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000121 }
122 size_t typeLengthSize = pos - buf;
123 m_size = typeLengthSize + length;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800124
Junxiao Shidb7464d2017-07-13 03:11:17 +0000125 m_buffer = make_shared<Buffer>(buf, m_size);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800126 m_begin = m_buffer->begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000127 m_end = m_valueEnd = m_buffer->end();
128 m_valueBegin = m_begin + typeLengthSize;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800129}
130
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800131Block::Block(uint32_t type)
132 : m_type(type)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000133 , m_size(tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(0))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800134{
135}
136
Junxiao Shidb7464d2017-07-13 03:11:17 +0000137Block::Block(uint32_t type, ConstBufferPtr value)
138 : m_buffer(std::move(value))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800139 , m_begin(m_buffer->end())
140 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000141 , m_valueBegin(m_buffer->begin())
142 , m_valueEnd(m_buffer->end())
143 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800144{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600145 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800146}
147
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700148Block::Block(uint32_t type, const Block& value)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800149 : m_buffer(value.m_buffer)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800150 , m_begin(m_buffer->end())
151 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000152 , m_valueBegin(value.begin())
153 , m_valueEnd(value.end())
154 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800155{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600156 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800157}
158
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700159Block
160Block::fromStream(std::istream& is)
161{
Junxiao Shif0da7892015-04-04 22:16:16 -0700162 std::istream_iterator<uint8_t> begin(is >> std::noskipws);
163 std::istream_iterator<uint8_t> end;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700164
Junxiao Shif0da7892015-04-04 22:16:16 -0700165 uint32_t type = tlv::readType(begin, end);
166 uint64_t length = tlv::readVarNumber(begin, end);
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000167 if (begin != end) {
168 is.putback(*begin);
Junxiao Shif0da7892015-04-04 22:16:16 -0700169 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700170
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000171 size_t tlSize = tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(length);
172 if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) {
173 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH from stream exceeds limit"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000174 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700175
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000176 EncodingBuffer eb(tlSize + length, length);
177 uint8_t* valueBuf = eb.buf();
178 is.read(reinterpret_cast<char*>(valueBuf), length);
179 if (length != static_cast<uint64_t>(is.gcount())) {
180 BOOST_THROW_EXCEPTION(Error("Not enough bytes from stream to fully parse TLV"));
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700181 }
182
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000183 eb.prependVarNumber(length);
184 eb.prependVarNumber(type);
185
186 // TLV-VALUE is directly written into eb.buf(), eb.end() is not incremented, but eb.getBuffer()
187 // has the correct layout.
188 return Block(eb.getBuffer());
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700189}
190
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700191std::tuple<bool, Block>
192Block::fromBuffer(ConstBufferPtr buffer, size_t offset)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700193{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000194 const Buffer::const_iterator begin = buffer->begin() + offset;
195 Buffer::const_iterator pos = begin;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700196
Junxiao Shidb7464d2017-07-13 03:11:17 +0000197 uint32_t type = 0;
198 bool isOk = tlv::readType(pos, buffer->end(), type);
199 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700200 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000201 }
202 uint64_t length = 0;
203 isOk = tlv::readVarNumber(pos, buffer->end(), length);
204 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700205 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000206 }
207 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700208
Junxiao Shidb7464d2017-07-13 03:11:17 +0000209 if (length > static_cast<uint64_t>(buffer->end() - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700210 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000211 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700212
Junxiao Shidb7464d2017-07-13 03:11:17 +0000213 return std::make_tuple(true, Block(buffer, type, begin, pos + length, pos, pos + length));
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700214}
215
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700216std::tuple<bool, Block>
Junxiao Shidb7464d2017-07-13 03:11:17 +0000217Block::fromBuffer(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700218{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000219 const uint8_t* pos = buf;
220 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700221
Mickey Sweatt632e0572014-04-20 00:36:32 -0700222 uint32_t type = 0;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000223 bool isOk = tlv::readType(pos, end, type);
224 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700225 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000226 }
227 uint64_t length = 0;
228 isOk = tlv::readVarNumber(pos, end, length);
229 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700230 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000231 }
232 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700233
Junxiao Shidb7464d2017-07-13 03:11:17 +0000234 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700235 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000236 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700237
Junxiao Shidb7464d2017-07-13 03:11:17 +0000238 size_t typeLengthSize = pos - buf;
239 auto b = make_shared<Buffer>(buf, pos + length);
240 return std::make_tuple(true, Block(b, type, b->begin(), b->end(),
241 b->begin() + typeLengthSize, b->end()));
242}
243
244// ---- wire format ----
245
246bool
247Block::hasWire() const
248{
249 return m_buffer != nullptr && m_begin != m_end;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700250}
251
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800252void
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700253Block::reset()
254{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000255 this->resetWire();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700256
257 m_type = std::numeric_limits<uint32_t>::max();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000258 m_elements.clear();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700259}
260
261void
262Block::resetWire()
263{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000264 m_buffer.reset(); // discard underlying buffer by resetting shared_ptr
265 m_begin = m_end = m_valueBegin = m_valueEnd = Buffer::const_iterator();
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400266}
267
268Buffer::const_iterator
269Block::begin() const
270{
271 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700272 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400273
274 return m_begin;
275}
276
277Buffer::const_iterator
278Block::end() const
279{
280 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700281 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400282
283 return m_end;
284}
285
286const uint8_t*
287Block::wire() const
288{
289 if (!hasWire())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000290 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400291
292 return &*m_begin;
293}
294
295size_t
296Block::size() const
297{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000298 if (empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700299 BOOST_THROW_EXCEPTION(Error("Block size cannot be determined (undefined block size)"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000300 }
301
302 return m_size;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400303}
304
Junxiao Shidb7464d2017-07-13 03:11:17 +0000305// ---- value ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400306
307const uint8_t*
308Block::value() const
309{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000310 return hasValue() ? &*m_valueBegin : nullptr;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400311}
312
313size_t
314Block::value_size() const
315{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000316 return hasValue() ? m_valueEnd - m_valueBegin : 0;
317}
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400318
Junxiao Shidb7464d2017-07-13 03:11:17 +0000319Block
320Block::blockFromValue() const
321{
322 if (!hasValue())
323 BOOST_THROW_EXCEPTION(Error("Block has no TLV-VALUE"));
324
325 return Block(*this, m_valueBegin, m_valueEnd, true);
326}
327
328// ---- sub elements ----
329
330void
331Block::parse() const
332{
333 if (!m_elements.empty() || value_size() == 0)
334 return;
335
336 Buffer::const_iterator begin = value_begin();
337 Buffer::const_iterator end = value_end();
338
339 while (begin != end) {
340 Buffer::const_iterator pos = begin;
341
342 uint32_t type = tlv::readType(pos, end);
343 uint64_t length = tlv::readVarNumber(pos, end);
344 if (length > static_cast<uint64_t>(end - pos)) {
345 m_elements.clear();
346 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH of sub-element of type " + to_string(type) +
347 " exceeds TLV-VALUE boundary of parent block"));
348 }
349 // pos now points to TLV-VALUE of sub element
350
351 Buffer::const_iterator subEnd = pos + length;
352 m_elements.emplace_back(m_buffer, type, begin, subEnd, pos, subEnd);
353
354 begin = subEnd;
355 }
356}
357
358void
359Block::encode()
360{
361 if (hasWire())
362 return;
363
Junxiao Shid2777fa2017-07-27 18:35:34 +0000364 EncodingEstimator estimator;
365 size_t estimatedSize = encode(estimator);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000366
Junxiao Shid2777fa2017-07-27 18:35:34 +0000367 EncodingBuffer buffer(estimatedSize, 0);
368 encode(buffer);
369}
370
371size_t
372Block::encode(EncodingEstimator& estimator) const
373{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000374 if (hasValue()) {
Junxiao Shid2777fa2017-07-27 18:35:34 +0000375 return m_size;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000376 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000377
Junxiao Shi72c0c642018-04-20 15:41:09 +0000378 size_t len = encodeValue(estimator);
379 len += estimator.prependVarNumber(len);
380 len += estimator.prependVarNumber(m_type);
381 return len;
382}
383
384size_t
385Block::encodeValue(EncodingEstimator& estimator) const
386{
Junxiao Shid2777fa2017-07-27 18:35:34 +0000387 size_t len = 0;
388 for (const Block& element : m_elements | boost::adaptors::reversed) {
389 len += element.encode(estimator);
390 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000391 return len;
392}
393
394size_t
395Block::encode(EncodingBuffer& encoder)
396{
397 size_t len = 0;
398 m_end = encoder.begin();
399 if (hasValue()) {
400 len += encoder.prependRange(m_valueBegin, m_valueEnd);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000401 }
402 else {
Junxiao Shid2777fa2017-07-27 18:35:34 +0000403 for (Block& element : m_elements | boost::adaptors::reversed) {
404 len += element.encode(encoder);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000405 }
406 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000407 m_valueEnd = m_end;
408 m_valueBegin = encoder.begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000409
Junxiao Shid2777fa2017-07-27 18:35:34 +0000410 len += encoder.prependVarNumber(len);
411 len += encoder.prependVarNumber(m_type);
412 m_begin = encoder.begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000413
Junxiao Shid2777fa2017-07-27 18:35:34 +0000414 m_buffer = encoder.getBuffer();
415 m_size = len;
416 return len;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000417}
418
419const Block&
420Block::get(uint32_t type) const
421{
422 auto it = this->find(type);
423 if (it != m_elements.end()) {
424 return *it;
425 }
426
427 BOOST_THROW_EXCEPTION(Error("No sub-element of type " + to_string(type) +
428 " is found in block of type " + to_string(m_type)));
429}
430
431Block::element_const_iterator
432Block::find(uint32_t type) const
433{
434 return std::find_if(m_elements.begin(), m_elements.end(),
435 [type] (const Block& subBlock) { return subBlock.type() == type; });
436}
437
438void
439Block::remove(uint32_t type)
440{
441 resetWire();
442
443 auto it = std::remove_if(m_elements.begin(), m_elements.end(),
444 [type] (const Block& subBlock) { return subBlock.type() == type; });
445 m_elements.resize(it - m_elements.begin());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400446}
447
448Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400449Block::erase(Block::element_const_iterator position)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400450{
451 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000452 return m_elements.erase(position);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400453}
454
455Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400456Block::erase(Block::element_const_iterator first, Block::element_const_iterator last)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400457{
458 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000459 return m_elements.erase(first, last);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400460}
461
462void
463Block::push_back(const Block& element)
464{
465 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000466 m_elements.push_back(element);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400467}
468
Joao Pereira7476ebf2015-07-07 14:54:39 -0400469Block::element_iterator
470Block::insert(Block::element_const_iterator pos, const Block& element)
471{
472 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000473 return m_elements.insert(pos, element);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400474}
475
Junxiao Shidb7464d2017-07-13 03:11:17 +0000476// ---- misc ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400477
Junxiao Shidb7464d2017-07-13 03:11:17 +0000478Block::operator boost::asio::const_buffer() const
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400479{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000480 return boost::asio::const_buffer(wire(), size());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400481}
482
483bool
Junxiao Shidb7464d2017-07-13 03:11:17 +0000484operator==(const Block& lhs, const Block& rhs)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400485{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000486 return lhs.type() == rhs.type() &&
487 lhs.value_size() == rhs.value_size() &&
Davide Pesaventoe245b052017-10-31 13:00:44 -0400488 (lhs.value_size() == 0 ||
489 std::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700490}
491
Junxiao Shi72c0c642018-04-20 15:41:09 +0000492std::ostream&
493operator<<(std::ostream& os, const Block& block)
494{
495 auto oldFmt = os.flags(std::ios_base::dec);
496
497 if (block.empty()) {
498 os << "[invalid]";
499 }
500 else if (!block.m_elements.empty()) {
501 EncodingEstimator estimator;
502 size_t tlvLength = block.encodeValue(estimator);
503 os << block.type() << '[' << tlvLength << "]={";
504 std::copy(block.elements_begin(), block.elements_end(), make_ostream_joiner(os, ','));
505 os << '}';
506 }
507 else if (block.value_size() > 0) {
508 os << block.type() << '[' << block.value_size() << "]=";
509 printHex(os, block.value(), block.value_size(), true);
510 }
511 else {
512 os << block.type() << "[empty]";
513 }
514
515 os.flags(oldFmt);
516 return os;
517}
518
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800519} // namespace ndn