ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
block-helpers.cpp
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
23
24#include <boost/endian/conversion.hpp>
25
26namespace ndn::encoding {
27
28// ---- empty ----
29
30template<Tag TAG>
31size_t
32prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type)
33{
34 size_t length = encoder.prependVarNumber(0);
35 length += encoder.prependVarNumber(type);
36
37 return length;
38}
39
40template size_t
42
43template size_t
45
47makeEmptyBlock(uint32_t type)
48{
49 EncodingEstimator estimator;
50 size_t totalLength = prependEmptyBlock(estimator, type);
51
52 EncodingBuffer encoder(totalLength, 0);
53 prependEmptyBlock(encoder, type);
54
55 return encoder.block();
56}
57
58// ---- non-negative integer ----
59
60template<Tag TAG>
61size_t
62prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value)
63{
64 size_t length = encoder.prependNonNegativeInteger(value);
65 length += encoder.prependVarNumber(length);
66 length += encoder.prependVarNumber(type);
67
68 return length;
69}
70
71template size_t
73
74template size_t
76
78makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
79{
80 EncodingEstimator estimator;
81 size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value);
82
83 EncodingBuffer encoder(totalLength, 0);
84 prependNonNegativeIntegerBlock(encoder, type, value);
85
86 return encoder.block();
87}
88
89uint64_t
91{
92 auto begin = block.value_begin();
93 return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
94}
95
96// ---- double ----
97
98static_assert(std::numeric_limits<double>::is_iec559, "This code requires IEEE-754 doubles");
99
100template<Tag TAG>
101size_t
102prependDoubleBlock(EncodingImpl<TAG>& encoder, uint32_t type, double value)
103{
104 uint64_t temp = 0;
105 std::memcpy(&temp, &value, 8);
106 boost::endian::native_to_big_inplace(temp);
107 return prependBinaryBlock(encoder, type, {reinterpret_cast<const uint8_t*>(&temp), 8});
108}
109
110template size_t
112
113template size_t
115
116Block
117makeDoubleBlock(uint32_t type, double value)
118{
119 EncodingEstimator estimator;
120 size_t totalLength = prependDoubleBlock(estimator, type, value);
121
122 EncodingBuffer encoder(totalLength, 0);
123 prependDoubleBlock(encoder, type, value);
124
125 return encoder.block();
126}
127
128double
129readDouble(const Block& block)
130{
131 if (block.value_size() != 8) {
132 NDN_THROW(tlv::Error("Invalid length for double (must be 8)"));
133 }
134 return boost::endian::endian_load<double, 8, boost::endian::order::big>(block.value());
135}
136
137// ---- binary ----
138
139template<Tag TAG>
140size_t
141prependBinaryBlock(EncodingImpl<TAG>& encoder, uint32_t type, span<const uint8_t> value)
142{
143 size_t length = encoder.prependBytes(value);
144 length += encoder.prependVarNumber(length);
145 length += encoder.prependVarNumber(type);
146
147 return length;
148}
149
150template size_t
151prependBinaryBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, span<const uint8_t>);
152
153template size_t
154prependBinaryBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, span<const uint8_t>);
155
156Block
157makeBinaryBlock(uint32_t type, span<const uint8_t> value)
158{
159 EncodingEstimator estimator;
160 size_t totalLength = prependBinaryBlock(estimator, type, value);
161
162 EncodingBuffer encoder(totalLength, 0);
163 prependBinaryBlock(encoder, type, value);
164
165 return encoder.block();
166}
167
168// ---- string ----
169
170template<Tag TAG>
171size_t
172prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, std::string_view value)
173{
174 return prependBinaryBlock(encoder, type, {reinterpret_cast<const uint8_t*>(value.data()), value.size()});
175}
176
177template size_t
179
180template size_t
181prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, std::string_view);
182
183std::string
184readString(const Block& block)
185{
186 return std::string(reinterpret_cast<const char*>(block.value()), block.value_size());
187}
188
189// ---- block ----
190
191template<Tag TAG>
192size_t
193prependBlock(EncodingImpl<TAG>& encoder, const Block& block)
194{
195 if (block.hasWire()) {
196 return encoder.prependBytes(block);
197 }
198 else {
199 // FIXME: blindly calling Block::value_bytes() is not safe if the value is not wire-encoded
200 return prependBinaryBlock(encoder, block.type(), block.value_bytes());
201 }
202}
203
204template size_t
206
207template size_t
209
210} // namespace ndn::encoding
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
const_iterator value_end() const noexcept
Get end iterator of TLV-VALUE.
Definition block.hpp:338
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition block.hpp:205
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
span< const uint8_t > value_bytes() const noexcept
Return a read-only view of TLV-VALUE as a contiguous range of bytes.
Definition block.hpp:308
const_iterator value_begin() const noexcept
Get begin iterator of TLV-VALUE.
Definition block.hpp:328
size_t value_size() const noexcept
Return the size of TLV-VALUE, i.e., the TLV-LENGTH.
Definition block.hpp:299
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition block.cpp:308
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition encoder.cpp:58
EncodingImpl specialization for actual TLV encoding.
EncodingImpl specialization for TLV size estimation.
Represents an error in TLV encoding or decoding.
Definition tlv.hpp:54
#define NDN_THROW(e)
Definition exception.hpp:56
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 >)
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.
size_t prependBlock(EncodingImpl< TAG > &encoder, const Block &block)
Prepend a 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.
constexpr uint64_t readNonNegativeInteger(size_t size, Iterator &begin, Iterator end)
Read a NonNegativeInteger in NDN-TLV encoding.
Definition tlv.hpp:429