Rizin
unix-like reverse engineering framework and cli tools
test_skipdata.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <capstone/platform.h>
#include <capstone/capstone.h>

Go to the source code of this file.

Classes

struct  platform
 

Macros

#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"
 

Functions

static void print_string_hex (unsigned char *str, size_t len)
 
static void test ()
 
int main ()
 

Macro Definition Documentation

◆ RANDOM_CODE

#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"

Function Documentation

◆ main()

int main ( void  )

Definition at line 159 of file test_skipdata.c.

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 }
static ut8 bytes[32]
Definition: asm_arc.c:23
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
voidpf void uLong size
Definition: ioapi.h:138
#define offsetof(type, member)
static void test()
Definition: test_skipdata.c:41
mnemonic
Definition: z80asm.h:48

References bytes, make_dist_html::groups, offsetof, printf(), and test().

◆ print_string_hex()

static void print_string_hex ( unsigned char *  str,
size_t  len 
)
static

Definition at line 22 of file test_skipdata.c.

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 }
size_t len
Definition: 6502dis.c:15
#define c(i)
Definition: sha256.c:43

References c, len, printf(), and cmd_descs_generate::str.

Referenced by test().

◆ test()

static void test ( )
static

Definition at line 41 of file test_skipdata.c.

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 }
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
static mcore_handle handle
Definition: asm_mcore.c:8
@ 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_32
32-bit mode (X86)
Definition: capstone.h:106
@ CS_MODE_ARM
32-bit ARM
Definition: capstone.h:104
@ 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_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
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
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
const char int mode
Definition: ioapi.h:137
int size_t
Definition: sftypes.h:40
unsigned long uint64_t
Definition: sftypes.h:28
Definition: inftree9.h:24
cs_opt_type opt_skipdata
Definition: test_skipdata.c:18
cs_opt_type opt_type
Definition: test_basic.c:16
cs_opt_value opt_value
Definition: test_basic.c:17
#define PRIx64
Definition: sysdefs.h:94
#define X86_CODE32
#define RANDOM_CODE
static void print_string_hex(unsigned char *str, size_t len)
Definition: test_skipdata.c:22

References arch, platform::comment, count, CS_ARCH_ARM, CS_ARCH_X86, cs_close(), cs_disasm(), cs_free(), CS_MODE_32, CS_MODE_ARM, cs_open(), CS_OPT_INVALID, CS_OPT_OFF, CS_OPT_ON, CS_OPT_SKIPDATA, CS_OPT_SKIPDATA_SETUP, cs_option(), err, handle, i, platform::opt_skipdata, platform::opt_type, platform::opt_value, platforms, print_string_hex(), printf(), PRIx64, RANDOM_CODE, and X86_CODE32.

Referenced by main().