blob: 594fc5cfb50925e9e236ea0b78e17630a2daac68 [file] [log] [blame]
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -07001/* -*- 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 "block-helpers.hpp"
23
24namespace ndn {
25namespace encoding {
26
27template<Tag TAG>
28size_t
29prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value)
30{
31 size_t valueLength = encoder.prependNonNegativeInteger(value);
32 size_t totalLength = valueLength;
33 totalLength += encoder.prependVarNumber(valueLength);
34 totalLength += encoder.prependVarNumber(type);
35
36 return totalLength;
37}
38
39template size_t
40prependNonNegativeIntegerBlock<EstimatorTag>(EncodingImpl<EstimatorTag>& encoder,
41 uint32_t type, uint64_t value);
42
43template size_t
44prependNonNegativeIntegerBlock<EncoderTag>(EncodingImpl<EncoderTag>& encoder,
45 uint32_t type, uint64_t value);
46
47
48Block
49makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
50{
51 EncodingEstimator estimator;
52 size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value);
53
54 EncodingBuffer encoder(totalLength, 0);
55 prependNonNegativeIntegerBlock(encoder, type, value);
56
57 return encoder.block();
58}
59
60uint64_t
61readNonNegativeInteger(const Block& block)
62{
63 Buffer::const_iterator begin = block.value_begin();
64 return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
65}
66
67////////
68
69template<Tag TAG>
70size_t
71prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type)
72{
73 size_t totalLength = encoder.prependVarNumber(0);
74 totalLength += encoder.prependVarNumber(type);
75
76 return totalLength;
77}
78
79template size_t
80prependEmptyBlock<EstimatorTag>(EncodingImpl<EstimatorTag>& encoder, uint32_t type);
81
82template size_t
83prependEmptyBlock<EncoderTag>(EncodingImpl<EncoderTag>& encoder, uint32_t type);
84
85
86Block
87makeEmptyBlock(uint32_t type)
88{
89 EncodingEstimator estimator;
90 size_t totalLength = prependEmptyBlock(estimator, type);
91
92 EncodingBuffer encoder(totalLength, 0);
93 prependEmptyBlock(encoder, type);
94
95 return encoder.block();
96}
97
98////////
99
100template<Tag TAG>
101size_t
102prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value)
103{
104 size_t valueLength = encoder.prependByteArray(reinterpret_cast<const uint8_t*>(value.data()),
105 value.size());
106 size_t totalLength = valueLength;
107 totalLength += encoder.prependVarNumber(valueLength);
108 totalLength += encoder.prependVarNumber(type);
109
110 return totalLength;
111}
112
113template size_t
114prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>& encoder,
115 uint32_t type, const std::string& value);
116
117template size_t
118prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>& encoder,
119 uint32_t type, const std::string& value);
120
121
122Block
123makeStringBlock(uint32_t type, const std::string& value)
124{
125 EncodingEstimator estimator;
126 size_t totalLength = prependStringBlock(estimator, type, value);
127
128 EncodingBuffer encoder(totalLength, 0);
129 prependStringBlock(encoder, type, value);
130
131 return encoder.block();
132}
133
134std::string
135readString(const Block& block)
136{
137 return std::string(reinterpret_cast<const char*>(block.value()), block.value_size());
138}
139
140////////
141
142Block
143makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length)
144{
145 EncodingEstimator estimator;
146 size_t totalLength = estimator.prependByteArrayBlock(type, value, length);
147
148 EncodingBuffer encoder(totalLength, 0);
149 encoder.prependByteArrayBlock(type, value, length);
150
151 return encoder.block();
152}
153
154Block
155makeBinaryBlock(uint32_t type, const char* value, size_t length)
156{
157 return makeBinaryBlock(type, reinterpret_cast<const uint8_t*>(value), length);
158}
159
160} // namespace encoding
161} // namespace ndn