Rizin
unix-like reverse engineering framework and cli tools
test_skipdata.c
Go to the documentation of this file.
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
static ut8 bytes[32]
Definition: asm_arc.c:23
static mcore_handle handle
Definition: asm_mcore.c:8
cs_arch
Architecture type.
Definition: capstone.h:74
@ CS_ARCH_X86
X86 architecture (including x86 & x86-64)
Definition: capstone.h:78
@ CS_ARCH_ARM
ARM architecture (including Thumb, Thumb-2)
Definition: capstone.h:75
cs_mode
Mode type.
Definition: capstone.h:102
@ CS_MODE_32
32-bit mode (X86)
Definition: capstone.h:106
@ CS_MODE_ARM
32-bit ARM
Definition: capstone.h:104
cs_opt_type
Runtime option for the disassembled engine.
Definition: capstone.h:168
@ CS_OPT_SKIPDATA_SETUP
Setup user-defined function for SKIPDATA option.
Definition: capstone.h:175
@ CS_OPT_INVALID
No option specified.
Definition: capstone.h:169
@ CS_OPT_SKIPDATA
Skip data when disassembling. Then engine is in SKIPDATA mode.
Definition: capstone.h:174
size_t csh
Definition: capstone.h:71
cs_opt_value
Runtime option value (associated with option type above)
Definition: capstone.h:181
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
@ CS_OPT_OFF
Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
Definition: capstone.h:182
#define CAPSTONE_API
Definition: capstone.h:32
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:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
cs_arch arch
Definition: cstool.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
struct platform platforms[]
Definition: fuzz_diff.c:18
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
#define offsetof(type, member)
void * p
Definition: libc.cpp:67
int size_t
Definition: sftypes.h:40
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
#define c(i)
Definition: sha256.c:43
Definition: buffer.h:15
Definition: inftree9.h:24
cs_opt_type opt_skipdata
Definition: test_skipdata.c:18
cs_opt_type opt_type
Definition: test_basic.c:16
size_t skipdata
Definition: test_skipdata.c:19
cs_opt_value opt_value
Definition: test_basic.c:17
unsigned char * code
#define PRIx64
Definition: sysdefs.h:94
#define X86_CODE32
static void test()
Definition: test_skipdata.c:41
int main()
#define RANDOM_CODE
static void print_string_hex(unsigned char *str, size_t len)
Definition: test_skipdata.c:22
mnemonic
Definition: z80asm.h:48
#define buffer_size(buffer)