24 #include <boost/endian/conversion.hpp>
28 namespace endian = boost::endian;
31 : m_buffer(make_shared<
Buffer>(totalReserve))
33 m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0);
37 : m_buffer(std::const_pointer_cast<
Buffer>(block.getBuffer()))
38 , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin()))
39 , m_end(m_buffer->begin() + (block.end() - m_buffer->begin()))
46 if (m_end +
size > m_buffer->end())
53 if (m_buffer->begin() +
size > m_begin)
60 return Block(m_buffer, m_begin, m_end, verifyLength);
66 if (size < m_buffer->
size()) {
67 size = m_buffer->size();
71 size_t diffEnd = m_buffer->end() - m_end;
72 size_t diffBegin = m_buffer->end() - m_begin;
75 std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end());
79 m_end = m_buffer->end() - diffEnd;
80 m_begin = m_buffer->end() - diffBegin;
83 size_t diffEnd = m_end - m_buffer->begin();
84 size_t diffBegin = m_begin - m_buffer->begin();
87 std::copy(m_buffer->begin(), m_buffer->end(), buf->begin());
91 m_end = m_buffer->begin() + diffEnd;
92 m_begin = m_buffer->begin() + diffBegin;
111 if (varNumber < 253) {
115 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
116 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
117 prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
121 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
122 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
123 prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
128 uint64_t value = endian::native_to_big(varNumber);
129 prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
138 if (varNumber < 253) {
142 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
144 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
145 appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
148 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
150 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
151 appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
156 uint64_t value = endian::native_to_big(varNumber);
157 appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
165 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
168 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
169 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
170 return prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
172 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
173 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
174 return prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
177 uint64_t value = endian::native_to_big(varNumber);
178 return prependBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
185 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
186 return appendBytes({
static_cast<uint8_t
>(varNumber)});
188 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
189 uint16_t value = endian::native_to_big(
static_cast<uint16_t
>(varNumber));
190 return appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 2});
192 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
193 uint32_t value = endian::native_to_big(
static_cast<uint32_t
>(varNumber));
194 return appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 4});
197 uint64_t value = endian::native_to_big(varNumber);
198 return appendBytes({
reinterpret_cast<const uint8_t*
>(&value), 8});
Represents a TLV element of the NDN packet format.
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 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.