Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 24 | #ifndef NDN_ENCODING_BLOCK_HPP |
| 25 | #define NDN_ENCODING_BLOCK_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 27 | #include "buffer.hpp" |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 28 | #include "encoding-buffer-fwd.hpp" |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 29 | #include "tlv.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 31 | namespace boost { |
| 32 | namespace asio { |
| 33 | class const_buffer; |
| 34 | } // namespace asio |
| 35 | } // namespace boost |
| 36 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 37 | namespace ndn { |
| 38 | |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 39 | /** @brief Represents a TLV element of NDN packet format |
| 40 | * @sa https://named-data.net/doc/ndn-tlv/tlv.html |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 41 | */ |
| 42 | class Block |
| 43 | { |
| 44 | public: |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 45 | using element_container = std::vector<Block>; |
| 46 | using element_iterator = element_container::iterator; |
| 47 | using element_const_iterator = element_container::const_iterator; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 48 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 49 | class Error : public tlv::Error |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 50 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 51 | public: |
| 52 | explicit |
| 53 | Error(const std::string& what) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 54 | : tlv::Error(what) |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 55 | { |
| 56 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 59 | public: // constructor, creation, assignment |
| 60 | /** @brief Create an empty Block |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 61 | */ |
| 62 | Block(); |
| 63 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 64 | /** @brief Parse Block from an EncodingBuffer |
| 65 | * @param buffer an EncodingBuffer containing one TLV element |
| 66 | * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 67 | */ |
| 68 | explicit |
| 69 | Block(const EncodingBuffer& buffer); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 71 | /** @brief Parse Block from a wire Buffer |
| 72 | * @param buffer a Buffer containing one TLV element |
| 73 | * @note This constructor takes shared ownership of @p buffer. |
| 74 | * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 75 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 76 | explicit |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 77 | Block(const ConstBufferPtr& buffer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 78 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 79 | /** @brief Parse Block within boundaries of a wire Buffer |
| 80 | * @param buffer a Buffer containing an TLV element at [@p begin,@p end) |
| 81 | * @param begin begin position of the TLV element within @p buffer |
| 82 | * @param end end position of the TLV element within @p buffer |
| 83 | * @param verifyLength if true, check TLV-LENGTH equals size of TLV-VALUE |
| 84 | * @throw std::invalid_argument @p buffer is empty, or [@p begin,@p end) range are not within @p buffer |
| 85 | * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE |
| 86 | * @note This overload automatically detects TLV-TYPE and position of TLV-VALUE. |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 87 | */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 88 | Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 89 | bool verifyLength = true); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 91 | /** @brief Parse Block within boundaries of an existing Block, reusing underlying wire Buffer |
| 92 | * @param block a Block whose buffer contains an TLV element at [@p begin,@p end) |
| 93 | * @param begin begin position of the TLV element within @p block |
| 94 | * @param end end position of the TLV element within @p block |
| 95 | * @param verifyLength if true, check TLV-LENGTH equals size of TLV-VALUE |
| 96 | * @throw std::invalid_argument [@p begin,@p end) range are not within @p block |
| 97 | * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 98 | */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 99 | Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end, |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 100 | bool verifyLength = true); |
| 101 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 102 | /** @brief Create a Block from a wire Buffer without parsing |
| 103 | * @param buffer a Buffer containing an TLV element at [@p begin,@p end) |
| 104 | * @param type TLV-TYPE |
| 105 | * @param begin begin position of the TLV element within @p buffer |
| 106 | * @param end end position of the TLV element within @p buffer |
| 107 | * @param valueBegin begin position of TLV-VALUE within @p buffer |
| 108 | * @param valueEnd end position of TLV-VALUE within @p buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 109 | */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 110 | Block(ConstBufferPtr buffer, uint32_t type, |
| 111 | Buffer::const_iterator begin, Buffer::const_iterator end, |
| 112 | Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 113 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 114 | /** @brief Parse Block from a raw buffer |
| 115 | * @param buf pointer to the first octet of an TLV element |
| 116 | * @param bufSize size of the raw buffer; may be more than size of the TLV element |
| 117 | * @throw tlv::Error Type-Length parsing fails, or size of TLV-VALUE exceeds @p bufSize |
| 118 | * @note This overload copies the TLV element into an internal wire buffer. |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 119 | */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 120 | Block(const uint8_t* buf, size_t bufSize); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 121 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 122 | /** @brief Create an empty Block with specified TLV-TYPE |
| 123 | * @param type TLV-TYPE |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 124 | */ |
Alexander Afanasyev | f42ce13 | 2014-01-07 13:32:30 -0800 | [diff] [blame] | 125 | explicit |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 126 | Block(uint32_t type); |
| 127 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 128 | /** @brief Create a Block with specified TLV-TYPE and TLV-VALUE |
| 129 | * @param type TLV-TYPE |
| 130 | * @param value a Buffer containing the TLV-VALUE |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 131 | */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 132 | Block(uint32_t type, ConstBufferPtr value); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 133 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 134 | /** @brief Create a Block with specified TLV-TYPE and TLV-VALUE |
| 135 | * @param type TLV-TYPE |
| 136 | * @param value a Block to be nested as TLV-VALUE |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 137 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 138 | Block(uint32_t type, const Block& value); |
| 139 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 140 | /** @brief Parse Block from an input stream |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 141 | * @throw tlv::Error TLV-LENGTH is zero or exceeds upper bound |
| 142 | * @warning If decoding fails, bytes are still consumed from the input stream. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 143 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 144 | static Block |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 145 | fromStream(std::istream& is); |
| 146 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 147 | /** @brief Try to parse Block from a wire buffer |
| 148 | * @param buffer a Buffer containing an TLV element at offset @p offset |
| 149 | * @param offset begin position of the TLV element within @p buffer |
| 150 | * @note This function does not throw exceptions upon decoding failure. |
| 151 | * @return true and the Block if parsing succeeds; otherwise false |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 152 | */ |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 153 | static std::tuple<bool, Block> |
| 154 | fromBuffer(ConstBufferPtr buffer, size_t offset); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 155 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 156 | /** @brief Try to parse Block from a raw buffer |
| 157 | * @param buf pointer to the first octet of an TLV element |
| 158 | * @param bufSize size of the raw buffer; may be more than size of the TLV element |
| 159 | * @note This function does not throw exceptions upon decoding failure. |
| 160 | * @note This overload copies the TLV element into an internal wire buffer. |
| 161 | * @return true and the Block if parsing succeeds; otherwise false |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 162 | */ |
| 163 | static std::tuple<bool, Block> |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 164 | fromBuffer(const uint8_t* buf, size_t bufSize); |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 165 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 166 | public: // wire format |
| 167 | /** @brief Check if the Block is empty |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 168 | * |
| 169 | * A Block is "empty" only if it is default-constructed. A Block with zero-length TLV-VALUE is |
| 170 | * not considered empty. |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 171 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 172 | bool |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 173 | empty() const |
| 174 | { |
| 175 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 176 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 178 | /** @brief Check if the Block has fully encoded wire |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 179 | * |
| 180 | * A Block has fully encoded wire if the underlying buffer exists and contains full |
| 181 | * Type-Length-Value instead of just TLV-VALUE field. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 182 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 183 | bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 184 | hasWire() const; |
| 185 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 186 | /** @brief Reset wire buffer of the element |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 187 | * @post empty() == true |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 188 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 189 | void |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 190 | reset(); |
| 191 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 192 | /** @brief Reset wire buffer but keep TLV-TYPE and sub elements (if any) |
| 193 | * @post hasWire() == false |
| 194 | * @post hasValue() == false |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 195 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 196 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 197 | resetWire(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 198 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 199 | /** @brief Get begin iterator of encoded wire |
| 200 | * @pre hasWire() == true |
| 201 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 202 | Buffer::const_iterator |
| 203 | begin() const; |
| 204 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 205 | /** @brief Get end iterator of encoded wire |
| 206 | * @pre hasWire() == true |
| 207 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 208 | Buffer::const_iterator |
| 209 | end() const; |
| 210 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 211 | /** @brief Get pointer to encoded wire |
| 212 | * @pre hasWire() == true |
| 213 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 214 | const uint8_t* |
| 215 | wire() const; |
| 216 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 217 | /** @brief Get size of encoded wire, including Type-Length-Value |
| 218 | * @pre empty() == false |
| 219 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 220 | size_t |
| 221 | size() const; |
| 222 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 223 | /** @brief Get underlying buffer |
| 224 | */ |
| 225 | shared_ptr<const Buffer> |
| 226 | getBuffer() const |
| 227 | { |
| 228 | return m_buffer; |
| 229 | } |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 230 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 231 | public: // type and value |
| 232 | /** @brief Get TLV-TYPE |
| 233 | */ |
| 234 | uint32_t |
| 235 | type() const |
| 236 | { |
| 237 | return m_type; |
| 238 | } |
| 239 | |
| 240 | /** @brief Get begin iterator of TLV-VALUE |
| 241 | * |
| 242 | * This property reflects whether the underlying buffer contains TLV-VALUE. If this is false, |
| 243 | * TLV-VALUE has zero-length. If this is true, TLV-VALUE may be zero-length. |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 244 | */ |
| 245 | bool |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 246 | hasValue() const |
| 247 | { |
| 248 | return m_buffer != nullptr; |
| 249 | } |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 250 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 251 | /** @brief Get begin iterator of TLV-VALUE |
| 252 | * @pre hasValue() == true |
| 253 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 254 | Buffer::const_iterator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 255 | value_begin() const |
| 256 | { |
| 257 | return m_valueBegin; |
| 258 | } |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 259 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 260 | /** @brief Get end iterator of TLV-VALUE |
| 261 | * @pre hasValue() == true |
| 262 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 263 | Buffer::const_iterator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 264 | value_end() const |
| 265 | { |
| 266 | return m_valueEnd; |
| 267 | } |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 268 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 269 | /** @brief Get pointer to TLV-VALUE |
| 270 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 271 | const uint8_t* |
| 272 | value() const; |
| 273 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 274 | /** @brief Get size of TLV-VALUE aka TLV-LENGTH |
| 275 | */ |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 276 | size_t |
| 277 | value_size() const; |
| 278 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 279 | Block |
| 280 | blockFromValue() const; |
| 281 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 282 | public: // sub elements |
| 283 | /** @brief Parse TLV-VALUE into sub elements |
| 284 | * @post elements() reflects sub elements found in TLV-VALUE |
| 285 | * @throw tlv::Error TLV-VALUE is not a sequence of TLV elements |
| 286 | * @note This method does not perform recursive parsing. |
| 287 | * @note This method has no effect if elements() is already populated. |
| 288 | * @note This method is not really const, but it does not modify any data. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 289 | */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 290 | void |
| 291 | parse() const; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 292 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 293 | /** @brief Encode sub elements into TLV-VALUE |
| 294 | * @post TLV-VALUE contains sub elements from elements() |
| 295 | */ |
| 296 | void |
| 297 | encode(); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 298 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 299 | /** @brief Get the first sub element of specified TLV-TYPE |
| 300 | * @pre parse() has been executed |
| 301 | * @throw Error sub element of @p type does not exist |
| 302 | */ |
| 303 | const Block& |
| 304 | get(uint32_t type) const; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 305 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 306 | /** @brief Find the first sub element of specified TLV-TYPE |
| 307 | * @pre parse() has been executed |
| 308 | * @return iterator in elements() to the found sub element, otherwise elements_end() |
| 309 | */ |
| 310 | element_const_iterator |
| 311 | find(uint32_t type) const; |
| 312 | |
| 313 | /** @brief Remove all sub elements of specified TLV-TYPE |
| 314 | * @pre parse() has been executed |
| 315 | * @post find(type) == elements_end() |
| 316 | */ |
| 317 | void |
| 318 | remove(uint32_t type); |
| 319 | |
| 320 | /** @brief Erase a sub element |
| 321 | */ |
| 322 | element_iterator |
| 323 | erase(element_const_iterator position); |
| 324 | |
| 325 | /** @brief Erase a range of sub elements |
| 326 | */ |
| 327 | element_iterator |
| 328 | erase(element_const_iterator first, element_const_iterator last); |
| 329 | |
| 330 | /** @brief Append a sub element |
| 331 | */ |
| 332 | void |
| 333 | push_back(const Block& element); |
| 334 | |
| 335 | /** @brief Insert a sub element |
| 336 | * @param pos position of new sub element |
| 337 | * @param element new sub element |
| 338 | * @return iterator in elements() to the new sub element |
| 339 | */ |
| 340 | element_iterator |
| 341 | insert(element_const_iterator pos, const Block& element); |
| 342 | |
| 343 | /** @brief Get container of sub elements |
| 344 | * @pre parse() has been executed |
| 345 | */ |
| 346 | const element_container& |
| 347 | elements() const |
| 348 | { |
| 349 | return m_elements; |
| 350 | } |
| 351 | |
| 352 | /** @brief Equivalent to elements().begin() |
| 353 | */ |
| 354 | element_const_iterator |
| 355 | elements_begin() const |
| 356 | { |
| 357 | return m_elements.begin(); |
| 358 | } |
| 359 | |
| 360 | /** @brief Equivalent to elements().end() |
| 361 | */ |
| 362 | element_const_iterator |
| 363 | elements_end() const |
| 364 | { |
| 365 | return m_elements.end(); |
| 366 | } |
| 367 | |
| 368 | /** @brief Equivalent to elements().size() |
| 369 | */ |
| 370 | size_t |
| 371 | elements_size() const |
| 372 | { |
| 373 | return m_elements.size(); |
| 374 | } |
| 375 | |
| 376 | public: // misc |
| 377 | /** @brief Implicit conversion to const_buffer |
| 378 | */ |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 379 | operator boost::asio::const_buffer() const; |
| 380 | |
Junxiao Shi | d2777fa | 2017-07-27 18:35:34 +0000 | [diff] [blame] | 381 | private: |
| 382 | /** @brief Estimate Block size as if sub elements are encoded into TLV-VALUE |
| 383 | */ |
| 384 | size_t |
| 385 | encode(EncodingEstimator& estimator) const; |
| 386 | |
| 387 | /** @brief Encode sub elements into TLV-VALUE and prepend Block to encoder |
| 388 | * @post TLV-VALUE contains sub elements from elements() |
| 389 | * @post internal buffer and iterators point to Encoder's buffer |
| 390 | */ |
| 391 | size_t |
| 392 | encode(EncodingBuffer& encoder); |
| 393 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 394 | protected: |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 395 | /** @brief underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields |
| 396 | * |
| 397 | * If m_buffer is nullptr, this is an empty Block with TLV-TYPE given in m_type. |
| 398 | * Otherwise, |
| 399 | * - [m_valueBegin, m_valueEnd) point to TLV-VALUE within m_buffer. |
| 400 | * - If m_begin != m_end, [m_begin,m_end) point to Type-Length-Value of this Block within m_buffer. |
| 401 | * Otherwise, m_buffer does not contain TLV-TYPE and TLV-LENGTH fields. |
| 402 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 403 | shared_ptr<const Buffer> m_buffer; |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 404 | Buffer::const_iterator m_begin; ///< @sa m_buffer |
| 405 | Buffer::const_iterator m_end; ///< @sa m_buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 406 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 407 | Buffer::const_iterator m_valueBegin; ///< @sa m_buffer |
| 408 | Buffer::const_iterator m_valueEnd; ///< @sa m_buffer |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 409 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 410 | uint32_t m_type; ///< TLV-TYPE |
| 411 | |
| 412 | /** @brief total size including Type-Length-Value |
| 413 | * |
| 414 | * This field is valid only if empty() is false. |
| 415 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 416 | uint32_t m_size; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 417 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 418 | /** @brief sub elements |
| 419 | * |
| 420 | * This field is valid only if parse() has been executed. |
| 421 | */ |
| 422 | mutable element_container m_elements; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 423 | }; |
| 424 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 425 | /** @brief Compare whether two Blocks have same TLV-TYPE, TLV-LENGTH, and TLV-VALUE |
| 426 | */ |
| 427 | bool |
| 428 | operator==(const Block& lhs, const Block& rhs); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 429 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 430 | inline bool |
| 431 | operator!=(const Block& lhs, const Block& rhs) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 432 | { |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 433 | return !(lhs == rhs); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 436 | } // namespace ndn |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 437 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 438 | #endif // NDN_ENCODING_BLOCK_HPP |