NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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 
24 namespace ndn {
25 namespace encoding {
26 
27 // ---- non-negative integer ----
28 
29 template<Tag TAG>
30 size_t
31 prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value)
32 {
33  size_t valueLength = encoder.prependNonNegativeInteger(value);
34  size_t totalLength = valueLength;
35  totalLength += encoder.prependVarNumber(valueLength);
36  totalLength += encoder.prependVarNumber(type);
37 
38  return totalLength;
39 }
40 
41 template size_t
43 
44 template size_t
46 
47 Block
48 makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
49 {
50  EncodingEstimator estimator;
51  size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value);
52 
53  EncodingBuffer encoder(totalLength, 0);
54  prependNonNegativeIntegerBlock(encoder, type, value);
55 
56  return encoder.block();
57 }
58 
59 uint64_t
61 {
62  Buffer::const_iterator begin = block.value_begin();
63  return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
64 }
65 
66 // ---- empty ----
67 
68 template<Tag TAG>
69 size_t
70 prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type)
71 {
72  size_t totalLength = encoder.prependVarNumber(0);
73  totalLength += encoder.prependVarNumber(type);
74 
75  return totalLength;
76 }
77 
78 template size_t
79 prependEmptyBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t);
80 
81 template size_t
82 prependEmptyBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t);
83 
84 Block
85 makeEmptyBlock(uint32_t type)
86 {
87  EncodingEstimator estimator;
88  size_t totalLength = prependEmptyBlock(estimator, type);
89 
90  EncodingBuffer encoder(totalLength, 0);
91  prependEmptyBlock(encoder, type);
92 
93  return encoder.block();
94 }
95 
96 // ---- string ----
97 
98 template<Tag TAG>
99 size_t
100 prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value)
101 {
102  return encoder.prependByteArrayBlock(type, reinterpret_cast<const uint8_t*>(value.data()), value.size());
103 }
104 
105 template size_t
106 prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, const std::string&);
107 
108 template size_t
109 prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, const std::string&);
110 
111 Block
112 makeStringBlock(uint32_t type, const std::string& value)
113 {
114  return makeBinaryBlock(type, value.data(), value.size());
115 }
116 
117 std::string
118 readString(const Block& block)
119 {
120  return std::string(reinterpret_cast<const char*>(block.value()), block.value_size());
121 }
122 
123 // ---- binary ----
124 
125 Block
126 makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length)
127 {
128  EncodingEstimator estimator;
129  size_t totalLength = estimator.prependByteArrayBlock(type, value, length);
130 
131  EncodingBuffer encoder(totalLength, 0);
132  encoder.prependByteArrayBlock(type, value, length);
133 
134  return encoder.block();
135 }
136 
137 Block
138 makeBinaryBlock(uint32_t type, const char* value, size_t length)
139 {
140  return makeBinaryBlock(type, reinterpret_cast<const uint8_t*>(value), length);
141 }
142 
143 } // namespace encoding
144 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
EncodingImpl specialization for actual TLV encoding.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Block makeEmptyBlock(uint32_t type)
Create an empty TLV block.
template size_t prependEmptyBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t)
template size_t prependNonNegativeIntegerBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, uint64_t)
template size_t prependNonNegativeIntegerBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, uint64_t)
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: estimator.cpp:105
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:255
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:264
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
EncodingImpl specialization for TLV size estimation.
template size_t prependEmptyBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t)
template size_t prependStringBlock< EncoderTag >(EncodingImpl< EncoderTag > &, uint32_t, const std::string &)
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, const Iterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
Definition: tlv.hpp:452
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Prepend an empty TLV element.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block containing a string.
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:60
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:241
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:317
template size_t prependStringBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &, uint32_t, const std::string &)
const uint8_t * value() const
Get pointer to TLV-VALUE.
Definition: block.cpp:311