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