util: Replace raw struct functions with stdplus

These functions were recently added to stdplus as they are useful across
projects.

Change-Id: Ia383715a253fce71dacc6bc72b1771393e8ed600
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/Makefile.am b/Makefile.am
index 3bb5e7b..3649ca8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -87,6 +87,7 @@
 		$(SYSTEMD_LIBS) \
 		$(SDBUSPLUS_LIBS) \
 		$(SDEVENTPLUS_LIBS) \
+		$(STDPLUS_LIBS) \
 		$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 		$(PHOSPHOR_LOGGING_LIBS) \
 		-lstdc++fs
@@ -95,6 +96,7 @@
 		$(SYSTEMD_CFLAGS) \
 		$(SDBUSPLUS_CFLAGS) \
 		$(SDEVENTPLUS_CFLAGS) \
+		$(STDPLUS_CFLAGS) \
 		$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
 		$(PHOSPHOR_LOGGING_CFLAGS) \
 		-flto
diff --git a/configure.ac b/configure.ac
index ee783e2..1dc2bf0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -32,6 +32,7 @@
 PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221])
 PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus])
 PKG_CHECK_MODULES([SDEVENTPLUS], [sdeventplus])
+PKG_CHECK_MODULES([STDPLUS], [stdplus])
 AC_PATH_PROG([SDBUSPLUSPLUS], [sdbus++])
 PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging])
 PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces])
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index f7343ad..2c8bbd4 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -24,6 +24,7 @@
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
 #include <sstream>
+#include <stdplus/raw.hpp>
 #include <string>
 #include <string_view>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -325,7 +326,7 @@
     static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= sizeof(ether_addr));
     std::string_view hwaddr(reinterpret_cast<char*>(ifr.ifr_hwaddr.sa_data),
                             sizeof(ifr.ifr_hwaddr.sa_data));
