blob: 13b10bc66b7bae14773889a174c6c0356f34b4a6 [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/*
3 * Copyright (c) 2013-2017 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"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070029#include <boost/asio/buffer.hpp>
Junxiao Shid2777fa2017-07-27 18:35:34 +000030#include <boost/range/adaptor/reversed.hpp>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080031
32namespace ndn {
33
Junxiao Shidc4277a2017-07-17 11:34:02 +000034BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
Junxiao Shi88681402015-06-30 09:58:53 -070035#if NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE
36static_assert(std::is_nothrow_move_constructible<Block>::value,
37 "Block must be MoveConstructible with noexcept");
38#endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070039
Junxiao Shi88681402015-06-30 09:58:53 -070040#if NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE
41static_assert(std::is_nothrow_move_assignable<Block>::value,
42 "Block must be MoveAssignable with noexcept");
43#endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070044
susmit8b156552016-01-12 13:10:55 -070045const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = MAX_NDN_PACKET_SIZE;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070046
Junxiao Shidb7464d2017-07-13 03:11:17 +000047// ---- constructor, creation, assignment ----
48
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080049Block::Block()
50 : m_type(std::numeric_limits<uint32_t>::max())
Junxiao Shidb7464d2017-07-13 03:11:17 +000051 , m_size(0)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080052{
53}
54
Alexander Afanasyev15151312014-02-16 00:53:51 -080055Block::Block(const EncodingBuffer& buffer)
Junxiao Shidb7464d2017-07-13 03:11:17 +000056 : Block(const_cast<EncodingBuffer&>(buffer).getBuffer(), buffer.begin(), buffer.end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080057{
58}
59
Alexander Afanasyeva465e972014-03-22 17:21:49 -070060Block::Block(const ConstBufferPtr& buffer)
Junxiao Shidb7464d2017-07-13 03:11:17 +000061 : Block(buffer, buffer->begin(), buffer->end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080062{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080063}
64
Junxiao Shidb7464d2017-07-13 03:11:17 +000065Block::Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
66 bool verifyLength)
67 : m_buffer(std::move(buffer))
Alexander Afanasyev187bc482014-02-06 15:04:04 -080068 , m_begin(begin)
69 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +000070 , m_valueBegin(m_begin)
71 , m_valueEnd(m_end)
Alexander Afanasyev380420b2014-02-09 20:52:29 -080072 , m_size(m_end - m_begin)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080073{
Junxiao Shidb7464d2017-07-13 03:11:17 +000074 if (m_buffer->size() == 0) {
75 BOOST_THROW_EXCEPTION(std::invalid_argument("buffer is empty"));
76 }
Alexander Afanasyev380420b2014-02-09 20:52:29 -080077
Junxiao Shidb7464d2017-07-13 03:11:17 +000078 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"));
83 }
84
85 m_type = tlv::readType(m_valueBegin, m_valueEnd);
86 uint64_t length = tlv::readVarNumber(m_valueBegin, m_valueEnd);
87 // m_valueBegin now points to TLV-VALUE
88
89 if (verifyLength && length != static_cast<uint64_t>(m_valueEnd - m_valueBegin)) {
90 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH doesn't match buffer size"));
Alexander Afanasyev4448d292015-08-09 20:11:37 -070091 }
92}
93
Junxiao Shidb7464d2017-07-13 03:11:17 +000094Block::Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
95 bool verifyLength)
96 : Block(block.m_buffer, begin, end, verifyLength)
97{
98}
99
100Block::Block(ConstBufferPtr buffer, uint32_t type,
101 Buffer::const_iterator begin, Buffer::const_iterator end,
102 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
103 : m_buffer(std::move(buffer))
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700104 , m_begin(begin)
105 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000106 , m_valueBegin(valueBegin)
107 , m_valueEnd(valueEnd)
108 , m_type(type)
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700109 , m_size(m_end - m_begin)
110{
Alexander Afanasyev187bc482014-02-06 15:04:04 -0800111}
112
Junxiao Shidb7464d2017-07-13 03:11:17 +0000113Block::Block(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800114{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000115 const uint8_t* pos = buf;
116 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700117
Junxiao Shidb7464d2017-07-13 03:11:17 +0000118 m_type = tlv::readType(pos, end);
119 uint64_t length = tlv::readVarNumber(pos, end);
120 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700121
Junxiao Shidb7464d2017-07-13 03:11:17 +0000122 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000123 BOOST_THROW_EXCEPTION(Error("Not enough bytes in the buffer to fully parse TLV"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000124 }
125 size_t typeLengthSize = pos - buf;
126 m_size = typeLengthSize + length;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800127
Junxiao Shidb7464d2017-07-13 03:11:17 +0000128 m_buffer = make_shared<Buffer>(buf, m_size);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800129 m_begin = m_buffer->begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000130 m_end = m_valueEnd = m_buffer->end();
131 m_valueBegin = m_begin + typeLengthSize;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800132}
133
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800134Block::Block(uint32_t type)
135 : m_type(type)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000136 , m_size(tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(0))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800137{
138}
139
Junxiao Shidb7464d2017-07-13 03:11:17 +0000140Block::Block(uint32_t type, ConstBufferPtr value)
141 : m_buffer(std::move(value))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800142 , m_begin(m_buffer->end())
143 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000144 , m_valueBegin(m_buffer->begin())
145 , m_valueEnd(m_buffer->end())
146 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800147{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600148 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800149}
150
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700151Block::Block(uint32_t type, const Block& value)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800152 : m_buffer(value.m_buffer)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800153 , m_begin(m_buffer->end())
154 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000155 , m_valueBegin(value.begin())
156 , m_valueEnd(value.end())
157 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800158{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600159 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800160}
161
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700162Block
163Block::fromStream(std::istream& is)
164{
Junxiao Shif0da7892015-04-04 22:16:16 -0700165 std::istream_iterator<uint8_t> begin(is >> std::noskipws);
166 std::istream_iterator<uint8_t> end;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700167
Junxiao Shif0da7892015-04-04 22:16:16 -0700168 uint32_t type = tlv::readType(begin, end);
169 uint64_t length = tlv::readVarNumber(begin, end);
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000170 if (begin != end) {
171 is.putback(*begin);
Junxiao Shif0da7892015-04-04 22:16:16 -0700172 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700173
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000174 size_t tlSize = tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(length);
175 if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) {
176 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH from stream exceeds limit"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000177 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700178
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000179 EncodingBuffer eb(tlSize + length, length);
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"));
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700184 }
185
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000186 eb.prependVarNumber(length);
187 eb.prependVarNumber(type);
188
189 // TLV-VALUE is directly written into eb.buf(), eb.end() is not incremented, but eb.getBuffer()
190 // has the correct layout.
191 return Block(eb.getBuffer());
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700192}
193
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700194std::tuple<bool, Block>
195Block::fromBuffer(ConstBufferPtr buffer, size_t offset)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700196{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000197 const Buffer::const_iterator begin = buffer->begin() + offset;
198 Buffer::const_iterator pos = begin;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700199
Junxiao Shidb7464d2017-07-13 03:11:17 +0000200 uint32_t type = 0;
201 bool isOk = tlv::readType(pos, buffer->end(), type);
202 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700203 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000204 }
205 uint64_t length = 0;
206 isOk = tlv::readVarNumber(pos, buffer->end(), length);
207 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700208 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000209 }
210 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700211
Junxiao Shidb7464d2017-07-13 03:11:17 +0000212 if (length > static_cast<uint64_t>(buffer->end() - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700213 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000214 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700215
Junxiao Shidb7464d2017-07-13 03:11:17 +0000216 return std::make_tuple(true, Block(buffer, type, begin, pos + length, pos, pos + length));
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700217}
218
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700219std::tuple<bool, Block>
Junxiao Shidb7464d2017-07-13 03:11:17 +0000220Block::fromBuffer(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700221{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000222 const uint8_t* pos = buf;
223 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700224
Mickey Sweatt632e0572014-04-20 00:36:32 -0700225 uint32_t type = 0;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000226 bool isOk = tlv::readType(pos, end, type);
227 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700228 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000229 }
230 uint64_t length = 0;
231 isOk = tlv::readVarNumber(pos, end, length);
232 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700233 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000234 }
235 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700236
Junxiao Shidb7464d2017-07-13 03:11:17 +0000237 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700238 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000239 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700240
Junxiao Shidb7464d2017-07-13 03:11:17 +0000241 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()));
245}
246
247// ---- wire format ----
248
249bool
250Block::hasWire() const
251{
252 return m_buffer != nullptr && m_begin != m_end;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700253}
254
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800255void
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700256Block::reset()
257{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000258 this->resetWire();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700259
260 m_type = std::numeric_limits<uint32_t>::max();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000261 m_elements.clear();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700262}
263
264void
265Block::resetWire()
266{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000267 m_buffer.reset(); // discard underlying buffer by resetting shared_ptr
268 m_begin = m_end = m_valueBegin = m_valueEnd = Buffer::const_iterator();
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400269}
270
271Buffer::const_iterator
272Block::begin() const
273{
274 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700275 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400276
277 return m_begin;
278}
279
280Buffer::const_iterator
281Block::end() const
282{
283 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700284 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400285
286 return m_end;
287}
288
289const uint8_t*
290Block::wire() const
291{
292 if (!hasWire())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000293 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400294
295 return &*m_begin;
296}
297
298size_t
299Block::size() const
300{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000301 if (empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700302 BOOST_THROW_EXCEPTION(Error("Block size cannot be determined (undefined block size)"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000303 }
304
305 return m_size;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400306}
307
Junxiao Shidb7464d2017-07-13 03:11:17 +0000308// ---- value ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400309
310const uint8_t*
311Block::value() const
312{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000313 return hasValue() ? &*m_valueBegin : nullptr;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400314}
315
316size_t
317Block::value_size() const
318{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000319 return hasValue() ? m_valueEnd - m_valueBegin : 0;
320}
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400321
Junxiao Shidb7464d2017-07-13 03:11:17 +0000322Block
323Block::blockFromValue() const
324{
325 if (!hasValue())
326 BOOST_THROW_EXCEPTION(Error("Block has no TLV-VALUE"));
327
328 return Block(*this, m_valueBegin, m_valueEnd, true);
329}
330
331// ---- sub elements ----
332
333void
334Block::parse() const
335{
336 if (!m_elements.empty() || value_size() == 0)
337 return;
338
339 Buffer::const_iterator begin = value_begin();
340 Buffer::const_iterator end = value_end();
341
342 while (begin != end) {
343 Buffer::const_iterator pos = begin;
344
345 uint32_t type = tlv::readType(pos, end);
346 uint64_t length = tlv::readVarNumber(pos, end);
347 if (length > static_cast<uint64_t>(end - pos)) {
348 m_elements.clear();
349 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH of sub-element of type " + to_string(type) +
350 " exceeds TLV-VALUE boundary of parent block"));
351 }
352 // pos now points to TLV-VALUE of sub element
353
354 Buffer::const_iterator subEnd = pos + length;
355 m_elements.emplace_back(m_buffer, type, begin, subEnd, pos, subEnd);
356
357 begin = subEnd;
358 }
359}
360
361void
362Block::encode()
363{
364 if (hasWire())
365 return;
366
Junxiao Shid2777fa2017-07-27 18:35:34 +0000367 EncodingEstimator estimator;
368 size_t estimatedSize = encode(estimator);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000369
Junxiao Shid2777fa2017-07-27 18:35:34 +0000370 EncodingBuffer buffer(estimatedSize, 0);
371 encode(buffer);
372}
373
374size_t
375Block::encode(EncodingEstimator& estimator) const
376{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000377 if (hasValue()) {
Junxiao Shid2777fa2017-07-27 18:35:34 +0000378 return m_size;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000379 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000380
381 size_t len = 0;
382 for (const Block& element : m_elements | boost::adaptors::reversed) {
383 len += element.encode(estimator);
384 }
385 len += estimator.prependVarNumber(len);
386 len += estimator.prependVarNumber(m_type);
387 return len;
388}
389
390size_t
391Block::encode(EncodingBuffer& encoder)
392{
393 size_t len = 0;
394 m_end = encoder.begin();
395 if (hasValue()) {
396 len += encoder.prependRange(m_valueBegin, m_valueEnd);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000397 }
398 else {
Junxiao Shid2777fa2017-07-27 18:35:34 +0000399 for (Block& element : m_elements | boost::adaptors::reversed) {
400 len += element.encode(encoder);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000401 }
402 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000403 m_valueEnd = m_end;
404 m_valueBegin = encoder.begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000405
Junxiao Shid2777fa2017-07-27 18:35:34 +0000406 len += encoder.prependVarNumber(len);
407 len += encoder.prependVarNumber(m_type);
408 m_begin = encoder.begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000409
Junxiao Shid2777fa2017-07-27 18:35:34 +0000410 m_buffer = encoder.getBuffer();
411 m_size = len;
412 return len;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000413}
414
415const Block&
416Block::get(uint32_t type) const
417{
418 auto it = this->find(type);
419 if (it != m_elements.end()) {
420 return *it;
421 }
422
423 BOOST_THROW_EXCEPTION(Error("No sub-element of type " + to_string(type) +
424 " is found in block of type " + to_string(m_type)));
425}
426
427Block::element_const_iterator
428Block::find(uint32_t type) const
429{
430 return std::find_if(m_elements.begin(), m_elements.end(),
431 [type] (const Block& subBlock) { return subBlock.type() == type; });
432}
433
434void
435Block::remove(uint32_t type)
436{
437 resetWire();
438
439 auto it = std::remove_if(m_elements.begin(), m_elements.end(),
440 [type] (const Block& subBlock) { return subBlock.type() == type; });
441 m_elements.resize(it - m_elements.begin());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400442}
443
444Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400445Block::erase(Block::element_const_iterator position)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400446{
447 resetWire();
Joao Pereira7476ebf2015-07-07 14:54:39 -0400448
449#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
Junxiao Shidb7464d2017-07-13 03:11:17 +0000450 return m_elements.erase(position);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400451#else
Junxiao Shidb7464d2017-07-13 03:11:17 +0000452 element_iterator it = m_elements.begin();
453 std::advance(it, std::distance(m_elements.cbegin(), position));
454 return m_elements.erase(it);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400455#endif
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400456}
457
458Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400459Block::erase(Block::element_const_iterator first, Block::element_const_iterator last)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400460{
461 resetWire();
Joao Pereira7476ebf2015-07-07 14:54:39 -0400462
463#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
Junxiao Shidb7464d2017-07-13 03:11:17 +0000464 return m_elements.erase(first, last);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400465#else
Junxiao Shidb7464d2017-07-13 03:11:17 +0000466 element_iterator itStart = m_elements.begin();
467 element_iterator itEnd = m_elements.begin();
468 std::advance(itStart, std::distance(m_elements.cbegin(), first));
469 std::advance(itEnd, std::distance(m_elements.cbegin(), last));
470 return m_elements.erase(itStart, itEnd);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400471#endif
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400472}
473
474void
475Block::push_back(const Block& element)
476{
477 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000478 m_elements.push_back(element);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400479}
480
Joao Pereira7476ebf2015-07-07 14:54:39 -0400481Block::element_iterator
482Block::insert(Block::element_const_iterator pos, const Block& element)
483{
484 resetWire();
485
486#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
Junxiao Shidb7464d2017-07-13 03:11:17 +0000487 return m_elements.insert(pos, element);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400488#else
Junxiao Shidb7464d2017-07-13 03:11:17 +0000489 element_iterator it = m_elements.begin();
490 std::advance(it, std::distance(m_elements.cbegin(), pos));
491 return m_elements.insert(it, element);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400492#endif
493}
494
Junxiao Shidb7464d2017-07-13 03:11:17 +0000495// ---- misc ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400496
Junxiao Shidb7464d2017-07-13 03:11:17 +0000497Block::operator boost::asio::const_buffer() const
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400498{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000499 return boost::asio::const_buffer(wire(), size());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400500}
501
502bool
Junxiao Shidb7464d2017-07-13 03:11:17 +0000503operator==(const Block& lhs, const Block& rhs)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400504{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000505 return lhs.type() == rhs.type() &&
506 lhs.value_size() == rhs.value_size() &&
507 ::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700508}
509
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800510} // namespace ndn