forked from hsn93/hexdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexdump.c
145 lines (113 loc) · 3.93 KB
/
hexdump.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "hexdump.h"
#define CONSOLE_COLOR_WHITE 15
#define CONSOLE_COLOR_GRAY 7
#define CONSOLE_COLOR_GRAY2 8
#define CONSOLE_COLOR_BLACK 0
#define CONSOLE_COLOR_RED 9
#define CONSOLE_COLOR_RED2 196
#define CONSOLE_COLOR_RED3 160
#define CONSOLE_COLOR_CYAN 14
#define CONSOLE_COLOR_CYAN2 51
#define CONSOLE_COLOR_GREEN 10
#define CONSOLE_COLOR_GREEN2 40
#define CONSOLE_COLOR_GREEN3 82
#define CONSOLE_COLOR_YELLOW 11
#define CONSOLE_COLOR_YELLOW2 220
#define CONSOLE_COLOR_YELLOW3 226
#define COLORED_PRINTF(str, fg, bg ,...) do { \
printf(console_color_set(fg,bg));\
printf(str __VA_OPT__(,) __VA_ARGS__);\
printf(console_color_reset());\
} while(0)
static char *console_color_set(uint8_t fg, uint8_t bg)
{
static char str[30] = {0};
sprintf( str,"\033[38;5;%i;48;5;%im", fg, bg);
return str;
}
static char *console_color_reset()
{
return "\033[00m";
}
static void header(size_t WIDTH)
{
printf("\r\n");
// title
COLORED_PRINTF(" ", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
const char str[] = " ~~[ H3X DUMP V1.0 ]~~ ";
size_t spaces = (WIDTH*3 - sizeof(str)) / 2;
for (int i=0; i<spaces; i++)
COLORED_PRINTF(" ", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
COLORED_PRINTF("%s", CONSOLE_COLOR_GREEN3, CONSOLE_COLOR_GRAY2, str);
for (int i=0; i<spaces+3; i++)
COLORED_PRINTF(" ", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
printf("\r\n");
//columns
COLORED_PRINTF("------ ", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
for (int i=0; i<WIDTH; i++){
COLORED_PRINTF("%02i ", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2, i);
}
COLORED_PRINTF(" |", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
printf("\r\n");
}
static void footer(size_t WIDTH)
{
COLORED_PRINTF("------| ", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
for (int i=0; i<WIDTH; i++){
COLORED_PRINTF(" ", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
}
COLORED_PRINTF(" |", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
printf("\r\n");
}
static void printAddress(uint32_t address){
COLORED_PRINTF("0x%04X|", CONSOLE_COLOR_RED, CONSOLE_COLOR_BLACK, address);
printf(" ");
}
static void fillRestOfLine(size_t WIDTH, int pos){
uint8_t tail = WIDTH - (pos % WIDTH);
for (int j=0; j<tail; j++)
printf(" ");
COLORED_PRINTF(" |", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
printf("\r\n");
}
void hex_dump(const void *const mem, int length)
{
hex_dump_highlight(mem, length, 0, 0);
}
void hex_dump_highlight(const void *const mem, int length, int highlight_start, int highlight_length)
{
if (length <= 0){
return;
}
const uint8_t *memBuf = mem;
const size_t WIDTH = 32U;
header(WIDTH);
for (int i=0; i<length; i++){
//print address
if (i % WIDTH == 0)
printAddress(i);
//print value
if(highlight_length != 0 && (i >= highlight_start && i<= (highlight_length+highlight_start))){
if (i != (highlight_length+highlight_start)){
COLORED_PRINTF("%02X ",CONSOLE_COLOR_BLACK,CONSOLE_COLOR_YELLOW3, memBuf[i]);
}else{
COLORED_PRINTF("%02X",CONSOLE_COLOR_BLACK,CONSOLE_COLOR_YELLOW3, memBuf[i]);
printf(" ");
}
}else{
printf("%02X ", memBuf[i]);
}
//print border
if (i % WIDTH == (WIDTH-1)){
COLORED_PRINTF(" |", CONSOLE_COLOR_GREEN, CONSOLE_COLOR_GRAY2);
printf("\r\n");
}
}
if (length % WIDTH != 0){
fillRestOfLine(WIDTH, length);
}
footer(WIDTH);
}