Debian Codegraph @

tests/test_skipdata.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */cxx . . $ `<file>/tests/test_skipdata.c`/
2 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <capstone/platform.h>cxx . . $ `<file>/include/capstone/platform.h`/
8 #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/
9
10 struct platformcxx . . $ platform# {
11 cs_archcxx . . $ cs_arch# archcxx . . $ platform#arch.;
12 cs_modecxx . . $ cs_mode# modecxx . . $ platform#mode.;
13 unsigned char *codecxx . . $ platform#code.;
14 size_t sizecxx . . $ platform#size.;
15 const char *commentcxx . . $ platform#comment.;
16 cs_opt_typecxx . . $ cs_opt_type# opt_typecxx . . $ platform#opt_type.;
17 cs_opt_valuecxx . . $ cs_opt_value# opt_valuecxx . . $ platform#opt_value.;
18 cs_opt_typecxx . . $ cs_opt_type# opt_skipdatacxx . . $ platform#opt_skipdata.;
19 size_t skipdatacxx . . $ platform#skipdata.;
20 };
21
22 static void print_string_hexcxx . . $ print_string_hex(6165b45f45ef689c).(unsigned char *str, size_t len)
23 {
24 unsigned char *c;
25
26 printfcxx . . $ printf(581fddd72a8db8fc).("Code: ");
27 for (c = str; c < str + len; c++) {
28 printfcxx . . $ printf(581fddd72a8db8fc).("0x%02x ", *c & 0xff);
29 }
30 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
31 }
32
33 #ifdef CAPSTONE_HAS_ARMcxx . . $ `<built-in>:412:9`!
34 static size_t CAPSTONE_APIcxx . . $ `include/capstone/capstone.h:32:9`! mycallbackcxx . . $ mycallback(3095598499184484).(const uint8_tcxx . . $ 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
41 static void testcxx . . $ test(d878ad36e69247c7).()
42 {
43 #ifdef CAPSTONE_HAS_X86cxx . . $ `<built-in>:419:9`!
44 #define X86_CODE32cxx . . $ `tests/test_skipdata.c:44:9`! "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"
45 #endif
46 #define RANDOM_CODEcxx . . $ `tests/test_skipdata.c:46:9`! "\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_X86cxx . . $ `<built-in>:419:9`!)
49 cs_opt_skipdatacxx . . $ cs_opt_skipdata# skipdata = {
50 // rename default "data" instruction from ".byte" to "db"
51 "db",
52 };
53 #endif
54
55 #ifdef CAPSTONE_HAS_ARMcxx . . $ `<built-in>:412:9`!
56 cs_opt_skipdatacxx . . $ cs_opt_skipdata# skipdata_callback = {
57 "db",
58 &mycallbackcxx . . $ mycallback(3095598499184484).,
59 };
60 #endif
61
62 struct platformcxx . . $ platform# platforms[] = {
63 #ifdef CAPSTONE_HAS_X86cxx . . $ `<built-in>:419:9`!
64 {
65 CS_ARCH_X86cxx . . $ CS_ARCH_X86.,
66 CS_MODE_32cxx . . $ CS_MODE_32.,
67 (unsigned char*)X86_CODE32cxx . . $ `tests/test_skipdata.c:44:9`!,
68 sizeof(X86_CODE32cxx . . $ `tests/test_skipdata.c:44:9`!) - 1,
69 "X86 32 (Intel syntax) - Skip data",
70 },
71 {
72 CS_ARCH_X86cxx . . $ CS_ARCH_X86.,
73 CS_MODE_32cxx . . $ CS_MODE_32.,
74 (unsigned char*)X86_CODE32cxx . . $ `tests/test_skipdata.c:44:9`!,
75 sizeof(X86_CODE32cxx . . $ `tests/test_skipdata.c:44:9`!) - 1,
76 "X86 32 (Intel syntax) - Skip data with custom mnemonic",
77 CS_OPT_INVALIDcxx . . $ CS_OPT_INVALID.,
78 CS_OPT_OFFcxx . . $ CS_OPT_OFF.,
79 CS_OPT_SKIPDATA_SETUPcxx . . $ CS_OPT_SKIPDATA_SETUP.,
80 (size_t) &skipdata,
81 },
82 #endif
83 #ifdef CAPSTONE_HAS_ARMcxx . . $ `<built-in>:412:9`!
84 {
85 CS_ARCH_ARMcxx . . $ CS_ARCH_ARM.,
86 CS_MODE_ARMcxx . . $ CS_MODE_ARM.,
87 (unsigned char*)RANDOM_CODEcxx . . $ `tests/test_skipdata.c:46:9`!,
88 sizeof(RANDOM_CODEcxx . . $ `tests/test_skipdata.c:46:9`!) - 1,
89 "Arm - Skip data",
90 },
91 {
92 CS_ARCH_ARMcxx . . $ CS_ARCH_ARM.,
93 CS_MODE_ARMcxx . . $ CS_MODE_ARM.,
94 (unsigned char*)RANDOM_CODEcxx . . $ `tests/test_skipdata.c:46:9`!,
95 sizeof(RANDOM_CODEcxx . . $ `tests/test_skipdata.c:46:9`!) - 1,
96 "Arm - Skip data with callback",
97 CS_OPT_INVALIDcxx . . $ CS_OPT_INVALID.,
98 CS_OPT_OFFcxx . . $ CS_OPT_OFF.,
99 CS_OPT_SKIPDATA_SETUPcxx . . $ CS_OPT_SKIPDATA_SETUP.,
100 (size_t) &skipdata_callback,
101 },
102 #endif
103 };
104
105 cshcxx . . $ csh# handle;
106 uint64_tcxx . . $ uint64_t# address = 0x1000;
107 cs_insncxx . . $ cs_insn# *insn;
108 cs_errcxx . . $ cs_err# err;
109 int i;
110 size_t count;
111
112 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
113 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
114 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
115 err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(platforms[i].archcxx . . $ platform#arch., platforms[i].modecxx . . $ platform#mode., &handle);
116 if (err) {
117 printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err);
118 abortcxx . . $ abort(8df32a62017fbbf6).();
119 }
120
121 if (platforms[i].opt_typecxx . . $ platform#opt_type.)
122 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handle, platforms[i].opt_typecxx . . $ platform#opt_type., platforms[i].opt_valuecxx . . $ platform#opt_value.);
123
124 // turn on SKIPDATA mode
125 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handle, CS_OPT_SKIPDATAcxx . . $ CS_OPT_SKIPDATA., CS_OPT_ONcxx . . $ CS_OPT_ON.);
126 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handle, platforms[i].opt_skipdatacxx . . $ platform#opt_skipdata., platforms[i].skipdatacxx . . $ platform#skipdata.);
127
128 count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handle, platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size., address, 0, &insn);
129 if (count) {
130 size_t j;
131
132 print_string_hexcxx . . $ print_string_hex(6165b45f45ef689c).(platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
133 printfcxx . . $ printf(581fddd72a8db8fc).("Disasm:\n");
134
135 for (j = 0; j < count; j++) {
136 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\t%s\t\t%s\n",
137 insn[j].addresscxx . . $ cs_insn#address., insn[j].mnemoniccxx . . $ cs_insn#mnemonic., insn[j].op_strcxx . . $ cs_insn#op_str.);
138 }
139
140 // print out the next offset, after the last insn
141 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\n", insn[j-1].addresscxx . . $ cs_insn#address. + insn[j-1].sizecxx . . $ cs_insn#size.);
142
143 // free memory allocated by cs_disasm()
144 cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count);
145 } else {
146 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
147 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
148 print_string_hexcxx . . $ print_string_hex(6165b45f45ef689c).(platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
149 printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n");
150 abortcxx . . $ abort(8df32a62017fbbf6).();
151 }
152
153 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
154
155 cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handle);
156 }
157 }
158
159 int maincxx . . $ main(d1b40a211a1a4e4c).()
160 {
161 testcxx . . $ test(d878ad36e69247c7).();
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 }