-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
60 lines (44 loc) · 1008 Bytes
/
data.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "tools.h"
#include "develop.h"
// FIXME: malloc size need free.
int new_packet (void **packet, const char *path) {
FILE *fp;
int len;
void *buf;
fp = fopen(path, "rb");
if (!fp) {
error("Failed to open %s", path);
return -1;
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
buf = (char *) malloc(len);
fseek(fp, 0, SEEK_SET);
fread(buf, len, 1, fp);
fclose(fp);
*packet = buf;
return len;
}
void free_packet (char *packet) {
free(packet);
}
int hexdump (void *ptr, int size) {
int i;
unsigned char *p;
for (i = 0; i < size; i++) {
p = (unsigned char *)(ptr + i);
if ((i % 16) == 0) {
content("\n0x%04X: %02X", i, *p);
} else if ((i % 2) == 0) {
content(" %02X", *p);
} else {
content("%02X", *p);
}
}
info();
return 0;
}