peltool: Add option to print PEL hexdump

This adds the "-x" option to simply hexdump PELs instead of parsing and
printing to JSON, for use with the --file, -i or -a options.

Signed-off-by: Harisuddin Mohamed Isa <harisuddin@gmail.com>
Change-Id: I13cc76ea36cca761ffa1cf3ab38a638e424bde83
diff --git a/extensions/openpower-pels/json_utils.cpp b/extensions/openpower-pels/json_utils.cpp
index d45b542..e6007ed 100644
--- a/extensions/openpower-pels/json_utils.cpp
+++ b/extensions/openpower-pels/json_utils.cpp
@@ -67,13 +67,17 @@
 
     return output;
 }
-char* dumpHex(const void* data, size_t size, size_t indentCount)
+char* dumpHex(const void* data, size_t size, size_t indentCount, bool toJson)
 {
     const int symbolSize = 100;
     std::string jsonIndent(indentLevel * indentCount, 0x20);
-    jsonIndent.append("\"");
+    if (toJson)
+    {
+        jsonIndent.append("\"");
+    }
     char* buffer = (char*)calloc(std::max(70, 10 * (int)size), sizeof(char));
     char* symbol = (char*)calloc(symbolSize, sizeof(char));
+    char* byteCount = (char*)calloc(11, sizeof(char));
     char ascii[17];
     size_t i, j;
     ascii[16] = '\0';
@@ -81,6 +85,11 @@
     {
         if (i % 16 == 0)
         {
+            if (!toJson)
+            {
+                snprintf(byteCount, 11, "%08X  ", static_cast<uint32_t>(i));
+                strcat(buffer, byteCount);
+            }
             strcat(buffer, jsonIndent.c_str());
         }
         snprintf(symbol, symbolSize, "%02X ", ((unsigned char*)data)[i]);
@@ -98,18 +107,27 @@
         if ((i + 1) % 8 == 0 || i + 1 == size)
         {
             std::string asciiString(ascii);
-            asciiString = escapeJSON(asciiString);
-            const char* asciiToPrint = asciiString.c_str();
+            if (toJson)
+            {
+                asciiString = escapeJSON(asciiString);
+            }
             strcat(buffer, " ");
             if ((i + 1) % 16 == 0)
             {
-                if (i + 1 != size)
+                if (i + 1 != size && toJson)
                 {
-                    snprintf(symbol, symbolSize, "|  %s\",\n", asciiToPrint);
+                    snprintf(symbol, symbolSize, "|  %s\",\n",
+                             asciiString.c_str());
+                }
+                else if (toJson)
+                {
+                    snprintf(symbol, symbolSize, "|  %s\"\n",
+                             asciiString.c_str());
                 }
                 else
                 {
-                    snprintf(symbol, symbolSize, "|  %s\"\n", asciiToPrint);
+                    snprintf(symbol, symbolSize, "|  %s\n",
+                             asciiString.c_str());
                 }
                 strcat(buffer, symbol);
                 memset(symbol, 0, strlen(symbol));
@@ -126,14 +144,24 @@
                     strcat(buffer, "   ");
                 }
                 std::string asciiString2(ascii);
-                asciiString2 = escapeJSON(asciiString2);
-                asciiToPrint = asciiString2.c_str();
-                snprintf(symbol, symbolSize, "|  %s\"\n", asciiToPrint);
+                if (toJson)
+                {
+                    asciiString2 = escapeJSON(asciiString2);
+                    snprintf(symbol, symbolSize, "|  %s\"\n",
+                             asciiString2.c_str());
+                }
+                else
+                {
+                    snprintf(symbol, symbolSize, "|  %s\n",
+                             asciiString2.c_str());
+                }
+
                 strcat(buffer, symbol);
                 memset(symbol, 0, strlen(symbol));
             }
         }
     }
+    free(byteCount);
     free(symbol);
     return buffer;
 }