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 | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | Block::Block() |
| 18 | : m_type(std::numeric_limits<uint32_t>::max()) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | Block::Block(const ConstBufferPtr &wire, |
| 23 | uint32_t type, |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 24 | const Buffer::const_iterator &begin, const Buffer::const_iterator &end, |
| 25 | const Buffer::const_iterator &valueBegin, const Buffer::const_iterator &valueEnd) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 26 | : m_buffer(wire) |
| 27 | , m_type(type) |
| 28 | , m_begin(begin) |
| 29 | , m_end(end) |
| 30 | , m_size(m_end - m_begin) |
| 31 | , m_value_begin(valueBegin) |
| 32 | , m_value_end(valueEnd) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | Block::Block(const ConstBufferPtr &buffer) |
| 37 | : m_buffer(buffer) |
| 38 | , m_begin(m_buffer->begin()) |
| 39 | , m_end(m_buffer->end()) |
| 40 | , m_size(m_end - m_begin) |
| 41 | { |
| 42 | m_value_begin = m_buffer->begin(); |
| 43 | m_value_end = m_buffer->end(); |
| 44 | |
| 45 | m_type = Tlv::readType(m_value_begin, m_value_end); |
| 46 | |
| 47 | uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end); |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 48 | if (length != static_cast<uint64_t>(m_value_end - m_value_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 49 | { |
| 50 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 51 | } |
| 52 | } |
| 53 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 54 | Block::Block(const ConstBufferPtr &buffer, |
| 55 | const Buffer::const_iterator &begin, const Buffer::const_iterator &end) |
| 56 | : m_buffer(buffer) |
| 57 | , m_begin(begin) |
| 58 | , m_end(end) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 59 | , m_size(m_end - m_begin) |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 60 | { |
| 61 | m_value_begin = m_buffer->begin(); |
| 62 | m_value_end = m_buffer->end(); |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 64 | m_type = Tlv::readType(m_value_begin, m_value_end); |
| 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 | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 67 | { |
| 68 | throw Tlv::Error("TLV length doesn't match buffer length"); |
| 69 | } |
| 70 | } |
| 71 | |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 72 | Block::Block(std::istream& is) |
| 73 | { |
| 74 | std::istream_iterator<uint8_t> tmp_begin(is); |
| 75 | std::istream_iterator<uint8_t> tmp_end; |
| 76 | |
| 77 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 78 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 79 | |
| 80 | // 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. |
| 81 | |
| 82 | OBufferStream os; |
| 83 | size_t headerLength = Tlv::writeVarNumber(os, m_type); |
| 84 | headerLength += Tlv::writeVarNumber(os, length); |
| 85 | |
| 86 | char* buf = new char[length]; |
| 87 | buf[0] = *tmp_begin; |
| 88 | is.read(buf+1, length-1); |
| 89 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 90 | if(length != static_cast<uint64_t>(is.gcount())+1) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 91 | { |
| 92 | delete [] buf; |
| 93 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 94 | } |
| 95 | |
| 96 | os.write(buf, length); |
| 97 | delete [] buf; |
| 98 | |
| 99 | m_buffer = os.buf(); |
| 100 | |
| 101 | m_begin = m_buffer->begin(); |
| 102 | m_end = m_buffer->end(); |
| 103 | m_size = m_end - m_begin; |
| 104 | |
| 105 | m_value_begin = m_buffer->begin() + headerLength; |
| 106 | m_value_end = m_buffer->end(); |
| 107 | } |
| 108 | |
| 109 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 110 | Block::Block(const uint8_t *buffer, size_t maxlength) |
| 111 | { |
| 112 | const uint8_t * tmp_begin = buffer; |
| 113 | const uint8_t * tmp_end = buffer + maxlength; |
| 114 | |
| 115 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 116 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 117 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 118 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 119 | { |
| 120 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 121 | } |
| 122 | |
| 123 | m_buffer = ptr_lib::make_shared<Buffer> (buffer, (tmp_begin - buffer) + length); |
| 124 | |
| 125 | m_begin = m_buffer->begin(); |
| 126 | m_end = m_buffer->end(); |
| 127 | m_size = m_end - m_begin; |
| 128 | |
| 129 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 130 | m_value_end = m_buffer->end(); |
| 131 | } |
| 132 | |
| 133 | Block::Block(const void *bufferX, size_t maxlength) |
| 134 | { |
| 135 | const uint8_t * buffer = reinterpret_cast<const uint8_t*>(bufferX); |
| 136 | |
| 137 | const uint8_t * tmp_begin = buffer; |
| 138 | const uint8_t * tmp_end = buffer + maxlength; |
| 139 | |
| 140 | m_type = Tlv::readType(tmp_begin, tmp_end); |
| 141 | uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end); |
| 142 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 143 | if (length > static_cast<uint64_t>(tmp_end - tmp_begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 144 | { |
| 145 | throw Tlv::Error("Not enough data in the buffer to fully parse TLV"); |
| 146 | } |
| 147 | |
| 148 | m_buffer = ptr_lib::make_shared<Buffer> (buffer, (tmp_begin - buffer) + length); |
| 149 | |
| 150 | m_begin = m_buffer->begin(); |
| 151 | m_end = m_buffer->end(); |
| 152 | m_size = m_end - m_begin; |
| 153 | |
| 154 | m_value_begin = m_buffer->begin() + (tmp_begin - buffer); |
| 155 | m_value_end = m_buffer->end(); |
| 156 | } |
| 157 | |
| 158 | Block::Block(uint32_t type) |
| 159 | : m_type(type) |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | Block::Block(uint32_t type, const ConstBufferPtr &value) |
| 164 | : m_buffer(value) |
| 165 | , m_type(type) |
| 166 | , m_begin(m_buffer->end()) |
| 167 | , m_end(m_buffer->end()) |
| 168 | , m_value_begin(m_buffer->begin()) |
| 169 | , m_value_end(m_buffer->end()) |
| 170 | { |
| 171 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 172 | } |
| 173 | |
| 174 | Block::Block(uint32_t type, const Block &value) |
| 175 | : m_buffer(value.m_buffer) |
| 176 | , m_type(type) |
| 177 | , m_begin(m_buffer->end()) |
| 178 | , m_end(m_buffer->end()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 179 | , m_value_begin(value.begin()) |
| 180 | , m_value_end(value.end()) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 181 | { |
| 182 | m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size(); |
| 183 | } |
| 184 | |
| 185 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 186 | Block::parse() const |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 187 | { |
| 188 | if (!m_subBlocks.empty() || value_size()==0) |
| 189 | return; |
| 190 | |
| 191 | Buffer::const_iterator begin = value_begin(), |
| 192 | end = value_end(); |
| 193 | |
| 194 | while (begin != end) |
| 195 | { |
| 196 | Buffer::const_iterator element_begin = begin; |
| 197 | |
| 198 | uint32_t type = Tlv::readType(begin, end); |
| 199 | uint64_t length = Tlv::readVarNumber(begin, end); |
| 200 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 201 | if (length > static_cast<uint64_t>(end - begin)) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 202 | { |
| 203 | m_subBlocks.clear(); |
| 204 | throw Tlv::Error("TLV length exceeds buffer length"); |
| 205 | } |
| 206 | Buffer::const_iterator element_end = begin + length; |
| 207 | |
| 208 | m_subBlocks.push_back(Block(m_buffer, |
| 209 | type, |
| 210 | element_begin, element_end, |
| 211 | begin, element_end)); |
| 212 | |
| 213 | begin = element_end; |
| 214 | // don't do recursive parsing, just the top level |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | Block::encode() |
| 220 | { |
| 221 | if (hasWire()) |
| 222 | return; |
| 223 | |
| 224 | OBufferStream os; |
| 225 | Tlv::writeVarNumber(os, type()); |
| 226 | |
| 227 | if (hasValue()) |
| 228 | { |
| 229 | Tlv::writeVarNumber(os, value_size()); |
| 230 | os.write(reinterpret_cast<const char*>(value()), value_size()); |
| 231 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 232 | else if (m_subBlocks.size() == 0) |
| 233 | { |
| 234 | Tlv::writeVarNumber(os, 0); |
| 235 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 236 | else |
| 237 | { |
| 238 | size_t valueSize = 0; |
| 239 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 240 | valueSize += i->size(); |
| 241 | } |
| 242 | |
| 243 | Tlv::writeVarNumber(os, valueSize); |
| 244 | |
| 245 | for (element_const_iterator i = m_subBlocks.begin(); i != m_subBlocks.end(); ++i) { |
| 246 | if (i->hasWire()) |
| 247 | os.write(reinterpret_cast<const char*>(i->wire()), i->size()); |
| 248 | else if (i->hasValue()) { |
| 249 | Tlv::writeVarNumber(os, i->type()); |
| 250 | Tlv::writeVarNumber(os, i->value_size()); |
| 251 | os.write(reinterpret_cast<const char*>(i->value()), i->value_size()); |
| 252 | } |
| 253 | else |
| 254 | throw Error("Underlying value buffer is empty"); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // now assign correct block |
| 259 | |
| 260 | m_buffer = os.buf(); |
| 261 | m_begin = m_buffer->begin(); |
| 262 | m_end = m_buffer->end(); |
| 263 | m_size = m_end - m_begin; |
| 264 | |
| 265 | m_value_begin = m_buffer->begin(); |
| 266 | m_value_end = m_buffer->end(); |
| 267 | |
| 268 | Tlv::readType(m_value_begin, m_value_end); |
| 269 | Tlv::readVarNumber(m_value_begin, m_value_end); |
| 270 | } |
| 271 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 272 | Block |
| 273 | Block::blockFromValue() const |
| 274 | { |
| 275 | if (value_size()==0) |
| 276 | throw Error("Underlying value buffer is empty"); |
| 277 | |
| 278 | Buffer::const_iterator begin = value_begin(), |
| 279 | end = value_end(); |
| 280 | |
| 281 | Buffer::const_iterator element_begin = begin; |
| 282 | |
| 283 | uint32_t type = Tlv::readType(begin, end); |
| 284 | uint64_t length = Tlv::readVarNumber(begin, end); |
| 285 | |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 286 | if (length != static_cast<uint64_t>(end - begin)) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 287 | throw Tlv::Error("TLV length mismatches buffer length"); |
| 288 | |
| 289 | return Block(m_buffer, |
| 290 | type, |
| 291 | element_begin, end, |
| 292 | begin, end); |
| 293 | } |
| 294 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 295 | } // namespace ndn |