forked from linux-msm/rmtfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
48 lines (40 loc) · 830 Bytes
/
util.c
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
45
46
47
48
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include "util.h"
static uint8_t to_hex(uint8_t ch)
{
ch &= 0xf;
return ch <= 9 ? '0' + ch : 'a' + ch - 10;
}
void print_hex_dump(const char *prefix, const void *buf, size_t len)
{
const uint8_t *ptr = buf;
size_t linelen;
uint8_t ch;
char line[16 * 3 + 16 + 1];
int li;
int i;
int j;
for (i = 0; i < len; i += 16) {
linelen = MIN(16, len - i);
li = 0;
for (j = 0; j < linelen; j++) {
ch = ptr[i + j];
line[li++] = to_hex(ch >> 4);
line[li++] = to_hex(ch);
line[li++] = ' ';
}
for (; j < 16; j++) {
line[li++] = ' ';
line[li++] = ' ';
line[li++] = ' ';
}
for (j = 0; j < linelen; j++) {
ch = ptr[i + j];
line[li++] = isprint(ch) ? ch : '.';
}
line[li] = '\0';
printf("%s %04x: %s\n", prefix, i, line);
}
}