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