Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Wentao Shang <wentao@cs.ucla.edu> |
| 8 | */ |
| 9 | |
| 10 | #ifndef NDN_ENCODING_BUFFER_HPP |
| 11 | #define NDN_ENCODING_BUFFER_HPP |
| 12 | |
| 13 | #include "../common.hpp" |
| 14 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 15 | #include "buffer.hpp" |
| 16 | #include "tlv.hpp" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 17 | #include "block.hpp" |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 18 | |
| 19 | #include <boost/lexical_cast.hpp> |
| 20 | |
| 21 | namespace ndn { |
| 22 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 23 | namespace encoding { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 24 | static const bool Buffer = true; |
| 25 | static const bool Estimator = false; |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 26 | } // encoding |
| 27 | |
| 28 | template<bool isRealEncoderNotEstimator> |
| 29 | class EncodingImpl; |
| 30 | |
| 31 | typedef EncodingImpl<encoding::Buffer> EncodingBuffer; |
| 32 | typedef EncodingImpl<encoding::Estimator> EncodingEstimator; |
| 33 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 34 | /** |
| 35 | * @brief Class representing wire element of the NDN packet |
| 36 | */ |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 37 | template<> |
| 38 | class EncodingImpl<encoding::Buffer> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 39 | { |
| 40 | public: |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 41 | /** |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 42 | * @brief Constructor to create a EncodingImpl with specified reserved sizes |
| 43 | * |
| 44 | * The caller should make sure that that reserveFromBack does not exceed totalReserve, |
| 45 | * otherwise behavior is undefined. |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 46 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 47 | EncodingImpl(size_t totalReserve = 8800, |
| 48 | size_t reserveFromBack = 400) |
| 49 | : m_buffer(new Buffer(totalReserve)) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 51 | m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 54 | /** |
| 55 | * @brief Create EncodingBlock from existing block |
| 56 | * |
| 57 | * This is a dangerous constructor and should be used with caution. |
| 58 | * It will modify contents of the buffer that is used by block and may |
| 59 | * impact data in other blocks. |
| 60 | * |
| 61 | * The primary purpose for this method is to be used to extend Block |
| 62 | * after sign operation. |
| 63 | */ |
| 64 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 65 | EncodingImpl(const Block& block) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 66 | : m_buffer(const_pointer_cast<Buffer>(block.m_buffer)) |
| 67 | , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin())) |
| 68 | , m_end(m_buffer->begin() + (block.end() - m_buffer->begin())) |
| 69 | { |
| 70 | } |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 71 | |
| 72 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 73 | size() const; |
| 74 | |
| 75 | inline size_t |
| 76 | capacity() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 77 | |
| 78 | inline uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 79 | buf(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 80 | |
| 81 | inline const uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 82 | buf() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 84 | /** |
| 85 | * @brief Create Block from the underlying EncodingBuffer |
| 86 | * |
| 87 | * @param verifyLength If this parameter set to true, Block's constructor |
| 88 | * will be requested to verify consistency of the encoded |
| 89 | * length in the Block, otherwise ignored |
| 90 | */ |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 91 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 92 | block(bool verifyLength = true) const; |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 93 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 94 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 95 | resize(size_t size, bool addInFront); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 96 | |
| 97 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 98 | begin(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 99 | |
| 100 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 101 | end(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 102 | |
| 103 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 104 | begin() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 105 | |
| 106 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 107 | end() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 108 | |
| 109 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 110 | prependByte(uint8_t value); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 111 | |
| 112 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 113 | prependByteArray(const uint8_t* array, size_t length); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 114 | |
| 115 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 116 | prependNonNegativeInteger(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 117 | |
| 118 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 119 | prependVarNumber(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 120 | |
| 121 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 122 | appendByte(uint8_t value); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 123 | |
| 124 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 125 | appendByteArray(const uint8_t* array, size_t length); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 126 | |
| 127 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 128 | appendNonNegativeInteger(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 129 | |
| 130 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 131 | appendVarNumber(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 133 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 134 | // removeByteFromFront(); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 135 | |
| 136 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 137 | // removeByteFromEnd(); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 138 | |
| 139 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 140 | // removeVarNumberFromFront(uint64_t varNumber); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 141 | |
| 142 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 143 | // removeVarNumberFromBack(uint64_t varNumber); |
| 144 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 145 | private: |
| 146 | BufferPtr m_buffer; |
| 147 | |
| 148 | // invariant: m_begin always points to the position of last-written byte (if prepending data) |
| 149 | Buffer::iterator m_begin; |
| 150 | // invariant: m_end always points to the position of next unwritten byte (if appending data) |
| 151 | Buffer::iterator m_end; |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 152 | |
| 153 | friend class Block; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * @brief Class representing wire element of the NDN packet |
| 159 | */ |
| 160 | template<> |
| 161 | class EncodingImpl<encoding::Estimator> |
| 162 | { |
| 163 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 164 | EncodingImpl(size_t totalReserve = 8800, |
| 165 | size_t reserveFromBack = 400) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 166 | { |
| 167 | } |
| 168 | |
| 169 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 170 | prependByte(uint8_t value); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 171 | |
| 172 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 173 | prependByteArray(const uint8_t* array, size_t length); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 174 | |
| 175 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 176 | prependNonNegativeInteger(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 177 | |
| 178 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 179 | prependVarNumber(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 180 | |
| 181 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 182 | appendByte(uint8_t value); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 183 | |
| 184 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 185 | appendByteArray(const uint8_t* array, size_t length); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 186 | |
| 187 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 188 | appendNonNegativeInteger(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 189 | |
| 190 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 191 | appendVarNumber(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 195 | //////////////////////////////////////////////////////////////////////////////// |
| 196 | //////////////////////////////////////////////////////////////////////////////// |
| 197 | //////////////////////////////////////////////////////////////////////////////// |
| 198 | |
| 199 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 200 | EncodingImpl<encoding::Buffer>::size() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 201 | { |
| 202 | return m_end - m_begin; |
| 203 | } |
| 204 | |
| 205 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 206 | EncodingImpl<encoding::Buffer>::capacity() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 207 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 208 | return m_buffer->size(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | inline uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 212 | EncodingImpl<encoding::Buffer>::buf() |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 213 | { |
| 214 | return &(*m_begin); |
| 215 | } |
| 216 | |
| 217 | inline const uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 218 | EncodingImpl<encoding::Buffer>::buf() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 219 | { |
| 220 | return &(*m_begin); |
| 221 | } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 222 | |
| 223 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 224 | EncodingImpl<encoding::Buffer>::block(bool verifyLength/* = true*/) const |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 225 | { |
| 226 | return Block(m_buffer, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 227 | m_begin, m_end, |
| 228 | verifyLength); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 231 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 232 | EncodingImpl<encoding::Buffer>::resize(size_t size, bool addInFront) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 233 | { |
| 234 | if (addInFront) |
| 235 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 236 | size_t diff_end = m_buffer->end() - m_end; |
| 237 | size_t diff_begin = m_buffer->end() - m_begin; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 238 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 239 | Buffer* buf = new Buffer(size); |
| 240 | std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end()); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 241 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 242 | m_buffer.reset(buf); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 243 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 244 | m_end = m_buffer->end() - diff_end; |
| 245 | m_begin = m_buffer->end() - diff_begin; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 246 | } |
| 247 | else |
| 248 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 249 | size_t diff_end = m_end - m_buffer->begin(); |
| 250 | size_t diff_begin = m_begin - m_buffer->begin(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 251 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 252 | Buffer* buf = new Buffer(size); |
| 253 | std::copy(m_buffer->begin(), m_buffer->end(), buf->begin()); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 254 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 255 | m_buffer.reset(buf); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 256 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 257 | m_end = m_buffer->begin() + diff_end; |
| 258 | m_begin = m_buffer->begin() + diff_begin; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 263 | EncodingImpl<encoding::Buffer>::begin() |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 264 | { |
| 265 | return m_begin; |
| 266 | } |
| 267 | |
| 268 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 269 | EncodingImpl<encoding::Buffer>::end() |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 270 | { |
| 271 | return m_end; |
| 272 | } |
| 273 | |
| 274 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 275 | EncodingImpl<encoding::Buffer>::begin() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 276 | { |
| 277 | return m_begin; |
| 278 | } |
| 279 | |
| 280 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 281 | EncodingImpl<encoding::Buffer>::end() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 282 | { |
| 283 | return m_end; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | ////////////////////////////////////////////////////////// |
| 288 | // Prepend to the back of the buffer. Resize if needed. // |
| 289 | ////////////////////////////////////////////////////////// |
| 290 | |
| 291 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 292 | EncodingImpl<encoding::Buffer>::prependByte(uint8_t value) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 293 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 294 | if (m_begin == m_buffer->begin()) |
| 295 | resize(m_buffer->size() * 2, true); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 296 | |
| 297 | m_begin--; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 298 | *m_begin = value; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 303 | EncodingImpl<encoding::Estimator>::prependByte(uint8_t value) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 304 | { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 309 | EncodingImpl<encoding::Buffer>::prependByteArray(const uint8_t* array, size_t length) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 310 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 311 | if ((m_buffer->begin() + length) > m_begin) |
| 312 | resize(m_buffer->size() * 2 + length, true); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 313 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 314 | m_begin -= length; |
| 315 | std::copy(array, array + length, m_begin); |
| 316 | return length; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 320 | EncodingImpl<encoding::Estimator>::prependByteArray(const uint8_t* array, size_t length) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 321 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 322 | return length; |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 326 | EncodingImpl<encoding::Buffer>::prependNonNegativeInteger(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 327 | { |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 328 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 329 | return prependByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 330 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 331 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 332 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 333 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 334 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 335 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 336 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 337 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 338 | } |
| 339 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 340 | uint64_t value = htobe64(varNumber); |
| 341 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 346 | EncodingImpl<encoding::Estimator>::prependNonNegativeInteger(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 347 | { |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 348 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 349 | return 1; |
| 350 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 351 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 352 | return 2; |
| 353 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 354 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 355 | return 4; |
| 356 | } |
| 357 | else { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 358 | return 8; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 363 | EncodingImpl<encoding::Buffer>::prependVarNumber(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 364 | { |
| 365 | if (varNumber < 253) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 366 | prependByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 367 | return 1; |
| 368 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 369 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 370 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 371 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
| 372 | prependByte(253); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 373 | return 3; |
| 374 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 375 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 376 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 377 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
| 378 | prependByte(254); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 379 | return 5; |
| 380 | } |
| 381 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 382 | uint64_t value = htobe64(varNumber); |
| 383 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
| 384 | prependByte(255); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 385 | return 9; |
| 386 | } |
| 387 | } |
| 388 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 389 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 390 | EncodingImpl<encoding::Estimator>::prependVarNumber(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 391 | { |
| 392 | if (varNumber < 253) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 393 | return 1; |
| 394 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 395 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 396 | return 3; |
| 397 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 398 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 399 | return 5; |
| 400 | } |
| 401 | else { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 402 | return 9; |
| 403 | } |
| 404 | } |
| 405 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 406 | ///////////////////////////////////////////////////////// |
| 407 | // Append to the back of the buffer. Resize if needed. // |
| 408 | ///////////////////////////////////////////////////////// |
| 409 | |
| 410 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 411 | EncodingImpl<encoding::Buffer>::appendByte(uint8_t value) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 412 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 413 | if (m_end == m_buffer->end()) |
| 414 | resize(m_buffer->size() * 2, false); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 415 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 416 | *m_end = value; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 417 | m_end++; |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 422 | EncodingImpl<encoding::Estimator>::appendByte(uint8_t value) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 423 | { |
| 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 428 | EncodingImpl<encoding::Buffer>::appendByteArray(const uint8_t* array, size_t length) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 429 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 430 | if ((m_end + length) > m_buffer->end()) |
| 431 | resize(m_buffer->size() * 2 + length, false); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 432 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 433 | std::copy(array, array + length, m_end); |
| 434 | m_end += length; |
| 435 | return length; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 439 | EncodingImpl<encoding::Estimator>::appendByteArray(const uint8_t* array, size_t length) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 440 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 441 | return prependByteArray(array, length); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 445 | EncodingImpl<encoding::Buffer>::appendNonNegativeInteger(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 446 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 447 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
| 448 | return appendByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 449 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 450 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 451 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 452 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 453 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 454 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 455 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 456 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 457 | } |
| 458 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 459 | uint64_t value = htobe64(varNumber); |
| 460 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 465 | EncodingImpl<encoding::Estimator>::appendNonNegativeInteger(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 466 | { |
| 467 | return prependNonNegativeInteger(varNumber); |
| 468 | } |
| 469 | |
| 470 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 471 | EncodingImpl<encoding::Buffer>::appendVarNumber(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 472 | { |
| 473 | if (varNumber < 253) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 474 | appendByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 475 | return 1; |
| 476 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 477 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 478 | appendByte(253); |
| 479 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 480 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 481 | return 3; |
| 482 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 483 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 484 | appendByte(254); |
| 485 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 486 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 487 | return 5; |
| 488 | } |
| 489 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 490 | appendByte(255); |
| 491 | uint64_t value = htobe64(varNumber); |
| 492 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 493 | return 9; |
| 494 | } |
| 495 | } |
| 496 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 497 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 498 | EncodingImpl<encoding::Estimator>::appendVarNumber(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 499 | { |
| 500 | return prependVarNumber(varNumber); |
| 501 | } |
| 502 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 503 | /// helper methods |
| 504 | |
| 505 | template<bool P> |
| 506 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 507 | prependNonNegativeIntegerBlock(EncodingImpl<P>& encoder, uint32_t type, uint64_t number) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 508 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 509 | size_t valueLength = encoder.prependNonNegativeInteger(number); |
| 510 | size_t totalLength = valueLength; |
| 511 | totalLength += encoder.prependVarNumber(valueLength); |
| 512 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 513 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 514 | return totalLength; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | template<bool P> |
| 518 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 519 | prependByteArrayBlock(EncodingImpl<P>& encoder, uint32_t type, |
| 520 | const uint8_t* array, size_t arraySize) |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 521 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 522 | size_t valueLength = encoder.prependByteArray(array, arraySize); |
| 523 | size_t totalLength = valueLength; |
| 524 | totalLength += encoder.prependVarNumber(valueLength); |
| 525 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 526 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 527 | return totalLength; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | template<bool P> |
| 531 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 532 | prependBooleanBlock(EncodingImpl<P>& encoder, uint32_t type) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 533 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 534 | size_t totalLength = encoder.prependVarNumber(0); |
| 535 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 536 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 537 | return totalLength; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
| 541 | template<bool P, class U> |
| 542 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 543 | prependNestedBlock(EncodingImpl<P>& encoder, uint32_t type, const U& nestedBlock) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 544 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 545 | size_t valueLength = nestedBlock.wireEncode(encoder); |
| 546 | size_t totalLength = valueLength; |
| 547 | totalLength += encoder.prependVarNumber(valueLength); |
| 548 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 549 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 550 | return totalLength; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 553 | template<bool P> |
| 554 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 555 | prependBlock(EncodingImpl<P>& encoder, const Block& block) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 556 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 557 | return encoder.prependByteArray(block.wire(), block.size()); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 560 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 561 | } // ndn |
| 562 | |
| 563 | #endif // NDN_ENCODING_BUFFER_HPP |