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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 "common.hpp" |
| 25 | |
| 26 | #include "block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | #include "block-helpers.hpp" |
| 28 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 29 | #include "tlv.hpp" |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 30 | #include "encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 31 | #include "buffer-stream.hpp" |
| 32 | |
| 33 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame^] | 34 | #include <boost/asio/buffer.hpp> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 38 | const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = 8800; |
| 39 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 40 | Block::Block() |
| 41 | : m_type(std::numeric_limits<uint32_t>::max()) |
| 42 | { |
| 43 | } |
| 44 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 45 | Block::Block(const EncodingBuffer& buffer) |
| 46 | : m_buffer(buffer.m_buffer) |
| 47 | , m_begin(buffer.begin()) |
| 48 | , m_end(buffer.end()) |
| 49 | , m_size(m_end - m_begin) |
| 50 | { |
| 51 | m_value_begin = m_begin; |
| 52 | m_value_end = m_end; |
| 53 | |
| 54 | m_type = Tlv::readType(m_value_begin, m_value_end); |
| 55 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
| 56 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
| 57 | { |
| 58 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 59 | } |
| 60 | } |
| 61 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 62 | Block::Block(const ConstBufferPtr& wire, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 63 | uint32_t type, |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 64 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
| 65 | const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 66 | : m_buffer(wire) |
| 67 | , m_type(type) |
| 68 | , m_begin(begin) |
| 69 | , m_end(end) |
| 70 | , m_size(m_end - m_begin) |
| 71 | , m_value_begin(valueBegin) |
| 72 | , m_value_end(valueEnd) |
| 73 | { |
| 74 | } |
| 75 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 76 | Block::Block(const ConstBufferPtr& buffer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 77 | : m_buffer(buffer) |
| 78 | , m_begin(m_buffer->begin()) |
| 79 | , m_end(m_buffer->end()) |
| 80 | , m_size(m_end - m_begin) |
| 81 | { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 82 | m_value_begin = m_begin; |
| 83 | m_value_end = m_end; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 85 | m_type = Tlv::readType(m_value_begin, m_value_end); |
| 86 | |
| 87 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 88 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 89 | { |
| 90 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 91 | } |
| 92 | } |
| 93 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 94 | Block::Block(const ConstBufferPtr& buffer, |
| 95 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 96 | bool verifyLength/* = true*/) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 97 | : m_buffer(buffer) |
| 98 | , m_begin(begin) |
| 99 | , m_end(end) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 100 | , m_size(m_end - m_begin) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 102 | m_value_begin = m_begin; |
| 103 | m_value_end = m_end; |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 105 | m_type = Tlv::readType(m_value_begin, m_value_end); |
Alexander Afanasyev | 63ab084 | 2014-03-19 18:39:31 -0700 | [diff] [blame] | 106 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 107 | if (verifyLength) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 108 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 109 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
| 110 | { |
| 111 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 112 | } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 117 | Block::Block(const uint8_t* buffer, size_t maxlength) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 118 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 119 | const uint8_t* tmp_begin = buffer; |
| 120 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 122 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 123 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 125 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 126 | { |
| 127 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 128 | } |
| 129 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 130 | m_buffer = make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 131 | |
| 132 | m_begin = m_buffer->begin(); |
| 133 | m_end = m_buffer->end(); |
| 134 | m_size = m_end - m_begin; |
| 135 | |
| 136 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 137 | m_value_end = m_buffer->end(); |
| 138 | } |
| 139 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 140 | Block::Block(const void* bufferX, size_t maxlength) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 141 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 142 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(bufferX); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 143 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 144 | const uint8_t* tmp_begin = buffer; |
| 145 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 146 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 147 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 148 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 150 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 151 | { |
| 152 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 153 | } |
| 154 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 155 | m_buffer = make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 156 | |
| 157 | m_begin = m_buffer->begin(); |
| 158 | m_end = m_buffer->end(); |
| 159 | m_size = m_end - m_begin; |
| 160 | |
| 161 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 162 | m_value_end = m_buffer->end(); |
| 163 | } |
| 164 | |
| 165 | Block::Block(uint32_t type) |
| 166 | : m_type(type) |
| 167 | { |
| 168 | } |
| 169 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 170 | Block::Block(uint32_t type, const ConstBufferPtr& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 171 | : m_buffer(value) |
| 172 | , m_type(type) |
| 173 | , m_begin(m_buffer->end()) |
| 174 | , m_end(m_buffer->end()) |
| 175 | , m_value_begin(m_buffer->begin()) |
| 176 | , m_value_end(m_buffer->end()) |
| 177 | { |
| 178 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 179 | } |
| 180 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 181 | Block::Block(uint32_t type, const Block& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 182 | : m_buffer(value.m_buffer) |
| 183 | , m_type(type) |
| 184 | , m_begin(m_buffer->end()) |
| 185 | , m_end(m_buffer->end()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 186 | , m_value_begin(value.begin()) |
| 187 | , m_value_end(value.end()) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 188 | { |
| 189 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 190 | } |
| 191 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 192 | Block |
| 193 | Block::fromStream(std::istream& is) |
| 194 | { |
| 195 | std::istream_iterator<uint8_t> tmp_begin(is); |
| 196 | std::istream_iterator<uint8_t> tmp_end; |
| 197 | |
| 198 | uint32_t type = Tlv::readType(tmp_begin, tmp_end); |
| 199 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 200 | |
| 201 | if (length > MAX_SIZE_OF_BLOCK_FROM_STREAM) |
| 202 | throw Tlv::Error("Length of block from stream is too large"); |
| 203 | |
| 204 | // We may still have some problem here, if some exception happens, |
| 205 | // we may completely lose all the bytes extracted from the stream. |
| 206 | |
| 207 | char buf[MAX_SIZE_OF_BLOCK_FROM_STREAM]; |
| 208 | buf[0] = *tmp_begin; |
| 209 | is.read(buf+1, length-1); |
| 210 | |
| 211 | if (length != static_cast<uint64_t>(is.gcount()) + 1) { |
| 212 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 213 | } |
| 214 | |
| 215 | return dataBlock(type, buf, length); |
| 216 | } |
| 217 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 218 | bool |
| 219 | Block::fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block) |
| 220 | { |
| 221 | Buffer::const_iterator tempBegin = wire->begin() + offset; |
| 222 | |
| 223 | uint32_t type; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 224 | bool isOk = Tlv::readType(tempBegin, wire->end(), type); |
| 225 | if (!isOk) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 226 | return false; |
| 227 | |
| 228 | uint64_t length; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 229 | isOk = Tlv::readVarNumber(tempBegin, wire->end(), length); |
| 230 | if (!isOk) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 231 | return false; |
| 232 | |
| 233 | if (length > static_cast<uint64_t>(wire->end() - tempBegin)) |
| 234 | { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | block = Block(wire, type, |
| 239 | wire->begin() + offset, tempBegin + length, |
| 240 | tempBegin, tempBegin + length); |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | bool |
| 246 | Block::fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block) |
| 247 | { |
| 248 | const uint8_t* tempBegin = buffer; |
| 249 | const uint8_t* tempEnd = buffer + maxSize; |
| 250 | |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 251 | uint32_t type = 0; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 252 | bool isOk = Tlv::readType(tempBegin, tempEnd, type); |
| 253 | if (!isOk) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 254 | return false; |
| 255 | |
| 256 | uint64_t length; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 257 | isOk = Tlv::readVarNumber(tempBegin, tempEnd, length); |
| 258 | if (!isOk) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 259 | return false; |
| 260 | |
| 261 | if (length > static_cast<uint64_t>(tempEnd - tempBegin)) |
| 262 | { |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | BufferPtr sharedBuffer = make_shared<Buffer>(buffer, tempBegin + length); |
| 267 | block = Block(sharedBuffer, type, |
| 268 | sharedBuffer->begin(), sharedBuffer->end(), |
| 269 | sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end()); |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 274 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 275 | Block::parse() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 276 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 277 | if (!m_subBlocks.empty() || value_size() == 0) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 278 | return; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 279 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 280 | Buffer::const_iterator begin = value_begin(); |
| 281 | Buffer::const_iterator end = value_end(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 282 | |
| 283 | while (begin != end) |
| 284 | { |
| 285 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 286 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 287 | uint32_t type = Tlv::readType(begin, end); |
| 288 | uint64_t length = Tlv::readVarNumber(begin, end); |
| 289 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 290 | if (length > static_cast<uint64_t>(end - begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 291 | { |
| 292 | m_subBlocks.clear(); |
| 293 | throw Tlv::Error("TLV length exceeds buffer length"); |
| 294 | } |
| 295 | Buffer::const_iterator element_end = begin + length; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 296 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 297 | m_subBlocks.push_back(Block(m_buffer, |
| 298 | type, |
| 299 | element_begin, element_end, |
| 300 | begin, element_end)); |
| 301 | |
| 302 | begin = element_end; |
| 303 | // don't do recursive parsing, just the top level |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | Block::encode() |
| 309 | { |
| 310 | if (hasWire()) |
| 311 | return; |
| 312 | |
| 313 | OBufferStream os; |
| 314 | Tlv::writeVarNumber(os, type()); |
| 315 | |
| 316 | if (hasValue()) |
| 317 | { |
| 318 | Tlv::writeVarNumber(os, value_size()); |
| 319 | os.write(reinterpret_cast<const char*>(value()), value_size()); |
| 320 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 321 | else if (m_subBlocks.size() == 0) |
| 322 | { |
| 323 | Tlv::writeVarNumber(os, 0); |
| 324 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 325 | else |
| 326 | { |
| 327 | size_t valueSize = 0; |
| 328 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 329 | valueSize += i->size(); |
| 330 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 331 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 332 | Tlv::writeVarNumber(os, valueSize); |
| 333 | |
| 334 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 335 | if (i->hasWire()) |
| 336 | os.write(reinterpret_cast<const char*>(i->wire()), i->size()); |
| 337 | else if (i->hasValue()) { |
| 338 | Tlv::writeVarNumber(os, i->type()); |
| 339 | Tlv::writeVarNumber(os, i->value_size()); |
| 340 | os.write(reinterpret_cast<const char*>(i->value()), i->value_size()); |
| 341 | } |
| 342 | else |
| 343 | throw Error("Underlying value buffer is empty"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // now assign correct block |
| 348 | |
| 349 | m_buffer = os.buf(); |
| 350 | m_begin = m_buffer->begin(); |
| 351 | m_end = m_buffer->end(); |
| 352 | m_size = m_end - m_begin; |
| 353 | |
| 354 | m_value_begin = m_buffer->begin(); |
| 355 | m_value_end = m_buffer->end(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 357 | Tlv::readType(m_value_begin, m_value_end); |
| 358 | Tlv::readVarNumber(m_value_begin, m_value_end); |
| 359 | } |
| 360 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 361 | Block |
| 362 | Block::blockFromValue() const |
| 363 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 364 | if (value_size() == 0) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 365 | throw Error("Underlying value buffer is empty"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 366 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 367 | Buffer::const_iterator begin = value_begin(), |
| 368 | end = value_end(); |
| 369 | |
| 370 | Buffer::const_iterator element_begin = begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 371 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 372 | uint32_t type = Tlv::readType(begin, end); |
| 373 | uint64_t length = Tlv::readVarNumber(begin, end); |
| 374 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 375 | if (length != static_cast<uint64_t>(end - begin)) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 376 | throw Tlv::Error("TLV length mismatches buffer length"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 377 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 378 | return Block(m_buffer, |
| 379 | type, |
| 380 | element_begin, end, |
| 381 | begin, end); |
| 382 | } |
| 383 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 384 | const Block& |
| 385 | Block::get(uint32_t type) const |
| 386 | { |
| 387 | for (element_const_iterator i = m_subBlocks.begin(); |
| 388 | i != m_subBlocks.end(); |
| 389 | i++) |
| 390 | { |
| 391 | if (i->type() == type) |
| 392 | { |
| 393 | return *i; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | throw Error("(Block::get) Requested a non-existed type [" + |
| 398 | boost::lexical_cast<std::string>(type) + "] from Block"); |
| 399 | } |
| 400 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame^] | 401 | Block::operator boost::asio::const_buffer() const |
| 402 | { |
| 403 | return boost::asio::const_buffer(wire(), size()); |
| 404 | } |
| 405 | |
| 406 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 407 | } // namespace ndn |