blob: 41ad60c98d1895485c8baec498bed7da3044a31d [file] [log] [blame]
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
4#include <stdio.h>
5#include <stdlib.h>
Cr4sh19ee2d12015-03-29 18:29:06 +08006#include "../myinttypes.h"
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +08007
pancake9c10ace2015-02-24 04:55:55 +01008#include <capstone/capstone.h>
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +08009
10struct platform {
11 cs_arch arch;
12 cs_mode mode;
13 unsigned char *code;
14 size_t size;
15 char *comment;
16 cs_opt_type opt_type;
17 cs_opt_value opt_value;
danghvu69a7c2d2014-05-19 20:52:25 -050018 cs_opt_type opt_skipdata;
19 size_t skipdata;
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080020};
21
Mr. eXoDia9be1f932014-08-26 12:46:15 +020022static void print_string_hex(unsigned char *str, size_t len)
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080023{
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
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +080033static size_t mycallback(const uint8_t *buffer, size_t buffer_size, size_t offset, void *p)
Nguyen Anh Quynh943cb2d2014-04-11 17:12:58 +080034{
35 // always skip 2 bytes when encountering data
36 return 2;
37}
38
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080039static void test()
40{
41#define X86_CODE32 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"
42#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"
43
danghvu69a7c2d2014-05-19 20:52:25 -050044 cs_opt_skipdata skipdata = {
45 // rename default "data" instruction from ".byte" to "db"
danghvu50fdc6c2014-05-19 21:21:03 -050046 "db",
danghvu69a7c2d2014-05-19 20:52:25 -050047 };
48
49 cs_opt_skipdata skipdata_callback = {
danghvu50fdc6c2014-05-19 21:21:03 -050050 "db",
Nguyen Anh Quynh2c616562014-05-20 10:30:33 +080051 &mycallback,
danghvu69a7c2d2014-05-19 20:52:25 -050052 };
53
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080054 struct platform platforms[] = {
55 {
Axel 0vercl0k Souchetde2dff42014-05-09 21:32:41 +010056 CS_ARCH_X86,
57 CS_MODE_32,
58 (unsigned char*)X86_CODE32,
59 sizeof(X86_CODE32) - 1,
danghvu50fdc6c2014-05-19 21:21:03 -050060 "X86 32 (Intel syntax) - Skip data",
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080061 },
danghvu69a7c2d2014-05-19 20:52:25 -050062 {
Axel 0vercl0k Souchetde2dff42014-05-09 21:32:41 +010063 CS_ARCH_ARM,
64 CS_MODE_ARM,
65 (unsigned char*)RANDOM_CODE,
66 sizeof(RANDOM_CODE) - 1,
danghvu50fdc6c2014-05-19 21:21:03 -050067 "Arm - Skip data",
danghvu69a7c2d2014-05-19 20:52:25 -050068 },
69 {
danghvu50fdc6c2014-05-19 21:21:03 -050070 CS_ARCH_X86,
71 CS_MODE_32,
72 (unsigned char*)X86_CODE32,
73 sizeof(X86_CODE32) - 1,
Nguyen Anh Quynh2c616562014-05-20 10:30:33 +080074 "X86 32 (Intel syntax) - Skip data with custom mnemonic",
danghvu50fdc6c2014-05-19 21:21:03 -050075 0, 0,
76 CS_OPT_SKIPDATA_SETUP,
77 (size_t) &skipdata,
danghvu69a7c2d2014-05-19 20:52:25 -050078 },
79 {
danghvu50fdc6c2014-05-19 21:21:03 -050080 CS_ARCH_ARM,
81 CS_MODE_ARM,
82 (unsigned char*)RANDOM_CODE,
83 sizeof(RANDOM_CODE) - 1,
Nguyen Anh Quynh2c616562014-05-20 10:30:33 +080084 "Arm - Skip data with callback",
danghvu50fdc6c2014-05-19 21:21:03 -050085 0, 0,
86 CS_OPT_SKIPDATA_SETUP,
87 (size_t) &skipdata_callback,
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080088 },
89 };
90
91 csh handle;
92 uint64_t address = 0x1000;
93 cs_insn *insn;
danghvu69a7c2d2014-05-19 20:52:25 -050094 cs_err err;
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080095 int i;
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +080096 size_t count;
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +080097
98 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
99 printf("****************\n");
100 printf("Platform: %s\n", platforms[i].comment);
Nguyen Anh Quynh655c7022014-04-11 12:15:33 +0800101 err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +0800102 if (err) {
103 printf("Failed on cs_open() with error returned: %u\n", err);
104 continue;
105 }
106
107 if (platforms[i].opt_type)
108 cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
109
Nguyen Anh Quynh943cb2d2014-04-11 17:12:58 +0800110 // turn on SKIPDATA mode
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +0800111 cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
danghvu69a7c2d2014-05-19 20:52:25 -0500112 cs_option(handle, platforms[i].opt_skipdata, platforms[i].skipdata);
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +0800113
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800114 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +0800115 if (count) {
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +0800116 size_t j;
117
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +0800118 print_string_hex(platforms[i].code, platforms[i].size);
119 printf("Disasm:\n");
120
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +0800121 for (j = 0; j < count; j++) {
122 printf("0x%"PRIx64":\t%s\t\t%s\n",
123 insn[j].address, insn[j].mnemonic, insn[j].op_str);
124 }
125
126 // print out the next offset, after the last insn
127 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
128
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800129 // free memory allocated by cs_disasm()
Nguyen Anh Quynhc8258192014-04-10 17:49:50 +0800130 cs_free(insn, count);
131 } else {
132 printf("****************\n");
133 printf("Platform: %s\n", platforms[i].comment);
134 print_string_hex(platforms[i].code, platforms[i].size);
135 printf("ERROR: Failed to disasm given code!\n");
136 }
137
138 printf("\n");
139
140 cs_close(&handle);
141 }
142}
143
144int main()
145{
146 test();
147
148#if 0
149 #define offsetof(st, m) __builtin_offsetof(st, m)
150
151 cs_insn insn;
152 printf("size: %lu\n", sizeof(insn));
153 printf("@id: %lu\n", offsetof(cs_insn, id));
154 printf("@address: %lu\n", offsetof(cs_insn, address));
155 printf("@size: %lu\n", offsetof(cs_insn, size));
156 printf("@bytes: %lu\n", offsetof(cs_insn, bytes));
157 printf("@mnemonic: %lu\n", offsetof(cs_insn, mnemonic));
158 printf("@op_str: %lu\n", offsetof(cs_insn, op_str));
159 printf("@regs_read: %lu\n", offsetof(cs_insn, regs_read));
160 printf("@regs_read_count: %lu\n", offsetof(cs_insn, regs_read_count));
161 printf("@regs_write: %lu\n", offsetof(cs_insn, regs_write));
162 printf("@regs_write_count: %lu\n", offsetof(cs_insn, regs_write_count));
163 printf("@groups: %lu\n", offsetof(cs_insn, groups));
164 printf("@groups_count: %lu\n", offsetof(cs_insn, groups_count));
165 printf("@arch: %lu\n", offsetof(cs_insn, x86));
166#endif
167
168 return 0;
169}