Skip to content

Instantly share code, notes, and snippets.

@iBug

iBug/util.c Secret

Created May 9, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iBug/2fb012978dc71d58ab9ad8f1417c1f3a to your computer and use it in GitHub Desktop.
Save iBug/2fb012978dc71d58ab9ad8f1417c1f3a to your computer and use it in GitHub Desktop.
Some utilities
// File: util.c
// Author: iBug
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void errorexit(const char* s) {
perror(s);
if (errno)
exit(1);
}
void hexdump(const unsigned char* s, size_t len) {
int x = 0;
while (x + 16 <= len) {
printf("%08X ", x);
for (int i = x; i < x + 8; i++)
printf("%02X ", s[i]);
for (int i = x + 8; i < x + 16; i++)
printf(" %02X", s[i]);
printf(" |");
for (int i = x; i < x + 16; i++)
putchar((s[i] >= 32 && s[i] < 127) ? s[i] : '.');
printf("|\n");
x += 16;
}
if (x < len) {
printf("%08X ", x);
for (int i = x; i < x + 8 && i < len; i++)
printf("%02X ", s[i]);
for (int i = x + 8; i < len; i++)
printf(" %02X", s[i]);
int padding = 2 + 3 * (16 + x - len);
char t[8];
snprintf(t, sizeof(t), "%%%ds|", padding);
printf(t, "");
for (int i = x; i < len; i++)
putchar((s[i] >= 32 && s[i] < 127) ? s[i] : '.');
printf("|\n");
}
printf("%08zX\n", len);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment