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 | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 40 | Block::Block(const ConstBufferPtr &wire, |
| 41 | uint32_t type, |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [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 | |
| 54 | Block::Block(const ConstBufferPtr &buffer) |
| 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 | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 62 | |
| 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 | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 72 | Block::Block(const ConstBufferPtr &buffer, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame^] | 73 | const Buffer::const_iterator &begin, const Buffer::const_iterator &end, |
| 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 | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame^] | 84 | if (verifyLength) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 85 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame^] | 86 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
| 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 | { |
| 96 | std::istream_iterator<uint8_t> tmp_begin(is); |
| 97 | std::istream_iterator<uint8_t> tmp_end; |
| 98 | |
| 99 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 100 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 101 | |
| 102 | // We may still have some problem here, if some exception happens in this constructor, we may completely lose all the bytes extracted from the stream. |
| 103 | |
| 104 | OBufferStream os; |
| 105 | size_t headerLength = Tlv::writeVarNumber(os, m_type); |
| 106 | headerLength += Tlv::writeVarNumber(os, length); |
| 107 | |
| 108 | char* buf = new char[length]; |
| 109 | buf[0] = *tmp_begin; |
| 110 | is.read(buf+1, length-1); |
| 111 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 112 | if(length != static_cast<uint64_t>(is.gcount())+1) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 113 | { |
| 114 | delete [] buf; |
| 115 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 116 | } |
| 117 | |
| 118 | os.write(buf, length); |
| 119 | delete [] buf; |
| 120 | |
| 121 | m_buffer = os.buf(); |
| 122 | |
| 123 | m_begin = m_buffer->begin(); |
| 124 | m_end = m_buffer->end(); |
| 125 | m_size = m_end - m_begin; |
| 126 | |
| 127 | m_value_begin = m_buffer->begin() + headerLength; |
| 128 | m_value_end = m_buffer->end(); |
| 129 | } |
| 130 | |
| 131 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 132 | Block::Block(const uint8_t *buffer, size_t maxlength) |
| 133 | { |
| 134 | const uint8_t * tmp_begin = buffer; |
| 135 | const uint8_t * tmp_end = buffer + maxlength; |
| 136 | |
| 137 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 138 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 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 | |
| 145 | m_buffer = ptr_lib::make_shared<Buffer> (buffer, (tmp_begin - buffer) + length); |
| 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(const void *bufferX, size_t maxlength) |
| 156 | { |
| 157 | const uint8_t * buffer = reinterpret_cast<const uint8_t*>(bufferX); |
| 158 | |
| 159 | const uint8_t * tmp_begin = buffer; |
| 160 | const uint8_t * tmp_end = buffer + maxlength; |
| 161 | |
| 162 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 163 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 164 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 165 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 166 | { |
| 167 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 168 | } |
| 169 | |
| 170 | m_buffer = ptr_lib::make_shared<Buffer> (buffer, (tmp_begin - buffer) + length); |
| 171 | |
| 172 | m_begin = m_buffer->begin(); |
| 173 | m_end = m_buffer->end(); |
| 174 | m_size = m_end - m_begin; |
| 175 | |
| 176 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 177 | m_value_end = m_buffer->end(); |
| 178 | } |
| 179 | |
| 180 | Block::Block(uint32_t type) |
| 181 | : m_type(type) |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | Block::Block(uint32_t type, const ConstBufferPtr &value) |
| 186 | : m_buffer(value) |
| 187 | , m_type(type) |
| 188 | , m_begin(m_buffer->end()) |
| 189 | , m_end(m_buffer->end()) |
| 190 | , m_value_begin(m_buffer->begin()) |
| 191 | , m_value_end(m_buffer->end()) |
| 192 | { |
| 193 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 194 | } |
| 195 | |
| 196 | Block::Block(uint32_t type, const Block &value) |
| 197 | : m_buffer(value.m_buffer) |
| 198 | , m_type(type) |
| 199 | , m_begin(m_buffer->end()) |
| 200 | , m_end(m_buffer->end()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 201 | , m_value_begin(value.begin()) |
| 202 | , m_value_end(value.end()) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 203 | { |
| 204 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 205 | } |
| 206 | |
| 207 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 208 | Block::parse() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 209 | { |
| 210 | if (!m_subBlocks.empty() || value_size()==0) |
| 211 | return; |
| 212 | |
| 213 | Buffer::const_iterator begin = value_begin(), |
| 214 | end = value_end(); |
| 215 | |
| 216 | while (begin != end) |
| 217 | { |
| 218 | Buffer::const_iterator element_begin = begin; |
| 219 | |
| 220 | uint32_t type = Tlv::readType(begin, end); |
| 221 | uint64_t length = Tlv::readVarNumber(begin, end); |
| 222 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 223 | if (length > static_cast<uint64_t>(end - begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 224 | { |
| 225 | m_subBlocks.clear(); |
| 226 | throw Tlv::Error("TLV length exceeds buffer length"); |
| 227 | } |
| 228 | Buffer::const_iterator element_end = begin + length; |
| 229 | |
| 230 | m_subBlocks.push_back(Block(m_buffer, |
| 231 | type, |
| 232 | element_begin, element_end, |
| 233 | begin, element_end)); |
| 234 | |
| 235 | begin = element_end; |
| 236 | // don't do recursive parsing, just the top level |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | Block::encode() |
| 242 | { |
| 243 | if (hasWire()) |
| 244 | return; |
| 245 | |
| 246 | OBufferStream os; |
| 247 | Tlv::writeVarNumber(os, type()); |
| 248 | |
| 249 | if (hasValue()) |
| 250 | { |
| 251 | Tlv::writeVarNumber(os, value_size()); |
| 252 | os.write(reinterpret_cast<const char*>(value()), value_size()); |
| 253 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 254 | else if (m_subBlocks.size() == 0) |
| 255 | { |
| 256 | Tlv::writeVarNumber(os, 0); |
| 257 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 258 | else |
| 259 | { |
| 260 | size_t valueSize = 0; |
| 261 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 262 | valueSize += i->size(); |
| 263 | } |
| 264 | |
| 265 | Tlv::writeVarNumber(os, valueSize); |
| 266 | |
| 267 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 268 | if (i->hasWire()) |
| 269 | os.write(reinterpret_cast<const char*>(i->wire()), i->size()); |
| 270 | else if (i->hasValue()) { |
| 271 | Tlv::writeVarNumber(os, i->type()); |
| 272 | Tlv::writeVarNumber(os, i->value_size()); |
| 273 | os.write(reinterpret_cast<const char*>(i->value()), i->value_size()); |
| 274 | } |
| 275 | else |
| 276 | throw Error("Underlying value buffer is empty"); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // now assign correct block |
| 281 | |
| 282 | m_buffer = os.buf(); |
| 283 | m_begin = m_buffer->begin(); |
| 284 | m_end = m_buffer->end(); |
| 285 | m_size = m_end - m_begin; |
| 286 | |
| 287 | m_value_begin = m_buffer->begin(); |
| 288 | m_value_end = m_buffer->end(); |
| 289 | |
| 290 | Tlv::readType(m_value_begin, m_value_end); |
| 291 | Tlv::readVarNumber(m_value_begin, m_value_end); |
| 292 | } |
| 293 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 294 | Block |
| 295 | Block::blockFromValue() const |
| 296 | { |
| 297 | if (value_size()==0) |
| 298 | throw Error("Underlying value buffer is empty"); |
| 299 | |
| 300 | Buffer::const_iterator begin = value_begin(), |
| 301 | end = value_end(); |
| 302 | |
| 303 | Buffer::const_iterator element_begin = begin; |
| 304 | |
| 305 | uint32_t type = Tlv::readType(begin, end); |
| 306 | uint64_t length = Tlv::readVarNumber(begin, end); |
| 307 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 308 | if (length != static_cast<uint64_t>(end - begin)) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 309 | throw Tlv::Error("TLV length mismatches buffer length"); |
| 310 | |
| 311 | return Block(m_buffer, |
| 312 | type, |
| 313 | element_begin, end, |
| 314 | begin, end); |
| 315 | } |
| 316 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 317 | } // namespace ndn |