From 10bed2f901130a08badc17f2de356b41afb931ab Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 21 Aug 2024 11:05:57 +0800
Subject: [PATCH] Add buffer dump print code

---
 booster/testcase/logger.c |    4 +-
 booster/testcase/dump.c   |   75 +++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/booster/testcase/dump.c b/booster/testcase/dump.c
new file mode 100644
index 0000000..5f70b6f
--- /dev/null
+++ b/booster/testcase/dump.c
@@ -0,0 +1,75 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
+ *                  All rights reserved.
+ *
+ *       Filename:  dump.c
+ *    Description:  This is the buffer dump print code.
+ *
+ *        Version:  1.0.0(08/08/2012~)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "08/08/2012 06:51:40 PM"
+ *
+ ********************************************************************************/
+
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+
+void dump_buf(const char *prompt, const unsigned char *buf, size_t len)
+{
+    char        line[256];
+    size_t      i, j;
+    int         offset;
+
+    if( prompt )
+    {
+        printf("%s", prompt);
+    }
+
+    for(i = 0; i < len; i += 16)
+    {
+        offset = snprintf(line, sizeof(line), "%08zx: ", i);
+
+        /* Print hex representation */
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < len)
+                offset += snprintf(line + offset, sizeof(line) - offset, "%02x ", buf[i + j]);
+            else
+                offset += snprintf(line + offset, sizeof(line) - offset, "   ");
+        }
+
+        offset += snprintf(line + offset, sizeof(line) - offset, " ");
+
+        /* Print ASCII representation */
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < len)
+            {
+                unsigned char c = buf[i + j];
+                offset += snprintf(line + offset, sizeof(line) - offset, "%c", (c >= 32 && c <= 126) ? c : '.');
+            }
+            else
+            {
+                offset += snprintf(line + offset, sizeof(line) - offset, " ");
+            }
+        }
+
+        /* Print the line */
+        printf("%s\n", line);
+    }
+}
+
+int main (int argc, char **argv)
+{
+    char    buf[256];
+    int     i;
+
+    for(i=0; i<sizeof(buf); i++)
+        buf[i] = i;
+
+    dump_buf("Hex dump buffer content:\n", buf, sizeof(buf));
+
+    return 0;
+}
+
diff --git a/booster/testcase/logger.c b/booster/testcase/logger.c
index e6ee293..86c2414 100644
--- a/booster/testcase/logger.c
+++ b/booster/testcase/logger.c
@@ -2,8 +2,8 @@
  *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
  *                  All rights reserved.
  *
- *       Filename:  test_logger.c
- *    Description:  This is the linux logger system test code.
+ *       Filename:  logger.c
+ *    Description:  This is the logger system test code.
  *
  *        Version:  1.0.0(08/08/2012~)
  *         Author:  Guo Wenxue <guowenxue@gmail.com>

--
Gitblit v1.9.1