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;
123 std::copy(array, array + length, m_begin);
132 std::copy(array, array + length, m_end);
140 if (varNumber < 253) {
144 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
145 uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber));
150 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
151 uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
157 uint64_t value = endian::native_to_big(varNumber);
167 if (varNumber < 253) {
171 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
173 uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber));
177 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
179 uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
185 uint64_t value = endian::native_to_big(varNumber);
194 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
195 return prependByte(static_cast<uint8_t>(varNumber));
197 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
198 uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber));
201 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
202 uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
206 uint64_t value = endian::native_to_big(varNumber);
214 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
215 return appendByte(static_cast<uint8_t>(varNumber));
217 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
218 uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber));
221 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
222 uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
226 uint64_t value = endian::native_to_big(varNumber);
size_t prependByteArray(const uint8_t *array, size_t length)
Prepend a byte array array of length length.
void reserveFront(size_t size)
Reserve at least isze bytes at the beginning of the underlying buffer.
size_t appendVarNumber(uint64_t varNumber)
Prepend VarNumber varNumber of NDN TLV encoding.
bool hasWire() const
Check if the Block has fully encoded wire.
Copyright (c) 2011-2015 Regents of the University of California.
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
const uint8_t * wire() const
Get pointer to encoded wire.
size_t prependByte(uint8_t value)
Prepend a byte.
Represents a TLV element of NDN packet format.
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
const uint8_t * value() const
Get pointer to TLV-VALUE.
size_t size() const noexcept
Get the size of the encoded buffer.
void reserveBack(size_t size)
Reserve at least size bytes at the back of the underlying buffer.
size_t prependVarNumber(uint64_t varNumber)
Prepend VarNumber varNumber of NDN TLV encoding.
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
size_t prependBlock(const Block &block)
Prepend TLV block block.
size_t appendBlock(const Block &block)
Append TLV block block.
size_t appendByte(uint8_t value)
Append a byte.
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.
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
uint8_t * buf()
Get a pointer to the first byte of the encoded buffer.
size_t prependNonNegativeInteger(uint64_t integer)
Prepend non-negative integer integer of NDN TLV encoding.
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 appendNonNegativeInteger(uint64_t integer)
Append non-negative integer integer of NDN TLV encoding.
General-purpose automatically managed/resized buffer.
Encoder(size_t totalReserve=MAX_NDN_PACKET_SIZE, size_t reserveFromBack=400)
Create instance of the encoder with the specified reserved sizes.
uint32_t type() const
Get TLV-TYPE.