1 /* Capstone Disassembler Engine */
2 /* By Spike <spikeinhouse@gmail.com>, 2018 */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9 
10 static csh handle;
11 
12 struct platform {
13 	cs_arch arch;
14 	cs_mode mode;
15 	unsigned char *code;
16 	size_t size;
17 	const char *comment;
18 };
19 
print_string_hex(const char * comment,unsigned char * str,size_t len)20 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
21 {
22 	unsigned char *c;
23 
24 	printf("%s", comment);
25 	for (c = str; c < str + len; c++) {
26 		printf("0x%02x ", *c & 0xff);
27 	}
28 
29 	printf("\n");
30 }
31 
print_insn_detail(csh cs_handle,cs_insn * ins)32 static void print_insn_detail(csh cs_handle, cs_insn *ins)
33 {
34 	cs_wasm *wasm;
35 
36 	// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
37 	if (ins->detail == NULL)
38 		return;
39 
40 	if (ins->detail->groups_count) {
41 		int j;
42 
43 		printf("\tGroups: ");
44 		for(j = 0; j < ins->detail->groups_count; j++) {
45 			printf("%s ", cs_group_name(handle, ins->detail->groups[j]));
46 		}
47 		printf("\n");
48 	}
49 
50 	wasm = &(ins->detail->wasm);
51 
52 	if (wasm->op_count > 0) {
53 		unsigned int i;
54 
55 		printf("\tOperand count: %u\n", wasm->op_count);
56 
57 		for (i = 0; i < wasm->op_count; i++) {
58 			switch (wasm->operands[i].type) {
59 				default:
60 					break;
61 				case WASM_OP_INT7:
62 					printf("\t\tOperand[%u] type: int7\n", i);
63 					printf("\t\tOperand[%u] value: %d\n", i, wasm->operands[i].int7);
64 					break;
65 				case WASM_OP_UINT32:
66 					printf("\t\tOperand[%u] type: uint32\n", i);
67 					printf("\t\tOperand[%u] value: 0x%x\n", i, wasm->operands[i].uint32);
68 					break;
69 				case WASM_OP_UINT64:
70 					printf("\t\tOperand[%u] type: uint64\n", i);
71 					printf("\t\tOperand[%u] value: 0x%" PRIx64 "\n", i, wasm->operands[i].uint64);
72 					break;
73 				case WASM_OP_VARUINT32:
74 					printf("\t\tOperand[%u] type: varuint32\n", i);
75 					printf("\t\tOperand[%u] value: 0x%x\n", i, wasm->operands[i].varuint32);
76 					break;
77 				case WASM_OP_VARUINT64:
78 					printf("\t\tOperand[%u] type: varuint64\n", i);
79 					printf("\t\tOperand[%u] value: 0x%" PRIx64 "\n", i, wasm->operands[i].varuint64);
80 					break;
81 			}
82 			printf("\t\tOperand[%u] size: %u\n", i, wasm->operands[i].size);
83 		}
84 	}
85 }
86 
test()87 static void test()
88 {
89 #define WASM_CODE "\x20\x00\x20\x01\x41\x20\x10\xc9\x01\x45\x0b"
90 	struct platform platforms[] = {
91 		{
92 			CS_ARCH_WASM,
93 			0,
94 			(unsigned char *)WASM_CODE,
95 			sizeof(WASM_CODE) - 1,
96 			"WASM"
97 		},
98 	};
99 
100 	uint64_t address = 0xffff;
101 	cs_insn *insn;
102 	size_t count;
103 	int i;
104 
105 	for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
106 		cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
107 		if (err) {
108 			printf("Failed on cs_open() with error returned: %u\n", err);
109 			abort();
110 		}
111 
112 		cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
113 
114 		count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
115 		if (count) {
116 			size_t j;
117 			printf("****************\n");
118 			printf("Platform: %s\n", platforms[i].comment);
119 			print_string_hex("Code: ", platforms[i].code, platforms[i].size);
120 			printf("Disasm:\n");
121 
122 			for (j = 0; j < count; j++) {
123 				printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
124 				print_insn_detail(handle, &insn[j]);
125 			}
126 			printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
127 
128 			// free memory allocated by cs_disasm()
129 			cs_free(insn, count);
130 		} else {
131 			printf("****************\n");
132 			printf("Platform: %s\n", platforms[i].comment);
133 			print_string_hex("Code: ", platforms[i].code, platforms[i].size);
134 			printf("ERROR: Failed to disasm given code!\n");
135 			abort();
136 		}
137 
138 		printf("\n");
139 
140 		cs_close(&handle);
141 	}
142 }
143 
main()144 int main()
145 {
146 	test();
147 	return 0;
148 }
149 
150