blob: 3451ffd7605d470e30ef95c2404ba48bb8fae3a2 [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 Afanasyev13bb51a2014-01-02 19:13:26 -080040Block::Block(const ConstBufferPtr &wire,
41 uint32_t type,
Alexander Afanasyev187bc482014-02-06 15:04:04 -080042 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
54Block::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 Afanasyev233750e2014-02-16 00:50:07 -080060 m_value_begin = m_begin;
61 m_value_end = m_end;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080062
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 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 Afanasyev187bc482014-02-06 15:04:04 -080072Block::Block(const ConstBufferPtr &buffer,
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080073 const Buffer::const_iterator &begin, const Buffer::const_iterator &end,
74 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 Afanasyev5964fb72014-02-18 12:42:45 -080084 if (verifyLength)
Alexander Afanasyev187bc482014-02-06 15:04:04 -080085 {
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080086 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 Afanasyev187bc482014-02-06 15:04:04 -080091 }
92}
93
Yingdi Yu27158392014-01-20 13:04:20 -080094Block::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 Afanasyev9c7ed112014-02-07 12:23:03 -0800112 if(length != static_cast<uint64_t>(is.gcount())+1)
Yingdi Yu27158392014-01-20 13:04:20 -0800113 {
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 Afanasyev13bb51a2014-01-02 19:13:26 -0800132Block::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 Afanasyev9c7ed112014-02-07 12:23:03 -0800140 if (length > static_cast<uint64_t>(tmp_end - tmp_begin))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800141 {
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
155Block::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 Afanasyev9c7ed112014-02-07 12:23:03 -0800165 if (length > static_cast<uint64_t>(tmp_end - tmp_begin))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800166 {
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
180Block::Block(uint32_t type)
181 : m_type(type)
182{
183}
184
185Block::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
196Block::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 Afanasyevc8823bc2014-02-09 19:33:33 -0800201 , m_value_begin(value.begin())
202 , m_value_end(value.end())
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800203{
204 m_size = Tlv::sizeOfVarNumber(m_type) + Tlv::sizeOfVarNumber(value_size()) + value_size();
205}
206
207void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800208Block::parse() const
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800209{
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 Afanasyev9c7ed112014-02-07 12:23:03 -0800223 if (length > static_cast<uint64_t>(end - begin))
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800224 {
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
240void
241Block::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 Afanasyev8ea763d2014-02-06 20:32:52 -0800254 else if (m_subBlocks.size() == 0)
255 {
256 Tlv::writeVarNumber(os, 0);
257 }
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800258 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 Yu4270f202014-01-28 14:19:16 -0800294Block
295Block::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 Afanasyev9c7ed112014-02-07 12:23:03 -0800308 if (length != static_cast<uint64_t>(end - begin))
Yingdi Yu4270f202014-01-28 14:19:16 -0800309 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 Afanasyev13bb51a2014-01-02 19:13:26 -0800317} // namespace ndn