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