encoding: use Boost.Endian for endianness conversions

Change-Id: I1ecaa4c4efa7cc50486b1e8403f049a9ee33156f
diff --git a/src/encoding/encoder.cpp b/src/encoding/encoder.cpp
index 12b5ea1..1fdddec 100644
--- a/src/encoding/encoder.cpp
+++ b/src/encoding/encoder.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -21,16 +21,19 @@
 
 #include "encoder.hpp"
 
+#include <boost/endian/conversion.hpp>
+
 namespace ndn {
 namespace encoding {
 
-Encoder::Encoder(size_t totalReserve/* = MAX_NDN_PACKET_SIZE*/, size_t reserveFromBack/* = 400*/)
-  : m_buffer(new Buffer(totalReserve))
+namespace endian = boost::endian;
+
+Encoder::Encoder(size_t totalReserve, size_t reserveFromBack)
+  : m_buffer(make_shared<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()))
@@ -38,30 +41,24 @@
 {
 }
 
-////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-
 void
 Encoder::reserveBack(size_t size)
 {
-  if ((m_end + size) > m_buffer->end())
+  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)
+  if (m_buffer->begin() + size > m_begin)
     reserve(m_buffer->size() * 2 + size, true);
 }
 
-
 Block
-Encoder::block(bool verifyLength/* = true*/) const
+Encoder::block(bool verifyLength) const
 {
-  return Block(m_buffer,
-               m_begin, m_end,
-               verifyLength);
+  return Block(m_buffer, m_begin, m_end, verifyLength);
 }
 
 void
@@ -97,9 +94,6 @@
   }
 }
 
-////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-
 size_t
 Encoder::prependByte(uint8_t value)
 {
@@ -120,7 +114,6 @@
   return 1;
 }
 
-
 size_t
 Encoder::prependByteArray(const uint8_t* array, size_t length)
 {
@@ -141,7 +134,6 @@
   return length;
 }
 
-
 size_t
 Encoder::prependVarNumber(uint64_t varNumber)
 {
@@ -150,19 +142,19 @@
     return 1;
   }
   else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
-    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    uint16_t value = endian::native_to_big(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));
+    uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
     prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
     prependByte(254);
     return 5;
   }
   else {
-    uint64_t value = htobe64(varNumber);
+    uint64_t value = endian::native_to_big(varNumber);
     prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
     prependByte(255);
     return 9;
@@ -178,25 +170,24 @@
   }
   else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
     appendByte(253);
-    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
+    uint16_t value = endian::native_to_big(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));
+    uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
     appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
     return 5;
   }
   else {
     appendByte(255);
-    uint64_t value = htobe64(varNumber);
+    uint64_t value = endian::native_to_big(varNumber);
     appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
     return 9;
   }
 }
 
-
 size_t
 Encoder::prependNonNegativeInteger(uint64_t varNumber)
 {
@@ -204,15 +195,15 @@
     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));
+    uint16_t value = endian::native_to_big(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));
+    uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
     return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
   }
   else {
-    uint64_t value = htobe64(varNumber);
+    uint64_t value = endian::native_to_big(varNumber);
     return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
   }
 }
@@ -224,15 +215,15 @@
     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));
+    uint16_t value = endian::native_to_big(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));
+    uint32_t value = endian::native_to_big(static_cast<uint32_t>(varNumber));
     return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
   }
   else {
-    uint64_t value = htobe64(varNumber);
+    uint64_t value = endian::native_to_big(varNumber);
     return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
   }
 }
diff --git a/src/encoding/endian.hpp b/src/encoding/endian.hpp
deleted file mode 100644
index e37fa98..0000000
--- a/src/encoding/endian.hpp
+++ /dev/null
@@ -1,57 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2014 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.
- *
- * @author Junxiao Shi <http://www.cs.arizona.edu/people/shijunxiao/>
- */
-
-#ifndef NDN_ENCODING_ENDIAN_HPP
-#define NDN_ENCODING_ENDIAN_HPP
-
-#ifdef __linux__
-
-#include <endian.h>
-
-#endif // __linux__
-
-#ifdef __FreeBSD__
-
-#include <sys/endian.h>
-
-#endif // __FreeBSD__
-
-#ifdef __APPLE__
-
-#include <libkern/OSByteOrder.h>
-#define htobe16(x) OSSwapHostToBigInt16(x)
-#define htole16(x) OSSwapHostToLittleInt16(x)
-#define be16toh(x) OSSwapBigToHostInt16(x)
-#define le16toh(x) OSSwapLittleToHostInt16(x)
-#define htobe32(x) OSSwapHostToBigInt32(x)
-#define htole32(x) OSSwapHostToLittleInt32(x)
-#define be32toh(x) OSSwapBigToHostInt32(x)
-#define le32toh(x) OSSwapLittleToHostInt32(x)
-#define htobe64(x) OSSwapHostToBigInt64(x)
-#define htole64(x) OSSwapHostToLittleInt64(x)
-#define be64toh(x) OSSwapBigToHostInt64(x)
-#define le64toh(x) OSSwapLittleToHostInt64(x)
-
-#endif // __APPLE__
-
-#endif // NDN_ENCODING_ENDIAN_HPP
diff --git a/src/encoding/tlv.hpp b/src/encoding/tlv.hpp
index 6a14a7c..5fb2b24 100644
--- a/src/encoding/tlv.hpp
+++ b/src/encoding/tlv.hpp
@@ -23,13 +23,14 @@
 #define NDN_ENCODING_TLV_HPP
 
 #include "buffer.hpp"
