aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2026-06-22 00:25:32 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2026-06-22 11:57:05 -0400
commitd8154024dd0e3baac34c29cad37aaf79c03e9069 (patch)
tree78af135e97e7d1d5f6052c4b2ab1f19147b265e5 /src
parent1ff5ae0d28c5525c50ce0962dd20725b4e8ad7c2 (diff)
downloadstasis-d8154024dd0e3baac34c29cad37aaf79c03e9069.tar.gz
Rewrite debug_hexdump
Diffstat (limited to 'src')
-rw-r--r--src/lib/core/include/utils.h2
-rw-r--r--src/lib/core/utils.c78
2 files changed, 33 insertions, 47 deletions
diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h
index 3f0fe9f..e75995a 100644
--- a/src/lib/core/include/utils.h
+++ b/src/lib/core/include/utils.h
@@ -429,7 +429,7 @@ int gen_file_extension_str(char *filename, size_t maxlen, const char *extension)
*/
char *remove_extras(char *s);
-void debug_hexdump(char *data, int len);
+void debug_hexdump(char *data, size_t len);
/**
* Realloc helper
diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c
index 31208ad..d745c51 100644
--- a/src/lib/core/utils.c
+++ b/src/lib/core/utils.c
@@ -946,57 +946,43 @@ int gen_file_extension_str(char *filename, const size_t maxlen, const char *exte
return replace_text(ext_orig, ext_orig, extension, 0);
}
-#define DEBUG_HEXDUMP_FMT_BYTES 6
-#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_FMT_BYTES + 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};
+void debug_hexdump(char *data, const size_t len) {
+ if (!data) {
+ SYSWARN("DATA NULL\n");
+ return;
+ }
+ if (len <= 0) {
+ SYSWARN("ZERO LENGTH\n");
+ return;
+ }
+ const size_t window = 16;
+ size_t avail = len;
+ const size_t chunk = avail / window ? window : avail;
char *start = data;
- char *end = data + len;
-
- char *pos = start;
- while (pos != end) {
- if (count == 0) {
- snprintf(addr + strlen(addr), sizeof(addr) - strlen(addr), "%p", pos);
+ while (avail > 0) {
+ const size_t need = chunk > avail ? avail : chunk;
+ char *p = start;
+ printf("%p | ", p);
+ size_t j = 0;
+ for (j = 0; j < need; j++) {
+ printf("%02x ", p[j]);
}
- if (count == 8) {
- safe_strncat(bytes, " ", sizeof(bytes));
+ const size_t padding = window - j;
+ for (size_t i = 0; i < padding; i++) {
+ printf("00 ");
}
- if (count > 15) {
- snprintf(output, sizeof(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;
+ printf("| ");
+ for (j = 0; j < need; j++) {
+ char *c = p + j;
+ printf("%c", isprint(*c) ? *c : '.');
}
-
- snprintf(bytes + strlen(bytes), sizeof(bytes) - strlen(bytes), "%02X ", (unsigned char) *pos);
- snprintf(ascii + strlen(ascii), sizeof(ascii) - strlen(ascii), "%c", isprint(*pos) ? *pos : '.');
-
- pos++;
- count++;
- }
-
- if (count <= 8) {
- // Add group padding
- safe_strncat(bytes, " ", sizeof(bytes));
- }
- const int padding = 16 - count;
- for (int i = 0; i < padding; i++) {
- safe_strncat(bytes, " ", sizeof(bytes));
+ for (size_t i = 0; i < padding; i++) {
+ printf(".");
+ }
+ printf(" |\n");
+ avail -= need;
+ start += need;
}
- snprintf(output, sizeof(output), "%s | %s | %s", addr, bytes, ascii);
- puts(output);
}
int grow(const size_t size_new, size_t *size_orig, char **data) {