blob: 9ec23360ec07d0420c6d6cb24d7a30910b26951b [file] [log] [blame]
Alexander Afanasyev74633892015-02-08 18:08:46 -08001/* -*- 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
24namespace ndn {
25namespace encoding {
26
27Estimator::Estimator(size_t totalReserve, size_t reserveFromBack)
28{
29}
30
31size_t
32Estimator::prependByte(uint8_t value)
33{
34 return 1;
35}
36
37size_t
38Estimator::appendByte(uint8_t value)
39{
40 return 1;
41}
42
43
44size_t
45Estimator::prependByteArray(const uint8_t* array, size_t length)
46{
47 return length;
48}
49
50size_t
51Estimator::appendByteArray(const uint8_t* array, size_t length)
52{
53 return prependByteArray(array, length);
54}
55
56size_t
57Estimator::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
73size_t
74Estimator::appendVarNumber(uint64_t varNumber)
75{
76 return prependVarNumber(varNumber);
77}
78
79
80size_t
81Estimator::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
97size_t
98Estimator::appendNonNegativeInteger(uint64_t varNumber)
99{
100 return prependNonNegativeInteger(varNumber);
101}
102
103
104size_t
105Estimator::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
114size_t
115Estimator::appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize)
116{
117 return prependByteArrayBlock(type, array, arraySize);
118}
119
120
121size_t
122Estimator::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
132size_t
133Estimator::appendBlock(const Block& block)
134{
135 return prependBlock(block);
136}
137
138
139} // namespace encoding
140} // namespace ndn