test_skipdata.c
Go to the documentation of this file.
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;
19  size_t skipdata;
20 };
21 
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
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 
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  {
66  CS_MODE_32,
67  (unsigned char*)X86_CODE32,
68  sizeof(X86_CODE32) - 1,
69  "X86 32 (Intel syntax) - Skip data",
70  },
71  {
73  CS_MODE_32,
74  (unsigned char*)X86_CODE32,
75  sizeof(X86_CODE32) - 1,
76  "X86 32 (Intel syntax) - Skip data with custom mnemonic",
78  CS_OPT_OFF,
80  (size_t) &skipdata,
81  },
82 #endif
83 #ifdef CAPSTONE_HAS_ARM
84  {
87  (unsigned char*)RANDOM_CODE,
88  sizeof(RANDOM_CODE) - 1,
89  "Arm - Skip data",
90  },
91  {
94  (unsigned char*)RANDOM_CODE,
95  sizeof(RANDOM_CODE) - 1,
96  "Arm - Skip data with callback",
98  CS_OPT_OFF,
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);
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)
123 
124  // turn on SKIPDATA mode
127 
128  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
129  if (count) {
130  size_t j;
131 
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);
149  printf("ERROR: Failed to disasm given code!\n");
150  abort();
151  }
152 
153  printf("\n");
154 
155  cs_close(&handle);
156  }
157 }
158 
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 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
cs_close
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:522
cs_opt_type
cs_opt_type
Runtime option for the disassembled engine.
Definition: capstone.h:169
CS_MODE_32
@ CS_MODE_32
32-bit mode (X86)
Definition: capstone.h:107
CS_OPT_INVALID
@ CS_OPT_INVALID
No option specified.
Definition: capstone.h:170
platform::opt_type
cs_opt_type opt_type
Definition: test_basic.c:16
platform::code
unsigned char * code
Definition: test_arm_regression.c:21
CS_MODE_ARM
@ CS_MODE_ARM
32-bit ARM
Definition: capstone.h:105
cs_disasm
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:822
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
error_ref_leak.err
err
Definition: error_ref_leak.py:35
cs_open
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:474
platform::mode
cs_mode mode
Definition: test_arm_regression.c:20
cs_arch
cs_arch
Architecture type.
Definition: capstone.h:74
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
xds_manager.p
p
Definition: xds_manager.py:60
CS_OPT_OFF
@ CS_OPT_OFF
Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
Definition: capstone.h:183
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
test
static void test()
Definition: test_skipdata.c:41
cs_option
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:670
cs_mode
cs_mode
Mode type.
Definition: capstone.h:103
capstone.h
platform::opt_skipdata
cs_opt_type opt_skipdata
Definition: test_skipdata.c:18
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
main
int main()
Definition: test_skipdata.c:159
CAPSTONE_API
#define CAPSTONE_API
Definition: capstone.h:32
cs_opt_value
cs_opt_value
Runtime option value (associated with option type above)
Definition: capstone.h:182
CS_ARCH_X86
@ CS_ARCH_X86
X86 architecture (including x86 & x86-64)
Definition: capstone.h:78
platform.h
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
CS_OPT_ON
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:184
platform::comment
char * comment
Definition: test_arm_regression.c:23
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
CS_OPT_SKIPDATA
@ CS_OPT_SKIPDATA
Skip data when disassembling. Then engine is in SKIPDATA mode.
Definition: capstone.h:175
arch
cs_arch arch
Definition: cstool.c:13
platform::arch
cs_arch arch
Definition: test_arm_regression.c:19
csh
size_t csh
Definition: capstone.h:71
make_dist_html.groups
list groups
Definition: make_dist_html.py:120
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
X86_CODE32
#define X86_CODE32
CS_OPT_SKIPDATA_SETUP
@ CS_OPT_SKIPDATA_SETUP
Setup user-defined function for SKIPDATA option.
Definition: capstone.h:176
CS_ARCH_ARM
@ CS_ARCH_ARM
ARM architecture (including Thumb, Thumb-2)
Definition: capstone.h:75
RANDOM_CODE
#define RANDOM_CODE
x86
Definition: test_winkernel.cpp:83
print_string_hex
static void print_string_hex(unsigned char *str, size_t len)
Definition: test_skipdata.c:22
platform::opt_value
cs_opt_value opt_value
Definition: test_basic.c:17
skipdata
Definition: test_winkernel.cpp:43
cs_free
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1039
handle
static csh handle
Definition: test_arm_regression.c:16
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
platforms
struct platform platforms[]
Definition: fuzz_diff.c:18
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
versiongenerate.buffer_size
int buffer_size
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/xcode/Scripts/versiongenerate.py:65
platform
Definition: test_arm_regression.c:18
platform::skipdata
size_t skipdata
Definition: test_skipdata.c:19
platform::size
size_t size
Definition: test_arm_regression.c:22
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:32