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