Rizin
unix-like reverse engineering framework and cli tools
test_customized_mnem.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.

Macros

#define X86_CODE32   "\x75\x01"
 

Functions

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

Macro Definition Documentation

◆ X86_CODE32

#define X86_CODE32   "\x75\x01"

Definition at line 13 of file test_customized_mnem.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 81 of file test_customized_mnem.c.

82 {
83  test();
84 
85  return 0;
86 }
static void test()

References test().

◆ print_insn()

static void print_insn ( csh  handle)
static

Definition at line 27 of file test_customized_mnem.c.

28 {
29  cs_insn *insn;
30  size_t count;
31 
32  count = cs_disasm(handle, (const uint8_t *)X86_CODE32, sizeof(X86_CODE32) - 1, 0x1000, 1, &insn);
33  if (count) {
34  print_string_hex((unsigned char *)X86_CODE32, sizeof(X86_CODE32) - 1);
35  printf("\t%s\t%s\n", insn[0].mnemonic, insn[0].op_str);
36  // Free memory allocated by cs_disasm()
37  cs_free(insn, count);
38  } else {
39  printf("ERROR: Failed to disasm given code!\n");
40  abort();
41  }
42 }
static mcore_handle handle
Definition: asm_mcore.c:8
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 void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
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
unsigned char uint8_t
Definition: sftypes.h:31
#define X86_CODE32
static void print_string_hex(unsigned char *str, size_t len)
mnemonic
Definition: z80asm.h:48

References count, cs_disasm(), cs_free(), handle, print_string_hex(), printf(), and X86_CODE32.

Referenced by test().

◆ print_string_hex()

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

Definition at line 16 of file test_customized_mnem.c.

17 {
18  unsigned char *c;
19 
20  for (c = str; c < str + len; c++) {
21  printf("%02x ", *c & 0xff);
22  }
23  printf("\t");
24 }
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 print_insn().

◆ test()

static void test ( )
static

Definition at line 44 of file test_customized_mnem.c.

45 {
46  csh handle;
47  cs_err err;
48  // Customize mnemonic JNE to "jnz"
49  cs_opt_mnem my_mnem = { X86_INS_JNE, "jnz" };
50  // Set .mnemonic to NULL to reset to default mnemonic
51  cs_opt_mnem default_mnem = { X86_INS_JNE, NULL };
52 
54  if (err) {
55  printf("Failed on cs_open() with error returned: %u\n", err);
56  abort();
57  }
58 
59  // 1. Print out the instruction in default setup.
60  printf("Disassemble X86 code with default instruction mnemonic\n");
62 
63  // Customized mnemonic JNE to JNZ using CS_OPT_MNEMONIC option
64  printf("\nNow customize engine to change mnemonic from 'JNE' to 'JNZ'\n");
65  cs_option(handle, CS_OPT_MNEMONIC, (size_t)&my_mnem);
66 
67  // 2. Now print out the instruction in newly customized setup.
69 
70  // Reset engine to use the default mnemonic of JNE
71  printf("\nReset engine to use the default mnemonic\n");
72  cs_option(handle, CS_OPT_MNEMONIC, (size_t)&default_mnem);
73 
74  // 3. Now print out the instruction in default setup.
76 
77  // Done
78  cs_close(&handle);
79 }
static bool err
Definition: armass.c:435
@ CS_ARCH_X86
X86 architecture (including x86 & x86-64)
Definition: capstone.h:78
@ CS_MODE_32
32-bit mode (X86)
Definition: capstone.h:106
@ CS_OPT_MNEMONIC
Customize instruction mnemonic.
Definition: capstone.h:176
size_t csh
Definition: capstone.h:71
@ X86_INS_JNE
Definition: x86.h:651
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
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
static void print_insn(csh handle)

References CS_ARCH_X86, cs_close(), CS_MODE_32, cs_open(), CS_OPT_MNEMONIC, cs_option(), err, handle, NULL, print_insn(), printf(), and X86_INS_JNE.

Referenced by main().