cleanup: bios: Avoid duplicates for table pad/checksum

Implement a function for table pad/checksum to avoid duplicates

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: Iafb4ff3429c204abedc5888cac7c00ac9e58069f
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index cc2d10d..a1288b4 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -55,6 +55,25 @@
     year = decimalToBcd(time->tm_year + 1900); // The number of years since 1900
 }
 
+size_t getTableTotalsize(size_t sizeWithoutPad)
+{
+    auto padSize = getNumPadBytes(sizeWithoutPad);
+    return sizeWithoutPad + padSize + sizeof(uint32_t) /* checksum */;
+}
+
+void padAndChecksum(Table& table)
+{
+    auto padSize = getNumPadBytes(table.size());
+    table.insert(table.end(), padSize, 0);
+
+    boost::crc_32_type result;
+    size_t size = table.size();
+    result.process_bytes(table.data(), size);
+    uint32_t checkSum = result.checksum();
+    uint8_t* checkSumPtr = reinterpret_cast<uint8_t*>(&checkSum);
+    table.insert(table.end(), checkSumPtr, checkSumPtr + sizeof(checkSum));
+}
+
 } // namespace utils
 
 Response getDateTime(const pldm_msg* request, size_t /*payloadLength*/)
@@ -150,6 +169,13 @@
     if (BIOSStringTable.isEmpty())
     { // no persisted table, constructing fresh table and file
         auto biosStrings = bios_parser::getStrings(biosJsonDir);
+        if (biosStrings.empty())
+        {
+            encode_get_bios_table_resp(instanceID, PLDM_BIOS_TABLE_UNAVAILABLE,
+                                       0, PLDM_START_AND_END, nullptr,
+                                       response.size(), responsePtr);
+            return response;
+        }
         std::sort(biosStrings.begin(), biosStrings.end());
         // remove all duplicate strings received from bios json
         biosStrings.erase(std::unique(biosStrings.begin(), biosStrings.end()),
@@ -162,17 +188,11 @@
         size_t sizeWithoutPad =
             allStringsLen +
             (biosStrings.size() * (sizeof(pldm_bios_string_table_entry) - 1));
-        uint8_t padSize = utils::getNumPadBytes(sizeWithoutPad);
-        uint32_t stringTableSize{};
-        uint32_t checkSum;
-        if (biosStrings.size())
-        {
-            stringTableSize = sizeWithoutPad + padSize + sizeof(checkSum);
-        }
-        Table stringTable(
-            stringTableSize,
-            0); // initializing to 0 so that pad will be automatically added
-        auto tablePtr = reinterpret_cast<uint8_t*>(stringTable.data());
+        Table stringTable;
+        stringTable.reserve(utils::getTableTotalsize(sizeWithoutPad));
+
+        stringTable.resize(sizeWithoutPad);
+        auto tablePtr = stringTable.data();
         for (const auto& elem : biosStrings)
         {
             auto stringPtr =
@@ -186,22 +206,12 @@
                         sizeof(stringPtr->string_length);
             tablePtr += elem.length();
         }
-        tablePtr += padSize;
 
-        if (stringTableSize)
-        {
-            // compute checksum
-            boost::crc_32_type result;
-            result.process_bytes(stringTable.data(), stringTableSize);
-            checkSum = result.checksum();
-            std::copy_n(reinterpret_cast<uint8_t*>(&checkSum), sizeof(checkSum),
-                        stringTable.data() + sizeWithoutPad + padSize);
-            BIOSStringTable.store(stringTable);
-        }
-
+        utils::padAndChecksum(stringTable);
+        BIOSStringTable.store(stringTable);
         response.resize(sizeof(pldm_msg_hdr) +
                             PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES +
-                            stringTableSize,
+                            stringTable.size(),
                         0);
         responsePtr = reinterpret_cast<pldm_msg*>(response.data());
         size_t respPayloadLength = response.size();
@@ -815,28 +825,8 @@
                                        response.size(), responsePtr);
             return response;
         }
-
-        // calculate pad
-        uint8_t padSize = utils::getNumPadBytes(attributeTable.size());
-        std::vector<uint8_t> pad(padSize, 0);
-        if (padSize)
-        {
-            std::move(pad.begin(), pad.end(),
-                      std::back_inserter(attributeTable));
-        }
-
-        if (!attributeTable.empty())
-        {
-            // compute checksum
-            boost::crc_32_type result;
-            size_t size = attributeTable.size();
-            result.process_bytes(attributeTable.data(), size);
-            uint32_t checkSum = result.checksum();
-            attributeTable.resize(size + sizeof(checkSum));
-            std::copy_n(reinterpret_cast<uint8_t*>(&checkSum), sizeof(checkSum),
-                        attributeTable.data() + size);
-            BIOSAttributeTable.store(attributeTable);
-        }
+        utils::padAndChecksum(attributeTable);
+        BIOSAttributeTable.store(attributeTable);
         response.resize(sizeof(pldm_msg_hdr) +
                         PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES +
                         attributeTable.size());