NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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 
26 namespace ndn {
27 namespace encoding {
28 
29 namespace endian = boost::endian;
30 
31 // ---- non-negative integer ----
32 
33 template<Tag TAG>
34 size_t
35 prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value)
36 {
37  size_t valueLength = encoder.prependNonNegativeInteger(value);
38  size_t totalLength = valueLength;
39  totalLength += encoder.prependVarNumber(valueLength);
40  totalLength += encoder.prependVarNumber(type);
41 
42  return totalLength;
43 }
44 
45 template size_t
47 
48 template size_t
50 
51 Block
52 makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
53 {
54  EncodingEstimator estimator;
55  size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value);
56 
57  EncodingBuffer encoder(totalLength, 0);
58  prependNonNegativeIntegerBlock(encoder, type, value);
59 
60  return encoder.block();
61 }
62 
63 uint64_t
65 {
66  auto begin = block.value_begin();
67  return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
68 }
69 
70 // ---- empty ----
71 
72 template<Tag TAG>
73 size_t
74 prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type)
75 {
76  size_t totalLength = encoder.prependVarNumber(0);
77  totalLength += encoder.prependVarNumber(type);
78 
79  return totalLength;
80 }
81 
82 template size_t
84 
85 template size_t
87 
88 Block
89 makeEmptyBlock(uint32_t type)
90 {
91  EncodingEstimator estimator;
92  size_t totalLength = prependEmptyBlock(estimator, type);
93 
94  EncodingBuffer encoder(totalLength, 0);
95  prependEmptyBlock(encoder, type);
96 
97  return encoder.block();
98 }
99 
100 // ---- string ----
101 
102 template<Tag TAG>
103 size_t
104 prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value)
105 {
106  return encoder.prependByteArrayBlock(type, reinterpret_cast<const uint8_t*>(value.data()), value.size());
107 }
108 
109 template size_t
110 prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, const std::string&);
111 
112 template size_t
113 prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, const std::string&);
114 
115 Block
116 makeStringBlock(uint32_t type, const std::string& value)
117 {
118  return makeBinaryBlock(type, value.data(), value.size());
119 }
120 
121 std::string
122 readString(const Block& block)
123 {
124  return std::string(reinterpret_cast<const char*>(block.value()), block.value_size());
125 }
126 
127 // ---- double ----
128 
129 static_assert(std::numeric_limits<double>::is_iec559, "This code requires IEEE-754 doubles");
130 
131 template<Tag TAG>
132 size_t
133 prependDoubleBlock(EncodingImpl<TAG>& encoder, uint32_t type, double value)
134 {
135  uint64_t temp = 0;
136  std::memcpy(&temp, &value, 8);
137  endian::native_to_big_inplace(temp);
138  return encoder.prependByteArrayBlock(type, reinterpret_cast<const uint8_t*>(&temp), 8);
139 }
140 
141 template size_t
143 
144 template size_t
146 
147 Block
148 makeDoubleBlock(uint32_t type, double value)
149 {
150  EncodingEstimator estimator;
151  size_t totalLength = prependDoubleBlock(estimator, type, value);
152 
153  EncodingBuffer encoder(totalLength, 0);
154  prependDoubleBlock(encoder, type, value);
155 
156  return encoder.block();
157 }
158 
159 double
160 readDouble(const Block& block)
161 {
162  if (block.value_size() != 8) {
163  NDN_THROW(tlv::Error("Invalid length for double (must be 8)"));
164  }
165 
166 #if BOOST_VERSION >= 107100
167  return endian::endian_load<double, 8, endian::order::big>(block.value());
168 #else
169  uint64_t temp = 0;
170  std::memcpy(&temp, block.value(), 8);
171  endian::big_to_native_inplace(temp);
172  double d = 0;
173  std::memcpy(&d, &temp, 8);
174  return d;
175 #endif
176 }
177 
178 // ---- binary ----
179 
180 Block
181 makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length)
182 {
183  EncodingEstimator estimator;
184  size_t totalLength = estimator.prependByteArrayBlock(type, value, length);
185 
186  EncodingBuffer encoder(totalLength, 0);
187  encoder.prependByteArrayBlock(type, value, length);
188 
189  return encoder.block();
190 }
191 
192 Block
193 makeBinaryBlock(uint32_t type, const char* value, size_t length)
194 {
195  return makeBinaryBlock(type, reinterpret_cast<const uint8_t*>(value), length);
196 }
197 
198 } // namespace encoding
199 } // namespace ndn
ndn::encoding::prependDoubleBlock< EstimatorTag >
template size_t prependDoubleBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, double)
ndn::Block::value_size
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:308
ndn::encoding::EncodingImpl< EstimatorTag >
EncodingImpl specialization for TLV size estimation.
Definition: encoding-buffer.hpp:57
ndn::encoding::readString
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
Definition: block-helpers.cpp:122
ndn::Block::value_end
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:305
ndn::encoding::readNonNegativeInteger
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Definition: block-helpers.cpp:64
ndn::encoding::EncodingImpl< EncoderTag >
EncodingImpl specialization for actual TLV encoding.
Definition: encoding-buffer.hpp:37
ndn::encoding::prependNonNegativeIntegerBlock< EncoderTag >
template size_t prependNonNegativeIntegerBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, uint64_t)
ndn::Block::value_begin
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:296
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::encoding::Encoder::block
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:59
ndn::encoding::makeNonNegativeIntegerBlock
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
Definition: block-helpers.cpp:52
ndn::encoding::prependStringBlock< EncoderTag >
template size_t prependStringBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, const std::string &)
ndn::encoding::prependNonNegativeIntegerBlock
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Definition: block-helpers.cpp:35
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
ndn::tlv::readNonNegativeInteger
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, Iterator end)
Read nonNegativeInteger in NDN-TLV encoding.
Definition: tlv.hpp:486
block-helpers.hpp
ndn::encoding::Estimator::prependByteArrayBlock
size_t prependByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize) const noexcept
Prepend TLV block of type type and value from buffer array of size arraySize.
Definition: estimator.cpp:74
ndn::Block::value
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:302
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::encoding::prependDoubleBlock
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: block-helpers.cpp:133
ndn::encoding::prependStringBlock< EstimatorTag >
template size_t prependStringBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, const std::string &)
ndn::encoding::prependEmptyBlock< EncoderTag >
template size_t prependEmptyBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t)
ndn::encoding::prependEmptyBlock
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Prepend an empty TLV element.
Definition: block-helpers.cpp:74
ndn::encoding::readDouble
double readDouble(const Block &block)
Read TLV-VALUE of a TLV element as an IEEE 754 double-precision floating-point number.
Definition: block-helpers.cpp:160
ndn::encoding::prependDoubleBlock< EncoderTag >
template size_t prependDoubleBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, double)
ndn::encoding::prependStringBlock
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
Definition: block-helpers.cpp:104
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::encoding::makeEmptyBlock
Block makeEmptyBlock(uint32_t type)
Create an empty TLV block.
Definition: block-helpers.cpp:89
ndn::encoding::Encoder::prependByteArrayBlock
size_t prependByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize)
Prepend TLV block of type type and value from buffer array of size arraySize.
Definition: encoder.cpp:232
ndn::encoding::makeBinaryBlock
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Definition: block-helpers.cpp:181
ndn::encoding::prependEmptyBlock< EstimatorTag >
template size_t prependEmptyBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t)
ndn::encoding::makeDoubleBlock
Block makeDoubleBlock(uint32_t type, double value)
Create a TLV element containing an IEEE 754 double-precision floating-point number.
Definition: block-helpers.cpp:148
ndn::encoding::makeStringBlock
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block containing a string.
Definition: block-helpers.cpp:116
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::prependNonNegativeIntegerBlock< EstimatorTag >
template size_t prependNonNegativeIntegerBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, uint64_t)