ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
block-helpers.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2023 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#ifndef NDN_CXX_ENCODING_BLOCK_HELPERS_HPP
23#define NDN_CXX_ENCODING_BLOCK_HELPERS_HPP
24
28
29namespace ndn {
30namespace encoding {
31
38template<Tag TAG>
39size_t
40prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type);
41
42extern template size_t
43prependEmptyBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t);
44
45extern template size_t
46prependEmptyBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t);
47
53Block
54makeEmptyBlock(uint32_t type);
55
62template<Tag TAG>
63size_t
64prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value);
65
66extern template size_t
67prependNonNegativeIntegerBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, uint64_t);
68
69extern template size_t
70prependNonNegativeIntegerBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, uint64_t);
71
77Block
78makeNonNegativeIntegerBlock(uint32_t type, uint64_t value);
79
85uint64_t
86readNonNegativeInteger(const Block& block);
87
94template<typename R>
95std::enable_if_t<std::is_integral_v<R>, R>
97{
98 uint64_t value = readNonNegativeInteger(block);
99 if (value > std::numeric_limits<R>::max()) {
100 NDN_THROW(tlv::Error("Value in TLV element of type " + to_string(block.type()) + " is too large"));
101 }
102 return static_cast<R>(value);
103}
104
113template<typename R>
114std::enable_if_t<std::is_enum_v<R>, R>
116{
117 return static_cast<R>(readNonNegativeIntegerAs<std::underlying_type_t<R>>(block));
118}
119
126template<Tag TAG>
127size_t
128prependDoubleBlock(EncodingImpl<TAG>& encoder, uint32_t type, double value);
129
130extern template size_t
132
133extern template size_t
135
141Block
142makeDoubleBlock(uint32_t type, double value);
143
149double
150readDouble(const Block& block);
151
159template<Tag TAG>
160size_t
161prependBinaryBlock(EncodingImpl<TAG>& encoder, uint32_t type, span<const uint8_t> value);
162
163extern template size_t
164prependBinaryBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, span<const uint8_t>);
165
166extern template size_t
167prependBinaryBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, span<const uint8_t>);
168
175Block
176makeBinaryBlock(uint32_t type, span<const uint8_t> value);
177
178namespace detail {
179
183template<class Iterator>
184Block
185makeBinaryBlockFast(uint32_t type, Iterator first, Iterator last)
186{
187 BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>));
188
189 EncodingEstimator estimator;
190 size_t valueLength = last - first;
191 size_t totalLength = valueLength;
192 totalLength += estimator.prependVarNumber(valueLength);
193 totalLength += estimator.prependVarNumber(type);
194
195 EncodingBuffer encoder(totalLength, 0);
196 encoder.prependRange(first, last);
197 encoder.prependVarNumber(valueLength);
198 encoder.prependVarNumber(type);
199
200 return encoder.block();
201}
202
206template<class Iterator>
207Block
208makeBinaryBlockSlow(uint32_t type, Iterator first, Iterator last)
209{
210 BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>));
211
212 // Reserve 4 bytes in front, common for 1(type)-3(length) encoding.
213 // Actual size will be adjusted as necessary by the encoder.
214 EncodingBuffer encoder(4, 4);
215 size_t valueLength = encoder.appendRange(first, last);
216 encoder.prependVarNumber(valueLength);
217 encoder.prependVarNumber(type);
218
219 return encoder.block();
220}
221
222} // namespace detail
223
233template<class Iterator>
234Block
235makeBinaryBlock(uint32_t type, Iterator first, Iterator last)
236{
237 if constexpr (std::is_base_of_v<std::random_access_iterator_tag,
238 typename std::iterator_traits<Iterator>::iterator_category>)
239 return detail::makeBinaryBlockFast(type, first, last);
240 else
241 return detail::makeBinaryBlockSlow(type, first, last);
242}
243
251template<Tag TAG>
252size_t
253prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, std::string_view value);
254
255extern template size_t
257
258extern template size_t
259prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, std::string_view);
260
267inline Block
268makeStringBlock(uint32_t type, std::string_view value)
269{
270 return makeBinaryBlock(type, {reinterpret_cast<const uint8_t*>(value.data()), value.size()});
271}
272
279std::string
280readString(const Block& block);
281
287template<Tag TAG>
288size_t
289prependBlock(EncodingImpl<TAG>& encoder, const Block& block);
290
291extern template size_t
293
294extern template size_t
296
304template<Tag TAG, class U>
305size_t
306prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, const U& value)
307{
308 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<U>));
309
310 size_t length = value.wireEncode(encoder);
311 length += encoder.prependVarNumber(length);
312 length += encoder.prependVarNumber(type);
313 return length;
314}
315
322template<class U>
323Block
324makeNestedBlock(uint32_t type, const U& value)
325{
326 EncodingEstimator estimator;
327 size_t totalLength = prependNestedBlock(estimator, type, value);
328
329 EncodingBuffer encoder(totalLength, 0);
330 prependNestedBlock(encoder, type, value);
331 return encoder.block();
332}
333
342template<Tag TAG, class I>
343size_t
344prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, I first, I last)
345{
346 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<typename std::iterator_traits<I>::value_type>));
347
348 auto rfirst = std::make_reverse_iterator(last);
349 auto rlast = std::make_reverse_iterator(first);
350
351 size_t length = 0;
352 for (auto i = rfirst; i != rlast; ++i) {
353 length += i->wireEncode(encoder);
354 }
355
356 length += encoder.prependVarNumber(length);
357 length += encoder.prependVarNumber(type);
358 return length;
359}
360
368template<class I>
369Block
370makeNestedBlock(uint32_t type, I first, I last)
371{
372 EncodingEstimator estimator;
373 size_t totalLength = prependNestedBlock(estimator, type, first, last);
374
375 EncodingBuffer encoder(totalLength, 0);
376 prependNestedBlock(encoder, type, first, last);
377 return encoder.block();
378}
379
380} // namespace encoding
381
390
391} // namespace ndn
392
393#endif // NDN_CXX_ENCODING_BLOCK_HELPERS_HPP
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
A concept check for TLV abstraction with a wireEncode(EncodingBuffer) method.
Definition concepts.hpp:62
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition encoder.cpp:58
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition encoder.hpp:252
size_t prependVarNumber(uint64_t number)
Prepend number encoded as a VAR-NUMBER in NDN-TLV format.
Definition encoder.cpp:109
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition encoder.hpp:267
EncodingImpl specialization for actual TLV encoding.
EncodingImpl specialization for TLV size estimation.
constexpr size_t prependVarNumber(uint64_t n) const noexcept
Prepend n in VarNumber encoding.
Definition estimator.hpp:81
Represents an error in TLV encoding or decoding.
Definition tlv.hpp:54
#define NDN_THROW(e)
Definition exception.hpp:56
Block makeBinaryBlockSlow(uint32_t type, Iterator first, Iterator last)
Create a binary block copying from generic InputIterator.
Block makeBinaryBlockFast(uint32_t type, Iterator first, Iterator last)
Create a binary block copying from RandomAccessIterator.
template size_t prependEmptyBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t)
template size_t prependDoubleBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, double)
template size_t prependBinaryBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, span< const uint8_t >)
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
Block makeStringBlock(uint32_t type, std::string_view value)
Create a TLV block containing a string.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
template size_t prependBlock< EncoderTag >(EncodingImpl< EncoderTag > &, const Block &)
template size_t prependBinaryBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, span< const uint8_t >)
template size_t prependDoubleBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, double)
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
template size_t prependStringBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, std::string_view)
Block makeEmptyBlock(uint32_t type)
Create an empty TLV block.
template size_t prependBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, const Block &)
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, std::string_view value)
Prepend a TLV element containing a string.
template size_t prependNonNegativeIntegerBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, uint64_t)
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Prepend an empty TLV element.
template size_t prependNonNegativeIntegerBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, uint64_t)
double readDouble(const Block &block)
Read TLV-VALUE of a TLV element as an IEEE 754 double-precision floating-point number.
template size_t prependEmptyBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t)
Block makeBinaryBlock(uint32_t type, span< const uint8_t > value)
Create a TLV block copying the TLV-VALUE from a byte range.
size_t prependBinaryBlock(EncodingImpl< TAG > &encoder, uint32_t type, span< const uint8_t > value)
Prepend a TLV element containing a sequence of raw bytes.
std::string readString(const Block &block)
Read the TLV-VALUE of a TLV element as a string.
std::enable_if_t< std::is_integral_v< R >, R > readNonNegativeIntegerAs(const Block &block)
Read a non-negative integer from a TLV element and cast to the specified type.
size_t prependBlock(EncodingImpl< TAG > &encoder, const Block &block)
Prepend a TLV element.
Block makeNestedBlock(uint32_t type, const U &value)
Create a TLV block containing a nested TLV element.
template size_t prependStringBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, std::string_view)
Block makeDoubleBlock(uint32_t type, double value)
Create a TLV element containing an IEEE 754 double-precision floating-point number.
size_t prependDoubleBlock(EncodingImpl< TAG > &encoder, uint32_t type, double value)
Prepend a TLV element containing an IEEE 754 double-precision floating-point number.
Definition data.cpp:25