-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
97 lines (78 loc) · 2.15 KB
/
utils.c
File metadata and controls
97 lines (78 loc) · 2.15 KB
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <udis86.h>
#include "log.h"
#include "utils.h"
#define MAP(v) X(v, #v)
#define X(a, b) b,
char *elf_status_names[] = {
ELF_STATUS_TABLE
};
#undef X
#undef MAP
char *
elf_status_to_str(telf_status status)
{
return elf_status_names[status];
}
telf_status
binary_to_asm(char *bin,
size_t bin_len,
char **bufp,
size_t *buf_lenp)
{
telf_status ret;
char *buf = NULL;
size_t buf_len = 0;
ud_t ud_obj;
char *tmpbuf = NULL;
ud_init(&ud_obj);
ud_set_input_buffer(&ud_obj, bin, bin_len);
ud_set_mode(&ud_obj, 64);
ud_set_syntax(&ud_obj, UD_SYN_INTEL);
if (! bin_len || ! bin) {
ret = ELF_SUCCESS;
goto end;
}
while (ud_disassemble(&ud_obj)) {
char line[64] = "";
size_t len;
len = sprintf(line, "%s\n", ud_insn_asm(&ud_obj));
tmpbuf = realloc(buf, buf_len + len);
if (! tmpbuf) {
ERR("realloc: %s", strerror(errno));
free(buf);
buf = NULL;
ret = ELF_ENOMEM;
goto end;
}
buf = tmpbuf;
memmove(buf + buf_len, line, len);
buf_len += len;
}
/* we didn't reserve any room for the nul-terminaison char */
if (buf) {
tmpbuf = realloc(buf, buf_len + 1);
if (! tmpbuf) {
ERR("realloc: %s", strerror(errno));
free(buf);
buf = NULL;
ret = ELF_ENOMEM;
goto end;
}
tmpbuf[buf_len] = 0;
buf_len++;
buf = tmpbuf;
}
ret = ELF_SUCCESS;
end:
if (bufp)
*bufp = buf;
else
free(buf);
if (buf_lenp)
*buf_lenp = buf_len;
return ret;
}