blob: a734c1310a0726b9fe301c7d9b8b8704fce0a243 [file] [log] [blame]
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08001/* -*- 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 Afanasyeve2dcdfd2014-02-07 15:53:28 -080010#include "common.hpp"
11
12#include "block.hpp"
13#include "tlv.hpp"
Alexander Afanasyev15151312014-02-16 00:53:51 -080014#include "encoding-buffer.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080015
16namespace ndn {
17
18Block::Block()
19 : m_type(std::numeric_limits<uint32_t>::max())
20{
21}
22
Alexander Afanasyev15151312014-02-16 00:53:51 -080023Block::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 Afanasyeva465e972014-03-22 17:21:49 -070040Block::Block(const ConstBufferPtr& wire,
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080041 uint32_t type,
Alexander Afanasyeva465e972014-03-22 17:21:49 -070042 const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
43 const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080044 : 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 Afanasyeva465e972014-03-22 17:21:49 -070054Block::Block(const ConstBufferPtr& buffer)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080055 : m_buffer(buffer)
56 , m_begin(m_buffer->begin())
57 , m_end(m_buffer->end())
58 , m_size(m_end - m_begin)
59{
Alexander Afanasyev233750e2014-02-16 00:50:07 -080060 m_value_begin = m_begin;
61 m_value_end = m_end;
Alexander Afanasyev937aa782014-03-21 13:17:57 -070062
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080063 m_type = Tlv::readType(m_value_begin, m_value_end);
64
65 uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end);
Alexander Afanasyev9c7ed112014-02-07 12:23:03 -080066 if (length != static_cast<uint64_t>(m_value_end - m_value_begin))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080067 {
68 throw Tlv::Error("TLV length doesn't match buffer length");
69 }
70}
71
Alexander Afanasyeva465e972014-03-22 17:21:49 -070072Block::Block(const ConstBufferPtr& buffer,
73 const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080074 bool verifyLength/* = true*/)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080075 : m_buffer(buffer)
76 , m_begin(begin)
77 , m_end(end)
Alexander Afanasyev380420b2014-02-09 20:52:29 -080078 , m_size(m_end - m_begin)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080079{
Alexander Afanasyev233750e2014-02-16 00:50:07 -080080 m_value_begin = m_begin;
81 m_value_end = m_end;
Alexander Afanasyev380420b2014-02-09 20:52:29 -080082
Alexander Afanasyev187bc482014-02-06 15:04:04 -080083 m_type = Tlv::readType(m_value_begin, m_value_end);
Alexander Afanasyev63ab0842014-03-19 18:39:31 -070084 uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080085 if (verifyLength)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080086 {
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080087 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 Afanasyev187bc482014-02-06 15:04:04 -080091 }
92}
93
Yingdi Yu27158392014-01-20 13:04:20 -080094Block::Block(std::istream& is)
95{
Alexander Afanasyev937aa782014-03-21 13:17:57 -070096 std::istream_iterator<uint8_t> tmp_begin(is);
Yingdi Yu27158392014-01-20 13:04:20 -080097 std::istream_iterator<uint8_t> tmp_end;
Alexander Afanasyev937aa782014-03-21 13:17:57 -070098
Yingdi Yu27158392014-01-20 13:04:20 -080099 m_type = Tlv::readType(tmp_begin, tmp_end);
100 uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end);
101
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700102 // 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 Yu27158392014-01-20 13:04:20 -0800104
105 OBufferStream os;
106 size_t headerLength = Tlv::writeVarNumber(os, m_type);
107 headerLength += Tlv::writeVarNumber(os, length);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700108
Yingdi Yu27158392014-01-20 13:04:20 -0800109 char* buf = new char[length];
110 buf[0] = *tmp_begin;
111 is.read(buf+1, length-1);
112
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700113 if(length != static_cast<uint64_t>(is.gcount()) + 1)
Yingdi Yu27158392014-01-20 13:04:20 -0800114 {
115 delete [] buf;
116 throw Tlv::Error("Not enough data in the buffer to fully parse TLV");
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700117 }
Yingdi Yu27158392014-01-20 13:04:20 -0800118
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 Afanasyeva465e972014-03-22 17:21:49 -0700133Block::Block(const uint8_t* buffer, size_t maxlength)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800134{
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700135 const uint8_t* tmp_begin = buffer;
136 const uint8_t* tmp_end = buffer + maxlength;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700137
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800138 m_type = Tlv::readType(tmp_begin, tmp_end);
139 uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700140
Alexander Afanasyev9c7ed112014-02-07 12:23:03 -0800141 if (length > static_cast<uint64_t>(tmp_end - tmp_begin))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800142 {
143 throw Tlv::Error("Not enough data in the buffer to fully parse TLV");
144 }
145
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700146 m_buffer = ptr_lib::make_shared<Buffer>(buffer, (tmp_begin - buffer) + length);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800147
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 Afanasyeva465e972014-03-22 17:21:49 -0700156Block::Block(const void* bufferX, size_t maxlength)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800157{
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700158 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(bufferX);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700159
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700160 const uint8_t* tmp_begin = buffer;
161 const uint8_t* tmp_end = buffer + maxlength;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700162
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800163 m_type = Tlv::readType(tmp_begin, tmp_end);
164 uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700165
Alexander Afanasyev9c7ed112014-02-07 12:23:03 -0800166 if (length > static_cast<uint64_t>(tmp_end - tmp_begin))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800167 {
168 throw Tlv::Error("Not enough data in the buffer to fully parse TLV");
169 }
170
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700171 m_buffer = ptr_lib::make_shared<Buffer>(buffer, (tmp_begin - buffer) + length);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800172
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
181Block::Block(uint32_t type)
182 : m_type(type)
183{
184}
185
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700186Block::Block(uint32_t type, const ConstBufferPtr& value)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800187 : 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 Afanasyeva465e972014-03-22 17:21:49 -0700197Block::Block(uint32_t type, const Block& value)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800198 : m_buffer(value.m_buffer)
199 , m_type(type)
200 , m_begin(m_buffer->end())
201 , m_end(m_buffer->end())
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800202 , m_value_begin(value.begin())
203 , m_value_end(value.end())
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800204{
205 m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size();
206}
207
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700208bool
209Block::fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block)
210{
211 Buffer::const_iterator tempBegin = wire->begin() + offset;
212
213 uint32_t type;
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700214 bool isOk = Tlv::readType(tempBegin, wire->end(), type);
215 if (!isOk)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700216 return false;
217
218 uint64_t length;
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700219 isOk = Tlv::readVarNumber(tempBegin, wire->end(), length);
220 if (!isOk)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700221 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
235bool
236Block::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 Afanasyeva465e972014-03-22 17:21:49 -0700242 bool isOk = Tlv::readType(tempBegin, tempEnd, type);
243 if (!isOk)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700244 return false;
245
246 uint64_t length;
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700247 isOk = Tlv::readVarNumber(tempBegin, tempEnd, length);
248 if (!isOk)
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700249 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 Afanasyev13bb51a2014-01-02 19:13:26 -0800264void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800265Block::parse() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800266{
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700267 if (!m_subBlocks.empty() || value_size() == 0)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800268 return;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700269
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700270 Buffer::const_iterator begin = value_begin();
271 Buffer::const_iterator end = value_end();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800272
273 while (begin != end)
274 {
275 Buffer::const_iterator element_begin = begin;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700276
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800277 uint32_t type = Tlv::readType(begin, end);
278 uint64_t length = Tlv::readVarNumber(begin, end);
279
Alexander Afanasyev9c7ed112014-02-07 12:23:03 -0800280 if (length > static_cast<uint64_t>(end - begin))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800281 {
282 m_subBlocks.clear();
283 throw Tlv::Error("TLV length exceeds buffer length");
284 }
285 Buffer::const_iterator element_end = begin + length;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700286
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800287 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
297void
298Block::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 Afanasyev8ea763d2014-02-06 20:32:52 -0800311 else if (m_subBlocks.size() == 0)
312 {
313 Tlv::writeVarNumber(os, 0);
314 }
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800315 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 Afanasyev937aa782014-03-21 13:17:57 -0700321
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800322 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 Afanasyev937aa782014-03-21 13:17:57 -0700346
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800347 Tlv::readType(m_value_begin, m_value_end);
348 Tlv::readVarNumber(m_value_begin, m_value_end);
349}
350
Yingdi Yu4270f202014-01-28 14:19:16 -0800351Block
352Block::blockFromValue() const
353{
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700354 if (value_size() == 0)
Yingdi Yu4270f202014-01-28 14:19:16 -0800355 throw Error("Underlying value buffer is empty");
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700356
Yingdi Yu4270f202014-01-28 14:19:16 -0800357 Buffer::const_iterator begin = value_begin(),
358 end = value_end();
359
360 Buffer::const_iterator element_begin = begin;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700361
Yingdi Yu4270f202014-01-28 14:19:16 -0800362 uint32_t type = Tlv::readType(begin, end);
363 uint64_t length = Tlv::readVarNumber(begin, end);
364
Alexander Afanasyev9c7ed112014-02-07 12:23:03 -0800365 if (length != static_cast<uint64_t>(end - begin))
Yingdi Yu4270f202014-01-28 14:19:16 -0800366 throw Tlv::Error("TLV length mismatches buffer length");
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700367
Yingdi Yu4270f202014-01-28 14:19:16 -0800368 return Block(m_buffer,
369 type,
370 element_begin, end,
371 begin, end);
372}
373
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800374} // namespace ndn