-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
86 lines (72 loc) · 2.12 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include "include/globals.h"
#include "include/asm.h"
#include "include/parser.h"
#include <keystone/keystone.h>
int main(int argc, char** argv)
{
if (argc < 3) {
printf("usage: dynastone <arch> <code> <emit_function_8> <emit_function_16> <emit_function_32> <emit_function_64>\n");
exit(0);
}
bool direct = true;
int i = 0;
while(argv[2][i]) {
if (argv[2][i] == SPECIAL) direct = false;
i++;
}
char *emit_8, *emit_16, *emit_32, *emit_64;
if (argc < 4) emit_8 = emit_8_default; else emit_8 = argv[3];
if (argc < 5) emit_16 = emit_16_default; else emit_16 = argv[4];
if (argc < 6) emit_32 = emit_32_default; else emit_32 = argv[5];
if (argc < 7) emit_64 = emit_64_default; else emit_64 = argv[6];
char* arch = argv[1];
char* instr = argv[2];
ks_engine *ks;
char* arch_be_inverse = must_inverse_bytes(arch);
if (arch_be_inverse && !direct) {
ks_open_arch(&ks, arch_be_inverse);
}
else {
ks_open_arch(&ks, arch);
}
char* c_code = NULL;
uint8_t* bytes;
if (direct) {
//todo: allow for grouping
size_t size;
size_t c;
char buffer[0x100];
c_code = (char*)calloc(1, MAX_C_CODE_LEN);
ks_asm(ks, instr, 0, &bytes, &size, &c);
for (i = 0; i < size; i++) {
sprintf(buffer, "%s(%02x);\n", emit_8, bytes[i]);
strcat(c_code, buffer);
}
printf("%s", c_code);
free(c_code);
exit(0);
}
parsed_data* pdata;
if (arch_be_inverse) bytes = compute_delimitations(ks, true, instr, &pdata);
else bytes = compute_delimitations(ks, is_big_endian_architecture(arch), instr, &pdata);
chunk_struct* chunk = make_chunks(pdata, bytes, pdata->binary_size);
chunk_struct* lv_chunk = make_lv_chunks(chunk, pdata);
int size;
c_code = generate_c_code(lv_chunk, emit_8, emit_16, emit_32, emit_64, &size);
char* c_code_inv;
if (arch_be_inverse) {
c_code_inv = reverse_lines(c_code);
free(c_code);
c_code = c_code_inv;
}
printf("%s", c_code);
free(c_code);
free_chunks(chunk);
free_chunks(lv_chunk);
free_parsed_data(pdata);
ks_free(bytes);
ks_close(ks);
exit(0);
}