blob: 5d8363593fd34032491363fc9012a706a2a6b406 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <stdio.h>
#include <ctype.h>
#include "xxd.h"
void xxd(const char * data, size_t size) {
size_t fake_size = size + (16 - size % 16) % 16;
for (size_t base = 0; base < fake_size; base += 16) {
printf("%08lx: ", base);
// print bytes
for (size_t offset = 0; offset < 16; offset++) {
size_t i = base + offset;
if (offset == 8) printf(" ");
if (i >= size) {
printf(" ");
continue;
}
printf("%02x ", data[i] & 0xff);
}
// print ascii representation
printf(" |");
for (size_t offset = 0; offset < 16; offset++) {
size_t i = base + offset;
if (i >= size) {
printf(" ");
continue;
}
if (isprint(data[i]))
printf("%c", data[i]);
else
printf(".");
}
printf("|\n");
}
}
|