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);
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.
iterator begin()
Get an iterator pointing to the first byte of the encoded buffer.
size_t prependByte(uint8_t value)
Prepend a byte.
iterator end()
Get an iterator pointing to the past-the-end byte of the encoded buffer.
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Represents a TLV element of NDN packet format.
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
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 size() const
Return the size of the encoded wire, i.e.
size_t prependVarNumber(uint64_t varNumber)
Prepend VarNumber varNumber of NDN TLV encoding.
size_t prependBlock(const Block &block)
Prepend TLV block block.
size_t appendBlock(const Block &block)
Append TLV block block.
shared_ptr< Buffer > getBuffer() const noexcept
Get underlying buffer.
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
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.
uint32_t type() const
Return the TLV-TYPE of the Block.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
uint8_t * buf()
Get a pointer to the first byte of the encoded buffer.
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
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.