blob: 31e6d223c874ad0cae4cb6e0bdf34ac60f5a84da [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 Shi2b322eb2018-08-24 10:43:27 -060028#include "../security/transform.hpp"
Junxiao Shi72c0c642018-04-20 15:41:09 +000029#include "../util/string-helper.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070031#include <boost/asio/buffer.hpp>
Junxiao Shid2777fa2017-07-27 18:35:34 +000032#include <boost/range/adaptor/reversed.hpp>
Davide Pesaventoe245b052017-10-31 13:00:44 -040033#include <cstring>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080034
35namespace ndn {
36
Junxiao Shidc4277a2017-07-17 11:34:02 +000037BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070038
susmit8b156552016-01-12 13:10:55 -070039const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = MAX_NDN_PACKET_SIZE;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070040
Junxiao Shidb7464d2017-07-13 03:11:17 +000041// ---- constructor, creation, assignment ----
42
Davide Pesavento3a3e1882018-07-17 14:49:15 -040043Block::Block() = default;
44
45Block::Block(const Block&) = default;
46
47Block&
48Block::operator=(const Block&) = default;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080049
Alexander Afanasyev15151312014-02-16 00:53:51 -080050Block::Block(const EncodingBuffer& buffer)
Davide Pesavento570b20d2018-07-15 21:53:14 -040051 : Block(buffer.getBuffer(), buffer.begin(), buffer.end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080052{
53}
54
Alexander Afanasyeva465e972014-03-22 17:21:49 -070055Block::Block(const ConstBufferPtr& buffer)
Junxiao Shidb7464d2017-07-13 03:11:17 +000056 : Block(buffer, buffer->begin(), buffer->end(), true)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080057{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080058}
59
Junxiao Shidb7464d2017-07-13 03:11:17 +000060Block::Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
61 bool verifyLength)
62 : m_buffer(std::move(buffer))
Alexander Afanasyev187bc482014-02-06 15:04:04 -080063 , m_begin(begin)
64 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +000065 , m_valueBegin(m_begin)
66 , m_valueEnd(m_end)
Alexander Afanasyev380420b2014-02-09 20:52:29 -080067 , m_size(m_end - m_begin)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080068{
Junxiao Shidb7464d2017-07-13 03:11:17 +000069 if (m_buffer->size() == 0) {
70 BOOST_THROW_EXCEPTION(std::invalid_argument("buffer is empty"));
71 }
Alexander Afanasyev380420b2014-02-09 20:52:29 -080072
Junxiao Shidb7464d2017-07-13 03:11:17 +000073 const uint8_t* bufferBegin = &m_buffer->front();
74 const uint8_t* bufferEnd = bufferBegin + m_buffer->size();
75 if (&*begin < bufferBegin || &*begin > bufferEnd ||
76 &*end < bufferBegin || &*end > bufferEnd) {
77 BOOST_THROW_EXCEPTION(std::invalid_argument("begin/end iterators points out of the buffer"));
78 }
79
80 m_type = tlv::readType(m_valueBegin, m_valueEnd);
81 uint64_t length = tlv::readVarNumber(m_valueBegin, m_valueEnd);
82 // m_valueBegin now points to TLV-VALUE
83
84 if (verifyLength && length != static_cast<uint64_t>(m_valueEnd - m_valueBegin)) {
85 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH doesn't match buffer size"));
Alexander Afanasyev4448d292015-08-09 20:11:37 -070086 }
87}
88
Junxiao Shidb7464d2017-07-13 03:11:17 +000089Block::Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
90 bool verifyLength)
91 : Block(block.m_buffer, begin, end, verifyLength)
92{
93}
94
95Block::Block(ConstBufferPtr buffer, uint32_t type,
96 Buffer::const_iterator begin, Buffer::const_iterator end,
97 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd)
98 : m_buffer(std::move(buffer))
Alexander Afanasyev4448d292015-08-09 20:11:37 -070099 , m_begin(begin)
100 , m_end(end)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000101 , m_valueBegin(valueBegin)
102 , m_valueEnd(valueEnd)
103 , m_type(type)
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700104 , m_size(m_end - m_begin)
105{
Alexander Afanasyev187bc482014-02-06 15:04:04 -0800106}
107
Junxiao Shidb7464d2017-07-13 03:11:17 +0000108Block::Block(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800109{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000110 const uint8_t* pos = buf;
111 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700112
Junxiao Shidb7464d2017-07-13 03:11:17 +0000113 m_type = tlv::readType(pos, end);
114 uint64_t length = tlv::readVarNumber(pos, end);
115 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700116
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400117 BOOST_ASSERT(pos <= end);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000118 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000119 BOOST_THROW_EXCEPTION(Error("Not enough bytes in the buffer to fully parse TLV"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000120 }
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400121
122 BOOST_ASSERT(pos > buf);
123 uint64_t typeLengthSize = static_cast<uint64_t>(pos - buf);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000124 m_size = typeLengthSize + length;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800125
Junxiao Shidb7464d2017-07-13 03:11:17 +0000126 m_buffer = make_shared<Buffer>(buf, m_size);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800127 m_begin = m_buffer->begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000128 m_end = m_valueEnd = m_buffer->end();
129 m_valueBegin = m_begin + typeLengthSize;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800130}
131
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800132Block::Block(uint32_t type)
133 : m_type(type)
Junxiao Shidb7464d2017-07-13 03:11:17 +0000134 , m_size(tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(0))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800135{
136}
137
Junxiao Shidb7464d2017-07-13 03:11:17 +0000138Block::Block(uint32_t type, ConstBufferPtr value)
139 : m_buffer(std::move(value))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800140 , m_begin(m_buffer->end())
141 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000142 , m_valueBegin(m_buffer->begin())
143 , m_valueEnd(m_buffer->end())
144 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800145{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600146 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800147}
148
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700149Block::Block(uint32_t type, const Block& value)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800150 : m_buffer(value.m_buffer)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800151 , m_begin(m_buffer->end())
152 , m_end(m_buffer->end())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000153 , m_valueBegin(value.begin())
154 , m_valueEnd(value.end())
155 , m_type(type)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800156{
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600157 m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800158}
159
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700160Block
161Block::fromStream(std::istream& is)
162{
Junxiao Shif0da7892015-04-04 22:16:16 -0700163 std::istream_iterator<uint8_t> begin(is >> std::noskipws);
164 std::istream_iterator<uint8_t> end;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700165
Junxiao Shif0da7892015-04-04 22:16:16 -0700166 uint32_t type = tlv::readType(begin, end);
167 uint64_t length = tlv::readVarNumber(begin, end);
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000168 if (begin != end) {
169 is.putback(*begin);
Junxiao Shif0da7892015-04-04 22:16:16 -0700170 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700171
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000172 size_t tlSize = tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(length);
173 if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) {
174 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH from stream exceeds limit"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000175 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700176
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000177 EncodingBuffer eb(tlSize + length, length);
178 uint8_t* valueBuf = eb.buf();
179 is.read(reinterpret_cast<char*>(valueBuf), length);
180 if (length != static_cast<uint64_t>(is.gcount())) {
181 BOOST_THROW_EXCEPTION(Error("Not enough bytes from stream to fully parse TLV"));
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700182 }
183
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000184 eb.prependVarNumber(length);
185 eb.prependVarNumber(type);
186
187 // TLV-VALUE is directly written into eb.buf(), eb.end() is not incremented, but eb.getBuffer()
188 // has the correct layout.
189 return Block(eb.getBuffer());
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700190}
191
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700192std::tuple<bool, Block>
193Block::fromBuffer(ConstBufferPtr buffer, size_t offset)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700194{
Davide Pesavento3b101d02018-07-21 22:44:09 -0400195 auto begin = buffer->begin() + offset;
196 auto pos = begin;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700197
Junxiao Shidb7464d2017-07-13 03:11:17 +0000198 uint32_t type = 0;
199 bool isOk = tlv::readType(pos, buffer->end(), type);
200 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700201 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000202 }
Davide Pesavento3b101d02018-07-21 22:44:09 -0400203
Junxiao Shidb7464d2017-07-13 03:11:17 +0000204 uint64_t length = 0;
205 isOk = tlv::readVarNumber(pos, buffer->end(), length);
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 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700210
Junxiao Shidb7464d2017-07-13 03:11:17 +0000211 if (length > static_cast<uint64_t>(buffer->end() - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700212 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000213 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700214
Davide Pesavento3b101d02018-07-21 22:44:09 -0400215 return std::make_tuple(true, Block(std::move(buffer), type, begin, pos + length, pos, pos + length));
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700216}
217
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700218std::tuple<bool, Block>
Junxiao Shidb7464d2017-07-13 03:11:17 +0000219Block::fromBuffer(const uint8_t* buf, size_t bufSize)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700220{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000221 const uint8_t* pos = buf;
222 const uint8_t* const end = buf + bufSize;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700223
Mickey Sweatt632e0572014-04-20 00:36:32 -0700224 uint32_t type = 0;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000225 bool isOk = tlv::readType(pos, end, type);
226 if (!isOk) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700227 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000228 }
229 uint64_t length = 0;
230 isOk = tlv::readVarNumber(pos, end, length);
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 // pos now points to TLV-VALUE
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700235
Junxiao Shidb7464d2017-07-13 03:11:17 +0000236 if (length > static_cast<uint64_t>(end - pos)) {
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700237 return std::make_tuple(false, Block());
Junxiao Shidb7464d2017-07-13 03:11:17 +0000238 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700239
Junxiao Shidb7464d2017-07-13 03:11:17 +0000240 size_t typeLengthSize = pos - buf;
241 auto b = make_shared<Buffer>(buf, pos + length);
242 return std::make_tuple(true, Block(b, type, b->begin(), b->end(),
243 b->begin() + typeLengthSize, b->end()));
244}
245
246// ---- wire format ----
247
248bool
249Block::hasWire() const
250{
251 return m_buffer != nullptr && m_begin != m_end;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700252}
253
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800254void
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700255Block::reset()
256{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000257 this->resetWire();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700258
259 m_type = std::numeric_limits<uint32_t>::max();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000260 m_elements.clear();
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700261}
262
263void
264Block::resetWire()
265{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000266 m_buffer.reset(); // discard underlying buffer by resetting shared_ptr
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400267 m_begin = m_end = m_valueBegin = m_valueEnd = {};
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400268}
269
270Buffer::const_iterator
271Block::begin() const
272{
273 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700274 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400275
276 return m_begin;
277}
278
279Buffer::const_iterator
280Block::end() const
281{
282 if (!hasWire())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700283 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400284
285 return m_end;
286}
287
288const uint8_t*
289Block::wire() const
290{
291 if (!hasWire())
Junxiao Shidb7464d2017-07-13 03:11:17 +0000292 BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty"));
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400293
294 return &*m_begin;
295}
296
297size_t
298Block::size() const
299{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000300 if (empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700301 BOOST_THROW_EXCEPTION(Error("Block size cannot be determined (undefined block size)"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000302 }
303
304 return m_size;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400305}
306
Junxiao Shidb7464d2017-07-13 03:11:17 +0000307// ---- value ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400308
309const uint8_t*
310Block::value() const
311{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000312 return hasValue() ? &*m_valueBegin : nullptr;
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400313}
314
315size_t
316Block::value_size() const
317{
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400318 return hasValue() ? static_cast<size_t>(m_valueEnd - m_valueBegin) : 0;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000319}
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400320
Junxiao Shidb7464d2017-07-13 03:11:17 +0000321Block
322Block::blockFromValue() const
323{
324 if (!hasValue())
325 BOOST_THROW_EXCEPTION(Error("Block has no TLV-VALUE"));
326
327 return Block(*this, m_valueBegin, m_valueEnd, true);
328}
329
330// ---- sub elements ----
331
332void
333Block::parse() const
334{
335 if (!m_elements.empty() || value_size() == 0)
336 return;
337
338 Buffer::const_iterator begin = value_begin();
339 Buffer::const_iterator end = value_end();
340
341 while (begin != end) {
342 Buffer::const_iterator pos = begin;
343
344 uint32_t type = tlv::readType(pos, end);
345 uint64_t length = tlv::readVarNumber(pos, end);
346 if (length > static_cast<uint64_t>(end - pos)) {
347 m_elements.clear();
348 BOOST_THROW_EXCEPTION(Error("TLV-LENGTH of sub-element of type " + to_string(type) +
349 " exceeds TLV-VALUE boundary of parent block"));
350 }
351 // pos now points to TLV-VALUE of sub element
352
353 Buffer::const_iterator subEnd = pos + length;
354 m_elements.emplace_back(m_buffer, type, begin, subEnd, pos, subEnd);
355
356 begin = subEnd;
357 }
358}
359
360void
361Block::encode()
362{
363 if (hasWire())
364 return;
365
Junxiao Shid2777fa2017-07-27 18:35:34 +0000366 EncodingEstimator estimator;
367 size_t estimatedSize = encode(estimator);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000368
Junxiao Shid2777fa2017-07-27 18:35:34 +0000369 EncodingBuffer buffer(estimatedSize, 0);
370 encode(buffer);
371}
372
373size_t
374Block::encode(EncodingEstimator& estimator) const
375{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000376 if (hasValue()) {
Junxiao Shid2777fa2017-07-27 18:35:34 +0000377 return m_size;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000378 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000379
Junxiao Shi72c0c642018-04-20 15:41:09 +0000380 size_t len = encodeValue(estimator);
381 len += estimator.prependVarNumber(len);
382 len += estimator.prependVarNumber(m_type);
383 return len;
384}
385
386size_t
387Block::encodeValue(EncodingEstimator& estimator) const
388{
Junxiao Shid2777fa2017-07-27 18:35:34 +0000389 size_t len = 0;
390 for (const Block& element : m_elements | boost::adaptors::reversed) {
391 len += element.encode(estimator);
392 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000393 return len;
394}
395
396size_t
397Block::encode(EncodingBuffer& encoder)
398{
399 size_t len = 0;
400 m_end = encoder.begin();
401 if (hasValue()) {
402 len += encoder.prependRange(m_valueBegin, m_valueEnd);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000403 }
404 else {
Junxiao Shid2777fa2017-07-27 18:35:34 +0000405 for (Block& element : m_elements | boost::adaptors::reversed) {
406 len += element.encode(encoder);
Junxiao Shidb7464d2017-07-13 03:11:17 +0000407 }
408 }
Junxiao Shid2777fa2017-07-27 18:35:34 +0000409 m_valueEnd = m_end;
410 m_valueBegin = encoder.begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000411
Junxiao Shid2777fa2017-07-27 18:35:34 +0000412 len += encoder.prependVarNumber(len);
413 len += encoder.prependVarNumber(m_type);
414 m_begin = encoder.begin();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000415
Junxiao Shid2777fa2017-07-27 18:35:34 +0000416 m_buffer = encoder.getBuffer();
417 m_size = len;
418 return len;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000419}
420
421const Block&
422Block::get(uint32_t type) const
423{
424 auto it = this->find(type);
425 if (it != m_elements.end()) {
426 return *it;
427 }
428
429 BOOST_THROW_EXCEPTION(Error("No sub-element of type " + to_string(type) +
430 " is found in block of type " + to_string(m_type)));
431}
432
433Block::element_const_iterator
434Block::find(uint32_t type) const
435{
436 return std::find_if(m_elements.begin(), m_elements.end(),
437 [type] (const Block& subBlock) { return subBlock.type() == type; });
438}
439
440void
441Block::remove(uint32_t type)
442{
443 resetWire();
444
445 auto it = std::remove_if(m_elements.begin(), m_elements.end(),
446 [type] (const Block& subBlock) { return subBlock.type() == type; });
447 m_elements.resize(it - m_elements.begin());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400448}
449
450Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400451Block::erase(Block::element_const_iterator position)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400452{
453 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000454 return m_elements.erase(position);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400455}
456
457Block::element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400458Block::erase(Block::element_const_iterator first, Block::element_const_iterator last)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400459{
460 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000461 return m_elements.erase(first, last);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400462}
463
464void
465Block::push_back(const Block& element)
466{
467 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000468 m_elements.push_back(element);
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400469}
470
Joao Pereira7476ebf2015-07-07 14:54:39 -0400471Block::element_iterator
472Block::insert(Block::element_const_iterator pos, const Block& element)
473{
474 resetWire();
Junxiao Shidb7464d2017-07-13 03:11:17 +0000475 return m_elements.insert(pos, element);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400476}
477
Junxiao Shidb7464d2017-07-13 03:11:17 +0000478// ---- misc ----
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400479
Junxiao Shidb7464d2017-07-13 03:11:17 +0000480Block::operator boost::asio::const_buffer() const
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400481{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000482 return boost::asio::const_buffer(wire(), size());
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400483}
484
485bool
Junxiao Shidb7464d2017-07-13 03:11:17 +0000486operator==(const Block& lhs, const Block& rhs)
Joao Pereira9c2a9a82015-07-10 14:45:48 -0400487{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000488 return lhs.type() == rhs.type() &&
489 lhs.value_size() == rhs.value_size() &&
Davide Pesaventoe245b052017-10-31 13:00:44 -0400490 (lhs.value_size() == 0 ||
491 std::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700492}
493
Junxiao Shi72c0c642018-04-20 15:41:09 +0000494std::ostream&
495operator<<(std::ostream& os, const Block& block)
496{
497 auto oldFmt = os.flags(std::ios_base::dec);
498
499 if (block.empty()) {
500 os << "[invalid]";
501 }
502 else if (!block.m_elements.empty()) {
503 EncodingEstimator estimator;
504 size_t tlvLength = block.encodeValue(estimator);
505 os << block.type() << '[' << tlvLength << "]={";
506 std::copy(block.elements_begin(), block.elements_end(), make_ostream_joiner(os, ','));
507 os << '}';
508 }
509 else if (block.value_size() > 0) {
510 os << block.type() << '[' << block.value_size() << "]=";
511 printHex(os, block.value(), block.value_size(), true);
512 }
513 else {
514 os << block.type() << "[empty]";
515 }
516
517 os.flags(oldFmt);
518 return os;
519}
520
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600521Block
522operator "" _block(const char* input, std::size_t len)
523{
524 namespace t = security::transform;
525 t::StepSource ss;
526 OBufferStream os;
527 ss >> t::hexDecode() >> t::streamSink(os);
528
529 for (const char* end = input + len; input != end; ++input) {
530 if (std::strchr("0123456789ABCDEF", *input) != nullptr) {
531 ss.write(reinterpret_cast<const uint8_t*>(input), 1);
532 }
533 }
534
535 try {
536 ss.end();
537 }
538 catch (const t::Error&) {
539 BOOST_THROW_EXCEPTION(std::invalid_argument("input has odd number of hexadecimal digits"));
540 }
541
542 return Block(os.buf());
543}
544
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800545} // namespace ndn