69 lines
1.4 KiB
C
69 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
int curgroup = 0; // To know where 8 group
|
|
int dumpgroup = 0; // To know where newline should be
|
|
int hexnum = 1; // To put space after every second hex number
|
|
bool canonical = true; // -C flag
|
|
|
|
int chardump(char *data, int start_of_line, int end_of_line /* to display only 16 chars */) {
|
|
for (int i=0; i < end_of_line; i++) {
|
|
if (data[i] == "\0" || data[i] == ' ')
|
|
printf(".");
|
|
else
|
|
printf("%c", data[i]);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
int hexdump(char *data) {
|
|
//printf("%s\n", data); // FIXME
|
|
int cur_start_of_line = 0;
|
|
for (int i=0; i < strlen(data); i++) {
|
|
hexnum++;
|
|
if (data[i] == '\0')
|
|
printf("00");
|
|
else {
|
|
if (!canonical)
|
|
printf("%02x", data[i]);
|
|
else {
|
|
printf("%02x ", data[i]);
|
|
//dumpgroup++;
|
|
}
|
|
}
|
|
if (hexnum%2 && hexnum >= 2) {
|
|
if (!canonical)
|
|
printf(" ");
|
|
curgroup++;
|
|
}
|
|
if (curgroup == 4) {
|
|
if (canonical) {
|
|
printf(" ");
|
|
}
|
|
curgroup = 0;
|
|
dumpgroup++;
|
|
}
|
|
if (dumpgroup == 2) {
|
|
dumpgroup = 0;
|
|
// printf("|");
|
|
// chardump(data, cur_start_of_line, hexnum);
|
|
// printf("|");
|
|
// cur_start_of_line = i;
|
|
printf("\n");
|
|
}
|
|
}
|
|
//printf("\n"); // FIXME
|
|
}
|
|
|
|
int main() {
|
|
FILE *infile = fopen("../tests/test2.bin", "r");//fopen("test.bin", "r");
|
|
char data[50];
|
|
while (fgets(data, 50, infile) != NULL) {
|
|
hexdump(data);
|
|
}
|
|
printf("\n"); // FIXME
|
|
fclose(infile);
|
|
}
|