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 | /** |
Alexander Afanasyev | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 46 | const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = 8800; |
| 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 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 66 | throw 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 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 98 | throw 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 | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 115 | if (verifyLength) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 116 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 117 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
| 118 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 119 | throw tlv::Error("TLV length doesn't match buffer length"); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 120 | } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 124 | Block::Block(const uint8_t* buffer, size_t maxlength) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 125 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 126 | const uint8_t* tmp_begin = buffer; |
| 127 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 128 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 129 | m_type = tlv::readType(tmp_begin, tmp_end); |
| 130 | uint64_t length = tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 132 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 133 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 134 | throw tlv::Error("Not enough data in the buffer to fully parse TLV"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 137 | m_buffer = make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 138 | |
| 139 | m_begin = m_buffer->begin(); |
| 140 | m_end = m_buffer->end(); |
| 141 | m_size = m_end - m_begin; |
| 142 | |
| 143 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 144 | m_value_end = m_buffer->end(); |
| 145 | } |
| 146 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 147 | Block::Block(const void* bufferX, 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* buffer = reinterpret_cast<const uint8_t*>(bufferX); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 151 | const uint8_t* tmp_begin = buffer; |
| 152 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 153 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 154 | m_type = tlv::readType(tmp_begin, tmp_end); |
| 155 | uint64_t length = tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 156 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 157 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 158 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 159 | throw tlv::Error("Not enough data in the buffer to fully parse TLV"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 162 | m_buffer = make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 163 | |
| 164 | m_begin = m_buffer->begin(); |
| 165 | m_end = m_buffer->end(); |
| 166 | m_size = m_end - m_begin; |
| 167 | |
| 168 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 169 | m_value_end = m_buffer->end(); |
| 170 | } |
| 171 | |
| 172 | Block::Block(uint32_t type) |
| 173 | : m_type(type) |
| 174 | { |
| 175 | } |
| 176 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 177 | Block::Block(uint32_t type, const ConstBufferPtr& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 178 | : m_buffer(value) |
| 179 | , m_type(type) |
| 180 | , m_begin(m_buffer->end()) |
| 181 | , m_end(m_buffer->end()) |
| 182 | , m_value_begin(m_buffer->begin()) |
| 183 | , m_value_end(m_buffer->end()) |
| 184 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 185 | m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 188 | Block::Block(uint32_t type, const Block& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 189 | : m_buffer(value.m_buffer) |
| 190 | , m_type(type) |
| 191 | , m_begin(m_buffer->end()) |
| 192 | , m_end(m_buffer->end()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 193 | , m_value_begin(value.begin()) |
| 194 | , m_value_end(value.end()) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 195 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 196 | m_size = tlv::sizeOfVarNumber(m_type) + tlv::sizeOfVarNumber(value_size()) + value_size(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 199 | Block |
| 200 | Block::fromStream(std::istream& is) |
| 201 | { |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 202 | std::istream_iterator<uint8_t> begin(is >> std::noskipws); |
| 203 | std::istream_iterator<uint8_t> end; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 204 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 205 | uint32_t type = tlv::readType(begin, end); |
| 206 | uint64_t length = tlv::readVarNumber(begin, end); |
| 207 | |
| 208 | if (length == 0) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 209 | return makeEmptyBlock(type); |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 210 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 211 | |
| 212 | if (length > MAX_SIZE_OF_BLOCK_FROM_STREAM) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 213 | throw tlv::Error("Length of block from stream is too large"); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 214 | |
| 215 | // We may still have some problem here, if some exception happens, |
| 216 | // we may completely lose all the bytes extracted from the stream. |
| 217 | |
| 218 | char buf[MAX_SIZE_OF_BLOCK_FROM_STREAM]; |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 219 | buf[0] = *begin; |
| 220 | is.read(buf + 1, length - 1); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 221 | |
| 222 | if (length != static_cast<uint64_t>(is.gcount()) + 1) { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 223 | throw tlv::Error("Not enough data in the buffer to fully parse TLV"); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 226 | return makeBinaryBlock(type, buf, length); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 229 | std::tuple<bool, Block> |
| 230 | Block::fromBuffer(ConstBufferPtr buffer, size_t offset) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 231 | { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 232 | Buffer::const_iterator tempBegin = buffer->begin() + offset; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 233 | |
| 234 | uint32_t type; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 235 | bool isOk = tlv::readType(tempBegin, buffer->end(), type); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 236 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 237 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 238 | |
| 239 | uint64_t length; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 240 | isOk = tlv::readVarNumber(tempBegin, buffer->end(), length); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 241 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 242 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 243 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 244 | if (length > static_cast<uint64_t>(buffer->end() - tempBegin)) |
| 245 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 246 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 247 | return std::make_tuple(true, Block(buffer, type, |
| 248 | buffer->begin() + offset, tempBegin + length, |
| 249 | tempBegin, tempBegin + length)); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -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(const uint8_t* buffer, size_t maxSize) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 254 | { |
| 255 | const uint8_t* tempBegin = buffer; |
| 256 | const uint8_t* tempEnd = buffer + maxSize; |
| 257 | |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 258 | uint32_t type = 0; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 259 | bool isOk = tlv::readType(tempBegin, tempEnd, type); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 260 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 261 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 262 | |
| 263 | uint64_t length; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 264 | isOk = tlv::readVarNumber(tempBegin, tempEnd, length); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 265 | if (!isOk) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 266 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 267 | |
| 268 | if (length > static_cast<uint64_t>(tempEnd - tempBegin)) |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 269 | return std::make_tuple(false, Block()); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 270 | |
| 271 | BufferPtr sharedBuffer = make_shared<Buffer>(buffer, tempBegin + length); |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 272 | return std::make_tuple(true, |
| 273 | Block(sharedBuffer, type, |
| 274 | sharedBuffer->begin(), sharedBuffer->end(), |
| 275 | sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end())); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 278 | void |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 279 | Block::reset() |
| 280 | { |
| 281 | m_buffer.reset(); // reset of the shared_ptr |
| 282 | m_subBlocks.clear(); // remove all parsed subelements |
| 283 | |
| 284 | m_type = std::numeric_limits<uint32_t>::max(); |
| 285 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | Block::resetWire() |
| 290 | { |
| 291 | m_buffer.reset(); // reset of the shared_ptr |
| 292 | // keep subblocks |
| 293 | |
| 294 | // keep type |
| 295 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
| 296 | } |
| 297 | |
| 298 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 299 | Block::parse() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 300 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 301 | if (!m_subBlocks.empty() || value_size() == 0) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 302 | return; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 303 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 304 | Buffer::const_iterator begin = value_begin(); |
| 305 | Buffer::const_iterator end = value_end(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 306 | |
| 307 | while (begin != end) |
| 308 | { |
| 309 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 310 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 311 | uint32_t type = tlv::readType(begin, end); |
| 312 | uint64_t length = tlv::readVarNumber(begin, end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 313 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 314 | if (length > static_cast<uint64_t>(end - begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 315 | { |
| 316 | m_subBlocks.clear(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 317 | throw tlv::Error("TLV length exceeds buffer length"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 318 | } |
| 319 | Buffer::const_iterator element_end = begin + length; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 320 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 321 | m_subBlocks.push_back(Block(m_buffer, |
| 322 | type, |
| 323 | element_begin, element_end, |
| 324 | begin, element_end)); |
| 325 | |
| 326 | begin = element_end; |
| 327 | // don't do recursive parsing, just the top level |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | Block::encode() |
| 333 | { |
| 334 | if (hasWire()) |
| 335 | return; |
| 336 | |
| 337 | OBufferStream os; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 338 | tlv::writeVarNumber(os, type()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 339 | |
| 340 | if (hasValue()) |
| 341 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 342 | tlv::writeVarNumber(os, value_size()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 343 | os.write(reinterpret_cast<const char*>(value()), value_size()); |
| 344 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 345 | else if (m_subBlocks.size() == 0) |
| 346 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 347 | tlv::writeVarNumber(os, 0); |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 348 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 349 | else |
| 350 | { |
| 351 | size_t valueSize = 0; |
| 352 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 353 | valueSize += i->size(); |
| 354 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 355 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 356 | tlv::writeVarNumber(os, valueSize); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 357 | |
| 358 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 359 | if (i->hasWire()) |
| 360 | os.write(reinterpret_cast<const char*>(i->wire()), i->size()); |
| 361 | else if (i->hasValue()) { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 362 | tlv::writeVarNumber(os, i->type()); |
| 363 | tlv::writeVarNumber(os, i->value_size()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 364 | os.write(reinterpret_cast<const char*>(i->value()), i->value_size()); |
| 365 | } |
| 366 | else |
| 367 | throw Error("Underlying value buffer is empty"); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // now assign correct block |
| 372 | |
| 373 | m_buffer = os.buf(); |
| 374 | m_begin = m_buffer->begin(); |
| 375 | m_end = m_buffer->end(); |
| 376 | m_size = m_end - m_begin; |
| 377 | |
| 378 | m_value_begin = m_buffer->begin(); |
| 379 | m_value_end = m_buffer->end(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 380 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 381 | tlv::readType(m_value_begin, m_value_end); |
| 382 | tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 385 | const Block& |
| 386 | Block::get(uint32_t type) const |
| 387 | { |
| 388 | element_const_iterator it = this->find(type); |
| 389 | if (it != m_subBlocks.end()) |
| 390 | return *it; |
| 391 | |
| 392 | throw Error("(Block::get) Requested a non-existed type [" + |
| 393 | boost::lexical_cast<std::string>(type) + "] from Block"); |
| 394 | } |
| 395 | |
| 396 | Block::element_const_iterator |
| 397 | Block::find(uint32_t type) const |
| 398 | { |
| 399 | return std::find_if(m_subBlocks.begin(), m_subBlocks.end(), |
| 400 | [type] (const Block& subBlock) { return subBlock.type() == type; }); |
| 401 | } |
| 402 | |
| 403 | void |
| 404 | Block::remove(uint32_t type) |
| 405 | { |
| 406 | resetWire(); |
| 407 | |
| 408 | auto it = std::remove_if(m_subBlocks.begin(), m_subBlocks.end(), |
| 409 | [type] (const Block& subBlock) { return subBlock.type() != type; }); |
| 410 | m_subBlocks.resize(it - m_subBlocks.begin()); |
| 411 | } |
| 412 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 413 | Block |
| 414 | Block::blockFromValue() const |
| 415 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 416 | if (value_size() == 0) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 417 | throw Error("Underlying value buffer is empty"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 418 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 419 | Buffer::const_iterator begin = value_begin(), |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 420 | end = value_end(); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 421 | |
| 422 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 423 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 424 | uint32_t type = tlv::readType(begin, end); |
| 425 | uint64_t length = tlv::readVarNumber(begin, end); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 426 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 427 | if (length != static_cast<uint64_t>(end - begin)) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 428 | throw tlv::Error("TLV length mismatches buffer length"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 429 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 430 | return Block(m_buffer, |
| 431 | type, |
| 432 | element_begin, end, |
| 433 | begin, end); |
| 434 | } |
| 435 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 436 | bool |
| 437 | Block::operator==(const Block& other) const |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 438 | { |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 439 | return this->size() == other.size() && |
| 440 | std::equal(this->begin(), this->end(), other.begin()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 443 | Block::operator boost::asio::const_buffer() const |
| 444 | { |
| 445 | return boost::asio::const_buffer(wire(), size()); |
| 446 | } |
| 447 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 448 | } // namespace ndn |