encoding: Refactoring EncodingBuffer

Breaks: nfd:commit:c0273e3505ac2ccf843401be77a513d8eb663127
Breaks: ChronoSync:commit:e042f83a1df184a8e7a90ef00034d11026891cd1

Change-Id: I8275c6276c5ecfa280f87f584189907521febf5f
Refs: #2494, #2490
diff --git a/src/encoding/encoder.cpp b/src/encoding/encoder.cpp
new file mode 100644
index 0000000..4b32c10
--- /dev/null
+++ b/src/encoding/encoder.cpp
@@ -0,0 +1,283 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "encoder.hpp"
+
+namespace ndn {
+namespace encoding {
+
+Encoder::Encoder(size_t totalReserve/* = 8800*/, size_t reserveFromBack/* = 400*/)
+  : m_buffer(new Buffer(totalReserve))
+{
+  m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0);
+}
+
+
+Encoder::Encoder(const Block& block)
+  : m_buffer(const_pointer_cast<Buffer>(block.getBuffer()))
+  , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin()))
+  , m_end(m_buffer->begin()   + (block.end()   - m_buffer->begin()))
+{
+}
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+void
+Encoder::reserveBack(size_t size)
+{
+  if ((m_end + size) > m_buffer->end())
+    reserve(m_buffer->size() * 2 + size, false);
+}
+
+void
+Encoder::reserveFront(size_t size)
+{
+  if ((m_buffer->begin() + size) > m_begin)
+    reserve(m_buffer->size() * 2 + size, true);
+}
+
+
+Block
+Encoder::block(bool verifyLength/* = true*/) const
+{
+  return Block(m_buffer,
+               m_begin, m_end,
+               verifyLength);
+}
+
+void
+Encoder::reserve(size_t size, bool addInFront)
+{
+  if (size < m_buffer->size()) {
+    size = m_buffer->size();
+  }
+
+  if (addInFront) {
+    size_t diffEnd = m_buffer->end() - m_end;
+    size_t diffBegin = m_buffer->end() - m_begin;
+
+    Buffer* buf = new Buffer(size);
+    std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end());
+
+    m_buffer.reset(buf);
+
+    m_end = m_buffer->end() - diffEnd;
+    m_begin = m_buffer->end() - diffBegin;
+  }
+  else {
+    size_t diffEnd = m_end - m_buffer->begin();
+    size_t diffBegin = m_begin - m_buffer->begin();
+
+    Buffer* buf = new Buffer(size);
+    std::copy(m_buffer->begin(), m_buffer->end(), buf->begin());
+
+    m_buffer.reset(buf);
+
+    m_end = m_buffer->begin() + diffEnd;
+    m_begin = m_buffer->begin() + diffBegin;
+  }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+size_t
+Encoder::prependByte(uint8_t value)
+{
+  reserveFront(1);
+
+  m_begin--;
+  *m_begin = value;
+  return 1;
+}
+
+size_t
+Encoder::appendByte(uint8_t value)
+{
+  reserveBack(1);
+
+  *m_end = value;
+  m_end++;
+  return 1;
+}
+
+
+size_t
+Encoder::prependByteArray(const uint8_t* array, size_t length)
+{
+  reserveFront(length);
+
+  m_begin -= length;
+  std::copy(array, array + length, m_begin);
+  return length;
+}
+
+size_t
+Encoder::appendByteArray(const uint8_t* array, size_t length)
+{
+  reserveBack(length);
+
+  std::copy(array, array + length, m_end);
+  m_end += length;
+  return length;
+}
+
+
+size_t
+Encoder::prependVarNumber(uint64_t varNumber)
+{
+  if (varNumber < 253) {
+    prependByte(static_cast<uint8_t>(varNumber));
+    return 1;
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+    prependByte(253);
+    return 3;
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+    prependByte(254);
+    return 5;
+  }
+  else {
+    uint64_t value = htobe64(varNumber);
+    prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+    prependByte(255);
+    return 9;
+  }
+}
+
+size_t
+Encoder::appendVarNumber(uint64_t varNumber)
+{
+  if (varNumber < 253) {
+    appendByte(static_cast<uint8_t>(varNumber));
+    return 1;
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    appendByte(253);
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+    return 3;
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    appendByte(254);
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+    return 5;
+  }
+  else {
+    appendByte(255);
+    uint64_t value = htobe64(varNumber);
+    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+    return 9;
+  }
+}
+
+
+size_t
+Encoder::prependNonNegativeInteger(uint64_t varNumber)
+{
+  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
+    return prependByte(static_cast<uint8_t>(varNumber));
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+  }
+  else {
+    uint64_t value = htobe64(varNumber);
+    return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+  }
+}
+
+size_t
+Encoder::appendNonNegativeInteger(uint64_t varNumber)
+{
+  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
+    return appendByte(static_cast<uint8_t>(varNumber));
+  }
+  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
+    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
+  }
+  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
+    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
+    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
+  }
+  else {
+    uint64_t value = htobe64(varNumber);
+    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
+  }
+}
+
+size_t
+Encoder::prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize)
+{
+  size_t totalLength = prependByteArray(array, arraySize);
+  totalLength += prependVarNumber(arraySize);
+  totalLength += prependVarNumber(type);
+
+  return totalLength;
+}
+
+size_t
+Encoder::appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize)
+{
+  size_t totalLength = appendVarNumber(type);
+  totalLength += appendVarNumber(arraySize);
+  totalLength += appendByteArray(array, arraySize);
+
+  return totalLength;
+}
+
+size_t
+Encoder::prependBlock(const Block& block)
+{
+  if (block.hasWire()) {
+    return prependByteArray(block.wire(), block.size());
+  }
+  else {
+    return prependByteArrayBlock(block.type(), block.value(), block.value_size());
+  }
+}
+
+size_t
+Encoder::appendBlock(const Block& block)
+{
+  if (block.hasWire()) {
+    return appendByteArray(block.wire(), block.size());
+  }
+  else {
+    return appendByteArrayBlock(block.type(), block.value(), block.value_size());
+  }
+}
+
+} // namespace encoding
+} // namespace ndn