Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 24 | #include "block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 25 | #include "buffer-stream.hpp" |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 26 | #include "encoding-buffer.hpp" |
| 27 | #include "tlv.hpp" |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 28 | #include "../util/string-helper.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 30 | #include <boost/asio/buffer.hpp> |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 31 | #include <boost/range/adaptor/reversed.hpp> |
Davide Pesavento | e245b05 | 2017-10-31 13:00:44 -0400 | [diff] [blame] | 32 | #include <cstring> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 36 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
Junxiao Shi | 8868140 | 2015-06-30 09:58:53 -0700 | [diff] [blame] | 37 | #if NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE |
| 38 | static_assert(std::is_nothrow_move_constructible<Block>::value, |
| 39 | "Block must be MoveConstructible with noexcept"); |
| 40 | #endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | 8868140 | 2015-06-30 09:58:53 -0700 | [diff] [blame] | 42 | #if NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE |
| 43 | static_assert(std::is_nothrow_move_assignable<Block>::value, |
| 44 | "Block must be MoveAssignable with noexcept"); |
| 45 | #endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_ASSIGNABLE |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 46 | |
susmit | 8b15655 | 2016-01-12 13:10:55 -0700 | [diff] [blame] | 47 | const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = MAX_NDN_PACKET_SIZE; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 49 | // ---- constructor, creation, assignment ---- |
| 50 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 51 | Block::Block() |
| 52 | : m_type(std::numeric_limits<uint32_t>::max()) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 53 | , m_size(0) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 57 | Block::Block(const EncodingBuffer& buffer) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 58 | : Block(const_cast<EncodingBuffer&>(buffer).getBuffer(), buffer.begin(), buffer.end(), true) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 62 | Block::Block(const ConstBufferPtr& buffer) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 63 | : Block(buffer, buffer->begin(), buffer->end(), true) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 64 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 67 | Block::Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end, |
| 68 | bool verifyLength) |
| 69 | : m_buffer(std::move(buffer)) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 70 | , m_begin(begin) |
| 71 | , m_end(end) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 72 | , m_valueBegin(m_begin) |
| 73 | , m_valueEnd(m_end) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 74 | , m_size(m_end - m_begin) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 75 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 76 | if (m_buffer->size() == 0) { |
| 77 | BOOST_THROW_EXCEPTION(std::invalid_argument("buffer is empty")); |
| 78 | } |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 79 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 80 | const uint8_t* bufferBegin = &m_buffer->front(); |
| 81 | const uint8_t* bufferEnd = bufferBegin + m_buffer->size(); |
| 82 | if (&*begin < bufferBegin || &*begin > bufferEnd || |
| 83 | &*end < bufferBegin || &*end > bufferEnd) { |
| 84 | BOOST_THROW_EXCEPTION(std::invalid_argument("begin/end iterators points out of the buffer")); |
| 85 | } |
| 86 | |
| 87 | m_type = tlv::readType(m_valueBegin, m_valueEnd); |
| 88 | uint64_t length = tlv::readVarNumber(m_valueBegin, m_valueEnd); |
| 89 | // m_valueBegin now points to TLV-VALUE |
| 90 | |
| 91 | if (verifyLength && length != static_cast<uint64_t>(m_valueEnd - m_valueBegin)) { |
| 92 | BOOST_THROW_EXCEPTION(Error("TLV-LENGTH doesn't match buffer size")); |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 96 | Block::Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end, |
| 97 | bool verifyLength) |
| 98 | : Block(block.m_buffer, begin, end, verifyLength) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | Block::Block(ConstBufferPtr buffer, uint32_t type, |
| 103 | Buffer::const_iterator begin, Buffer::const_iterator end, |
| 104 | Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd) |
| 105 | : m_buffer(std::move(buffer)) |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 106 | , m_begin(begin) |
| 107 | , m_end(end) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 108 | , m_valueBegin(valueBegin) |
| 109 | , m_valueEnd(valueEnd) |
| 110 | , m_type(type) |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 111 | , m_size(m_end - m_begin) |
| 112 | { |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 115 | Block::Block(const uint8_t* buf, size_t bufSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 116 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 117 | const uint8_t* pos = buf; |
| 118 | const uint8_t* const end = buf + bufSize; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 119 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 120 | m_type = tlv::readType(pos, end); |
| 121 | uint64_t length = tlv::readVarNumber(pos, end); |
| 122 | // pos now points to TLV-VALUE |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 123 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 124 | if (length > static_cast<uint64_t>(end - pos)) { |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 125 | BOOST_THROW_EXCEPTION(Error("Not enough bytes in the buffer to fully parse TLV")); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 126 | } |
| 127 | size_t typeLengthSize = pos - buf; |
| 128 | m_size = typeLengthSize + length; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 129 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 130 | m_buffer = make_shared<Buffer>(buf, m_size); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 131 | m_begin = m_buffer->begin(); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 132 | m_end = m_valueEnd = m_buffer->end(); |
| 133 | m_valueBegin = m_begin + typeLengthSize; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 136 | Block::Block(uint32_t type) |
| 137 | : m_type(type) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 138 | , m_size(tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(0)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 139 | { |
| 140 | } |
| 141 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 142 | Block::Block(uint32_t type, ConstBufferPtr value) |
| 143 | : m_buffer(std::move(value)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 144 | , m_begin(m_buffer->end()) |
| 145 | , m_end(m_buffer->end()) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 146 | , m_valueBegin(m_buffer->begin()) |
| 147 | , m_valueEnd(m_buffer->end()) |
| 148 | , m_type(type) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 149 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 150 | m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 153 | Block::Block(uint32_t type, const Block& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 154 | : m_buffer(value.m_buffer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 155 | , m_begin(m_buffer->end()) |
| 156 | , m_end(m_buffer->end()) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 157 | , m_valueBegin(value.begin()) |
| 158 | , m_valueEnd(value.end()) |
| 159 | , m_type(type) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 160 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 161 | m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 164 | Block |
| 165 | Block::fromStream(std::istream& is) |
| 166 | { |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 167 | std::istream_iterator<uint8_t> begin(is >> std::noskipws); |
| 168 | std::istream_iterator<uint8_t> end; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 169 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 170 | uint32_t type = tlv::readType(begin, end); |
| 171 | uint64_t length = tlv::readVarNumber(begin, end); |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 172 | if (begin != end) { |
| 173 | is.putback(*begin); |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 174 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 175 | |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 176 | size_t tlSize = tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(length); |
| 177 | if (tlSize + length > MAX_SIZE_OF_BLOCK_FROM_STREAM) { |
| 178 | BOOST_THROW_EXCEPTION(Error("TLV-LENGTH from stream exceeds limit")); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 179 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 180 | |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 181 | EncodingBuffer eb(tlSize + length, length); |
| 182 | uint8_t* valueBuf = eb.buf(); |
| 183 | is.read(reinterpret_cast<char*>(valueBuf), length); |
| 184 | if (length != static_cast<uint64_t>(is.gcount())) { |
| 185 | BOOST_THROW_EXCEPTION(Error("Not enough bytes from stream to fully parse TLV")); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 188 | eb.prependVarNumber(length); |
| 189 | eb.prependVarNumber(type); |
| 190 | |
| 191 | // TLV-VALUE is directly written into eb.buf(), eb.end() is not incremented, but eb.getBuffer() |
| 192 | // has the correct layout. |
| 193 | return Block(eb.getBuffer()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 196 | std::tuple<bool, Block> |
| 197 | Block::fromBuffer(ConstBufferPtr buffer, size_t offset) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 198 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 199 | const Buffer::const_iterator begin = buffer->begin() + offset; |
| 200 | Buffer::const_iterator pos = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 201 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 202 | uint32_t type = 0; |
| 203 | bool isOk = tlv::readType(pos, buffer->end(), type); |
| 204 | if (!isOk) { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 205 | return std::make_tuple(false, Block()); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 206 | } |
| 207 | uint64_t length = 0; |
| 208 | isOk = tlv::readVarNumber(pos, buffer->end(), length); |
| 209 | if (!isOk) { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 210 | return std::make_tuple(false, Block()); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 211 | } |
| 212 | // pos now points to TLV-VALUE |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 213 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 214 | if (length > static_cast<uint64_t>(buffer->end() - pos)) { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 215 | return std::make_tuple(false, Block()); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 216 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 217 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 218 | return std::make_tuple(true, Block(buffer, type, begin, pos + length, pos, pos + length)); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 221 | std::tuple<bool, Block> |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 222 | Block::fromBuffer(const uint8_t* buf, size_t bufSize) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 223 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 224 | const uint8_t* pos = buf; |
| 225 | const uint8_t* const end = buf + bufSize; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 226 | |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 227 | uint32_t type = 0; |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 228 | bool isOk = tlv::readType(pos, end, type); |
| 229 | if (!isOk) { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 230 | return std::make_tuple(false, Block()); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 231 | } |
| 232 | uint64_t length = 0; |
| 233 | isOk = tlv::readVarNumber(pos, end, length); |
| 234 | if (!isOk) { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 235 | return std::make_tuple(false, Block()); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 236 | } |
| 237 | // pos now points to TLV-VALUE |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 238 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 239 | if (length > static_cast<uint64_t>(end - pos)) { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 240 | return std::make_tuple(false, Block()); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 241 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 242 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 243 | size_t typeLengthSize = pos - buf; |
| 244 | auto b = make_shared<Buffer>(buf, pos + length); |
| 245 | return std::make_tuple(true, Block(b, type, b->begin(), b->end(), |
| 246 | b->begin() + typeLengthSize, b->end())); |
| 247 | } |
| 248 | |
| 249 | // ---- wire format ---- |
| 250 | |
| 251 | bool |
| 252 | Block::hasWire() const |
| 253 | { |
| 254 | return m_buffer != nullptr && m_begin != m_end; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 257 | void |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 258 | Block::reset() |
| 259 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 260 | this->resetWire(); |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 261 | |
| 262 | m_type = std::numeric_limits<uint32_t>::max(); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 263 | m_elements.clear(); |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void |
| 267 | Block::resetWire() |
| 268 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 269 | m_buffer.reset(); // discard underlying buffer by resetting shared_ptr |
| 270 | m_begin = m_end = m_valueBegin = m_valueEnd = Buffer::const_iterator(); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | Buffer::const_iterator |
| 274 | Block::begin() const |
| 275 | { |
| 276 | if (!hasWire()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 277 | BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty")); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 278 | |
| 279 | return m_begin; |
| 280 | } |
| 281 | |
| 282 | Buffer::const_iterator |
| 283 | Block::end() const |
| 284 | { |
| 285 | if (!hasWire()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 286 | BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty")); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 287 | |
| 288 | return m_end; |
| 289 | } |
| 290 | |
| 291 | const uint8_t* |
| 292 | Block::wire() const |
| 293 | { |
| 294 | if (!hasWire()) |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 295 | BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty")); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 296 | |
| 297 | return &*m_begin; |
| 298 | } |
| 299 | |
| 300 | size_t |
| 301 | Block::size() const |
| 302 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 303 | if (empty()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 304 | BOOST_THROW_EXCEPTION(Error("Block size cannot be determined (undefined block size)")); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | return m_size; |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 310 | // ---- value ---- |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 311 | |
| 312 | const uint8_t* |
| 313 | Block::value() const |
| 314 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 315 | return hasValue() ? &*m_valueBegin : nullptr; |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | size_t |
| 319 | Block::value_size() const |
| 320 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 321 | return hasValue() ? m_valueEnd - m_valueBegin : 0; |
| 322 | } |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 323 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 324 | Block |
| 325 | Block::blockFromValue() const |
| 326 | { |
| 327 | if (!hasValue()) |
| 328 | BOOST_THROW_EXCEPTION(Error("Block has no TLV-VALUE")); |
| 329 | |
| 330 | return Block(*this, m_valueBegin, m_valueEnd, true); |
| 331 | } |
| 332 | |
| 333 | // ---- sub elements ---- |
| 334 | |
| 335 | void |
| 336 | Block::parse() const |
| 337 | { |
| 338 | if (!m_elements.empty() || value_size() == 0) |
| 339 | return; |
| 340 | |
| 341 | Buffer::const_iterator begin = value_begin(); |
| 342 | Buffer::const_iterator end = value_end(); |
| 343 | |
| 344 | while (begin != end) { |
| 345 | Buffer::const_iterator pos = begin; |
| 346 | |
| 347 | uint32_t type = tlv::readType(pos, end); |
| 348 | uint64_t length = tlv::readVarNumber(pos, end); |
| 349 | if (length > static_cast<uint64_t>(end - pos)) { |
| 350 | m_elements.clear(); |
| 351 | BOOST_THROW_EXCEPTION(Error("TLV-LENGTH of sub-element of type " + to_string(type) + |
| 352 | " exceeds TLV-VALUE boundary of parent block")); |
| 353 | } |
| 354 | // pos now points to TLV-VALUE of sub element |
| 355 | |
| 356 | Buffer::const_iterator subEnd = pos + length; |
| 357 | m_elements.emplace_back(m_buffer, type, begin, subEnd, pos, subEnd); |
| 358 | |
| 359 | begin = subEnd; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void |
| 364 | Block::encode() |
| 365 | { |
| 366 | if (hasWire()) |
| 367 | return; |
| 368 | |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 369 | EncodingEstimator estimator; |
| 370 | size_t estimatedSize = encode(estimator); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 371 | |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 372 | EncodingBuffer buffer(estimatedSize, 0); |
| 373 | encode(buffer); |
| 374 | } |
| 375 | |
| 376 | size_t |
| 377 | Block::encode(EncodingEstimator& estimator) const |
| 378 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 379 | if (hasValue()) { |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 380 | return m_size; |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 381 | } |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 382 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 383 | size_t len = encodeValue(estimator); |
| 384 | len += estimator.prependVarNumber(len); |
| 385 | len += estimator.prependVarNumber(m_type); |
| 386 | return len; |
| 387 | } |
| 388 | |
| 389 | size_t |
| 390 | Block::encodeValue(EncodingEstimator& estimator) const |
| 391 | { |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 392 | size_t len = 0; |
| 393 | for (const Block& element : m_elements | boost::adaptors::reversed) { |
| 394 | len += element.encode(estimator); |
| 395 | } |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 396 | return len; |
| 397 | } |
| 398 | |
| 399 | size_t |
| 400 | Block::encode(EncodingBuffer& encoder) |
| 401 | { |
| 402 | size_t len = 0; |
| 403 | m_end = encoder.begin(); |
| 404 | if (hasValue()) { |
| 405 | len += encoder.prependRange(m_valueBegin, m_valueEnd); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 406 | } |
| 407 | else { |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 408 | for (Block& element : m_elements | boost::adaptors::reversed) { |
| 409 | len += element.encode(encoder); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 412 | m_valueEnd = m_end; |
| 413 | m_valueBegin = encoder.begin(); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 414 | |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 415 | len += encoder.prependVarNumber(len); |
| 416 | len += encoder.prependVarNumber(m_type); |
| 417 | m_begin = encoder.begin(); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 418 | |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 419 | m_buffer = encoder.getBuffer(); |
| 420 | m_size = len; |
| 421 | return len; |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | const Block& |
| 425 | Block::get(uint32_t type) const |
| 426 | { |
| 427 | auto it = this->find(type); |
| 428 | if (it != m_elements.end()) { |
| 429 | return *it; |
| 430 | } |
| 431 | |
| 432 | BOOST_THROW_EXCEPTION(Error("No sub-element of type " + to_string(type) + |
| 433 | " is found in block of type " + to_string(m_type))); |
| 434 | } |
| 435 | |
| 436 | Block::element_const_iterator |
| 437 | Block::find(uint32_t type) const |
| 438 | { |
| 439 | return std::find_if(m_elements.begin(), m_elements.end(), |
| 440 | [type] (const Block& subBlock) { return subBlock.type() == type; }); |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | Block::remove(uint32_t type) |
| 445 | { |
| 446 | resetWire(); |
| 447 | |
| 448 | auto it = std::remove_if(m_elements.begin(), m_elements.end(), |
| 449 | [type] (const Block& subBlock) { return subBlock.type() == type; }); |
| 450 | m_elements.resize(it - m_elements.begin()); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | Block::element_iterator |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 454 | Block::erase(Block::element_const_iterator position) |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 455 | { |
| 456 | resetWire(); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 457 | |
| 458 | #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 459 | return m_elements.erase(position); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 460 | #else |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 461 | element_iterator it = m_elements.begin(); |
| 462 | std::advance(it, std::distance(m_elements.cbegin(), position)); |
| 463 | return m_elements.erase(it); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 464 | #endif |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | Block::element_iterator |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 468 | Block::erase(Block::element_const_iterator first, Block::element_const_iterator last) |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 469 | { |
| 470 | resetWire(); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 471 | |
| 472 | #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 473 | return m_elements.erase(first, last); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 474 | #else |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 475 | element_iterator itStart = m_elements.begin(); |
| 476 | element_iterator itEnd = m_elements.begin(); |
| 477 | std::advance(itStart, std::distance(m_elements.cbegin(), first)); |
| 478 | std::advance(itEnd, std::distance(m_elements.cbegin(), last)); |
| 479 | return m_elements.erase(itStart, itEnd); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 480 | #endif |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void |
| 484 | Block::push_back(const Block& element) |
| 485 | { |
| 486 | resetWire(); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 487 | m_elements.push_back(element); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 490 | Block::element_iterator |
| 491 | Block::insert(Block::element_const_iterator pos, const Block& element) |
| 492 | { |
| 493 | resetWire(); |
| 494 | |
| 495 | #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 496 | return m_elements.insert(pos, element); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 497 | #else |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 498 | element_iterator it = m_elements.begin(); |
| 499 | std::advance(it, std::distance(m_elements.cbegin(), pos)); |
| 500 | return m_elements.insert(it, element); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 501 | #endif |
| 502 | } |
| 503 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 504 | // ---- misc ---- |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 505 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 506 | Block::operator boost::asio::const_buffer() const |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 507 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 508 | return boost::asio::const_buffer(wire(), size()); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | bool |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 512 | operator==(const Block& lhs, const Block& rhs) |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 513 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 514 | return lhs.type() == rhs.type() && |
| 515 | lhs.value_size() == rhs.value_size() && |
Davide Pesavento | e245b05 | 2017-10-31 13:00:44 -0400 | [diff] [blame] | 516 | (lhs.value_size() == 0 || |
| 517 | std::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 520 | std::ostream& |
| 521 | operator<<(std::ostream& os, const Block& block) |
| 522 | { |
| 523 | auto oldFmt = os.flags(std::ios_base::dec); |
| 524 | |
| 525 | if (block.empty()) { |
| 526 | os << "[invalid]"; |
| 527 | } |
| 528 | else if (!block.m_elements.empty()) { |
| 529 | EncodingEstimator estimator; |
| 530 | size_t tlvLength = block.encodeValue(estimator); |
| 531 | os << block.type() << '[' << tlvLength << "]={"; |
| 532 | std::copy(block.elements_begin(), block.elements_end(), make_ostream_joiner(os, ',')); |
| 533 | os << '}'; |
| 534 | } |
| 535 | else if (block.value_size() > 0) { |
| 536 | os << block.type() << '[' << block.value_size() << "]="; |
| 537 | printHex(os, block.value(), block.value_size(), true); |
| 538 | } |
| 539 | else { |
| 540 | os << block.type() << "[empty]"; |
| 541 | } |
| 542 | |
| 543 | os.flags(oldFmt); |
| 544 | return os; |
| 545 | } |
| 546 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 547 | } // namespace ndn |