Last active
November 6, 2021 20:26
-
-
Save ripmeep/61a80516b87e1069d9cf3cf39b8130a7 to your computer and use it in GitHub Desktop.
A hexdump header for C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: ripmeep | |
Instagram: @rip.meep | |
GitHub: https://github.com/ripmeep | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include <ctype.h> | |
void hexdump(const char *s, size_t len, size_t row_len, int show_chars, int colors) { | |
int b = 0; | |
int xc_offset = 0; | |
int cw = 0; | |
int is_printable = 0; | |
bool use_colors = (colors > -1); | |
for (int i = 0; i < len; ++i) { | |
if (b%row_len == 0) | |
printf("[%04x]\t", i); | |
if ((isalpha(s[i]) || ispunct(s[i]) || isdigit(s[i])) && use_colors) { | |
is_printable = 1; | |
printf("\033[9%dm", colors); | |
} else | |
is_printable = 0; | |
printf("%02X ", s[i] & 0xFF); | |
if (use_colors) | |
printf("\033[0m"); | |
b++; | |
if (b == row_len/2) | |
printf(" "); | |
if (b%row_len == 0 || i + 1 == len) { | |
if (show_chars) { | |
for (int p = 0; p < (3*row_len) - (3*b); ++p) | |
printf(" "); | |
printf("\t| "); | |
if (i + 1 == len) | |
xc_offset = ((i - row_len) + 1) + (row_len - b); | |
else | |
xc_offset = (i - row_len) + 1; | |
cw = 0; | |
for (int x = xc_offset; x < i + 1; ++x) { | |
if (isalpha(s[x]) || ispunct(s[x]) || isdigit(s[x])) | |
if ((int)s[x] == 0x20) | |
printf("."); | |
else { | |
if ((isalpha(s[x]) || ispunct(s[x]) || isdigit(s[x])) && colors > -1) | |
printf("\033[9%dm", colors); | |
printf("%c", s[x]); | |
if (use_colors) | |
printf("\033[0m"); | |
} | |
else | |
printf("."); | |
cw++; | |
} | |
for (int p = 0; p < (row_len - cw); ++p) | |
printf(" "); | |
printf(" |"); | |
} | |
printf("\n"); | |
b = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment