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 | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 88 | /** |
| 89 | * @brief Create Block from the underlying EncodingBuffer |
| 90 | * |
| 91 | * @param verifyLength If this parameter set to true, Block's constructor |
| 92 | * will be requested to verify consistency of the encoded |
| 93 | * length in the Block, otherwise ignored |
| 94 | */ |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 95 | inline Block |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 96 | block (bool verifyLength = true) const; |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 97 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 98 | inline void |
| 99 | resize (size_t sz, bool addInFront); |
| 100 | |
| 101 | inline Buffer::iterator |
| 102 | begin (); |
| 103 | |
| 104 | inline Buffer::iterator |
| 105 | end (); |
| 106 | |
| 107 | inline Buffer::const_iterator |
| 108 | begin () const; |
| 109 | |
| 110 | inline Buffer::const_iterator |
| 111 | end () const; |
| 112 | |
| 113 | inline size_t |
| 114 | prependByte (uint8_t val); |
| 115 | |
| 116 | inline size_t |
| 117 | prependByteArray (const uint8_t *arr, size_t len); |
| 118 | |
| 119 | inline size_t |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 120 | prependNonNegativeInteger (uint64_t varNumber); |
| 121 | |
| 122 | inline size_t |
| 123 | prependVarNumber (uint64_t varNumber); |
| 124 | |
| 125 | inline size_t |
| 126 | appendByte (uint8_t val); |
| 127 | |
| 128 | inline size_t |
| 129 | appendByteArray (const uint8_t *arr, size_t len); |
| 130 | |
| 131 | inline size_t |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 132 | appendNonNegativeInteger (uint64_t varNumber); |
| 133 | |
| 134 | inline size_t |
| 135 | appendVarNumber (uint64_t varNumber); |
| 136 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 137 | // inline void |
| 138 | // removeByteFromFront (); |
| 139 | |
| 140 | // inline void |
| 141 | // removeByteFromEnd (); |
| 142 | |
| 143 | // inline void |
| 144 | // removeVarNumberFromFront (uint64_t varNumber); |
| 145 | |
| 146 | // inline void |
| 147 | // removeVarNumberFromBack (uint64_t varNumber); |
| 148 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 149 | private: |
| 150 | BufferPtr m_buffer; |
| 151 | |
| 152 | // invariant: m_begin always points to the position of last-written byte (if prepending data) |
| 153 | Buffer::iterator m_begin; |
| 154 | // invariant: m_end always points to the position of next unwritten byte (if appending data) |
| 155 | Buffer::iterator m_end; |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 156 | |
| 157 | friend class Block; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * @brief Class representing wire element of the NDN packet |
| 163 | */ |
| 164 | template<> |
| 165 | class EncodingImpl<encoding::Estimator> |
| 166 | { |
| 167 | public: |
| 168 | EncodingImpl (size_t totalReserve = 8800, |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 169 | size_t reserveFromBack = 400) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 170 | { |
| 171 | } |
| 172 | |
| 173 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 174 | prependByte (uint8_t val); |
| 175 | |
| 176 | inline size_t |
| 177 | prependByteArray (const uint8_t *arr, size_t len); |
| 178 | |
| 179 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 180 | prependNonNegativeInteger (uint64_t varNumber); |
| 181 | |
| 182 | inline size_t |
| 183 | prependVarNumber (uint64_t varNumber); |
| 184 | |
| 185 | inline size_t |
| 186 | appendByte (uint8_t val); |
| 187 | |
| 188 | inline size_t |
| 189 | appendByteArray (const uint8_t *arr, size_t len); |
| 190 | |
| 191 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 192 | appendNonNegativeInteger (uint64_t varNumber); |
| 193 | |
| 194 | inline size_t |
| 195 | appendVarNumber (uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 199 | //////////////////////////////////////////////////////////////////////////////// |
| 200 | //////////////////////////////////////////////////////////////////////////////// |
| 201 | //////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 204 | EncodingImpl<encoding::Buffer>::size () const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 205 | { |
| 206 | return m_end - m_begin; |
| 207 | } |
| 208 | |
| 209 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 210 | EncodingImpl<encoding::Buffer>::capacity () const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 211 | { |
| 212 | return m_buffer->size (); |
| 213 | } |
| 214 | |
| 215 | inline uint8_t* |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 216 | EncodingImpl<encoding::Buffer>::buf () |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 217 | { |
| 218 | return &(*m_begin); |
| 219 | } |
| 220 | |
| 221 | inline const uint8_t* |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 222 | EncodingImpl<encoding::Buffer>::buf () const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 223 | { |
| 224 | return &(*m_begin); |
| 225 | } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 226 | |
| 227 | inline Block |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 228 | EncodingImpl<encoding::Buffer>::block (bool verifyLength/* = true*/) const |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 229 | { |
| 230 | return Block(m_buffer, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 231 | m_begin, m_end, |
| 232 | verifyLength); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 235 | inline void |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 236 | EncodingImpl<encoding::Buffer>::resize (size_t sz, bool addInFront) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 237 | { |
| 238 | if (addInFront) |
| 239 | { |
| 240 | size_t diff_end = m_buffer->end () - m_end; |
| 241 | size_t diff_begin = m_buffer->end () - m_begin; |
| 242 | |
| 243 | Buffer* buf = new Buffer (sz); |
| 244 | std::copy_backward (m_buffer->begin (), m_buffer->end (), buf->end ()); |
| 245 | |
| 246 | m_buffer.reset (buf); |
| 247 | |
| 248 | m_end = m_buffer->end () - diff_end; |
| 249 | m_begin = m_buffer->end () - diff_begin; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | size_t diff_end = m_end - m_buffer->begin (); |
| 254 | size_t diff_begin = m_begin - m_buffer->begin (); |
| 255 | |
| 256 | Buffer* buf = new Buffer (sz); |
| 257 | std::copy (m_buffer->begin (), m_buffer->end (), buf->begin ()); |
| 258 | |
| 259 | m_buffer.reset (buf); |
| 260 | |
| 261 | m_end = m_buffer->begin () + diff_end; |
| 262 | m_begin = m_buffer->begin () + diff_begin; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | inline Buffer::iterator |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 267 | EncodingImpl<encoding::Buffer>::begin () |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 268 | { |
| 269 | return m_begin; |
| 270 | } |
| 271 | |
| 272 | inline Buffer::iterator |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 273 | EncodingImpl<encoding::Buffer>::end () |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 274 | { |
| 275 | return m_end; |
| 276 | } |
| 277 | |
| 278 | inline Buffer::const_iterator |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 279 | EncodingImpl<encoding::Buffer>::begin () const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 280 | { |
| 281 | return m_begin; |
| 282 | } |
| 283 | |
| 284 | inline Buffer::const_iterator |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 285 | EncodingImpl<encoding::Buffer>::end () const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 286 | { |
| 287 | return m_end; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | ////////////////////////////////////////////////////////// |
| 292 | // Prepend to the back of the buffer. Resize if needed. // |
| 293 | ////////////////////////////////////////////////////////// |
| 294 | |
| 295 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 296 | EncodingImpl<encoding::Buffer>::prependByte (uint8_t val) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 297 | { |
| 298 | if (m_begin == m_buffer->begin ()) |
| 299 | resize (m_buffer->size () * 2, true); |
| 300 | |
| 301 | m_begin--; |
| 302 | *m_begin = val; |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 307 | EncodingImpl<encoding::Estimator>::prependByte (uint8_t val) |
| 308 | { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | inline size_t |
| 313 | EncodingImpl<encoding::Buffer>::prependByteArray (const uint8_t *arr, size_t len) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 314 | { |
| 315 | if ((m_buffer->begin () + len) > m_begin) |
| 316 | resize (m_buffer->size () * 2 + len, true); |
| 317 | |
| 318 | m_begin -= len; |
| 319 | std::copy (arr, arr + len, m_begin); |
| 320 | return len; |
| 321 | } |
| 322 | |
| 323 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 324 | EncodingImpl<encoding::Estimator>::prependByteArray (const uint8_t *arr, size_t len) |
| 325 | { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 326 | return len; |
| 327 | } |
| 328 | |
| 329 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 330 | EncodingImpl<encoding::Buffer>::prependNonNegativeInteger (uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 331 | { |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 332 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 333 | return prependByte (static_cast<uint8_t> (varNumber)); |
| 334 | } |
| 335 | else if (varNumber <= std::numeric_limits<uint16_t>::max ()) { |
| 336 | uint16_t value = htobe16 (static_cast<uint16_t> (varNumber)); |
| 337 | return prependByteArray (reinterpret_cast<const uint8_t*> (&value), 2); |
| 338 | } |
| 339 | else if (varNumber <= std::numeric_limits<uint32_t>::max ()) { |
| 340 | uint32_t value = htobe32 (static_cast<uint32_t> (varNumber)); |
| 341 | return prependByteArray (reinterpret_cast<const uint8_t*> (&value), 4); |
| 342 | } |
| 343 | else { |
| 344 | uint64_t value = htobe64 (varNumber); |
| 345 | return prependByteArray (reinterpret_cast<const uint8_t*> (&value), 8); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 350 | EncodingImpl<encoding::Estimator>::prependNonNegativeInteger (uint64_t varNumber) |
| 351 | { |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 352 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 353 | return 1; |
| 354 | } |
| 355 | else if (varNumber <= std::numeric_limits<uint16_t>::max ()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 356 | return 2; |
| 357 | } |
| 358 | else if (varNumber <= std::numeric_limits<uint32_t>::max ()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 359 | return 4; |
| 360 | } |
| 361 | else { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 362 | return 8; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | inline size_t |
| 367 | EncodingImpl<encoding::Buffer>::prependVarNumber (uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 368 | { |
| 369 | if (varNumber < 253) { |
| 370 | prependByte (static_cast<uint8_t> (varNumber)); |
| 371 | return 1; |
| 372 | } |
| 373 | else if (varNumber <= std::numeric_limits<uint16_t>::max ()) { |
| 374 | uint16_t value = htobe16 (static_cast<uint16_t> (varNumber)); |
| 375 | prependByteArray (reinterpret_cast<const uint8_t*> (&value), 2); |
| 376 | prependByte (253); |
| 377 | return 3; |
| 378 | } |
| 379 | else if (varNumber <= std::numeric_limits<uint32_t>::max ()) { |
| 380 | uint32_t value = htobe32 (static_cast<uint32_t> (varNumber)); |
| 381 | prependByteArray (reinterpret_cast<const uint8_t*> (&value), 4); |
| 382 | prependByte (254); |
| 383 | return 5; |
| 384 | } |
| 385 | else { |
| 386 | uint64_t value = htobe64 (varNumber); |
| 387 | prependByteArray (reinterpret_cast<const uint8_t*> (&value), 8); |
| 388 | prependByte (255); |
| 389 | return 9; |
| 390 | } |
| 391 | } |
| 392 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 393 | inline size_t |
| 394 | EncodingImpl<encoding::Estimator>::prependVarNumber (uint64_t varNumber) |
| 395 | { |
| 396 | if (varNumber < 253) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | else if (varNumber <= std::numeric_limits<uint16_t>::max ()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 400 | return 3; |
| 401 | } |
| 402 | else if (varNumber <= std::numeric_limits<uint32_t>::max ()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 403 | return 5; |
| 404 | } |
| 405 | else { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 406 | return 9; |
| 407 | } |
| 408 | } |
| 409 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 410 | ///////////////////////////////////////////////////////// |
| 411 | // Append to the back of the buffer. Resize if needed. // |
| 412 | ///////////////////////////////////////////////////////// |
| 413 | |
| 414 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 415 | EncodingImpl<encoding::Buffer>::appendByte (uint8_t val) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 416 | { |
| 417 | if (m_end == m_buffer->end ()) |
| 418 | resize (m_buffer->size () * 2, false); |
| 419 | |
| 420 | *m_end = val; |
| 421 | m_end++; |
| 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 426 | EncodingImpl<encoding::Estimator>::appendByte (uint8_t val) |
| 427 | { |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | inline size_t |
| 432 | EncodingImpl<encoding::Buffer>::appendByteArray (const uint8_t *arr, size_t len) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 433 | { |
| 434 | if ((m_end + len) > m_buffer->end ()) |
| 435 | resize (m_buffer->size () * 2 + len, false); |
| 436 | |
| 437 | std::copy (arr, arr + len, m_end); |
| 438 | m_end += len; |
| 439 | return len; |
| 440 | } |
| 441 | |
| 442 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 443 | EncodingImpl<encoding::Estimator>::appendByteArray (const uint8_t *arr, size_t len) |
| 444 | { |
| 445 | return prependByteArray(arr, len); |
| 446 | } |
| 447 | |
| 448 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 449 | EncodingImpl<encoding::Buffer>::appendNonNegativeInteger (uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -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 ()) { |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 452 | return appendByte (static_cast<uint8_t> (varNumber)); |
| 453 | } |
| 454 | else if (varNumber <= std::numeric_limits<uint16_t>::max ()) { |
| 455 | uint16_t value = htobe16 (static_cast<uint16_t> (varNumber)); |
| 456 | return appendByteArray (reinterpret_cast<const uint8_t*> (&value), 2); |
| 457 | } |
| 458 | else if (varNumber <= std::numeric_limits<uint32_t>::max ()) { |
| 459 | uint32_t value = htobe32 (static_cast<uint32_t> (varNumber)); |
| 460 | return appendByteArray (reinterpret_cast<const uint8_t*> (&value), 4); |
| 461 | } |
| 462 | else { |
| 463 | uint64_t value = htobe64 (varNumber); |
| 464 | return appendByteArray (reinterpret_cast<const uint8_t*> (&value), 8); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | inline size_t |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 469 | EncodingImpl<encoding::Estimator>::appendNonNegativeInteger (uint64_t varNumber) |
| 470 | { |
| 471 | return prependNonNegativeInteger(varNumber); |
| 472 | } |
| 473 | |
| 474 | inline size_t |
| 475 | EncodingImpl<encoding::Buffer>::appendVarNumber (uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 476 | { |
| 477 | if (varNumber < 253) { |
| 478 | appendByte (static_cast<uint8_t> (varNumber)); |
| 479 | return 1; |
| 480 | } |
| 481 | else if (varNumber <= std::numeric_limits<uint16_t>::max ()) { |
| 482 | appendByte (253); |
| 483 | uint16_t value = htobe16 (static_cast<uint16_t> (varNumber)); |
| 484 | appendByteArray (reinterpret_cast<const uint8_t*> (&value), 2); |
| 485 | return 3; |
| 486 | } |
| 487 | else if (varNumber <= std::numeric_limits<uint32_t>::max ()) { |
| 488 | appendByte (254); |
| 489 | uint32_t value = htobe32 (static_cast<uint32_t> (varNumber)); |
| 490 | appendByteArray (reinterpret_cast<const uint8_t*> (&value), 4); |
| 491 | return 5; |
| 492 | } |
| 493 | else { |
| 494 | appendByte (255); |
| 495 | uint64_t value = htobe64 (varNumber); |
| 496 | appendByteArray (reinterpret_cast<const uint8_t*> (&value), 8); |
| 497 | return 9; |
| 498 | } |
| 499 | } |
| 500 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 501 | inline size_t |
| 502 | EncodingImpl<encoding::Estimator>::appendVarNumber (uint64_t varNumber) |
| 503 | { |
| 504 | return prependVarNumber(varNumber); |
| 505 | } |
| 506 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 507 | /// helper methods |
| 508 | |
| 509 | template<bool P> |
| 510 | inline size_t |
| 511 | prependNonNegativeIntegerBlock(EncodingImpl<P>& blk, uint32_t type, uint64_t number) |
| 512 | { |
| 513 | size_t var_len = blk.prependNonNegativeInteger(number); |
| 514 | size_t total_len = var_len; |
| 515 | total_len += blk.prependVarNumber(var_len); |
| 516 | total_len += blk.prependVarNumber(type); |
| 517 | |
| 518 | return total_len; |
| 519 | } |
| 520 | |
| 521 | template<bool P> |
| 522 | inline size_t |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 523 | prependByteArrayBlock(EncodingImpl<P>& blk, uint32_t type, const uint8_t* array, size_t arraySize) |
| 524 | { |
| 525 | size_t var_len = blk.prependByteArray(array, arraySize); |
| 526 | size_t total_len = var_len; |
| 527 | total_len += blk.prependVarNumber(var_len); |
| 528 | total_len += blk.prependVarNumber(type); |
| 529 | |
| 530 | return total_len; |
| 531 | } |
| 532 | |
| 533 | template<bool P> |
| 534 | inline size_t |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 535 | prependBooleanBlock(EncodingImpl<P>& blk, uint32_t type) |
| 536 | { |
| 537 | size_t total_len = blk.prependVarNumber(0); |
| 538 | total_len += blk.prependVarNumber(type); |
| 539 | |
| 540 | return total_len; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | template<bool P, class U> |
| 545 | inline size_t |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 546 | prependNestedBlock(EncodingImpl<P>& blk, uint32_t type, const U& nestedBlock) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 547 | { |
| 548 | size_t var_len = nestedBlock.wireEncode(blk); |
| 549 | size_t total_len = var_len; |
| 550 | total_len += blk.prependVarNumber(var_len); |
| 551 | total_len += blk.prependVarNumber(type); |
| 552 | |
| 553 | return total_len; |
| 554 | } |
| 555 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 556 | template<bool P> |
| 557 | inline size_t |
| 558 | prependBlock(EncodingImpl<P>& blk, const Block& block) |
| 559 | { |
| 560 | return blk.prependByteArray(block.wire(), block.size()); |
| 561 | } |
| 562 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 563 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 564 | } // ndn |
| 565 | |
| 566 | #endif // NDN_ENCODING_BUFFER_HPP |