-    return mac_address::toString(copyFrom<ether_addr>(hwaddr));
+    return mac_address::toString(stdplus::raw::copyFrom<ether_addr>(hwaddr));
 }
 
 std::string EthernetInterface::generateId(const std::string& ipaddress,
@@ -945,7 +946,7 @@
 
     // We don't need to update the system if the address is unchanged
     ether_addr oldMAC = mac_address::fromString(MacAddressIntf::mACAddress());
-    if (!equal(newMAC, oldMAC))
+    if (!stdplus::raw::equal(newMAC, oldMAC))
     {
         if (!mac_address::isLocalAdmin(newMAC))
         {
@@ -953,7 +954,7 @@
             {
                 auto inventoryMAC =
                     mac_address::getfromInventory(bus, interfaceName());
-                if (!equal(newMAC, inventoryMAC))
+                if (!stdplus::raw::equal(newMAC, inventoryMAC))
                 {
                     log<level::ERR>(
                         "Given MAC address is neither a local Admin "
diff --git a/neighbor.cpp b/neighbor.cpp
index 717719c..dc6a581 100644
--- a/neighbor.cpp
+++ b/neighbor.cpp
@@ -14,6 +14,7 @@
 #include <sys/types.h>
 
 #include <stdexcept>
+#include <stdplus/raw.hpp>
 #include <string_view>
 #include <utility>
 #include <vector>
@@ -32,7 +33,7 @@
     {
         throw std::runtime_error("Not a neighbor msg");
     }
-    auto ndm = extract<ndmsg>(msg, "Bad neighbor msg");
+    auto ndm = stdplus::raw::extract<ndmsg>(msg);
 
     // Filter out neighbors we don't care about
     unsigned ifindex = ndm.ndm_ifindex;
@@ -55,7 +56,7 @@
         auto [hdr, data] = netlink::extractRtAttr(msg);
         if (hdr.rta_type == NDA_LLADDR)
         {
-            neighbor.mac = copyFrom<ether_addr>(data, "Bad neighbor MAC");
+            neighbor.mac = stdplus::raw::copyFrom<ether_addr>(data);
         }
         else if (hdr.rta_type == NDA_DST)
         {
diff --git a/netlink.cpp b/netlink.cpp
index 947b828..9039575 100644
--- a/netlink.cpp
+++ b/netlink.cpp
@@ -9,6 +9,7 @@
 
 #include <array>
 #include <stdexcept>
+#include <stdplus/raw.hpp>
 #include <system_error>
 
 namespace phosphor
@@ -23,7 +24,7 @@
 void processMsg(std::string_view& msgs, bool& done, const ReceiveCallback& cb)
 {
     // Parse and update the message buffer
-    auto hdr = copyFrom<nlmsghdr>(msgs, "Bad netlink header");
+    auto hdr = stdplus::raw::copyFrom<nlmsghdr>(msgs);
     if (hdr.nlmsg_len < sizeof(hdr))
     {
         throw std::runtime_error("Invalid nlmsg length");
@@ -56,7 +57,7 @@
     }
     else if (hdr.nlmsg_type == NLMSG_ERROR)
     {
-        auto err = copyFrom<nlmsgerr>(msg, "Bad netlink error");
+        auto err = stdplus::raw::copyFrom<nlmsgerr>(msg);
         // This is just an ACK so don't do the callback
         if (err.error <= 0)
         {
@@ -175,7 +176,7 @@
 
 std::tuple<rtattr, std::string_view> extractRtAttr(std::string_view& data)
 {
-    auto hdr = copyFrom<rtattr>(data, "Bad rtattr header");
+    auto hdr = stdplus::raw::copyFrom<rtattr>(data);
     if (hdr.rta_len < RTA_LENGTH(0))
     {
         throw std::runtime_error("Invalid rtattr length");
diff --git a/test/Makefile.am b/test/Makefile.am
index 45dab5f..d728099 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,6 +21,7 @@
 					$(SYSTEMD_CFLAGS) \
 					$(SDBUSPLUS_CFLAGS) \
 					$(SDEVENTPLUS_CFLAGS) \
+					$(STDPLUS_CFLAGS) \
 					$(PHOSPHOR_LOGGING_CFLAGS) \
 					$(PHOSPHOR_DBUS_INTERFACES_CFLAGS)
 
@@ -29,6 +30,7 @@
 					$(SYSTEMD_LIBS) \
 					$(SDBUSPLUS_LIBS) \
 					$(SDEVENTPLUS_LIBS) \
+					$(STDPLUS_LIBS) \
 					$(PHOSPHOR_LOGGING_LIBS) \
 					$(PHOSPHOR_DBUS_INTERFACES_LIBS)
 
diff --git a/test/mock_syscall.cpp b/test/mock_syscall.cpp
index 5493c58..665094d 100644
--- a/test/mock_syscall.cpp
+++ b/test/mock_syscall.cpp
@@ -19,6 +19,7 @@
 #include <map>
 #include <queue>
 #include <stdexcept>
+#include <stdplus/raw.hpp>
 #include <string>
 #include <string_view>
 #include <vector>
@@ -128,7 +129,7 @@
 ssize_t sendmsg_link_dump(std::queue<std::string>& msgs, std::string_view in)
 {
     const ssize_t ret = in.size();
-    const auto& hdrin = phosphor::copyFrom<nlmsghdr>(in);
+    const auto& hdrin = stdplus::raw::copyFrom<nlmsghdr>(in);
     if (hdrin.nlmsg_type != RTM_GETLINK)
     {
         return 0;
diff --git a/test/test_neighbor.cpp b/test/test_neighbor.cpp
index c7d926e..6b14912 100644
--- a/test/test_neighbor.cpp
+++ b/test/test_neighbor.cpp
@@ -8,6 +8,7 @@
 
 #include <cstring>
 #include <stdexcept>
+#include <stdplus/raw.hpp>
 #include <string>
 #include <vector>
 
@@ -115,7 +116,8 @@
     EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
     EXPECT_EQ(msg.ndm_state, neighbors[0].state);
     EXPECT_FALSE(neighbors[0].mac);
-    EXPECT_TRUE(equal(addr, std::get<in_addr>(neighbors[0].address)));
+    EXPECT_TRUE(
+        stdplus::raw::equal(addr, std::get<in_addr>(neighbors[0].address)));
 }
 
 TEST(ParseNeighbor, FilterInterface)
@@ -150,7 +152,8 @@
     EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
     EXPECT_EQ(msg.ndm_state, neighbors[0].state);
     EXPECT_FALSE(neighbors[0].mac);
-    EXPECT_TRUE(equal(addr, std::get<in_addr>(neighbors[0].address)));
+    EXPECT_TRUE(
+        stdplus::raw::equal(addr, std::get<in_addr>(neighbors[0].address)));
 }
 
 TEST(ParseNeighbor, FilterState)
@@ -185,7 +188,8 @@
     EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
     EXPECT_EQ(msg.ndm_state, neighbors[0].state);
     EXPECT_FALSE(neighbors[0].mac);
-    EXPECT_TRUE(equal(addr, std::get<in_addr>(neighbors[0].address)));
+    EXPECT_TRUE(
+        stdplus::raw::equal(addr, std::get<in_addr>(neighbors[0].address)));
 }
 
 TEST(ParseNeighbor, Full)
@@ -225,8 +229,9 @@
     EXPECT_EQ(msg.ndm_ifindex, neighbors[0].interface);
     EXPECT_EQ(msg.ndm_state, neighbors[0].state);
     EXPECT_TRUE(neighbors[0].mac);
-    EXPECT_TRUE(equal(mac, *neighbors[0].mac));
-    EXPECT_TRUE(equal(addr, std::get<in6_addr>(neighbors[0].address)));
+    EXPECT_TRUE(stdplus::raw::equal(mac, *neighbors[0].mac));
+    EXPECT_TRUE(
+        stdplus::raw::equal(addr, std::get<in6_addr>(neighbors[0].address)));
 }
 
 } // namespace detail
diff --git a/test/test_netlink.cpp b/test/test_netlink.cpp
index 8d20c39..6e5a198 100644
--- a/test/test_netlink.cpp
+++ b/test/test_netlink.cpp
@@ -7,6 +7,7 @@
 
 #include <cstring>
 #include <stdexcept>
+#include <stdplus/raw.hpp>
 #include <string_view>
 
 #include <gtest/gtest.h>
@@ -125,8 +126,9 @@
     processMsg(data, done, cb);
     EXPECT_EQ(0, data.size());
     EXPECT_EQ(1, cbCalls);
-    EXPECT_TRUE(equal(hdr, hdrOut));
-    EXPECT_TRUE(equal(err, extract<nlmsgerr>(dataOut)));
+    EXPECT_TRUE(stdplus::raw::equal(hdr, hdrOut));
+    EXPECT_TRUE(
+        stdplus::raw::equal(err, stdplus::raw::extract<nlmsgerr>(dataOut)));
     EXPECT_EQ(0, dataOut.size());
     EXPECT_TRUE(done);
 }
diff --git a/test/test_util.cpp b/test/test_util.cpp
index 89723f5..83fcb83 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -5,6 +5,7 @@
 
 #include <cstddef>
 #include <cstring>
+#include <stdplus/raw.hpp>
 #include <string>
 #include <string_view>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -213,94 +214,6 @@
     EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28"));
 }
 
-TEST_F(TestUtil, CopyFromTooSmall)
-{
-    constexpr auto expected = "abcde"sv;
-    struct
-    {
-        uint8_t data[10];
-    } data;
-    static_assert(sizeof(data) > expected.size());
-    EXPECT_THROW(copyFrom<decltype(data)>(expected), std::runtime_error);
-}
-
-TEST_F(TestUtil, CopyFromSome)
-{
-    constexpr auto expected = "abcde"sv;
-    struct
-    {
-        uint8_t data[2];
-    } data;
-    static_assert(sizeof(data) < expected.size());
-    data = copyFrom<decltype(data)>(expected);
-    EXPECT_EQ(0, memcmp(&data, expected.data(), sizeof(data)));
-}
-
-TEST_F(TestUtil, CopyFromAll)
-{
-    constexpr auto expected = "abcde"sv;
-    struct
-    {
-        uint8_t data[5];
-    } data;
-    static_assert(sizeof(data) == expected.size());
-    data = copyFrom<decltype(data)>(expected);
-    EXPECT_EQ(0, memcmp(&data, expected.data(), sizeof(data)));
-}
-
-TEST_F(TestUtil, ExtractSome)
-{
-    constexpr auto expected = "abcde"sv;
-    auto buf = expected;
-    struct
-    {
-        uint8_t data[2];
-    } data;
-    static_assert(sizeof(data) < expected.size());
-    data = extract<decltype(data)>(buf);
-    EXPECT_EQ(0, memcmp(&data, expected.data(), sizeof(data)));
-    EXPECT_EQ(3, buf.size());
-    EXPECT_EQ(expected.substr(2), buf);
-}
-
-TEST_F(TestUtil, ExtractAll)
-{
-    constexpr auto expected = "abcde"sv;
-    auto buf = expected;
-    struct
-    {
-        uint8_t data[5];
-    } data;
-    static_assert(sizeof(data) == expected.size());
-    data = extract<decltype(data)>(buf);
-    EXPECT_EQ(0, memcmp(&data, expected.data(), sizeof(data)));
-    EXPECT_EQ(0, buf.size());
-}
-
-TEST_F(TestUtil, Equal)
-{
-    struct
-    {
-        int i;
-    } a, b{};
-    a.i = 4;
-    b.i = 4;
-
-    EXPECT_TRUE(equal(a, b));
-}
-
-TEST_F(TestUtil, NotEqual)
-{
-    struct
-    {
-        int i;
-    } a, b{};
-    a.i = 2;
-    b.i = 4;
-
-    EXPECT_FALSE(equal(a, b));
-}
-
 namespace mac_address
 {
 
@@ -313,11 +226,14 @@
 
 TEST(MacFromString, Valid)
 {
-    EXPECT_TRUE(equal(ether_addr{}, fromString("00:00:00:00:00:00")));
-    EXPECT_TRUE(equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
-                      fromString("FF:EE:DD:cc:bb:aa")));
-    EXPECT_TRUE(equal(ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05},
-                      fromString("0:1:2:3:4:5")));
+    EXPECT_TRUE(
+        stdplus::raw::equal(ether_addr{}, fromString("00:00:00:00:00:00")));
+    EXPECT_TRUE(
+        stdplus::raw::equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
+                            fromString("FF:EE:DD:cc:bb:aa")));
+    EXPECT_TRUE(
+        stdplus::raw::equal(ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05},
+                            fromString("0:1:2:3:4:5")));
 }
 
 TEST(MacToString, Valid)
diff --git a/util.cpp b/util.cpp
index 13a607f..fbe576d 100644
--- a/util.cpp
+++ b/util.cpp
@@ -17,6 +17,7 @@
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
 #include <stdexcept>
+#include <stdplus/raw.hpp>
 #include <string>
 #include <variant>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -622,7 +623,7 @@
 
 bool isEmpty(const ether_addr& mac)
 {
-    return equal(mac, ether_addr{});
+    return stdplus::raw::equal(mac, ether_addr{});
 }
 
 bool isMulticast(const ether_addr& mac)
diff --git a/util.hpp b/util.hpp
index 251aa0d..f591888 100644
--- a/util.hpp
+++ b/util.hpp
@@ -185,53 +185,6 @@
 
 } // namespace network
 
-/** @brief Copies data from a buffer into a copyable type
- *
- *  @param[in] data - The data buffer being extracted from
- *  @param[in] emsg - The message to print if extraction fails
- *  @return The copyable type with data populated
- */
-template <typename T>
-T copyFrom(std::string_view data, const char* emsg = "Extract Failed")
-{
-    static_assert(std::is_trivially_copyable_v<T>);
-    T ret;
-    if (data.size() < sizeof(ret))
-    {
-        throw std::runtime_error(emsg);
-    }
-    std::memcpy(&ret, data.data(), sizeof(ret));
-    return ret;
-}
-
-/** @brief Extracts data from a buffer into a copyable type
- *         Updates the data buffer to show that data was removed
- *
- *  @param[in,out] data - The data buffer being extracted from
- *  @param[in] emsg     - The message to print if extraction fails
- *  @return The copyable type with data populated
- */
-template <typename T>
-T extract(std::string_view& data, const char* emsg = "Extract Failed")
-{
-    T ret = copyFrom<T>(data, emsg);
-    data.remove_prefix(sizeof(ret));
-    return ret;
-}
-
-/** @brief Compares two of the same trivially copyable types
- *
- *  @param[in] a - The data buffer being extracted from
- *  @param[in] b - The message to print if extraction fails
- *  @return True if the parameters are bitwise identical
- */
-template <typename T>
-bool equal(const T& a, const T& b)
-{
-    static_assert(std::is_trivially_copyable_v<T>);
-    return memcmp(&a, &b, sizeof(T)) == 0;
-}
-
 class Descriptor
 {
   private: