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