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 | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 36 | #if NDN_CXX_HAVE_IS_MOVE_CONSTRUCTIBLE |
| 37 | static_assert(std::is_move_constructible<Buffer>::value, |
| 38 | "Buffer must be MoveConstructible"); |
| 39 | #endif // NDN_CXX_HAVE_IS_MOVE_CONSTRUCTIBLE |
| 40 | |
| 41 | #if NDN_CXX_HAVE_IS_MOVE_ASSIGNABLE |
| 42 | static_assert(std::is_move_assignable<Buffer>::value, |
| 43 | "Buffer must be MoveAssignable"); |
| 44 | #endif // NDN_CXX_HAVE_IS_MOVE_ASSIGNABLE |
| 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 | { |
| 202 | std::istream_iterator<uint8_t> tmp_begin(is); |
| 203 | std::istream_iterator<uint8_t> tmp_end; |
| 204 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 205 | uint32_t type = tlv::readType(tmp_begin, tmp_end); |
| 206 | uint64_t length = tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 207 | |
| 208 | if (length > MAX_SIZE_OF_BLOCK_FROM_STREAM) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 209 | throw tlv::Error("Length of block from stream is too large"); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 210 | |
| 211 | // We may still have some problem here, if some exception happens, |
| 212 | // we may completely lose all the bytes extracted from the stream. |
| 213 | |
| 214 | char buf[MAX_SIZE_OF_BLOCK_FROM_STREAM]; |
| 215 | buf[0] = *tmp_begin; |
| 216 | is.read(buf+1, length-1); |
| 217 | |
| 218 | if (length != static_cast<uint64_t>(is.gcount()) + 1) { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 219 | 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] | 220 | } |
| 221 | |
| 222 | return dataBlock(type, buf, length); |
| 223 | } |
| 224 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 225 | bool |
| 226 | Block::fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block) |
| 227 | { |
| 228 | Buffer::const_iterator tempBegin = wire->begin() + offset; |
| 229 | |
| 230 | uint32_t type; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 231 | bool isOk = tlv::readType(tempBegin, wire->end(), type); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 232 | if (!isOk) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 233 | return false; |
| 234 | |
| 235 | uint64_t length; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 236 | isOk = tlv::readVarNumber(tempBegin, wire->end(), length); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 237 | if (!isOk) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 238 | return false; |
| 239 | |
| 240 | if (length > static_cast<uint64_t>(wire->end() - tempBegin)) |
| 241 | { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | block = Block(wire, type, |
| 246 | wire->begin() + offset, tempBegin + length, |
| 247 | tempBegin, tempBegin + length); |
| 248 | |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | bool |
| 253 | Block::fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block) |
| 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) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 261 | return false; |
| 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) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 266 | return false; |
| 267 | |
| 268 | if (length > static_cast<uint64_t>(tempEnd - tempBegin)) |
| 269 | { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | BufferPtr sharedBuffer = make_shared<Buffer>(buffer, tempBegin + length); |
| 274 | block = Block(sharedBuffer, type, |
| 275 | sharedBuffer->begin(), sharedBuffer->end(), |
| 276 | sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end()); |
| 277 | |
| 278 | return true; |
| 279 | } |
| 280 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 281 | void |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 282 | Block::reset() |
| 283 | { |
| 284 | m_buffer.reset(); // reset of the shared_ptr |
| 285 | m_subBlocks.clear(); // remove all parsed subelements |
| 286 | |
| 287 | m_type = std::numeric_limits<uint32_t>::max(); |
| 288 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | Block::resetWire() |
| 293 | { |
| 294 | m_buffer.reset(); // reset of the shared_ptr |
| 295 | // keep subblocks |
| 296 | |
| 297 | // keep type |
| 298 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
| 299 | } |
| 300 | |
| 301 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 302 | Block::parse() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 303 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 304 | if (!m_subBlocks.empty() || value_size() == 0) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 305 | return; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 306 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 307 | Buffer::const_iterator begin = value_begin(); |
| 308 | Buffer::const_iterator end = value_end(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 309 | |
| 310 | while (begin != end) |
| 311 | { |
| 312 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 313 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 314 | uint32_t type = tlv::readType(begin, end); |
| 315 | uint64_t length = tlv::readVarNumber(begin, end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 316 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 317 | if (length > static_cast<uint64_t>(end - begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 318 | { |
| 319 | m_subBlocks.clear(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 320 | throw tlv::Error("TLV length exceeds buffer length"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 321 | } |
| 322 | Buffer::const_iterator element_end = begin + length; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 323 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 324 | m_subBlocks.push_back(Block(m_buffer, |
| 325 | type, |
| 326 | element_begin, element_end, |
| 327 | begin, element_end)); |
| 328 | |
| 329 | begin = element_end; |
| 330 | // don't do recursive parsing, just the top level |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | Block::encode() |
| 336 | { |
| 337 | if (hasWire()) |
| 338 | return; |
| 339 | |
| 340 | OBufferStream os; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 341 | tlv::writeVarNumber(os, type()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 342 | |
| 343 | if (hasValue()) |
| 344 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 345 | tlv::writeVarNumber(os, value_size()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 346 | os.write(reinterpret_cast<const char*>(value()), value_size()); |
| 347 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 348 | else if (m_subBlocks.size() == 0) |
| 349 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 350 | tlv::writeVarNumber(os, 0); |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 351 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 352 | else |
| 353 | { |
| 354 | size_t valueSize = 0; |
| 355 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 356 | valueSize += i->size(); |
| 357 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 358 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 359 | tlv::writeVarNumber(os, valueSize); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 360 | |
| 361 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 362 | if (i->hasWire()) |
| 363 | os.write(reinterpret_cast<const char*>(i->wire()), i->size()); |
| 364 | else if (i->hasValue()) { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 365 | tlv::writeVarNumber(os, i->type()); |
| 366 | tlv::writeVarNumber(os, i->value_size()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 367 | os.write(reinterpret_cast<const char*>(i->value()), i->value_size()); |
| 368 | } |
| 369 | else |
| 370 | throw Error("Underlying value buffer is empty"); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // now assign correct block |
| 375 | |
| 376 | m_buffer = os.buf(); |
| 377 | m_begin = m_buffer->begin(); |
| 378 | m_end = m_buffer->end(); |
| 379 | m_size = m_end - m_begin; |
| 380 | |
| 381 | m_value_begin = m_buffer->begin(); |
| 382 | m_value_end = m_buffer->end(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 383 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 384 | tlv::readType(m_value_begin, m_value_end); |
| 385 | tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 388 | const Block& |
| 389 | Block::get(uint32_t type) const |
| 390 | { |
| 391 | element_const_iterator it = this->find(type); |
| 392 | if (it != m_subBlocks.end()) |
| 393 | return *it; |
| 394 | |
| 395 | throw Error("(Block::get) Requested a non-existed type [" + |
| 396 | boost::lexical_cast<std::string>(type) + "] from Block"); |
| 397 | } |
| 398 | |
| 399 | Block::element_const_iterator |
| 400 | Block::find(uint32_t type) const |
| 401 | { |
| 402 | return std::find_if(m_subBlocks.begin(), m_subBlocks.end(), |
| 403 | [type] (const Block& subBlock) { return subBlock.type() == type; }); |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | Block::remove(uint32_t type) |
| 408 | { |
| 409 | resetWire(); |
| 410 | |
| 411 | auto it = std::remove_if(m_subBlocks.begin(), m_subBlocks.end(), |
| 412 | [type] (const Block& subBlock) { return subBlock.type() != type; }); |
| 413 | m_subBlocks.resize(it - m_subBlocks.begin()); |
| 414 | } |
| 415 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 416 | Block |
| 417 | Block::blockFromValue() const |
| 418 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 419 | if (value_size() == 0) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 420 | throw Error("Underlying value buffer is empty"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 421 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 422 | Buffer::const_iterator begin = value_begin(), |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 423 | end = value_end(); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 424 | |
| 425 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 426 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 427 | uint32_t type = tlv::readType(begin, end); |
| 428 | uint64_t length = tlv::readVarNumber(begin, end); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 429 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 430 | if (length != static_cast<uint64_t>(end - begin)) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 431 | throw tlv::Error("TLV length mismatches buffer length"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 432 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 433 | return Block(m_buffer, |
| 434 | type, |
| 435 | element_begin, end, |
| 436 | begin, end); |
| 437 | } |
| 438 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 439 | bool |
| 440 | Block::operator==(const Block& other) const |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 441 | { |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 442 | return this->size() == other.size() && |
| 443 | std::equal(this->begin(), this->end(), other.begin()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 446 | Block::operator boost::asio::const_buffer() const |
| 447 | { |
| 448 | return boost::asio::const_buffer(wire(), size()); |
| 449 | } |
| 450 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 451 | } // namespace ndn |