24 #include <boost/endian/conversion.hpp>
29 namespace endian = boost::endian;
32 : m_buffer(make_shared<
Buffer>(totalReserve))
34 m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0);
38 : m_buffer(const_pointer_cast<
Buffer>(block.getBuffer()))
39 , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin()))
40 , m_end(m_buffer->begin() + (block.end() - m_buffer->begin()))
47 if (m_end +
size > m_buffer->end())
54 if (m_buffer->begin() +
size > m_begin)
61 return Block(m_buffer, m_begin, m_end, verifyLength);
67 if (size < m_buffer->
size()) {
68 size = m_buffer->size();
72 size_t diffEnd = m_buffer->end() - m_end;
73 size_t diffBegin = m_buffer->end() - m_begin;
76 std::copy_backward(m_buffer->begin(), m_buffer->end(),
buf->end());
80 m_end = m_buffer->end() - diffEnd;
81 m_begin = m_buffer->end() - diffBegin;
84 size_t diffEnd = m_end - m_buffer->begin();
85 size_t diffBegin = m_begin - m_buffer->begin();
88 std::copy(m_buffer->begin(), m_buffer->end(),
buf->begin());
92 m_end = m_buffer->begin() + diffEnd;
93 m_begin = m_buffer->begin() + diffBegin;
112 if (varNumber < 253) {
116 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
117 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
118 prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
122 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
123 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
124 prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
129 uint64_t value = endian::native_to_big(varNumber);
130 prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
139 if (varNumber < 253) {
143 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
145 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
146 appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
149 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
151 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
152 appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
157 uint64_t value = endian::native_to_big(varNumber);
158 appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
166 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
169 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
170 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
171 return prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
173 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
174 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
175 return prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
178 uint64_t value = endian::native_to_big(varNumber);
179 return prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
186 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
187 return appendBytes({
static_cast<uint8_t
>(varNumber)});
189 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
190 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
191 return appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
193 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
194 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
195 return appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
198 uint64_t value = endian::native_to_big(varNumber);
199 return appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
223 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Represents a TLV element of the NDN packet format.
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
uint32_t type() const
Return the TLV-TYPE of the Block.
size_t size() const
Return the size of the encoded wire, i.e.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
General-purpose automatically managed/resized buffer.
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
size_t size() const noexcept
Returns the size of the encoded buffer.
Encoder(size_t totalReserve=MAX_NDN_PACKET_SIZE, size_t reserveFromBack=400)
Create instance of the encoder with the specified reserved sizes.
size_t appendBytes(span< const uint8_t > bytes)
Append a sequence of bytes.
void reserveFront(size_t size)
Reserve at least isze bytes at the beginning of the underlying buffer.
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
size_t prependBytes(span< const uint8_t > bytes)
Prepend a sequence of bytes.
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
size_t prependVarNumber(uint64_t number)
Prepend number encoded as a VAR-NUMBER in NDN-TLV format.
size_t prependNonNegativeInteger(uint64_t integer)
Prepend integer encoded as a NonNegativeInteger in NDN-TLV format.
size_t appendByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize)
Append TLV block of type type and value from buffer array of size arraySize.
size_t appendBlock(const Block &block)
Append TLV block block.
size_t appendNonNegativeInteger(uint64_t integer)
Append integer encoded as a NonNegativeInteger in NDN-TLV format.
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
size_t appendVarNumber(uint64_t number)
Append number encoded as a VAR-NUMBER in NDN-TLV format.
void reserveBack(size_t size)
Reserve at least size bytes at the back of the underlying buffer.
size_t prependBlock(const Block &block)
Prepend TLV block block.
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.