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