Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 6617e49 | 2016-01-19 07:33:00 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_ENCODING_ENCODER_HPP |
| 23 | #define NDN_ENCODING_ENCODER_HPP |
| 24 | |
| 25 | #include "../common.hpp" |
| 26 | #include "block.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace encoding { |
| 30 | |
| 31 | /** |
| 32 | * @brief Helper class to perform TLV encoding |
| 33 | * Interface of this class (mostly) matches interface of Estimator class |
| 34 | * @sa Estimator |
| 35 | */ |
| 36 | class Encoder |
| 37 | { |
| 38 | public: // common interface between Encoder and Estimator |
| 39 | /** |
| 40 | * @brief Create instance of the encoder with the specified reserved sizes |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 41 | * @param totalReserve initial buffer size to reserve |
| 42 | * @param reserveFromBack number of bytes to reserve for append* operations |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 43 | */ |
| 44 | explicit |
susmit | 8b15655 | 2016-01-12 13:10:55 -0700 | [diff] [blame] | 45 | Encoder(size_t totalReserve = MAX_NDN_PACKET_SIZE, size_t reserveFromBack = 400); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 46 | |
| 47 | Encoder(const Encoder&) = delete; |
| 48 | |
| 49 | Encoder& |
| 50 | operator=(const Encoder&) = delete; |
| 51 | |
| 52 | /** |
| 53 | * @brief Prepend a byte |
| 54 | */ |
| 55 | size_t |
| 56 | prependByte(uint8_t value); |
| 57 | |
| 58 | /** |
| 59 | * @brief Append a byte |
| 60 | */ |
| 61 | size_t |
| 62 | appendByte(uint8_t value); |
| 63 | |
| 64 | /** |
| 65 | * @brief Prepend a byte array @p array of length @p length |
| 66 | */ |
| 67 | size_t |
| 68 | prependByteArray(const uint8_t* array, size_t length); |
| 69 | |
| 70 | /** |
| 71 | * @brief Append a byte array @p array of length @p length |
| 72 | */ |
| 73 | size_t |
| 74 | appendByteArray(const uint8_t* array, size_t length); |
| 75 | |
| 76 | /** |
| 77 | * @brief Prepend range of bytes from the range [@p first, @p last) |
| 78 | */ |
| 79 | template<class Iterator> |
| 80 | size_t |
| 81 | prependRange(Iterator first, Iterator last); |
| 82 | |
| 83 | /** |
| 84 | * @brief Append range of bytes from the range [@p first, @p last) |
| 85 | */ |
| 86 | template<class Iterator> |
| 87 | size_t |
| 88 | appendRange(Iterator first, Iterator last); |
| 89 | |
| 90 | /** |
| 91 | * @brief Prepend VarNumber @p varNumber of NDN TLV encoding |
| 92 | * @sa http://named-data.net/doc/ndn-tlv/ |
| 93 | */ |
| 94 | size_t |
| 95 | prependVarNumber(uint64_t varNumber); |
| 96 | |
| 97 | /** |
| 98 | * @brief Prepend VarNumber @p varNumber of NDN TLV encoding |
| 99 | * @sa http://named-data.net/doc/ndn-tlv/ |
| 100 | */ |
| 101 | size_t |
| 102 | appendVarNumber(uint64_t varNumber); |
| 103 | |
| 104 | /** |
| 105 | * @brief Prepend non-negative integer @p integer of NDN TLV encoding |
| 106 | * @sa http://named-data.net/doc/ndn-tlv/ |
| 107 | */ |
| 108 | size_t |
| 109 | prependNonNegativeInteger(uint64_t integer); |
| 110 | |
| 111 | /** |
| 112 | * @brief Append non-negative integer @p integer of NDN TLV encoding |
| 113 | * @sa http://named-data.net/doc/ndn-tlv/ |
| 114 | */ |
| 115 | size_t |
| 116 | appendNonNegativeInteger(uint64_t integer); |
| 117 | |
| 118 | /** |
| 119 | * @brief Prepend TLV block of type @p type and value from buffer @p array of size @p arraySize |
| 120 | */ |
| 121 | size_t |
| 122 | prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize); |
| 123 | |
| 124 | /** |
| 125 | * @brief Append TLV block of type @p type and value from buffer @p array of size @p arraySize |
| 126 | */ |
| 127 | size_t |
| 128 | appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize); |
| 129 | |
| 130 | /** |
| 131 | * @brief Prepend TLV block @p block |
| 132 | */ |
| 133 | size_t |
| 134 | prependBlock(const Block& block); |
| 135 | |
| 136 | /** |
| 137 | * @brief Append TLV block @p block |
| 138 | */ |
| 139 | size_t |
| 140 | appendBlock(const Block& block); |
| 141 | |
| 142 | public: // unique interface to the Encoder |
| 143 | typedef Buffer::value_type value_type; |
| 144 | typedef Buffer::iterator iterator; |
| 145 | typedef Buffer::const_iterator const_iterator; |
| 146 | |
| 147 | /** |
| 148 | * @brief Create EncodingBlock from existing block |
| 149 | * |
| 150 | * This is a dangerous constructor and should be used with caution. |
| 151 | * It will modify contents of the buffer that is used by block and may |
| 152 | * impact data in other blocks. |
| 153 | * |
| 154 | * The primary purpose for this method is to be used to extend Block |
| 155 | * after sign operation. |
| 156 | */ |
| 157 | explicit |
| 158 | Encoder(const Block& block); |
| 159 | |
| 160 | /** |
| 161 | * @brief Reserve @p size bytes for the underlying buffer |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 162 | * @param size amount of bytes to reserve in the underlying buffer |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 163 | * @param addInFront if true, then @p size bytes will be available in front (i.e., subsequent call |
| 164 | * to prepend* will not need to allocate memory). If false, then reservation will be done |
| 165 | * at the end of the buffer (i.d., for subsequent append* calls) |
| 166 | * @note Reserve size is exact, unlike reserveFront and reserveBack methods |
| 167 | * @sa reserveFront, reserveBack |
| 168 | */ |
| 169 | void |
| 170 | reserve(size_t size, bool addInFront); |
| 171 | |
| 172 | /** |
| 173 | * @brief Reserve at least @p size bytes at the back of the underlying buffer |
| 174 | * @note the actual reserve size can be (and in most cases is) larger than specified reservation |
| 175 | * length |
| 176 | */ |
| 177 | void |
| 178 | reserveBack(size_t size); |
| 179 | |
| 180 | /** |
| 181 | * @brief Reserve at least @p isze bytes at the beginning of the underlying buffer |
| 182 | * @note the actual reserve size can be (and in most cases is) larger than specified reservation |
| 183 | * length |
| 184 | */ |
| 185 | void |
| 186 | reserveFront(size_t size); |
| 187 | |
| 188 | /** |
| 189 | * @brief Get size of the underlying buffer |
| 190 | */ |
| 191 | size_t |
| 192 | capacity() const; |
| 193 | |
| 194 | /** |
| 195 | * @brief Get underlying buffer |
| 196 | */ |
| 197 | shared_ptr<Buffer> |
| 198 | getBuffer(); |
| 199 | |
| 200 | public: // accessors |
| 201 | |
| 202 | /** |
| 203 | * @brief Get an iterator pointing to the first byte of the encoded buffer |
| 204 | */ |
| 205 | iterator |
| 206 | begin(); |
| 207 | |
| 208 | /** |
| 209 | * @brief Get an iterator pointing to the past-the-end byte of the encoded buffer |
| 210 | */ |
| 211 | iterator |
| 212 | end(); |
| 213 | |
| 214 | /** |
| 215 | * @brief Get an iterator pointing to the first byte of the encoded buffer |
| 216 | */ |
| 217 | const_iterator |
| 218 | begin() const; |
| 219 | |
| 220 | const_iterator |
| 221 | end() const; |
| 222 | |
| 223 | /** |
| 224 | * @brief Get a pointer to the first byte of the encoded buffer |
| 225 | */ |
| 226 | uint8_t* |
| 227 | buf(); |
| 228 | |
| 229 | /** |
| 230 | * @brief Get a pointer to the first byte of the encoded buffer |
| 231 | */ |
| 232 | const uint8_t* |
| 233 | buf() const; |
| 234 | |
| 235 | /** |
| 236 | * @brief Get size of the encoded buffer |
| 237 | */ |
| 238 | size_t |
| 239 | size() const; |
| 240 | |
| 241 | /** |
| 242 | * @brief Create Block from the underlying buffer |
| 243 | * |
| 244 | * @param verifyLength If this parameter set to true, Block's constructor |
| 245 | * will be requested to verify consistency of the encoded |
| 246 | * length in the Block, otherwise ignored |
| 247 | */ |
| 248 | Block |
| 249 | block(bool verifyLength = true) const; |
| 250 | |
| 251 | private: |
| 252 | shared_ptr<Buffer> m_buffer; |
| 253 | |
| 254 | // invariant: m_begin always points to the position of last-written byte (if prepending data) |
| 255 | iterator m_begin; |
| 256 | // invariant: m_end always points to the position of next unwritten byte (if appending data) |
| 257 | iterator m_end; |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | inline size_t |
| 262 | Encoder::size() const |
| 263 | { |
| 264 | return m_end - m_begin; |
| 265 | } |
| 266 | |
| 267 | inline shared_ptr<Buffer> |
| 268 | Encoder::getBuffer() |
| 269 | { |
| 270 | return m_buffer; |
| 271 | } |
| 272 | |
| 273 | inline size_t |
| 274 | Encoder::capacity() const |
| 275 | { |
| 276 | return m_buffer->size(); |
| 277 | } |
| 278 | |
| 279 | inline Buffer::iterator |
| 280 | Encoder::begin() |
| 281 | { |
| 282 | return m_begin; |
| 283 | } |
| 284 | |
| 285 | inline Buffer::iterator |
| 286 | Encoder::end() |
| 287 | { |
| 288 | return m_end; |
| 289 | } |
| 290 | |
| 291 | inline Buffer::const_iterator |
| 292 | Encoder::begin() const |
| 293 | { |
| 294 | return m_begin; |
| 295 | } |
| 296 | |
| 297 | inline Buffer::const_iterator |
| 298 | Encoder::end() const |
| 299 | { |
| 300 | return m_end; |
| 301 | } |
| 302 | |
| 303 | inline uint8_t* |
| 304 | Encoder::buf() |
| 305 | { |
| 306 | return &(*m_begin); |
| 307 | } |
| 308 | |
| 309 | inline const uint8_t* |
| 310 | Encoder::buf() const |
| 311 | { |
| 312 | return &(*m_begin); |
| 313 | } |
| 314 | |
| 315 | template<class Iterator> |
| 316 | inline size_t |
| 317 | Encoder::prependRange(Iterator first, Iterator last) |
| 318 | { |
| 319 | size_t length = std::distance(first, last); |
| 320 | reserveFront(length); |
| 321 | |
| 322 | m_begin -= length; |
| 323 | std::copy(first, last, m_begin); |
| 324 | return length; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | template<class Iterator> |
| 329 | inline size_t |
| 330 | Encoder::appendRange(Iterator first, Iterator last) |
| 331 | { |
| 332 | size_t length = std::distance(first, last); |
| 333 | reserveBack(length); |
| 334 | |
| 335 | std::copy(first, last, m_end); |
| 336 | m_end += length; |
| 337 | return length; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | } // namespace encoding |
| 342 | } // namespace ndn |
| 343 | |
| 344 | #endif // NDN_ENCODING_ENCODER_HPP |