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 <platform.h>
8 #include <capstone.h>
9 
10 struct 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;
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 
mycallback(const uint8_t * buffer,size_t buffer_size,size_t offset,void * p)33 static size_t CAPSTONE_API mycallback(const uint8_t *buffer, size_t buffer_size, size_t offset, void *p)
34 {
35 	// always skip 2 bytes when encountering data
36 	return 2;
37 }
38 
test()39 static 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 
44 	cs_opt_skipdata skipdata = {
45 		// rename default "data" instruction from ".byte" to "db"
46 		"db",
47 	};
48 
49 	cs_opt_skipdata skipdata_callback = {
50 		"db",
51 		&mycallback,
52 	};
53 
54 	struct platform platforms[] = {
55 		{
56 			CS_ARCH_X86,
57 			CS_MODE_32,
58 			(unsigned char*)X86_CODE32,
59 			sizeof(X86_CODE32) - 1,
60 			"X86 32 (Intel syntax) - Skip data",
61 		},
62 		{
63 			CS_ARCH_ARM,
64 			CS_MODE_ARM,
65 			(unsigned char*)RANDOM_CODE,
66 			sizeof(RANDOM_CODE) - 1,
67 			"Arm - Skip data",
68 		},
69 		{
70 			CS_ARCH_X86,
71 			CS_MODE_32,
72 			(unsigned char*)X86_CODE32,
73 			sizeof(X86_CODE32) - 1,
74 			"X86 32 (Intel syntax) - Skip data with custom mnemonic",
75 			CS_OPT_INVALID,
76 			CS_OPT_OFF,
77 			CS_OPT_SKIPDATA_SETUP,
78 			(size_t) &skipdata,
79 		},
80 		{
81 			CS_ARCH_ARM,
82 			CS_MODE_ARM,
83 			(unsigned char*)RANDOM_CODE,
84 			sizeof(RANDOM_CODE) - 1,
85 			"Arm - Skip data with callback",
86 			CS_OPT_INVALID,
87 			CS_OPT_OFF,
88 			CS_OPT_SKIPDATA_SETUP,
89 			(size_t) &skipdata_callback,
90 		},
91 	};
92 
93 	csh handle;
94 	uint64_t address = 0x1000;
95 	cs_insn *insn;
96 	cs_err err;
97 	int i;
98 	size_t count;
99 
100 	for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
101 		printf("****************\n");
102 		printf("Platform: %s\n", platforms[i].comment);
103 		err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
104 		if (err) {
105 			printf("Failed on cs_open() with error returned: %u\n", err);
106 			continue;
107 		}
108 
109 		if (platforms[i].opt_type)
110 			cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
111 
112 		// turn on SKIPDATA mode
113 		cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
114 		cs_option(handle, platforms[i].opt_skipdata, platforms[i].skipdata);
115 
116 		count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
117 		if (count) {
118 			size_t j;
119 
120 			print_string_hex(platforms[i].code, platforms[i].size);
121 			printf("Disasm:\n");
122 
123 			for (j = 0; j < count; j++) {
124 				printf("0x%" PRIx64 ":\t%s\t\t%s\n",
125 						insn[j].address, insn[j].mnemonic, insn[j].op_str);
126 			}
127 
128 			// print out the next offset, after the last insn
129 			printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
130 
131 			// free memory allocated by cs_disasm()
132 			cs_free(insn, count);
133 		} else {
134 			printf("****************\n");
135 			printf("Platform: %s\n", platforms[i].comment);
136 			print_string_hex(platforms[i].code, platforms[i].size);
137 			printf("ERROR: Failed to disasm given code!\n");
138 		}
139 
140 		printf("\n");
141 
142 		cs_close(&handle);
143 	}
144 }
145 
main()146 int main()
147 {
148 	test();
149 
150 #if 0
151 	#define offsetof(st, m) __builtin_offsetof(st, m)
152 
153 	cs_insn insn;
154 	printf("size: %lu\n", sizeof(insn));
155 	printf("@id: %lu\n", offsetof(cs_insn, id));
156 	printf("@address: %lu\n", offsetof(cs_insn, address));
157 	printf("@size: %lu\n", offsetof(cs_insn, size));
158 	printf("@bytes: %lu\n", offsetof(cs_insn, bytes));
159 	printf("@mnemonic: %lu\n", offsetof(cs_insn, mnemonic));
160 	printf("@op_str: %lu\n", offsetof(cs_insn, op_str));
161 	printf("@regs_read: %lu\n", offsetof(cs_insn, regs_read));
162 	printf("@regs_read_count: %lu\n", offsetof(cs_insn, regs_read_count));
163 	printf("@regs_write: %lu\n", offsetof(cs_insn, regs_write));
164 	printf("@regs_write_count: %lu\n", offsetof(cs_insn, regs_write_count));
165 	printf("@groups: %lu\n", offsetof(cs_insn, groups));
166 	printf("@groups_count: %lu\n", offsetof(cs_insn, groups_count));
167 	printf("@arch: %lu\n", offsetof(cs_insn, x86));
168 #endif
169 
170 	return 0;
171 }
172