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