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