blob: 3aa2701844fc48f24341c3bb72030042cd5907b7 [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>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080030
31namespace ndn {
32
Junxiao Shidc4277a2017-07-17 11:34:02 +000033BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
Junxiao Shi88681402015-06-30 09:58:53 -070034#if NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE
35static_assert(std::is_nothrow_move_constructible<Block>::value,
36 "Block must be MoveConstructible with noexcept");
37#endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070038
Junxiao Shi88681402015-06-30 09:58:53 -070039#if NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE
40static_assert(std::is_nothrow_move_assignable<Block>::value,
41 "Block must be MoveAssignable with noexcept");
42#endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070043
susmit8b156552016-01-12 13:10:55 -070044const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = MAX_NDN_PACKET_SIZE;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070045
Junxiao Shidb7464d2017-07-13 03:11:17 +000046// ---- constructor, creation, assignment ----
47
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080048Block::Block()
49 : m_type(std::numeric_limits<uint32_t>::max())
Junxiao Shidb7464d2017-07-13 03:11:17 +000050 , m_size(0)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080051{
52}
53
Alexander Afanasyev15151312014-02-16 00:53:51 -080054Block::Block(const EncodingBuffer& buffer)
Junxiao Shidb7464d2017-07-13 03:11:17 +000055 : Block(const_cast<EncodingBuffer&>(buffer).getBuffer(), buffer.begin(), buffer.end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080056{
57}
58
Alexander Afanasyeva465e972014-03-22 17:21:49 -070059Block::Block(const ConstBufferPtr& buffer)
Junxiao Shidb7464d2017-07-13 03:11:17 +000060 : Block(buffer, buffer->begin(), buffer->end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080061{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080062}
63
Junxiao Shidb7464d2017-07-13 03:11:17 +000064Block::Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
65 bool verifyLength)
66 : m_buffer(std::move(buffer))
Alexander Afanasyev187bc482014-02-06 15:04:04 -080067 , m_begin(begin)
68 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +000069 , m_valueBegin(m_begin)
70 , m_valueEnd(m_end)
Alexander Afanasyev380420b2014-02-09 20:52:29 -080071 , m_size(m_end - m_begin)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080072{
Junxiao Shidb7464d2017-07-13 03:11:17 +000073 if (m_buffer->size() == 0) {
74 BOOST_THROW_EXCEPTION(std::invalid_argument("buffer is empty"));
75 }
Alexander Afanasyev380420b2014-02-09 20:52:29 -080076
Junxiao Shidb7464d2017-07-13 03:11:17 +000077 const uint8_t* bufferBegin = &m_buffer->front();
78 const uint8_t* bufferEnd = bufferBegin + m_buffer->size();
79 if (&*begin < bufferBegin || &*begin > bufferEnd ||
80 &*end < bufferBegin || &*end > bufferEnd) {
81 BOOST_THROW_EXCEPTION(std::invalid_argument("begin/end iterators points out of the buffer"));
82 }
83
84 m_type = tlv::readType(m_valueBegin, m_valueEnd);
85 uint64_t length = tlv::readVarNumber(m_valueBegin, m_valueEnd);
86 // m_valueBegin now points to TLV-VALUE
87
88 if (verifyLength && length != static_cast<uint64_t>(m_valueEnd - m_valueBegin)) {
89 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH doesn't match buffer size"));
Alexander Afanasyev4448d292015-08-09 20:11:37 -070090 }
91}
92
Junxiao Shidb7464d2017-07-13 03:11:17 +000093Block::Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
94 bool verifyLength)
95 : Block(block.m_buffer, begin, end, verifyLength)
96{
97}
98
99Block::Block(ConstBufferPtr buffer, uint32_t type,
100 Buffer::const_iterator begin, Buffer::const_iterator end,
101 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
102 : m_buffer(std::move(buffer))
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700103 , m_begin(begin)
104 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000105 , m_valueBegin(valueBegin)
106 , m_valueEnd(valueEnd)
107 , m_type(type)
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700108 , m_size(m_end - m_begin)
109{
Alexander Afanasyev187bc482014-02-06 15:04:04 -0800110}
111
Junxiao Shidb7464d2017-07-13 03:11:17 +0000112Block::Block(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800113{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000114 const uint8_t* pos = buf;
115 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700116
Junxiao Shidb7464d2017-07-13 03:11:17 +0000117 m_type = tlv::readType(pos, end);
118 uint64_t length = tlv::readVarNumber(pos, end);
119 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700120
Junxiao Shidb7464d2017-07-13 03:11:17 +0000121 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000122 BOOST_THROW_EXCEPTION(Error("Not enough bytes in the buffer to fully parse TLV"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000123 }
124 size_t typeLengthSize = pos - buf;
125 m_size = typeLengthSize + length;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800126
Junxiao Shidb7464d2017-07-13 03:11:17 +0000127 m_buffer = make_shared<Buffer>(buf, m_size);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800128 m_begin = m_buffer->begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000129 m_end = m_valueEnd = m_buffer->end();
130 m_valueBegin = m_begin + typeLengthSize;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800131}
132
Junxiao Shidb7464d2017-07-13 03:11:17 +0000133Block::Block(const void* buf, size_t bufSize)
134 : Block(reinterpret_cast<const uint8_t*>(buf), bufSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800135{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800136}
137
138Block::Block(uint32_t type)
139 : m_type(type)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000140 , m_size(tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(0))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800141{
142}
143
Junxiao Shidb7464d2017-07-13 03:11:17 +0000144Block::Block(uint32_t type, ConstBufferPtr value)
145 : m_buffer(std::move(value))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800146 , m_begin(m_buffer->end())
147 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000148 , m_valueBegin(m_buffer->begin())
149 , m_valueEnd(m_buffer->end())
150 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800151{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600152 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800153}
154
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700155Block::Block(uint32_t type, const Block& value)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800156 : m_buffer(value.m_buffer)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800157 , m_begin(m_buffer->end())
158 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000159 , m_valueBegin(value.begin())
160 , m_valueEnd(value.end())
161 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800162{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600163 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800164}
165
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700166Block
167Block::fromStream(std::istream& is)
168{
Junxiao Shif0da7892015-04-04 22:16:16 -0700169 std::istream_iterator<uint8_t> begin(is >> std::noskipws);
170 std::istream_iterator<uint8_t> end;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700171
Junxiao Shif0da7892015-04-04 22:16:16 -0700172 uint32_t type = tlv::readType(begin, end);
173 uint64_t length = tlv::readVarNumber(begin, end);
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000174 if (begin != end) {
175 is.putback(*begin);
Junxiao Shif0da7892015-04-04 22:16:16 -0700176 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700177
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000178 size_t tlSize = tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(length);
179 if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) {
180 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH from stream exceeds limit"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000181 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700182
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000183 EncodingBuffer eb(tlSize + length, length);
184 uint8_t* valueBuf = eb.buf();
185 is.read(reinterpret_cast<char*>(valueBuf), length);
186 if (length != static_cast<uint64_t>(is.gcount())) {
187 BOOST_THROW_EXCEPTION(Error("Not enough bytes from stream to fully parse TLV"));
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700188 }
189
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000190 eb.prependVarNumber(length);
191 eb.prependVarNumber(type);
192
193 // TLV-VALUE is directly written into eb.buf(), eb.end() is not incremented, but eb.getBuffer()
194 // has the correct layout.
195 return Block(eb.getBuffer());
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700196}
197
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700198std::tuple<bool, Block>
199Block::fromBuffer(ConstBufferPtr buffer, size_t offset)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700200{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000201 const Buffer::const_iterator begin = buffer->begin() + offset;
202 Buffer::const_iterator pos = begin;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700203
Junxiao Shidb7464d2017-07-13 03:11:17 +0000204 uint32_t type = 0;
205 bool isOk = tlv::readType(pos, buffer->end(), type);
206 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700207 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000208 }
209 uint64_t length = 0;
210 isOk = tlv::readVarNumber(pos, buffer->end(), length);
211 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700212 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000213 }
214 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700215
Junxiao Shidb7464d2017-07-13 03:11:17 +0000216 if (length > static_cast<uint64_t>(buffer->end() - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700217 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000218 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700219
Junxiao Shidb7464d2017-07-13 03:11:17 +0000220 return std::make_tuple(true, Block(buffer, type, begin, pos + length, pos, pos + length));
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700221}
222
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700223std::tuple<bool, Block>
Junxiao Shidb7464d2017-07-13 03:11:17 +0000224Block::fromBuffer(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700225{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000226 const uint8_t* pos = buf;
227 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700228
Mickey Sweatt632e0572014-04-20 00:36:32 -0700229 uint32_t type = 0;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000230 bool isOk = tlv::readType(pos, end, type);
231 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700232 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000233 }
234 uint64_t length = 0;
235 isOk = tlv::readVarNumber(pos, end, length);
236 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700237 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000238 }
239 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700240
Junxiao Shidb7464d2017-07-13 03:11:17 +0000241 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700242 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000243 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700244
Junxiao Shidb7464d2017-07-13 03:11:17 +0000245 size_t typeLengthSize = pos - buf;
246 auto b = make_shared<Buffer>(buf, pos + length);
247 return std::make_tuple(true, Block(b, type, b->begin(), b->end(),
248 b->begin() + typeLengthSize, b->end()));
249}
250
251// ---- wire format ----
252
253bool
254Block::hasWire() const
255{
256 return m_buffer != nullptr && m_begin != m_end;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700257}
258
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800259void
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700260Block::reset()
261{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000262 this->resetWire();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700263
264 m_type = std::numeric_limits<uint32_t>::max();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000265 m_elements.clear();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700266}
267
268void
269Block::resetWire()
270{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000271 m_buffer.reset(); // discard underlying buffer by resetting shared_ptr
272 m_begin = m_end = m_valueBegin = m_valueEnd = Buffer::const_iterator();
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400273}
274
275Buffer::const_iterator
276Block::begin() const
277{
278 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700279 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400280
281 return m_begin;
282}
283
284Buffer::const_iterator
285Block::end() const
286{
287 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700288 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400289
290 return m_end;
291}
292
293const uint8_t*
294Block::wire() const
295{
296 if (!hasWire())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000297 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400298
299 return &*m_begin;
300}
301
302size_t
303Block::size() const
304{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000305 if (empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700306 BOOST_THROW_EXCEPTION(Error("Block size cannot be determined (undefined block size)"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000307 }
308
309 return m_size;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400310}
311
Junxiao Shidb7464d2017-07-13 03:11:17 +0000312// ---- value ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400313
314const uint8_t*
315Block::value() const
316{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000317 return hasValue() ? &*m_valueBegin : nullptr;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400318}
319
320size_t
321Block::value_size() const
322{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000323 return hasValue() ? m_valueEnd - m_valueBegin : 0;
324}
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400325
Junxiao Shidb7464d2017-07-13 03:11:17 +0000326Block
327Block::blockFromValue() const
328{
329 if (!hasValue())
330 BOOST_THROW_EXCEPTION(Error("Block has no TLV-VALUE"));
331
332 return Block(*this, m_valueBegin, m_valueEnd, true);
333}
334
335// ---- sub elements ----
336
337void
338Block::parse() const
339{
340 if (!m_elements.empty() || value_size() == 0)
341 return;
342
343 Buffer::const_iterator begin = value_begin();
344 Buffer::const_iterator end = value_end();
345
346 while (begin != end) {
347 Buffer::const_iterator pos = begin;
348
349 uint32_t type = tlv::readType(pos, end);
350 uint64_t length = tlv::readVarNumber(pos, end);
351 if (length > static_cast<uint64_t>(end - pos)) {
352 m_elements.clear();
353 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH of sub-element of type " + to_string(type) +
354 " exceeds TLV-VALUE boundary of parent block"));
355 }
356 // pos now points to TLV-VALUE of sub element
357
358 Buffer::const_iterator subEnd = pos + length;
359 m_elements.emplace_back(m_buffer, type, begin, subEnd, pos, subEnd);
360
361 begin = subEnd;
362 }
363}
364
365void
366Block::encode()
367{
368 if (hasWire())
369 return;
370
371 OBufferStream os;
372 tlv::writeVarNumber(os, type());
373
374 if (hasValue()) {
375 tlv::writeVarNumber(os, value_size());
376 os.write(reinterpret_cast<const char*>(value()), value_size());
377 }
378 else if (m_elements.size() == 0) {
379 tlv::writeVarNumber(os, 0);
380 }
381 else {
382 size_t valueSize = 0;
383 for (element_const_iterator i = m_elements.begin(); i != m_elements.end(); ++i) {
384 valueSize += i->size();
385 }
386
387 tlv::writeVarNumber(os, valueSize);
388
389 for (element_const_iterator i = m_elements.begin(); i != m_elements.end(); ++i) {
390 if (i->hasWire())
391 os.write(reinterpret_cast<const char*>(i->wire()), i->size());
392 else if (i->hasValue()) {
393 tlv::writeVarNumber(os, i->type());
394 tlv::writeVarNumber(os, i->value_size());
395 os.write(reinterpret_cast<const char*>(i->value()), i->value_size());
396 }
397 else
398 BOOST_THROW_EXCEPTION(Error("Underlying value buffer is empty"));
399 }
400 }
401
402 // now assign correct block
403
404 m_buffer = os.buf();
405 m_begin = m_buffer->begin();
406 m_end = m_buffer->end();
407 m_size = m_end - m_begin;
408
409 m_valueBegin = m_buffer->begin();
410 m_valueEnd = m_buffer->end();
411
412 tlv::readType(m_valueBegin, m_valueEnd);
413 tlv::readVarNumber(m_valueBegin, m_valueEnd);
414}
415
416const Block&
417Block::get(uint32_t type) const
418{
419 auto it = this->find(type);
420 if (it != m_elements.end()) {
421 return *it;
422 }
423
424 BOOST_THROW_EXCEPTION(Error("No sub-element of type " + to_string(type) +
425 " is found in block of type " + to_string(m_type)));
426}
427
428Block::element_const_iterator
429Block::find(uint32_t type) const
430{
431 return std::find_if(m_elements.begin(), m_elements.end(),
432 [type] (const Block& subBlock) { return subBlock.type() == type; });
433}
434
435void
436Block::remove(uint32_t type)
437{
438 resetWire();
439
440 auto it = std::remove_if(m_elements.begin(), m_elements.end(),
441 [type] (const Block& subBlock) { return subBlock.type() == type; });
442 m_elements.resize(it - m_elements.begin());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400443}
444
445Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400446Block::erase(Block::element_const_iterator position)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400447{
448 resetWire();
Joao Pereira7476ebf2015-07-07 14:54:39 -0400449
450#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
Junxiao Shidb7464d2017-07-13 03:11:17 +0000451 return m_elements.erase(position);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400452#else
Junxiao Shidb7464d2017-07-13 03:11:17 +0000453 element_iterator it = m_elements.begin();
454 std::advance(it, std::distance(m_elements.cbegin(), position));
455 return m_elements.erase(it);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400456#endif
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400457}
458
459Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400460Block::erase(Block::element_const_iterator first, Block::element_const_iterator last)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400461{
462 resetWire();
Joao Pereira7476ebf2015-07-07 14:54:39 -0400463
464#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
Junxiao Shidb7464d2017-07-13 03:11:17 +0000465 return m_elements.erase(first, last);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400466#else
Junxiao Shidb7464d2017-07-13 03:11:17 +0000467 element_iterator itStart = m_elements.begin();
468 element_iterator itEnd = m_elements.begin();
469 std::advance(itStart, std::distance(m_elements.cbegin(), first));
470 std::advance(itEnd, std::distance(m_elements.cbegin(), last));
471 return m_elements.erase(itStart, itEnd);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400472#endif
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400473}
474
475void
476Block::push_back(const Block& element)
477{
478 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000479 m_elements.push_back(element);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400480}
481
Joao Pereira7476ebf2015-07-07 14:54:39 -0400482Block::element_iterator
483Block::insert(Block::element_const_iterator pos, const Block& element)
484{
485 resetWire();
486
487#ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR
Junxiao Shidb7464d2017-07-13 03:11:17 +0000488 return m_elements.insert(pos, element);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400489#else
Junxiao Shidb7464d2017-07-13 03:11:17 +0000490 element_iterator it = m_elements.begin();
491 std::advance(it, std::distance(m_elements.cbegin(), pos));
492 return m_elements.insert(it, element);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400493#endif
494}
495
Junxiao Shidb7464d2017-07-13 03:11:17 +0000496// ---- misc ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400497
Junxiao Shidb7464d2017-07-13 03:11:17 +0000498Block::operator boost::asio::const_buffer() const
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400499{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000500 return boost::asio::const_buffer(wire(), size());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400501}
502
503bool
Junxiao Shidb7464d2017-07-13 03:11:17 +0000504operator==(const Block& lhs, const Block& rhs)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400505{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000506 return lhs.type() == rhs.type() &&
507 lhs.value_size() == rhs.value_size() &&
508 ::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700509}
510
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800511} // namespace ndn