-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.c
executable file
·71 lines (61 loc) · 2.04 KB
/
dump.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Copyright (C) 2014 bitpick
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
**/
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#ifndef NUM_COLS
#define NUM_COLS 8
#endif
void dump_hex(void *addr, size_t len, bool show_real_address, bool show_ascii)
{
size_t offset = 0;
if(show_real_address)
offset = (size_t) addr;
size_t i;
// fix not NUM_COLS aligned data length
unsigned int block_align_correction_bytes;
if(len % NUM_COLS)
block_align_correction_bytes = NUM_COLS - (len % NUM_COLS);
else
block_align_correction_bytes = 0;
/* iterate through data and print rows of NUM_COLS bytes, each */
for(i = 0; i < (len + block_align_correction_bytes); i++)
{
// print offset address
if(i % NUM_COLS == 0)
printf("0x%06x: ", (i + offset));
// print block as hex value
if(i < len)
printf("%02x ", 0xff & ((char*)addr)[i]);
else
printf(" "); // blank for alignment correction
if(!show_ascii)
continue;
// print block as ASCII chars
if((i % NUM_COLS) == (NUM_COLS - 1))
{
size_t k;
for(k = i - (NUM_COLS - 1); k <= i; k++)
{
if(k >= len) // add space at end of block
putchar(' ');
else if(isprint(((char*)addr)[k])) // printable ASCII char
putchar(0xff & ((char*)addr)[k]);
else // no printable ASCII char
putchar('.');
}
putchar('\n');
}
}
}