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