1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013 */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9
10 struct platform {
11 cs_arch arch;
12 cs_mode mode;
13 unsigned char *code;
14 size_t size;
15 const char *comment;
16 cs_opt_type opt_type;
17 cs_opt_value opt_value;
18 cs_opt_type opt_skipdata;
19 size_t skipdata;
20 };
21
print_string_hex(unsigned char * str,size_t len)22 static void print_string_hex(unsigned char *str, size_t len)
23 {
24 unsigned char *c;
25
26 printf("Code: ");
27 for (c = str; c < str + len; c++) {
28 printf("0x%02x ", *c & 0xff);
29 }
30 printf("\n");
31 }
32
33 #ifdef CAPSTONE_HAS_ARM
mycallback(const uint8_t * buffer,size_t buffer_size,size_t offset,void * p)34 static size_t CAPSTONE_API mycallback(const uint8_t *buffer, size_t buffer_size, size_t offset, void *p)
35 {
36 // always skip 2 bytes when encountering data
37 return 2;
38 }
39 #endif
40
test()41 static void test()
42 {
43 #ifdef CAPSTONE_HAS_X86
44 #define X86_CODE32 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"
45 #endif
46 #define RANDOM_CODE "\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78"
47
48 #if defined(CAPSTONE_HAS_X86)
49 cs_opt_skipdata skipdata = {
50 // rename default "data" instruction from ".byte" to "db"
51 "db",
52 };
53 #endif
54
55 #ifdef CAPSTONE_HAS_ARM
56 cs_opt_skipdata skipdata_callback = {
57 "db",
58 &mycallback,
59 };
60 #endif
61
62 struct platform platforms[] = {
63 #ifdef CAPSTONE_HAS_X86
64 {
65 CS_ARCH_X86,
66 CS_MODE_32,
67 (unsigned char*)X86_CODE32,
68 sizeof(X86_CODE32) - 1,
69 "X86 32 (Intel syntax) - Skip data",
70 },
71 {
72 CS_ARCH_X86,
73 CS_MODE_32,
74 (unsigned char*)X86_CODE32,
75 sizeof(X86_CODE32) - 1,
76 "X86 32 (Intel syntax) - Skip data with custom mnemonic",
77 CS_OPT_INVALID,
78 CS_OPT_OFF,
79 CS_OPT_SKIPDATA_SETUP,
80 (size_t) &skipdata,
81 },
82 #endif
83 #ifdef CAPSTONE_HAS_ARM
84 {
85 CS_ARCH_ARM,
86 CS_MODE_ARM,
87 (unsigned char*)RANDOM_CODE,
88 sizeof(RANDOM_CODE) - 1,
89 "Arm - Skip data",
90 },
91 {
92 CS_ARCH_ARM,
93 CS_MODE_ARM,
94 (unsigned char*)RANDOM_CODE,
95 sizeof(RANDOM_CODE) - 1,
96 "Arm - Skip data with callback",
97 CS_OPT_INVALID,
98 CS_OPT_OFF,
99 CS_OPT_SKIPDATA_SETUP,
100 (size_t) &skipdata_callback,
101 },
102 #endif
103 };
104
105 csh handle;
106 uint64_t address = 0x1000;
107 cs_insn *insn;
108 cs_err err;
109 int i;
110 size_t count;
111
112 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
113 printf("****************\n");
114 printf("Platform: %s\n", platforms[i].comment);
115 err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
116 if (err) {
117 printf("Failed on cs_open() with error returned: %u\n", err);
118 abort();
119 }
120
121 if (platforms[i].opt_type)
122 cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
123
124 // turn on SKIPDATA mode
125 cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
126 cs_option(handle, platforms[i].opt_skipdata, platforms[i].skipdata);
127
128 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
129 if (count) {
130 size_t j;
131
132 print_string_hex(platforms[i].code, platforms[i].size);
133 printf("Disasm:\n");
134
135 for (j = 0; j < count; j++) {
136 printf("0x%" PRIx64 ":\t%s\t\t%s\n",
137 insn[j].address, insn[j].mnemonic, insn[j].op_str);
138 }
139
140 // print out the next offset, after the last insn
141 printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
142
143 // free memory allocated by cs_disasm()
144 cs_free(insn, count);
145 } else {
146 printf("****************\n");
147 printf("Platform: %s\n", platforms[i].comment);
148 print_string_hex(platforms[i].code, platforms[i].size);
149 printf("ERROR: Failed to disasm given code!\n");
150 abort();
151 }
152
153 printf("\n");
154
155 cs_close(&handle);
156 }
157 }
158
main()159 int main()
160 {
161 test();
162
163 #if 0
164 #define offsetof(st, m) __builtin_offsetof(st, m)
165
166 cs_insn insn;
167 printf("size: %lu\n", sizeof(insn));
168 printf("@id: %lu\n", offsetof(cs_insn, id));
169 printf("@address: %lu\n", offsetof(cs_insn, address));
170 printf("@size: %lu\n", offsetof(cs_insn, size));
171 printf("@bytes: %lu\n", offsetof(cs_insn, bytes));
172 printf("@mnemonic: %lu\n", offsetof(cs_insn, mnemonic));
173 printf("@op_str: %lu\n", offsetof(cs_insn, op_str));
174 printf("@regs_read: %lu\n", offsetof(cs_insn, regs_read));
175 printf("@regs_read_count: %lu\n", offsetof(cs_insn, regs_read_count));
176 printf("@regs_write: %lu\n", offsetof(cs_insn, regs_write));
177 printf("@regs_write_count: %lu\n", offsetof(cs_insn, regs_write_count));
178 printf("@groups: %lu\n", offsetof(cs_insn, groups));
179 printf("@groups_count: %lu\n", offsetof(cs_insn, groups_count));
180 printf("@arch: %lu\n", offsetof(cs_insn, x86));
181 #endif
182
183 return 0;
184 }
185