Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "estimator.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace encoding { |
| 26 | |
| 27 | Estimator::Estimator(size_t totalReserve, size_t reserveFromBack) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | size_t |
| 32 | Estimator::prependByte(uint8_t value) |
| 33 | { |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | size_t |
| 38 | Estimator::appendByte(uint8_t value) |
| 39 | { |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | size_t |
| 45 | Estimator::prependByteArray(const uint8_t* array, size_t length) |
| 46 | { |
| 47 | return length; |
| 48 | } |
| 49 | |
| 50 | size_t |
| 51 | Estimator::appendByteArray(const uint8_t* array, size_t length) |
| 52 | { |
| 53 | return prependByteArray(array, length); |
| 54 | } |
| 55 | |
| 56 | size_t |
| 57 | Estimator::prependVarNumber(uint64_t varNumber) |
| 58 | { |
| 59 | if (varNumber < 253) { |
| 60 | return 1; |
| 61 | } |
| 62 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 63 | return 3; |
| 64 | } |
| 65 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 66 | return 5; |
| 67 | } |
| 68 | else { |
| 69 | return 9; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | size_t |
| 74 | Estimator::appendVarNumber(uint64_t varNumber) |
| 75 | { |
| 76 | return prependVarNumber(varNumber); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | size_t |
| 81 | Estimator::prependNonNegativeInteger(uint64_t varNumber) |
| 82 | { |
| 83 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
| 84 | return 1; |
| 85 | } |
| 86 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 87 | return 2; |
| 88 | } |
| 89 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 90 | return 4; |
| 91 | } |
| 92 | else { |
| 93 | return 8; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | size_t |
| 98 | Estimator::appendNonNegativeInteger(uint64_t varNumber) |
| 99 | { |
| 100 | return prependNonNegativeInteger(varNumber); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | size_t |
| 105 | Estimator::prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) |
| 106 | { |
| 107 | size_t totalLength = arraySize; |
| 108 | totalLength += prependVarNumber(arraySize); |
| 109 | totalLength += prependVarNumber(type); |
| 110 | |
| 111 | return totalLength; |
| 112 | } |
| 113 | |
| 114 | size_t |
| 115 | Estimator::appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) |
| 116 | { |
| 117 | return prependByteArrayBlock(type, array, arraySize); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | size_t |
| 122 | Estimator::prependBlock(const Block& block) |
| 123 | { |
| 124 | if (block.hasWire()) { |
| 125 | return block.size(); |
| 126 | } |
| 127 | else { |
| 128 | return prependByteArrayBlock(block.type(), block.value(), block.value_size()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | size_t |
| 133 | Estimator::appendBlock(const Block& block) |
| 134 | { |
| 135 | return prependBlock(block); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | } // namespace encoding |
| 140 | } // namespace ndn |