Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 8 | */ |
| 9 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 10 | #include "common.hpp" |
| 11 | |
| 12 | #include "block.hpp" |
| 13 | #include "tlv.hpp" |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 14 | #include "encoding-buffer.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
| 17 | |
| 18 | Block::Block() |
| 19 | : m_type(std::numeric_limits<uint32_t>::max()) |
| 20 | { |
| 21 | } |
| 22 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 23 | Block::Block(const EncodingBuffer& buffer) |
| 24 | : m_buffer(buffer.m_buffer) |
| 25 | , m_begin(buffer.begin()) |
| 26 | , m_end(buffer.end()) |
| 27 | , m_size(m_end - m_begin) |
| 28 | { |
| 29 | m_value_begin = m_begin; |
| 30 | m_value_end = m_end; |
| 31 | |
| 32 | m_type = Tlv::readType(m_value_begin, m_value_end); |
| 33 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
| 34 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
| 35 | { |
| 36 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 37 | } |
| 38 | } |
| 39 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 40 | Block::Block(const ConstBufferPtr& wire, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 41 | uint32_t type, |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 42 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
| 43 | const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 44 | : m_buffer(wire) |
| 45 | , m_type(type) |
| 46 | , m_begin(begin) |
| 47 | , m_end(end) |
| 48 | , m_size(m_end - m_begin) |
| 49 | , m_value_begin(valueBegin) |
| 50 | , m_value_end(valueEnd) |
| 51 | { |
| 52 | } |
| 53 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 54 | Block::Block(const ConstBufferPtr& buffer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 55 | : m_buffer(buffer) |
| 56 | , m_begin(m_buffer->begin()) |
| 57 | , m_end(m_buffer->end()) |
| 58 | , m_size(m_end - m_begin) |
| 59 | { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 60 | m_value_begin = m_begin; |
| 61 | m_value_end = m_end; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 63 | m_type = Tlv::readType(m_value_begin, m_value_end); |
| 64 | |
| 65 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 66 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 67 | { |
| 68 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 69 | } |
| 70 | } |
| 71 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 72 | Block::Block(const ConstBufferPtr& buffer, |
| 73 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 74 | bool verifyLength/* = true*/) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 75 | : m_buffer(buffer) |
| 76 | , m_begin(begin) |
| 77 | , m_end(end) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 78 | , m_size(m_end - m_begin) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 79 | { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 80 | m_value_begin = m_begin; |
| 81 | m_value_end = m_end; |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 83 | m_type = Tlv::readType(m_value_begin, m_value_end); |
Alexander Afanasyev | 63ab084 | 2014-03-19 18:39:31 -0700 | [diff] [blame] | 84 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 85 | if (verifyLength) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 86 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 87 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
| 88 | { |
| 89 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 90 | } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 94 | Block::Block(std::istream& is) |
| 95 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 96 | std::istream_iterator<uint8_t> tmp_begin(is); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 97 | std::istream_iterator<uint8_t> tmp_end; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 98 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 99 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 100 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 101 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 102 | // We may still have some problem here, if some exception happens in this constructor, |
| 103 | // we may completely lose all the bytes extracted from the stream. |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 104 | |
| 105 | OBufferStream os; |
| 106 | size_t headerLength = Tlv::writeVarNumber(os, m_type); |
| 107 | headerLength += Tlv::writeVarNumber(os, length); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 108 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 109 | char* buf = new char[length]; |
| 110 | buf[0] = *tmp_begin; |
| 111 | is.read(buf+1, length-1); |
| 112 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 113 | if(length != static_cast<uint64_t>(is.gcount()) + 1) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 114 | { |
| 115 | delete [] buf; |
| 116 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 117 | } |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 118 | |
| 119 | os.write(buf, length); |
| 120 | delete [] buf; |
| 121 | |
| 122 | m_buffer = os.buf(); |
| 123 | |
| 124 | m_begin = m_buffer->begin(); |
| 125 | m_end = m_buffer->end(); |
| 126 | m_size = m_end - m_begin; |
| 127 | |
| 128 | m_value_begin = m_buffer->begin() + headerLength; |
| 129 | m_value_end = m_buffer->end(); |
| 130 | } |
| 131 | |
| 132 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 133 | Block::Block(const uint8_t* buffer, size_t maxlength) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 134 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 135 | const uint8_t* tmp_begin = buffer; |
| 136 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 138 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 139 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 141 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 142 | { |
| 143 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 144 | } |
| 145 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 146 | m_buffer = ptr_lib::make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 147 | |
| 148 | m_begin = m_buffer->begin(); |
| 149 | m_end = m_buffer->end(); |
| 150 | m_size = m_end - m_begin; |
| 151 | |
| 152 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 153 | m_value_end = m_buffer->end(); |
| 154 | } |
| 155 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 156 | Block::Block(const void* bufferX, size_t maxlength) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 157 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 158 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(bufferX); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 159 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 160 | const uint8_t* tmp_begin = buffer; |
| 161 | const uint8_t* tmp_end = buffer + maxlength; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 163 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 164 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 166 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 167 | { |
| 168 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 169 | } |
| 170 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 171 | m_buffer = ptr_lib::make_shared<Buffer>(buffer, (tmp_begin - buffer) + length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 172 | |
| 173 | m_begin = m_buffer->begin(); |
| 174 | m_end = m_buffer->end(); |
| 175 | m_size = m_end - m_begin; |
| 176 | |
| 177 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 178 | m_value_end = m_buffer->end(); |
| 179 | } |
| 180 | |
| 181 | Block::Block(uint32_t type) |
| 182 | : m_type(type) |
| 183 | { |
| 184 | } |
| 185 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 186 | Block::Block(uint32_t type, const ConstBufferPtr& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 187 | : m_buffer(value) |
| 188 | , m_type(type) |
| 189 | , m_begin(m_buffer->end()) |
| 190 | , m_end(m_buffer->end()) |
| 191 | , m_value_begin(m_buffer->begin()) |
| 192 | , m_value_end(m_buffer->end()) |
| 193 | { |
| 194 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 195 | } |
| 196 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 197 | Block::Block(uint32_t type, const Block& value) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 198 | : m_buffer(value.m_buffer) |
| 199 | , m_type(type) |
| 200 | , m_begin(m_buffer->end()) |
| 201 | , m_end(m_buffer->end()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 202 | , m_value_begin(value.begin()) |
| 203 | , m_value_end(value.end()) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 204 | { |
| 205 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 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 | |
| 241 | uint32_t type; |
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 | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 374 | } // namespace ndn |