blob: 7ade6e30de5dee75c4c75975066ee3485dedd902 [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/**
Junxiao Shi19d7a572016-07-26 12:55:09 +00003 * Copyright (c) 2013-2016 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
Alexander Afanasyev4448d292015-08-09 20:11:37 -070084 /** @brief Create a Block from existing block (reusing the underlying buffer), directly
85 * specifying boundaries of the block within the buffer
86 *
87 * This overload will automatically detect type and position of the value within the block
88 */
89 Block(const Block& block,
90 const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
91 bool verifyLength = true);
92
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070093 /** @brief Create a Block from the raw buffer with Type-Length parsing
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080094 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -070095 Block(const uint8_t* buffer, size_t maxlength);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080096
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070097 /** @brief Create a Block from the raw buffer with Type-Length parsing
98 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -070099 Block(const void* buffer, size_t maxlength);
Yingdi Yu27158392014-01-20 13:04:20 -0800100
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700101 /** @brief Create a Block from the wire buffer (no parsing)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800102 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700103 * This overload does not do any parsing
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800104 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700105 Block(const ConstBufferPtr& wire,
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800106 uint32_t type,
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700107 const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
108 const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800109
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700110 /** @brief Create Block of a specific type with empty wire buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800111 */
Alexander Afanasyevf42ce132014-01-07 13:32:30 -0800112 explicit
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800113 Block(uint32_t type);
114
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700115 /** @brief Create a Block of a specific type with the specified value
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800116 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700117 * The underlying buffer holds only value Additional operations are needed
118 * to construct wire encoding, one need to prepend the wire buffer with type
119 * and value-length VAR-NUMBERs
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800120 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700121 Block(uint32_t type, const ConstBufferPtr& value);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800122
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700123 /** @brief Create a nested Block of a specific type with the specified value
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800124 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700125 * The underlying buffer holds only value. Additional operations are needed
126 * to construct wire encoding, one need to prepend the wire buffer with type
127 * and value-length VAR-NUMBERs
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800128 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700129 Block(uint32_t type, const Block& value);
130
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700131 /** @brief Create a Block from an input stream
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700132 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700133 static Block
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700134 fromStream(std::istream& is);
135
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700136 /** @brief Try to construct block from Buffer
137 * @param buffer the buffer to construct block from
138 * @note buffer is passed by value because the constructed block
139 * takes shared ownership of the buffer
140 * @param offset offset from beginning of \p buffer to construct Block from
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700141 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700142 * This method does not throw upon decoding error.
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700143 * This method does not copy the bytes.
144 *
145 * @return true and the Block, if Block is successfully created; otherwise false
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700146 */
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700147 static std::tuple<bool, Block>
148 fromBuffer(ConstBufferPtr buffer, size_t offset);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700149
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700150 /** @brief Try to construct block from raw buffer
151 * @param buffer the raw buffer to copy bytes from
152 * @param maxSize the maximum size of constructed block;
153 * @p buffer must have a size of at least @p maxSize
154 *
155 * This method does not throw upon decoding error.
156 * This method copies the bytes into a new Buffer.
157 *
158 * @return true and the Block, if Block is successfully created; otherwise false
159 */
160 static std::tuple<bool, Block>
161 fromBuffer(const uint8_t* buffer, size_t maxSize);
162
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700163public: // wire format
164 /** @brief Check if the Block is empty
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800165 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700166 bool
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800167 empty() const;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700168
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700169 /** @brief Check if the Block has fully encoded wire
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800170 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700171 bool
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800172 hasWire() const;
173
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700174 /** @brief Reset wire buffer of the element
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800175 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700176 void
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800177 reset();
178
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700179 /** @brief Reset wire buffer but keep sub elements (if any)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800180 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700181 void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800182 resetWire();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800183
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700184 Buffer::const_iterator
185 begin() const;
186
187 Buffer::const_iterator
188 end() const;
189
190 const uint8_t*
191 wire() const;
192
193 size_t
194 size() const;
195
196public: // type and value
197 uint32_t
198 type() const;
199
200 /** @brief Check if the Block has value block (no type and length are encoded)
201 */
202 bool
203 hasValue() const;
204
205 Buffer::const_iterator
206 value_begin() const;
207
208 Buffer::const_iterator
209 value_end() const;
210
211 const uint8_t*
212 value() const;
213
214 size_t
215 value_size() const;
216
217public: // sub elements
218 /** @brief Parse wire buffer into subblocks
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800219 *
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700220 * This method is not really const, but it does not modify any data. It simply
221 * parses contents of the buffer into subblocks
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800222 */
223 void
224 parse() const;
225
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700226 /** @brief Encode subblocks into wire buffer
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800227 */
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800228 void
229 encode();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700230
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700231 /** @brief Get the first subelement of the requested type
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800232 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700233 const Block&
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800234 get(uint32_t type) const;
235
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700236 element_const_iterator
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800237 find(uint32_t type) const;
238
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700239 /**
240 * @brief remove all subelements of \p type
241 * @param type TLV-TYPE of subelements to remove
242 * @pre parse() has been invoked
243 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700244 void
Alexander Afanasyevf5c35ae2014-01-17 16:06:31 -0800245 remove(uint32_t type);
246
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700247 element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400248 erase(element_const_iterator position);
Alexander Afanasyevf5c35ae2014-01-17 16:06:31 -0800249
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700250 element_iterator
Joao Pereira7476ebf2015-07-07 14:54:39 -0400251 erase(element_const_iterator first, element_const_iterator last);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700252
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700253 void
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700254 push_back(const Block& element);
255
Joao Pereira7476ebf2015-07-07 14:54:39 -0400256 /**
257 * @brief insert Insert a new element in a specific position
258 * @param pos Position to insert the new element
259 * @param element Element to be inserted
260 * @return An iterator that points to the first of the newly inserted elements.
261 */
262 element_iterator
263 insert(element_const_iterator pos, const Block& element);
264
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700265 /** @brief Get all subelements
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800266 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700267 const element_container&
268 elements() const;
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800269
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700270 element_const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800271 elements_begin() const;
272
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700273 element_const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800274 elements_end() const;
275
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700276 size_t
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800277 elements_size() const;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700278
Yingdi Yu4270f202014-01-28 14:19:16 -0800279 Block
280 blockFromValue() const;
281
Alexander Afanasyev74633892015-02-08 18:08:46 -0800282 /**
283 * @brief Get underlying buffer
284 */
285 shared_ptr<const Buffer>
286 getBuffer() const;
287
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700288public: // EqualityComparable concept
289 bool
290 operator==(const Block& other) const;
291
292 bool
293 operator!=(const Block& other) const;
294
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700295public: // ConvertibleToConstBuffer
296 operator boost::asio::const_buffer() const;
297
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800298protected:
Alexander Afanasyev74633892015-02-08 18:08:46 -0800299 shared_ptr<const Buffer> m_buffer;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800300
301 uint32_t m_type;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700302
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800303 Buffer::const_iterator m_begin;
304 Buffer::const_iterator m_end;
305 uint32_t m_size;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700306
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800307 Buffer::const_iterator m_value_begin;
308 Buffer::const_iterator m_value_end;
309
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800310 mutable element_container m_subBlocks;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800311};
312
313////////////////////////////////////////////////////////////////////////////////
314////////////////////////////////////////////////////////////////////////////////
315////////////////////////////////////////////////////////////////////////////////
316
Alexander Afanasyev74633892015-02-08 18:08:46 -0800317inline shared_ptr<const Buffer>
318Block::getBuffer() const
319{
320 return m_buffer;
321}
322
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700323inline uint32_t
324Block::type() const
325{
326 return m_type;
327}
328
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800329inline Buffer::const_iterator
330Block::value_begin() const
331{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800332 return m_value_begin;
333}
334
335inline Buffer::const_iterator
336Block::value_end() const
337{
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800338 return m_value_end;
339}
340
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800341inline const Block::element_container&
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700342Block::elements() const
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800343{
344 return m_subBlocks;
345}
346
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700347} // namespace ndn
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800348
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700349#endif // NDN_ENCODING_BLOCK_HPP