diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2025-04-08 12:13:57 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2025-04-08 12:13:57 -0400 | 
| commit | 9e1586c531456ad511342e82ffe1b2056c983c8d (patch) | |
| tree | ab5dd796962a7a6825121e6fbf5c5ad58ba58208 /src/lib | |
| parent | 39cd11ecc9a9e740f2596d2983da07dc7d5a78c0 (diff) | |
| download | stasis-9e1586c531456ad511342e82ffe1b2056c983c8d.tar.gz | |
Add debug_hexdump utility
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/core/include/utils.h | 2 | ||||
| -rw-r--r-- | src/lib/core/utils.c | 51 | 
2 files changed, 53 insertions, 0 deletions
diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index 1906808..b405b2a 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -417,4 +417,6 @@ int gen_file_extension_str(char *filename, const char *extension);   * Remove [extra]s from a spec string   */  char *remove_extras(char *s); + +void debug_hexdump(char *data, int len);  #endif //STASIS_UTILS_H diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index a248f58..b63b089 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  | 