-#include "endian.hpp"
 
 #include <cstring>
 #include <iterator>
 #include <ostream>
 #include <type_traits>
 
+#include <boost/endian/conversion.hpp>
+
 namespace ndn {
 
 /** @brief practical limit of network layer packet size
@@ -323,21 +324,21 @@
         uint16_t value = 0;
         std::memcpy(&value, &*begin, 2);
         begin += 2;
-        number = be16toh(value);
+        number = boost::endian::big_to_native(value);
         return true;
       }
       case 4: {
         uint32_t value = 0;
         std::memcpy(&value, &*begin, 4);
         begin += 4;
-        number = be32toh(value);
+        number = boost::endian::big_to_native(value);
         return true;
       }
       case 8: {
         uint64_t value = 0;
         std::memcpy(&value, &*begin, 8);
         begin += 8;
-        number = be64toh(value);
+        number = boost::endian::big_to_native(value);
         return true;
       }
       default: {
@@ -452,19 +453,19 @@
   }
   else if (number <= std::numeric_limits<uint16_t>::max()) {
     os.put(static_cast<char>(253));
-    uint16_t value = htobe16(static_cast<uint16_t>(number));
+    uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(number));
     os.write(reinterpret_cast<const char*>(&value), 2);
     return 3;
   }
   else if (number <= std::numeric_limits<uint32_t>::max()) {
     os.put(static_cast<char>(254));
-    uint32_t value = htobe32(static_cast<uint32_t>(number));
+    uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(number));
     os.write(reinterpret_cast<const char*>(&value), 4);
     return 5;
   }
   else {
     os.put(static_cast<char>(255));
-    uint64_t value = htobe64(number);
+    uint64_t value = boost::endian::native_to_big(number);
     os.write(reinterpret_cast<const char*>(&value), 8);
     return 9;
   }
@@ -504,17 +505,17 @@
     return 1;
   }
   else if (integer <= std::numeric_limits<uint16_t>::max()) {
-    uint16_t value = htobe16(static_cast<uint16_t>(integer));
+    uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(integer));
     os.write(reinterpret_cast<const char*>(&value), 2);
     return 2;
   }
   else if (integer <= std::numeric_limits<uint32_t>::max()) {
-    uint32_t value = htobe32(static_cast<uint32_t>(integer));
+    uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(integer));
     os.write(reinterpret_cast<const char*>(&value), 4);
     return 4;
   }
   else {
-    uint64_t value = htobe64(integer);
+    uint64_t value = boost::endian::native_to_big(integer);
     os.write(reinterpret_cast<const char*>(&value), 8);
     return 8;
   }
diff --git a/src/lp/field-decl.hpp b/src/lp/field-decl.hpp
index 26336ff..d1c39f0 100644
--- a/src/lp/field-decl.hpp
+++ b/src/lp/field-decl.hpp
@@ -26,10 +26,11 @@
 #include "field.hpp"
 #include "sequence.hpp"
 #include "tlv.hpp"
-
 #include "../encoding/block-helpers.hpp"
 #include "../util/concepts.hpp"
+
 #include <boost/concept/requires.hpp>
+#include <boost/endian/conversion.hpp>
 
 namespace ndn {
 namespace lp {
@@ -145,9 +146,9 @@
   static size_t
   encode(EncodingImpl<TAG>& encoder, uint64_t value)
   {
-    uint64_t be = htobe64(value);
-    const uint8_t* buf = reinterpret_cast<const uint8_t*>(&be);
-    return encoder.prependByteArrayBlock(TlvType::value, buf, sizeof(be));
+    boost::endian::native_to_big_inplace(value);
+    return encoder.prependByteArrayBlock(TlvType::value,
+                                         reinterpret_cast<const uint8_t*>(&value), sizeof(value));
   }
 };
 
diff --git a/tests/unit-tests/util/sha256.t.cpp b/tests/unit-tests/util/sha256.t.cpp
index 5775785..094f5f6 100644
--- a/tests/unit-tests/util/sha256.t.cpp
+++ b/tests/unit-tests/util/sha256.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -21,10 +21,10 @@
 
 #include "util/sha256.hpp"
 #include "util/string-helper.hpp"
-#include "encoding/endian.hpp"
 
 #include "boost-test.hpp"
 
+#include <boost/endian/conversion.hpp>
 #include <sstream>
 
 namespace ndn {
@@ -149,7 +149,7 @@
 
   Sha256 statefulSha256;
   for (size_t i = 0; i < sizeof(input) / sizeof(uint64_t); ++i) {
-    statefulSha256 << htobe64(input[i]);
+    statefulSha256 << boost::endian::native_to_big(input[i]);
   }
   ConstBufferPtr digest = statefulSha256.computeDigest();