-
Notifications
You must be signed in to change notification settings - Fork 182
/
util.c
81 lines (66 loc) · 1.49 KB
/
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
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
#include <ctype.h>
#include "buffer.h"
#include "test.h"
#include "util.h"
void print_hex(unsigned char *data, unsigned int length)
{
unsigned int i;
for(i = 0; i < length; i++)
printf("%02x", data[i]);
printf("\n");
}
static char get_character_from_byte(uint8_t byte)
{
if(byte < 0x20 || byte > 0x7F)
return '.';
return byte;
}
void print_hex_fancy(uint8_t *data, uint64_t length)
{
uint64_t i, j;
for(i = 0; i < length; i++)
{
if(!(i % 16))
{
if(i > 0)
{
printf(" ");
for(j = 16; j > 0; j--)
{
printf("%c", get_character_from_byte(data[i - j]));
}
}
printf("\n%04X: ", (int)i);
}
printf("%02X ", data[i]);
}
for(i = length % 16; i < 17; i++)
printf(" ");
for(i = length - (length % 16); i < length; i++)
printf("%c", get_character_from_byte(data[i]));
printf("\nLength: 0x%X (%d)\n", (int)length, (int)length);
}
void die(char *msg)
{
fprintf(stderr, "FATAL ERROR: %s\n", msg);
exit(EXIT_FAILURE);
}
void die_MEM(void)
{
die("Out of memory");
}
/* Read and return an entire file. */
uint8_t *read_file(char *filename, uint64_t *out_length)
{
char buffer[1024];
size_t bytes_read;
buffer_t *b = buffer_create(BO_HOST);
FILE *f = fopen(filename, "rb");
if(!f)
die("Couldn't open input file");
while((bytes_read = fread(buffer, 1, 1024, f)) != 0)
{
buffer_add_bytes(b, buffer, bytes_read);
}
return buffer_create_string_and_destroy(b, out_length);
}