add cs_option() API. move ATT & Intel syntax here, rather than having them as CS_MODE, which is wrong
diff --git a/cs.c b/cs.c
index 5e1e68f..250fa22 100644
--- a/cs.c
+++ b/cs.c
@@ -70,10 +70,8 @@
switch (ud->arch) {
case CS_ARCH_X86:
- if (ud->mode & CS_MODE_SYNTAX_ATT)
- ud->printer = X86_ATT_printInst;
- else
- ud->printer = X86_Intel_printInst;
+ // by default, we use Intel syntax
+ ud->printer = X86_Intel_printInst;
ud->printer_info = NULL;
ud->disasm = X86_getInstruction;
ud->reg_name = X86_reg_name;
@@ -198,6 +196,21 @@
insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
}
+cs_err cs_option(csh ud, unsigned int option)
+{
+ cs_struct *handle = (cs_struct *)(uintptr_t)ud;
+ if (!handle)
+ return CS_ERR_CSH;
+
+ if (option & CS_OPT_X86_INTEL)
+ handle->printer = X86_Intel_printInst;
+
+ if (option & CS_OPT_X86_ATT)
+ handle->printer = X86_ATT_printInst;
+
+ return CS_ERR_OK;
+}
+
size_t cs_disasm(csh ud, unsigned char *buffer, size_t size, uint64_t offset, size_t count, cs_insn *insn)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;
diff --git a/include/capstone.h b/include/capstone.h
index a2f5a44..3609ae7 100644
--- a/include/capstone.h
+++ b/include/capstone.h
@@ -26,7 +26,6 @@
// Mode type
typedef enum cs_mode {
CS_MODE_LITTLE_ENDIAN = 0, // little endian mode (default mode)
- CS_MODE_SYNTAX_INTEL = 0, // Intel X86 asm syntax (CS_ARCH_X86 architecture)
CS_MODE_ARM = 0, // 32-bit ARM
CS_MODE_16 = 1 << 1, // 16-bit mode
CS_MODE_32 = 1 << 2, // 32-bit mode
@@ -34,11 +33,17 @@
CS_MODE_THUMB = 1 << 4, // ARM's Thumb mode, including Thumb-2
CS_MODE_MICRO = 1 << 4, // MicroMips mode (MIPS architecture)
CS_MODE_N64 = 1 << 5, // Nintendo-64 mode (MIPS architecture)
- CS_MODE_SYNTAX_ATT = 1 << 30, // ATT asm syntax (CS_ARCH_X86 architecture)
CS_MODE_BIG_ENDIAN = 1 << 31 // big endian mode
} cs_mode;
+// Option type
+typedef enum cs_opt {
+ CS_OPT_X86_INTEL = 1 << 0, // Intel X86 asm syntax (CS_ARCH_X86 arch)
+ CS_OPT_X86_ATT = 1 << 1, // ATT asm syntax (CS_ARCH_X86 arch)
+} cs_opt;
+
+
#include "arm.h"
#include "arm64.h"
#include "mips.h"
@@ -125,6 +130,17 @@
cs_err cs_close(csh handle);
/*
+ Set option for disassembling
+
+ @handle: handle returned by cs_open()
+ @option: option to be set, which can be OR by several cs_opt enums
+
+ @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
+ for detailed error).
+*/
+cs_err cs_option(csh handle, unsigned int option);
+
+/*
Report the last error number when some API function fail.
Like glibc's errno, cs_errno might not retain its old value once accessed.
diff --git a/tests/test.c b/tests/test.c
index d6ab076..1f935a0 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -13,6 +13,7 @@
unsigned char *code;
size_t size;
char *comment;
+ cs_opt option;
};
static void print_string_hex(unsigned char *str, int len)
@@ -54,10 +55,11 @@
},
{
.arch = CS_ARCH_X86,
- .mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT,
+ .mode = CS_MODE_32,
.code = (unsigned char*)X86_CODE32,
.size = sizeof(X86_CODE32) - 1,
- .comment = "X86 32bit (ATT syntax)"
+ .comment = "X86 32bit (ATT syntax)",
+ .option = CS_OPT_X86_ATT,
},
{
.arch = CS_ARCH_X86,
@@ -137,6 +139,9 @@
return;
}
+ if (platforms[i].option)
+ cs_option(handle, platforms[i].option);
+
//size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, insn);
size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
diff --git a/tests/test_detail.c b/tests/test_detail.c
index 48bddfa..041f6d7 100644
--- a/tests/test_detail.c
+++ b/tests/test_detail.c
@@ -13,6 +13,7 @@
unsigned char *code;
size_t size;
char *comment;
+ cs_opt option;
};
static void print_string_hex(unsigned char *str, int len)
@@ -59,10 +60,11 @@
},
{
.arch = CS_ARCH_X86,
- .mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT,
+ .mode = CS_MODE_32,
.code = (unsigned char *)X86_CODE32,
.size = sizeof(X86_CODE32) - 1,
- .comment = "X86 32bit (ATT syntax)"
+ .comment = "X86 32bit (ATT syntax)",
+ .option = CS_OPT_X86_ATT,
},
{
.arch = CS_ARCH_X86,
@@ -139,6 +141,9 @@
if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
return;
+ if (platforms[i].option)
+ cs_option(handle, platforms[i].option);
+
//size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, all_insn);
size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &all_insn);
if (count) {
diff --git a/tests/test_x86.c b/tests/test_x86.c
index 9b085bd..f1f4bb4 100644
--- a/tests/test_x86.c
+++ b/tests/test_x86.c
@@ -15,6 +15,7 @@
unsigned char *code;
size_t size;
char *comment;
+ cs_opt option;
};
static void print_string_hex(char *comment, unsigned char *str, int len)
@@ -131,10 +132,11 @@
},
{
.arch = CS_ARCH_X86,
- .mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT,
+ .mode = CS_MODE_32,
.code = (unsigned char *)X86_CODE32,
.size = sizeof(X86_CODE32) - 1,
- .comment = "X86 32 (AT&T syntax)"
+ .comment = "X86 32 (AT&T syntax)",
+ .option = CS_OPT_X86_ATT,
},
{
.arch = CS_ARCH_X86,
@@ -161,6 +163,9 @@
if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
return;
+ if (platforms[i].option)
+ cs_option(handle, platforms[i].option);
+
//size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, insn);
size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {