aboutsummaryrefslogtreecommitdiff
path: root/client/xxd.c
blob: 06b99604fd0e4d090ca5863bee1bc50d08e393a4 (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 "parse.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[size]);
		}

		// 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[size]))
				printf("%c", data[size]);
			else
				printf(".");
		}
		printf("|\n");
	}
}