blob: 4bbfe52ff63644e167b3112f06972096ffab67ca [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080022 */
23
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070024#ifndef NDN_ENCODING_BLOCK_HPP
25#define NDN_ENCODING_BLOCK_HPP
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080026
Alexander Afanasyev19508852014-01-29 01:01:51 -080027#include "../common.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080028
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080029#include "buffer.hpp"
30#include "tlv.hpp"
Alexander Afanasyev74633892015-02-08 18:08:46 -080031#include "encoding-buffer-fwd.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080032
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070033namespace boost {
34namespace asio {
35class const_buffer;
36} // namespace asio
37} // namespace boost
38
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080039namespace ndn {
40
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070041/** @brief Class representing a wire element of NDN-TLV packet format
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080042 */
43class Block
44{
45public:
Alexander Afanasyev8ea763d2014-02-06 20:32:52 -080046 typedef std::vector<Block> element_container;
47 typedef element_container::iterator element_iterator;
48 typedef element_container::const_iterator element_const_iterator;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080049
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060050 class Error : public tlv::Error
Alexander Afanasyev937aa782014-03-21 13:17:57 -070051 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -070052 public:
53 explicit
54 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060055 : tlv::Error(what)
Alexander Afanasyeva465e972014-03-22 17:21:49 -070056 {
57 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -070058 };
59
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070060public: // constructor, creation, assignment
61 /** @brief Create an empty Block
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080062 */
63 Block();
64
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070065 /** @brief Create a Block based on EncodingBuffer object
Alexander Afanasyev15151312014-02-16 00:53:51 -080066 */
67 explicit
68 Block(const EncodingBuffer& buffer);
Alexander Afanasyev937aa782014-03-21 13:17:57 -070069
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070070 /** @brief Create a Block from the raw buffer with Type-Length parsing
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080071 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070072 explicit
Alexander Afanasyev937aa782014-03-21 13:17:57 -070073 Block(const ConstBufferPtr& buffer);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080074
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070075 /** @brief Create a Block from a buffer, directly specifying boundaries
76 * of the block within the buffer
Alexander Afanasyev187bc482014-02-06 15:04:04 -080077 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070078 * This overload will automatically detect type and position of the value within the block
Alexander Afanasyev187bc482014-02-06 15:04:04 -080079 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -070080 Block(const ConstBufferPtr& buffer,
81 const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080082 bool verifyLength = true);
Alexander Afanasyev937aa782014-03-21 13:17:57 -070083
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070084 /** @brief Create a Block from the raw buffer with Type-Length parsing
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080085 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -070086 Block(const uint8_t* buffer, size_t maxlength);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080087
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070088 /** @brief Create a Block from the raw buffer with Type-Length parsing
89 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -070090 Block(const void* buffer, size_t maxlength);
Yingdi Yu27158392014-01-20 13:04:20 -080091
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070092 /** @brief Create a Block from the wire buffer (no parsing)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080093 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070094 * This overload does not do any parsing
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080095 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -070096 Block(const ConstBufferPtr& wire,
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080097 uint32_t type,
Alexander Afanasyev937aa782014-03-21 13:17:57 -070098 const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
99 const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800100
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700101 /** @brief Create Block of a specific type with empty wire buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800102 */
Alexander Afanasyevf42ce132014-01-07 13:32:30 -0800103 explicit
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800104 Block(uint32_t type);
105
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700106 /** @brief Create a Block of a specific type with the specified value
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800107 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700108 * The underlying buffer holds only value Additional operations are needed
109 * to construct wire encoding, one need to prepend the wire buffer with type
110 * and value-length VAR-NUMBERs
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800111 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700112 Block(uint32_t type, const ConstBufferPtr& value);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800113
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700114 /** @brief Create a nested Block of a specific type with the specified value
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800115 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700116 * The underlying buffer holds only value. Additional operations are needed
117 * to construct wire encoding, one need to prepend the wire buffer with type
118 * and value-length VAR-NUMBERs
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800119 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700120 Block(uint32_t type, const Block& value);
121
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700122 /** @brief Create a Block from an input stream
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700123 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700124 static Block
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700125 fromStream(std::istream& is);
126
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700127 /** @brief Try to construct block from Buffer, referencing data block pointed by wire
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700128 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700129 * This method does not throw upon decoding error.
130 * @return true if Block successfully created, false if block cannot be created
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700131 */
132 static bool
133 fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block);
134
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700135 /** @brief Try to construct block from Buffer, referencing data block pointed by wire
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700136 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700137 * This method does not throw upon decoding error.
138 * @return true if Block successfully created, false if block cannot be created
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700139 */
140 static bool
141 fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block);
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800142
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700143public: // wire format
144 /** @brief Check if the Block is empty
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800145 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700146 bool
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800147 empty() const;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700148
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700149 /** @brief Check if the Block has fully encoded wire
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800150 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700151 bool
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800152 hasWire() const;
153
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700154 /** @brief Reset wire buffer of the element
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800155 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700156 void
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800157 reset();
158
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700159 /** @brief Reset wire buffer but keep sub elements (if any)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800160 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700161 void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800162 resetWire();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800163
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700164 Buffer::const_iterator
165 begin() const;
166
167 Buffer::const_iterator
168 end() const;
169
170 const uint8_t*
171 wire() const;
172
173 size_t
174 size() const;
175
176public: // type and value
177 uint32_t
178 type() const;
179
180 /** @brief Check if the Block has value block (no type and length are encoded)
181 */
182 bool
183 hasValue() const;
184
185 Buffer::const_iterator
186 value_begin() const;
187
188 Buffer::const_iterator
189 value_end() const;
190
191 const uint8_t*
192 value() const;
193
194 size_t
195 value_size() const;
196
197public: // sub elements
198 /** @brief Parse wire buffer into subblocks
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800199 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700200 * This method is not really const, but it does not modify any data. It simply
201 * parses contents of the buffer into subblocks
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800202 */
203 void
204 parse() const;
205
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700206 /** @brief Encode subblocks into wire buffer
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800207 */
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800208 void
209 encode();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700210
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700211 /** @brief Get the first subelement of the requested type
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800212 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700213 const Block&
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800214 get(uint32_t type) const;
215
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700216 element_const_iterator
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800217 find(uint32_t type) const;
218
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700219 void
Alexander Afanasyevf5c35ae2014-01-17 16:06:31 -0800220 remove(uint32_t type);
221
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700222 element_iterator
Alexander Afanasyevf5c35ae2014-01-17 16:06:31 -0800223 erase(element_iterator position);
224
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700225 element_iterator
Alexander Afanasyevf5c35ae2014-01-17 16:06:31 -0800226 erase(element_iterator first, element_iterator last);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700227
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700228 void
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700229 push_back(const Block& element);
230
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700231 /** @brief Get all subelements
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800232 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700233 const element_container&
234 elements() const;
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800235
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700236 element_const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800237 elements_begin() const;
238
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700239 element_const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800240 elements_end() const;
241
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700242 size_t
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800243 elements_size() const;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700244
Yingdi Yu4270f202014-01-28 14:19:16 -0800245 Block
246 blockFromValue() const;
247
Alexander Afanasyev74633892015-02-08 18:08:46 -0800248 /**
249 * @brief Get underlying buffer
250 */
251 shared_ptr<const Buffer>
252 getBuffer() const;
253
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700254public: // EqualityComparable concept
255 bool
256 operator==(const Block& other) const;
257
258 bool
259 operator!=(const Block& other) const;
260
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700261public: // ConvertibleToConstBuffer
262 operator boost::asio::const_buffer() const;
263
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800264protected:
Alexander Afanasyev74633892015-02-08 18:08:46 -0800265 shared_ptr<const Buffer> m_buffer;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800266
267 uint32_t m_type;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700268
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800269 Buffer::const_iterator m_begin;
270 Buffer::const_iterator m_end;
271 uint32_t m_size;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700272
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800273 Buffer::const_iterator m_value_begin;
274 Buffer::const_iterator m_value_end;
275
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800276 mutable element_container m_subBlocks;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800277};
278
279////////////////////////////////////////////////////////////////////////////////
280////////////////////////////////////////////////////////////////////////////////
281////////////////////////////////////////////////////////////////////////////////
282
Alexander Afanasyev74633892015-02-08 18:08:46 -0800283inline shared_ptr<const Buffer>
284Block::getBuffer() const
285{
286 return m_buffer;
287}
288
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800289inline bool
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800290Block::empty() const
291{
292 return m_type == std::numeric_limits<uint32_t>::max();
293}
294
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800295inline bool
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800296Block::hasWire() const
297{
298 return m_buffer && (m_begin != m_end);
299}
300
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800301inline Buffer::const_iterator
302Block::begin() const
303{
304 if (!hasWire())
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700305 throw Error("Underlying wire buffer is empty");
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800306
307 return m_begin;
308}
309
310inline Buffer::const_iterator
311Block::end() const
312{
313 if (!hasWire())
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700314 throw Error("Underlying wire buffer is empty");
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800315
316 return m_end;
317}
318
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700319inline const uint8_t*
320Block::wire() const
321{
322 if (!hasWire())
323 throw Error("(Block::wire) Underlying wire buffer is empty");
324
325 return &*m_begin;
326}
327
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800328inline size_t
329Block::size() const
330{
331 if (hasWire() || hasValue()) {
332 return m_size;
333 }
334 else
335 throw Error("Block size cannot be determined (undefined block size)");
336}
337
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700338inline uint32_t
339Block::type() const
340{
341 return m_type;
342}
343
344inline bool
345Block::hasValue() const
346{
347 return static_cast<bool>(m_buffer);
348}
349
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800350inline Buffer::const_iterator
351Block::value_begin() const
352{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800353 return m_value_begin;
354}
355
356inline Buffer::const_iterator
357Block::value_end() const
358{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800359 return m_value_end;
360}
361
362inline const uint8_t*
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800363Block::value() const
364{
365 if (!hasValue())
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800366 return 0;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700367
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800368 return &*m_value_begin;
369}
370
371inline size_t
372Block::value_size() const
373{
374 if (!hasValue())
Alexander Afanasyev8ea763d2014-02-06 20:32:52 -0800375 return 0;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800376
377 return m_value_end - m_value_begin;
378}
379
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700380inline Block::element_iterator
381Block::erase(Block::element_iterator position)
382{
383 resetWire();
384 return m_subBlocks.erase(position);
385}
386
387inline Block::element_iterator
388Block::erase(Block::element_iterator first, Block::element_iterator last)
389{
390 resetWire();
391 return m_subBlocks.erase(first, last);
392}
393
394inline void
395Block::push_back(const Block& element)
396{
397 resetWire();
398 m_subBlocks.push_back(element);
399}
400
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800401inline const Block::element_container&
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700402Block::elements() const
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800403{
404 return m_subBlocks;
405}
406
407inline Block::element_const_iterator
408Block::elements_begin() const
409{
410 return m_subBlocks.begin();
411}
412
413inline Block::element_const_iterator
414Block::elements_end() const
415{
416 return m_subBlocks.end();
417}
418
419inline size_t
420Block::elements_size() const
421{
422 return m_subBlocks.size();
423}
424
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700425inline bool
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700426Block::operator!=(const Block& other) const
427{
428 return !this->operator==(other);
429}
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800430
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700431} // namespace ndn
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800432
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700433#endif // NDN_ENCODING_BLOCK_HPP