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-2022 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 
29 namespace ndn {
30 namespace encoding {
31 
38 template<Tag TAG>
39 size_t
40 prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value);
41 
42 extern template size_t
43 prependNonNegativeIntegerBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, uint64_t);
44 
45 extern template size_t
46 prependNonNegativeIntegerBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, uint64_t);
47 
53 Block
54 makeNonNegativeIntegerBlock(uint32_t type, uint64_t value);
55 
61 uint64_t
62 readNonNegativeInteger(const Block& block);
63 
70 template<typename R>
71 std::enable_if_t<std::is_integral<R>::value, R>
73 {
74  uint64_t value = readNonNegativeInteger(block);
75  if (value > std::numeric_limits<R>::max()) {
76  NDN_THROW(tlv::Error("Value in TLV element of type " + to_string(block.type()) + " is too large"));
77  }
78  return static_cast<R>(value);
79 }
80 
89 template<typename R>
90 std::enable_if_t<std::is_enum<R>::value, R>
92 {
93  return static_cast<R>(readNonNegativeIntegerAs<std::underlying_type_t<R>>(block));
94 }
95 
102 template<Tag TAG>
103 size_t
104 prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type);
105 
106 extern template size_t
108 
109 extern template size_t
111 
117 Block
118 makeEmptyBlock(uint32_t type);
119 
126 template<Tag TAG>
127 size_t
128 prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value);
129 
130 extern template size_t
131 prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, const std::string&);
132 
133 extern template size_t
134 prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, const std::string&);
135 
141 Block
142 makeStringBlock(uint32_t type, const std::string& value);
143 
149 std::string
150 readString(const Block& block);
151 
158 template<Tag TAG>
159 size_t
160 prependDoubleBlock(EncodingImpl<TAG>& encoder, uint32_t type, double value);
161 
162 extern template size_t
164 
165 extern template size_t
167 
173 Block
174 makeDoubleBlock(uint32_t type, double value);
175 
181 double
182 readDouble(const Block& block);
183 
191 template<Tag TAG>
192 size_t
193 prependBinaryBlock(EncodingImpl<TAG>& encoder, uint32_t type, span<const uint8_t> value);
194 
195 extern template size_t
196 prependBinaryBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, span<const uint8_t>);
197 
198 extern template size_t
199 prependBinaryBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, span<const uint8_t>);
200 
207 Block
208 makeBinaryBlock(uint32_t type, span<const uint8_t> value);
209 
216 [[deprecated("use the overload that takes a span<>")]]
217 inline Block
218 makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length)
219 {
220  return makeBinaryBlock(type, {value, length});
221 }
222 
229 inline Block
230 makeBinaryBlock(uint32_t type, const char* value, size_t length)
231 {
232  return makeBinaryBlock(type, {reinterpret_cast<const uint8_t*>(value), length});
233 }
234 
235 namespace detail {
236 
240 template<class Iterator>
242 {
243 public:
244  BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>));
245 
246  static Block
247  makeBlock(uint32_t type, Iterator first, Iterator last)
248  {
249  EncodingEstimator estimator;
250  size_t valueLength = last - first;
251  size_t totalLength = valueLength;
252  totalLength += estimator.prependVarNumber(valueLength);
253  totalLength += estimator.prependVarNumber(type);
254 
255  EncodingBuffer encoder(totalLength, 0);
256  encoder.prependRange(first, last);
257  encoder.prependVarNumber(valueLength);
258  encoder.prependVarNumber(type);
259 
260  return encoder.block();
261  }
262 };
263 
267 template<class Iterator>
269 {
270 public:
271  BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>));
272 
273  static Block
274  makeBlock(uint32_t type, Iterator first, Iterator last)
275  {
276  // reserve 4 bytes in front (common for 1(type)-3(length) encoding
277  // Actual size will be adjusted as necessary by the encoder
278  EncodingBuffer encoder(4, 4);
279  size_t valueLength = encoder.appendRange(first, last);
280  encoder.prependVarNumber(valueLength);
281  encoder.prependVarNumber(type);
282 
283  return encoder.block();
284  }
285 };
286 
287 } // namespace detail
288 
297 template<class Iterator>
298 Block
299 makeBinaryBlock(uint32_t type, Iterator first, Iterator last)
300 {
301  using BinaryBlockHelper = std::conditional_t<
302  std::is_base_of<std::random_access_iterator_tag,
303  typename std::iterator_traits<Iterator>::iterator_category>::value,
306 
307  return BinaryBlockHelper::makeBlock(type, first, last);
308 }
309 
315 template<Tag TAG>
316 size_t
317 prependBlock(EncodingImpl<TAG>& encoder, const Block& block);
318 
319 extern template size_t
321 
322 extern template size_t
324 
332 template<Tag TAG, class U>
333 size_t
334 prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, const U& value)
335 {
336  BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<U>));
337 
338  size_t length = value.wireEncode(encoder);
339  length += encoder.prependVarNumber(length);
340  length += encoder.prependVarNumber(type);
341  return length;
342 }
343 
350 template<class U>
351 Block
352 makeNestedBlock(uint32_t type, const U& value)
353 {
354  EncodingEstimator estimator;
355  size_t totalLength = prependNestedBlock(estimator, type, value);
356 
357  EncodingBuffer encoder(totalLength, 0);
358  prependNestedBlock(encoder, type, value);
359  return encoder.block();
360 }
361 
370 template<Tag TAG, class I>
371 size_t
372 prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, I first, I last)
373 {
374  BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<typename std::iterator_traits<I>::value_type>));
375 
376  auto rfirst = std::make_reverse_iterator(last);
377  auto rlast = std::make_reverse_iterator(first);
378 
379  size_t length = 0;
380  for (auto i = rfirst; i != rlast; ++i) {
381  length += i->wireEncode(encoder);
382  }
383 
384  length += encoder.prependVarNumber(length);
385  length += encoder.prependVarNumber(type);
386  return length;
387 }
388 
396 template<class I>
397 Block
398 makeNestedBlock(uint32_t type, I first, I last)
399 {
400  EncodingEstimator estimator;
401  size_t totalLength = prependNestedBlock(estimator, type, first, last);
402 
403  EncodingBuffer encoder(totalLength, 0);
404  prependNestedBlock(encoder, type, first, last);
405  return encoder.block();
406 }
407 
408 } // namespace encoding
409 
418 
419 } // namespace ndn
420 
421 #endif // NDN_CXX_ENCODING_BLOCK_HELPERS_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:61
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:59
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition: encoder.hpp:347
size_t prependVarNumber(uint64_t number)
Prepend number encoded as a VAR-NUMBER in NDN-TLV format.
Definition: encoder.cpp:110
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition: encoder.hpp:362
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:126
Create a binary block copying from RandomAccessIterator.
static Block makeBlock(uint32_t type, Iterator first, Iterator last)
Create a binary block copying from generic InputIterator.
static Block makeBlock(uint32_t type, Iterator first, Iterator last)
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
#define NDN_THROW(e)
Definition: exception.hpp:61
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.
std::enable_if_t< std::is_integral< R >::value, R > readNonNegativeIntegerAs(const Block &block)
Read a non-negative integer from a TLV element and cast to the specified type.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
template size_t prependStringBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, const std::string &)
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
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.
Block makeEmptyBlock(uint32_t type)
Create an empty TLV block.
template size_t prependBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, const Block &)
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)
template size_t prependStringBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, const std::string &)
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 TLV-VALUE of a TLV element as a string.
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.
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block containing a string.
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.
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:31
Definition: data.cpp:25