aboutsummaryrefslogtreecommitdiff
path: root/src/lib/core/utils.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2025-04-08 14:27:48 -0400
committerGitHub <noreply@github.com>2025-04-08 14:27:48 -0400
commita4fb4b69dc05368c8e7f605c6bbaab244fcdd426 (patch)
tree8c972260225c7104f26d9531676ba77b625c8bfa /src/lib/core/utils.c
parent4b28b550de153ea917b7c8736a4c61f2a42c8028 (diff)
parentd3f943a9606e8d7b992ae3a999fc2c74962e1a0a (diff)
downloadstasis-master.tar.gz
Merge pull request #105 from jhunkeler/order-of-battleHEADmaster
Fix garbage after generated release notes
Diffstat (limited to 'src/lib/core/utils.c')
-rw-r--r--src/lib/core/utils.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c
index a248f58..a9c9ea5 100644
--- a/src/lib/core/utils.c
+++ b/src/lib/core/utils.c
@@ -892,3 +892,54 @@ int gen_file_extension_str(char *filename, const char *extension) {
return replace_text(ext_orig, ext_orig, extension, 0);
}
+#define DEBUG_HEXDUMP_ADDR_MAXLEN 20
+#define DEBUG_HEXDUMP_BYTES_MAXLEN (16 * 3 + 2)
+#define DEBUG_HEXDUMP_ASCII_MAXLEN (16 + 1)
+#define DEBUG_HEXDUMP_OUTPUT_MAXLEN (DEBUG_HEXDUMP_ADDR_MAXLEN + DEBUG_HEXDUMP_BYTES_MAXLEN + DEBUG_HEXDUMP_ASCII_MAXLEN + 1)
+
+void debug_hexdump(char *data, int len) {
+ int count = 0;
+ char addr[DEBUG_HEXDUMP_ADDR_MAXLEN] = {0};
+ char bytes[DEBUG_HEXDUMP_BYTES_MAXLEN] = {0};
+ char ascii[DEBUG_HEXDUMP_ASCII_MAXLEN] = {0};
+ char output[DEBUG_HEXDUMP_OUTPUT_MAXLEN] = {0};
+ char *start = data;
+ char *end = data + len;
+
+ char *pos = start;
+ while (pos != end) {
+ if (count == 0) {
+ sprintf(addr + strlen(addr), "%p", pos);
+ }
+ if (count == 8) {
+ strcat(bytes, " ");
+ }
+ if (count > 15) {
+ sprintf(output, "%s | %s | %s", addr, bytes, ascii);
+ puts(output);
+ memset(output, 0, sizeof(output));
+ memset(addr, 0, sizeof(addr));
+ memset(bytes, 0, sizeof(bytes));
+ memset(ascii, 0, sizeof(ascii));
+ count = 0;
+ continue;
+ }
+
+ sprintf(bytes + strlen(bytes), "%02X ", (unsigned char) *pos);
+ sprintf(ascii + strlen(ascii), "%c", isprint(*pos) ? *pos : '.');
+
+ pos++;
+ count++;
+ }
+
+ if (count <= 8) {
+ // Add group padding
+ strcat(bytes, " ");
+ }
+ int padding = 16 - count;
+ for (int i = 0; i < padding; i++) {
+ strcat(bytes, " ");
+ }
+ sprintf(output, "%s | %s | %s", addr, bytes, ascii);
+ puts(output);
+} \ No newline at end of file