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