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