Use the boost endian library for storing/retrieving size

When serializing/deserializing the size, use the boost endian
library and its built-in conversion instead of defining custom
functions.

Signed-off-by: Kun Yi <kunyi@google.com>
Change-Id: I903a492d8d5fa5deb87a3a78e31a57317db39047
diff --git a/Makefile.am b/Makefile.am
index 2ad19a0..33d8ac5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -6,6 +6,11 @@
 AM_DEFAULT_SOURCE_EXT = .cpp
 SUFFIXES = .proto .pb.cc
 
+BOOST_CXX = \
+	-DBOOST_ERROR_CODE_HEADER_ONLY \
+	-DBOOST_SYSTEM_NO_DEPRECATED \
+	-DBOOST_ALL_NO_LIB
+
 libbinarystoredir = ${libdir}/ipmid-providers
 libbinarystore_LTLIBRARIES = libbinarystore.la
 libbinarystore_la_SOURCES = main.cpp \
@@ -20,6 +25,7 @@
 			    -lprotobuf
 
 libbinarystore_la_CXXFLAGS = -I$(srcdir) $(PHOSPHOR_LOGGING_CFLAGS) \
+			     $(BOOST_CXX) \
 			     -flto
 
 binaryblob.pb.h binaryblob.pb.cc: $(srcdir)/$(PROTOS_PATH)/binaryblob.proto
diff --git a/binarystore.cpp b/binarystore.cpp
index 22e2e48..dff423e 100644
--- a/binarystore.cpp
+++ b/binarystore.cpp
@@ -4,6 +4,7 @@
 
 #include <algorithm>
 #include <blobs-ipmid/blobs.hpp>
+#include <boost/endian/arithmetic.hpp>
 #include <cstdint>
 #include <memory>
 #include <string>
@@ -20,42 +21,6 @@
 namespace binstore
 {
 
-namespace internal
-{
-
-/* Helper methods to interconvert an uint64_t and bytes, LSB first.
-   Convert bytes to uint64. Input should be exactly 8 bytes. */
-uint64_t bytesToUint64(const std::string& bytes)
-{
-    if (bytes.size() != sizeof(uint64_t))
-    {
-        return 0;
-    }
-
-    return static_cast<uint64_t>(bytes[7]) << 56 |
-           static_cast<uint64_t>(bytes[6]) << 48 |
-           static_cast<uint64_t>(bytes[5]) << 40 |
-           static_cast<uint64_t>(bytes[4]) << 32 |
-           static_cast<uint64_t>(bytes[3]) << 24 |
-           static_cast<uint64_t>(bytes[2]) << 16 |
-           static_cast<uint64_t>(bytes[1]) << 8 |
-           static_cast<uint64_t>(bytes[0]);
-}
-
-/* Convert uint64 to bytes, LSB first. */
-std::string uint64ToBytes(uint64_t num)
-{
-    std::string result;
-    for (size_t i = 0; i < sizeof(uint64_t); ++i)
-    {
-        result += static_cast<char>(num & 0xff);
-        num >>= 8;
-    }
-    return result;
-}
-
-} // namespace internal
-
 std::unique_ptr<BinaryStoreInterface>
     BinaryStore::createFromConfig(const std::string& baseBlobId,
                                   std::unique_ptr<SysFile> file,
@@ -114,8 +79,9 @@
         try
         {
             /* Parse length-prefixed format to protobuf */
-            auto size =
-                internal::bytesToUint64(file_->readAsStr(0, sizeof(uint64_t)));
+            boost::endian::little_uint64_t size = 0;
+            file_->readToBuf(0, sizeof(size), reinterpret_cast<char*>(&size));
+
             if (!blob_.ParseFromString(
                     file_->readAsStr(sizeof(uint64_t), size)))
             {
@@ -213,9 +179,10 @@
 
 bool BinaryStore::commit()
 {
-
+    /* Store as little endian to be platform agnostic. Consistent with read. */
     auto blobData = blob_.SerializeAsString();
-    std::string commitData(internal::uint64ToBytes((uint64_t)blobData.size()));
+    boost::endian::little_uint64_t sizeLE = blobData.size();
+    std::string commitData(sizeLE.data(), sizeof(sizeLE));
     commitData += blobData;
 
     try
@@ -224,7 +191,6 @@
     }
     catch (const std::exception& e)
     {
-        // TODO: logging
         commitState_ = CommitState::CommitError;
         return false;
     };
diff --git a/configure.ac b/configure.ac
index f41f662..2b09776 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,6 +23,7 @@
 PKG_CHECK_MODULES([PROTOBUF], [protobuf], [], [AC_MSG_ERROR(["protobuf required and not found"])])
 PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging],, [AC_MSG_ERROR([Could not find phosphor-logging...openbmc/phosphor-logging package required])])
 AC_CHECK_HEADER([blobs-ipmid], [AC_MSG_ERROR(["phosphor-ipmi-blobs required and not found."])])
+AC_CHECK_HEADER(boost/endian/arithmetic.hpp, [])
 
 # Check/set gtest specific functions.
 PKG_CHECK_MODULES([GTEST], [gtest], [], [AC_MSG_NOTICE([gtest not found, tests will not build])])