| 1 | /* Capstone Disassembly Engine */cxx . . $ `<file>/tests/test_customized_mnem.c`/ |
| 2 | /* By Nguyen Anh Quynh <[email protected]>, 2015-2019 */ |
| 3 | |
| 4 | // This sample code demonstrates the option CS_OPT_MNEMONIC |
| 5 | // to customize instruction mnemonic. |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | |
| 10 | #include <capstone/platform.h>cxx . . $ `<file>/include/capstone/platform.h`/ |
| 11 | #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/ |
| 12 | |
| 13 | #define X86_CODE32cxx . . $ `tests/test_customized_mnem.c:13:9`! "\x75\x01" |
| 14 | |
| 15 | // Print out the input code in hexadecimal format |
| 16 | static void print_string_hexcxx . . $ print_string_hex(6165b45f45ef689c).(unsigned char *str, size_t len) |
| 17 | { |
| 18 | unsigned char *c; |
| 19 | |
| 20 | for (c = str; c < str + len; c++) { |
| 21 | printfcxx . . $ printf(581fddd72a8db8fc).("%02x ", *c & 0xff); |
| 22 | } |
| 23 | printfcxx . . $ printf(581fddd72a8db8fc).("\t"); |
| 24 | } |
| 25 | |
| 26 | // Print one instruction |
| 27 | static void print_insncxx . . $ print_insn(2c44cd906ae4e829).(cshcxx . . $ csh# handle) |
| 28 | { |
| 29 | cs_insncxx . . $ cs_insn# *insn; |
| 30 | size_t count; |
| 31 | |
| 32 | count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handle, (const uint8_tcxx . . $ uint8_t# *)X86_CODE32cxx . . $ `tests/test_customized_mnem.c:13:9`!, sizeof(X86_CODE32cxx . . $ `tests/test_customized_mnem.c:13:9`!) - 1, 0x1000, 1, &insn); |
| 33 | if (count) { |
| 34 | print_string_hexcxx . . $ print_string_hex(6165b45f45ef689c).((unsigned char *)X86_CODE32cxx . . $ `tests/test_customized_mnem.c:13:9`!, sizeof(X86_CODE32cxx . . $ `tests/test_customized_mnem.c:13:9`!) - 1); |
| 35 | printfcxx . . $ printf(581fddd72a8db8fc).("\t%s\t%s\n", insn[0].mnemoniccxx . . $ cs_insn#mnemonic., insn[0].op_strcxx . . $ cs_insn#op_str.); |
| 36 | // Free memory allocated by cs_disasm() |
| 37 | cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count); |
| 38 | } else { |
| 39 | printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n"); |
| 40 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static void testcxx . . $ test(d878ad36e69247c7).() |
| 45 | { |
| 46 | cshcxx . . $ csh# handle; |
| 47 | cs_errcxx . . $ cs_err# err; |
| 48 | // Customize mnemonic JNE to "jnz" |
| 49 | cs_opt_mnemcxx . . $ cs_opt_mnem# my_mnem = { X86_INS_JNEcxx . . $ X86_INS_JNE., "jnz" }; |
| 50 | // Set .mnemonic to NULL to reset to default mnemonic |
| 51 | cs_opt_mnemcxx . . $ cs_opt_mnem# default_mnem = { X86_INS_JNEcxx . . $ X86_INS_JNE., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }; |
| 52 | |
| 53 | err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(CS_ARCH_X86cxx . . $ CS_ARCH_X86., CS_MODE_32cxx . . $ CS_MODE_32., &handle); |
| 54 | if (err) { |
| 55 | if (cs_supportcxx . . $ cs_support(83c267b20bac841a).(CS_ARCH_X86cxx . . $ CS_ARCH_X86.)) { |
| 56 | printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err); |
| 57 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 58 | } else |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // 1. Print out the instruction in default setup. |
| 63 | printfcxx . . $ printf(581fddd72a8db8fc).("Disassemble X86 code with default instruction mnemonic\n"); |
| 64 | print_insncxx . . $ print_insn(2c44cd906ae4e829).(handle); |
| 65 | |
| 66 | // Customized mnemonic JNE to JNZ using CS_OPT_MNEMONIC option |
| 67 | printfcxx . . $ printf(581fddd72a8db8fc).("\nNow customize engine to change mnemonic from 'JNE' to 'JNZ'\n"); |
| 68 | cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handle, CS_OPT_MNEMONICcxx . . $ CS_OPT_MNEMONIC., (size_t)&my_mnem); |
| 69 | |
| 70 | // 2. Now print out the instruction in newly customized setup. |
| 71 | print_insncxx . . $ print_insn(2c44cd906ae4e829).(handle); |
| 72 | |
| 73 | // Reset engine to use the default mnemonic of JNE |
| 74 | printfcxx . . $ printf(581fddd72a8db8fc).("\nReset engine to use the default mnemonic\n"); |
| 75 | cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handle, CS_OPT_MNEMONICcxx . . $ CS_OPT_MNEMONIC., (size_t)&default_mnem); |
| 76 | |
| 77 | // 3. Now print out the instruction in default setup. |
| 78 | print_insncxx . . $ print_insn(2c44cd906ae4e829).(handle); |
| 79 | |
| 80 | // Done |
| 81 | cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handle); |
| 82 | } |
| 83 | |
| 84 | int maincxx . . $ main(d1b40a211a1a4e4c).() |
| 85 | { |
| 86 | testcxx . . $ test(d878ad36e69247c7).(); |
| 87 | |
| 88 | return 0; |
| 89 | } |