Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Junxiao Shi | 6617e49 | 2016-01-19 07:33:00 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 "block-helpers.hpp" |
| 26 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 27 | #include "tlv.hpp" |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 28 | #include "encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | #include "buffer-stream.hpp" |
| 30 | |
| 31 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 32 | #include <boost/asio/buffer.hpp> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | |
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 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 48 | Block::Block() |
| 49 | : m_type(std::numeric_limits<uint32_t>::max()) |
| 50 | { |
| 51 | } |
| 52 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 53 | Block::Block(const EncodingBuffer& buffer) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 54 | : m_buffer(const_cast<EncodingBuffer&>(buffer).getBuffer()) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 55 | , m_begin(buffer.begin()) |
| 56 | , m_end(buffer.end()) |
| 57 | , m_size(m_end - m_begin) |
| 58 | { |
| 59 | m_value_begin = m_begin; |
| 60 | m_value_end = m_end; |
| 61 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 62 | m_type = tlv::readType(m_value_begin, m_value_end); |
| 63 | uint64_t length = tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 64 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
| 65 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 66 | BOOST_THROW_EXCEPTION(tlv::Error("TLV length doesn't match buffer length")); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 70 | Block::Block(const ConstBufferPtr& wire, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 71 | uint32_t type, |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 72 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
| 73 | const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 74 | : m_buffer(wire) |
| 75 | , m_type(type) |
| 76 | , m_begin(begin) |
| 77 | , m_end(end) |
| 78 | , m_size(m_end - m_begin) |
| 79 | , m_value_begin(valueBegin) |
| 80 | , m_value_end(valueEnd) |
| 81 | { |
| 82 | } |
| 83 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 84 | Block::Block(const ConstBufferPtr& buffer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 85 | : m_buffer(buffer) |
| 86 | , m_begin(m_buffer->begin()) |
| 87 | , m_end(m_buffer->end()) |
| 88 | , m_size(m_end - m_begin) |
| 89 | { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 90 | m_value_begin = m_begin; |
| 91 | m_value_end = m_end; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 92 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 93 | m_type = tlv::readType(m_value_begin, m_value_end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 94 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 95 | uint64_t length = tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 96 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 97 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 98 | BOOST_THROW_EXCEPTION(tlv::Error("TLV length doesn't match buffer length")); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 102 | Block::Block(const ConstBufferPtr& buffer, |
| 103 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 104 | bool verifyLength/* = true*/) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 105 | : m_buffer(buffer) |
| 106 | , m_begin(begin) |
| 107 | , m_end(end) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 108 | , m_size(m_end - m_begin) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 109 | { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 110 | m_value_begin = m_begin; |
| 111 | m_value_end = m_end; |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 112 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 113 | m_type = tlv::readType(m_value_begin, m_value_end); |
| 114 | uint64_t length = tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 115 | if (verifyLength) { |
| 116 | if (length != static_cast<uint64_t>(std::distance(m_value_begin, m_value_end))) { |
| 117 | BOOST_THROW_EXCEPTION(tlv::Error("TLV length doesn't match buffer length")); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 118 | } |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | Block::Block(const Block& block, |
| 123 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
| 124 | bool verifyLength/* = true*/) |
| 125 | : m_buffer(block.m_buffer) |
| 126 | , m_begin(begin) |
| 127 | , m_end(end) |
| 128 | , m_size(m_end - m_begin) |
| 129 | { |
| 130 | if (!(m_buffer->begin() <= begin && begin <= m_buffer->end()) || |
| 131 | !(m_buffer->begin() <= end && end <= m_buffer->end())) { |
| 132 | BOOST_THROW_EXCEPTION(Error("begin/end iterators do not point to the underlying buffer of the block")); |
| 133 | } |
| 134 | |
| 135 | m_value_begin = m_begin; |
| 136 | m_value_end = m_end; |
| 137 | |
| 138 | m_type = tlv::readType(m_value_begin, m_value_end); |
| 139 | uint64_t length = tlv::readVarNumber(m_value_begin, m_value_end); |
| 140 | if (verifyLength) { |
| 141 | if (length != static_cast<uint64_t>(std::distance(m_value_begin, m_value_end))) { |
| 142 | BOOST_THROW_EXCEPTION(tlv::Error("TLV length doesn't match buffer length")); |
| 143 | } |
| 144 | } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 147 | Block::Block(const uint8_t* buffer, size_t maxlength) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 148 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 149 | const uint8_t* tmp_begin = buffer; |
| 150 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 151 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 152 | m_type = tlv::readType(tmp_begin, tmp_end); |
| 153 | uint64_t length = tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 155 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 156 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 157 | BOOST_THROW_EXCEPTION(tlv::Error("Not enough data in the buffer to fully parse TLV")); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 160 | m_buffer = make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 161 | |
| 162 | m_begin = m_buffer->begin(); |
| 163 | m_end = m_buffer->end(); |
| 164 | m_size = m_end - m_begin; |
| 165 | |
| 166 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 167 | m_value_end = m_buffer->end(); |
| 168 | } |
| 169 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 170 | Block::Block(const void* bufferX, size_t maxlength) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 171 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 172 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(bufferX); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 174 | const uint8_t* tmp_begin = buffer; |
| 175 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 176 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 177 | m_type = tlv::readType(tmp_begin, tmp_end); |
| 178 | uint64_t length = tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 180 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 181 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 182 | BOOST_THROW_EXCEPTION(tlv::Error("Not enough data in the buffer to fully parse TLV")); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 185 | m_buffer = make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 186 | |
| 187 | m_begin = m_buffer->begin(); |
| 188 | m_end = m_buffer->end(); |
| 189 | m_size = m_end - m_begin; |
| 190 | |
| 191 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 192 | m_value_end = m_buffer->end(); |
| 193 | } |
| 194 | |
| 195 | Block::Block(uint32_t type) |
| 196 | : m_type(type) |
| 197 | { |
| 198 | } |
| 199 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 200 | Block::Block(uint32_t type, const ConstBufferPtr& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 201 | : m_buffer(value) |
| 202 | , m_type(type) |
| 203 | , m_begin(m_buffer->end()) |
| 204 | , m_end(m_buffer->end()) |
| 205 | , m_value_begin(m_buffer->begin()) |
| 206 | , m_value_end(m_buffer->end()) |
| 207 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 208 | m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 211 | Block::Block(uint32_t type, const Block& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 212 | : m_buffer(value.m_buffer) |
| 213 | , m_type(type) |
| 214 | , m_begin(m_buffer->end()) |
| 215 | , m_end(m_buffer->end()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 216 | , m_value_begin(value.begin()) |
| 217 | , m_value_end(value.end()) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 218 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 219 | m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 222 | Block |
| 223 | Block::fromStream(std::istream& is) |
| 224 | { |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 225 | std::istream_iterator<uint8_t> begin(is >> std::noskipws); |
| 226 | std::istream_iterator<uint8_t> end; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 227 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 228 | uint32_t type = tlv::readType(begin, end); |
| 229 | uint64_t length = tlv::readVarNumber(begin, end); |
| 230 | |
| 231 | if (length == 0) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 232 | return makeEmptyBlock(type); |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 233 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 234 | |
| 235 | if (length > MAX_SIZE_OF_BLOCK_FROM_STREAM) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 236 | BOOST_THROW_EXCEPTION(tlv::Error("Length of block from stream is too large")); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 237 | |
| 238 | // We may still have some problem here, if some exception happens, |
| 239 | // we may completely lose all the bytes extracted from the stream. |
| 240 | |
| 241 | char buf[MAX_SIZE_OF_BLOCK_FROM_STREAM]; |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 242 | buf[0] = *begin; |
| 243 | is.read(buf + 1, length - 1); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 244 | |
| 245 | if (length != static_cast<uint64_t>(is.gcount()) + 1) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 246 | BOOST_THROW_EXCEPTION(tlv::Error("Not enough data in the buffer to fully parse TLV")); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 249 | return makeBinaryBlock(type, buf, length); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 252 | std::tuple<bool, Block> |
| 253 | Block::fromBuffer(ConstBufferPtr buffer, size_t offset) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 254 | { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 255 | Buffer::const_iterator tempBegin = buffer->begin() + offset; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 256 | |
| 257 | uint32_t type; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 258 | bool isOk = tlv::readType(tempBegin, buffer->end(), type); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 259 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 260 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 261 | |
| 262 | uint64_t length; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 263 | isOk = tlv::readVarNumber(tempBegin, buffer->end(), length); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 264 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 265 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 266 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 267 | if (length > static_cast<uint64_t>(buffer->end() - tempBegin)) |
| 268 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 269 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 270 | return std::make_tuple(true, Block(buffer, type, |
| 271 | buffer->begin() + offset, tempBegin + length, |
| 272 | tempBegin, tempBegin + length)); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 275 | std::tuple<bool, Block> |
| 276 | Block::fromBuffer(const uint8_t* buffer, size_t maxSize) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 277 | { |
| 278 | const uint8_t* tempBegin = buffer; |
| 279 | const uint8_t* tempEnd = buffer + maxSize; |
| 280 | |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 281 | uint32_t type = 0; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 282 | bool isOk = tlv::readType(tempBegin, tempEnd, type); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 283 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 284 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 285 | |
| 286 | uint64_t length; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 287 | isOk = tlv::readVarNumber(tempBegin, tempEnd, length); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 288 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 289 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 290 | |
| 291 | if (length > static_cast<uint64_t>(tempEnd - tempBegin)) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 292 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 293 | |
| 294 | BufferPtr sharedBuffer = make_shared<Buffer>(buffer, tempBegin + length); |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 295 | return std::make_tuple(true, |
| 296 | Block(sharedBuffer, type, |
| 297 | sharedBuffer->begin(), sharedBuffer->end(), |
| 298 | sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end())); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 301 | void |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 302 | Block::reset() |
| 303 | { |
| 304 | m_buffer.reset(); // reset of the shared_ptr |
| 305 | m_subBlocks.clear(); // remove all parsed subelements |
| 306 | |
| 307 | m_type = std::numeric_limits<uint32_t>::max(); |
| 308 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | Block::resetWire() |
| 313 | { |
| 314 | m_buffer.reset(); // reset of the shared_ptr |
| 315 | // keep subblocks |
| 316 | |
| 317 | // keep type |
| 318 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
| 319 | } |
| 320 | |
| 321 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 322 | Block::parse() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 323 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 324 | if (!m_subBlocks.empty() || value_size() == 0) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 325 | return; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 326 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 327 | Buffer::const_iterator begin = value_begin(); |
| 328 | Buffer::const_iterator end = value_end(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 329 | |
| 330 | while (begin != end) |
| 331 | { |
| 332 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 333 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 334 | uint32_t type = tlv::readType(begin, end); |
| 335 | uint64_t length = tlv::readVarNumber(begin, end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 336 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 337 | if (length > static_cast<uint64_t>(end - begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 338 | { |
| 339 | m_subBlocks.clear(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 340 | BOOST_THROW_EXCEPTION(tlv::Error("TLV length exceeds buffer length")); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 341 | } |
| 342 | Buffer::const_iterator element_end = begin + length; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 343 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 344 | m_subBlocks.push_back(Block(m_buffer, |
| 345 | type, |
| 346 | element_begin, element_end, |
| 347 | begin, element_end)); |
| 348 | |
| 349 | begin = element_end; |
| 350 | // don't do recursive parsing, just the top level |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | Block::encode() |
| 356 | { |
| 357 | if (hasWire()) |
| 358 | return; |
| 359 | |
| 360 | OBufferStream os; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 361 | tlv::writeVarNumber(os, type()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 362 | |
| 363 | if (hasValue()) |
| 364 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 365 | tlv::writeVarNumber(os, value_size()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 366 | os.write(reinterpret_cast<const char*>(value()), value_size()); |
| 367 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 368 | else if (m_subBlocks.size() == 0) |
| 369 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 370 | tlv::writeVarNumber(os, 0); |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 371 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 372 | else |
| 373 | { |
| 374 | size_t valueSize = 0; |
| 375 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 376 | valueSize += i->size(); |
| 377 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 378 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 379 | tlv::writeVarNumber(os, valueSize); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 380 | |
| 381 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 382 | if (i->hasWire()) |
| 383 | os.write(reinterpret_cast<const char*>(i->wire()), i->size()); |
| 384 | else if (i->hasValue()) { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 385 | tlv::writeVarNumber(os, i->type()); |
| 386 | tlv::writeVarNumber(os, i->value_size()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 387 | os.write(reinterpret_cast<const char*>(i->value()), i->value_size()); |
| 388 | } |
| 389 | else |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 390 | BOOST_THROW_EXCEPTION(Error("Underlying value buffer is empty")); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | // now assign correct block |
| 395 | |
| 396 | m_buffer = os.buf(); |
| 397 | m_begin = m_buffer->begin(); |
| 398 | m_end = m_buffer->end(); |
| 399 | m_size = m_end - m_begin; |
| 400 | |
| 401 | m_value_begin = m_buffer->begin(); |
| 402 | m_value_end = m_buffer->end(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 403 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 404 | tlv::readType(m_value_begin, m_value_end); |
| 405 | tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 408 | const Block& |
| 409 | Block::get(uint32_t type) const |
| 410 | { |
| 411 | element_const_iterator it = this->find(type); |
| 412 | if (it != m_subBlocks.end()) |
| 413 | return *it; |
| 414 | |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 415 | BOOST_THROW_EXCEPTION(Error("(Block::get) Requested a non-existed type [" + |
| 416 | boost::lexical_cast<std::string>(type) + "] from Block")); |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | Block::element_const_iterator |
| 420 | Block::find(uint32_t type) const |
| 421 | { |
| 422 | return std::find_if(m_subBlocks.begin(), m_subBlocks.end(), |
| 423 | [type] (const Block& subBlock) { return subBlock.type() == type; }); |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | Block::remove(uint32_t type) |
| 428 | { |
| 429 | resetWire(); |
| 430 | |
| 431 | auto it = std::remove_if(m_subBlocks.begin(), m_subBlocks.end(), |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 432 | [type] (const Block& subBlock) { return subBlock.type() == type; }); |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 433 | m_subBlocks.resize(it - m_subBlocks.begin()); |
| 434 | } |
| 435 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 436 | Block |
| 437 | Block::blockFromValue() const |
| 438 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 439 | if (value_size() == 0) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 440 | BOOST_THROW_EXCEPTION(Error("Underlying value buffer is empty")); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 441 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 442 | Buffer::const_iterator begin = value_begin(), |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 443 | end = value_end(); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 444 | |
| 445 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 446 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 447 | uint32_t type = tlv::readType(begin, end); |
| 448 | uint64_t length = tlv::readVarNumber(begin, end); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 449 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 450 | if (length != static_cast<uint64_t>(end - begin)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 451 | BOOST_THROW_EXCEPTION(tlv::Error("TLV length mismatches buffer length")); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 452 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 453 | return Block(m_buffer, |
| 454 | type, |
| 455 | element_begin, end, |
| 456 | begin, end); |
| 457 | } |
| 458 | |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 459 | Block::operator boost::asio::const_buffer() const |
| 460 | { |
| 461 | return boost::asio::const_buffer(wire(), size()); |
| 462 | } |
| 463 | |
| 464 | bool |
| 465 | Block::empty() const |
| 466 | { |
| 467 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 468 | } |
| 469 | |
| 470 | bool |
| 471 | Block::hasWire() const |
| 472 | { |
| 473 | return m_buffer && (m_begin != m_end); |
| 474 | } |
| 475 | |
| 476 | Buffer::const_iterator |
| 477 | Block::begin() const |
| 478 | { |
| 479 | if (!hasWire()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 480 | BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty")); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 481 | |
| 482 | return m_begin; |
| 483 | } |
| 484 | |
| 485 | Buffer::const_iterator |
| 486 | Block::end() const |
| 487 | { |
| 488 | if (!hasWire()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 489 | BOOST_THROW_EXCEPTION(Error("Underlying wire buffer is empty")); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 490 | |
| 491 | return m_end; |
| 492 | } |
| 493 | |
| 494 | const uint8_t* |
| 495 | Block::wire() const |
| 496 | { |
| 497 | if (!hasWire()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 498 | BOOST_THROW_EXCEPTION(Error("(Block::wire) Underlying wire buffer is empty")); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 499 | |
| 500 | return &*m_begin; |
| 501 | } |
| 502 | |
| 503 | size_t |
| 504 | Block::size() const |
| 505 | { |
| 506 | if (hasWire() || hasValue()) { |
| 507 | return m_size; |
| 508 | } |
| 509 | else |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 510 | BOOST_THROW_EXCEPTION(Error("Block size cannot be determined (undefined block size)")); |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | bool |
| 514 | Block::hasValue() const |
| 515 | { |
| 516 | return static_cast<bool>(m_buffer); |
| 517 | } |
| 518 | |
| 519 | const uint8_t* |
| 520 | Block::value() const |
| 521 | { |
| 522 | if (!hasValue()) |
| 523 | return 0; |
| 524 | |
| 525 | return &*m_value_begin; |
| 526 | } |
| 527 | |
| 528 | size_t |
| 529 | Block::value_size() const |
| 530 | { |
| 531 | if (!hasValue()) |
| 532 | return 0; |
| 533 | |
| 534 | return m_value_end - m_value_begin; |
| 535 | } |
| 536 | |
| 537 | Block::element_iterator |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 538 | Block::erase(Block::element_const_iterator position) |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 539 | { |
| 540 | resetWire(); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 541 | |
| 542 | #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 543 | return m_subBlocks.erase(position); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 544 | #else |
| 545 | element_iterator it = m_subBlocks.begin(); |
| 546 | std::advance(it, std::distance(m_subBlocks.cbegin(), position)); |
| 547 | return m_subBlocks.erase(it); |
| 548 | #endif |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | Block::element_iterator |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 552 | Block::erase(Block::element_const_iterator first, Block::element_const_iterator last) |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 553 | { |
| 554 | resetWire(); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 555 | |
| 556 | #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 557 | return m_subBlocks.erase(first, last); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 558 | #else |
| 559 | element_iterator itStart = m_subBlocks.begin(); |
| 560 | element_iterator itEnd = m_subBlocks.begin(); |
| 561 | std::advance(itStart, std::distance(m_subBlocks.cbegin(), first)); |
| 562 | std::advance(itEnd, std::distance(m_subBlocks.cbegin(), last)); |
| 563 | return m_subBlocks.erase(itStart, itEnd); |
| 564 | #endif |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void |
| 568 | Block::push_back(const Block& element) |
| 569 | { |
| 570 | resetWire(); |
| 571 | m_subBlocks.push_back(element); |
| 572 | } |
| 573 | |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 574 | Block::element_iterator |
| 575 | Block::insert(Block::element_const_iterator pos, const Block& element) |
| 576 | { |
| 577 | resetWire(); |
| 578 | |
| 579 | #ifdef NDN_CXX_HAVE_VECTOR_INSERT_ERASE_CONST_ITERATOR |
| 580 | return m_subBlocks.insert(pos, element); |
| 581 | #else |
| 582 | element_iterator it = m_subBlocks.begin(); |
| 583 | std::advance(it, std::distance(m_subBlocks.cbegin(), pos)); |
| 584 | return m_subBlocks.insert(it, element); |
| 585 | #endif |
| 586 | } |
| 587 | |
Joao Pereira | 9c2a9a8 | 2015-07-10 14:45:48 -0400 | [diff] [blame] | 588 | Block::element_const_iterator |
| 589 | Block::elements_begin() const |
| 590 | { |
| 591 | return m_subBlocks.begin(); |
| 592 | } |
| 593 | |
| 594 | Block::element_const_iterator |
| 595 | Block::elements_end() const |
| 596 | { |
| 597 | return m_subBlocks.end(); |
| 598 | } |
| 599 | |
| 600 | size_t |
| 601 | Block::elements_size() const |
| 602 | { |
| 603 | return m_subBlocks.size(); |
| 604 | } |
| 605 | |
| 606 | bool |
| 607 | Block::operator!=(const Block& other) const |
| 608 | { |
| 609 | return !this->operator==(other); |
| 610 | } |
| 611 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 612 | bool |
| 613 | Block::operator==(const Block& other) const |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 614 | { |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 615 | return this->size() == other.size() && |
| 616 | std::equal(this->begin(), this->end(), other.begin()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 619 | } // namespace ndn |