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