Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 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 "encoder.hpp" |
| 23 | |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 24 | #include <boost/endian/conversion.hpp> |
| 25 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 26 | namespace ndn { |
| 27 | namespace encoding { |
| 28 | |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 29 | namespace endian = boost::endian; |
| 30 | |
| 31 | Encoder::Encoder(size_t totalReserve, size_t reserveFromBack) |
| 32 | : m_buffer(make_shared<Buffer>(totalReserve)) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 33 | { |
| 34 | m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0); |
| 35 | } |
| 36 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 37 | Encoder::Encoder(const Block& block) |
| 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())) |
| 41 | { |
| 42 | } |
| 43 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 44 | void |
| 45 | Encoder::reserveBack(size_t size) |
| 46 | { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 47 | if (m_end + size > m_buffer->end()) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 48 | reserve(m_buffer->size() * 2 + size, false); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | Encoder::reserveFront(size_t size) |
| 53 | { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 54 | if (m_buffer->begin() + size > m_begin) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 55 | reserve(m_buffer->size() * 2 + size, true); |
| 56 | } |
| 57 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 58 | Block |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 59 | Encoder::block(bool verifyLength) const |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 60 | { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 61 | return Block(m_buffer, m_begin, m_end, verifyLength); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
| 65 | Encoder::reserve(size_t size, bool addInFront) |
| 66 | { |
| 67 | if (size < m_buffer->size()) { |
| 68 | size = m_buffer->size(); |
| 69 | } |
| 70 | |
| 71 | if (addInFront) { |
| 72 | size_t diffEnd = m_buffer->end() - m_end; |
| 73 | size_t diffBegin = m_buffer->end() - m_begin; |
| 74 | |
| 75 | Buffer* buf = new Buffer(size); |
| 76 | std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end()); |
| 77 | |
| 78 | m_buffer.reset(buf); |
| 79 | |
| 80 | m_end = m_buffer->end() - diffEnd; |
| 81 | m_begin = m_buffer->end() - diffBegin; |
| 82 | } |
| 83 | else { |
| 84 | size_t diffEnd = m_end - m_buffer->begin(); |
| 85 | size_t diffBegin = m_begin - m_buffer->begin(); |
| 86 | |
| 87 | Buffer* buf = new Buffer(size); |
| 88 | std::copy(m_buffer->begin(), m_buffer->end(), buf->begin()); |
| 89 | |
| 90 | m_buffer.reset(buf); |
| 91 | |
| 92 | m_end = m_buffer->begin() + diffEnd; |
| 93 | m_begin = m_buffer->begin() + diffBegin; |
| 94 | } |
| 95 | } |
| 96 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 97 | size_t |
| 98 | Encoder::prependByte(uint8_t value) |
| 99 | { |
| 100 | reserveFront(1); |
| 101 | |
| 102 | m_begin--; |
| 103 | *m_begin = value; |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | size_t |
| 108 | Encoder::appendByte(uint8_t value) |
| 109 | { |
| 110 | reserveBack(1); |
| 111 | |
| 112 | *m_end = value; |
| 113 | m_end++; |
| 114 | return 1; |
| 115 | } |
| 116 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 117 | size_t |
| 118 | Encoder::prependByteArray(const uint8_t* array, size_t length) |
| 119 | { |
| 120 | reserveFront(length); |
| 121 | |
| 122 | m_begin -= length; |
| 123 | std::copy(array, array + length, m_begin); |
| 124 | return length; |
| 125 | } |
| 126 | |
| 127 | size_t |
| 128 | Encoder::appendByteArray(const uint8_t* array, size_t length) |
| 129 | { |
| 130 | reserveBack(length); |
| 131 | |
| 132 | std::copy(array, array + length, m_end); |
| 133 | m_end += length; |
| 134 | return length; |
| 135 | } |
| 136 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 137 | size_t |
| 138 | Encoder::prependVarNumber(uint64_t varNumber) |
| 139 | { |
| 140 | if (varNumber < 253) { |
| 141 | prependByte(static_cast<uint8_t>(varNumber)); |
| 142 | return 1; |
| 143 | } |
| 144 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 145 | uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 146 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
| 147 | prependByte(253); |
| 148 | return 3; |
| 149 | } |
| 150 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 151 | uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 152 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
| 153 | prependByte(254); |
| 154 | return 5; |
| 155 | } |
| 156 | else { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 157 | uint64_t value = endian::native_to_big(varNumber); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 158 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
| 159 | prependByte(255); |
| 160 | return 9; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | size_t |
| 165 | Encoder::appendVarNumber(uint64_t varNumber) |
| 166 | { |
| 167 | if (varNumber < 253) { |
| 168 | appendByte(static_cast<uint8_t>(varNumber)); |
| 169 | return 1; |
| 170 | } |
| 171 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 172 | appendByte(253); |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 173 | uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 174 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
| 175 | return 3; |
| 176 | } |
| 177 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 178 | appendByte(254); |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 179 | uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 180 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
| 181 | return 5; |
| 182 | } |
| 183 | else { |
| 184 | appendByte(255); |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 185 | uint64_t value = endian::native_to_big(varNumber); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 186 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
| 187 | return 9; |
| 188 | } |
| 189 | } |
| 190 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 191 | size_t |
| 192 | Encoder::prependNonNegativeInteger(uint64_t varNumber) |
| 193 | { |
| 194 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
| 195 | return prependByte(static_cast<uint8_t>(varNumber)); |
| 196 | } |
| 197 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 198 | uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 199 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
| 200 | } |
| 201 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 202 | uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 203 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
| 204 | } |
| 205 | else { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 206 | uint64_t value = endian::native_to_big(varNumber); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 207 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | size_t |
| 212 | Encoder::appendNonNegativeInteger(uint64_t varNumber) |
| 213 | { |
| 214 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
| 215 | return appendByte(static_cast<uint8_t>(varNumber)); |
| 216 | } |
| 217 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 218 | uint16_t value = endian::native_to_big(static_cast<uint16_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 219 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
| 220 | } |
| 221 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 222 | uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 223 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
| 224 | } |
| 225 | else { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame^] | 226 | uint64_t value = endian::native_to_big(varNumber); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 227 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | size_t |
| 232 | Encoder::prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) |
| 233 | { |
| 234 | size_t totalLength = prependByteArray(array, arraySize); |
| 235 | totalLength += prependVarNumber(arraySize); |
| 236 | totalLength += prependVarNumber(type); |
| 237 | |
| 238 | return totalLength; |
| 239 | } |
| 240 | |
| 241 | size_t |
| 242 | Encoder::appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) |
| 243 | { |
| 244 | size_t totalLength = appendVarNumber(type); |
| 245 | totalLength += appendVarNumber(arraySize); |
| 246 | totalLength += appendByteArray(array, arraySize); |
| 247 | |
| 248 | return totalLength; |
| 249 | } |
| 250 | |
| 251 | size_t |
| 252 | Encoder::prependBlock(const Block& block) |
| 253 | { |
| 254 | if (block.hasWire()) { |
| 255 | return prependByteArray(block.wire(), block.size()); |
| 256 | } |
| 257 | else { |
| 258 | return prependByteArrayBlock(block.type(), block.value(), block.value_size()); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | size_t |
| 263 | Encoder::appendBlock(const Block& block) |
| 264 | { |
| 265 | if (block.hasWire()) { |
| 266 | return appendByteArray(block.wire(), block.size()); |
| 267 | } |
| 268 | else { |
| 269 | return appendByteArrayBlock(block.type(), block.value(), block.value_size()); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | } // namespace encoding |
| 274 | } // namespace ndn |