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