Skip to content

Commit

Permalink
change cs_disasm() and cs_disasm_dyn() to be portable API. fix relate…
Browse files Browse the repository at this point in the history
…d code using these API
  • Loading branch information
aquynh committed Nov 29, 2013
1 parent 0b6f1bd commit b42a657
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 151 deletions.
8 changes: 4 additions & 4 deletions arch/AArch64/AArch64Disassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ void AArch64_init(MCRegisterInfo *MRI)


static DecodeStatus _getInstruction(MCInst *MI,
unsigned char *code, uint64_t code_len,
unsigned char *code, size_t code_len,
uint16_t *Size,
uint64_t Address, MCRegisterInfo *MRI)
size_t Address, MCRegisterInfo *MRI)
{
if (code_len < 4) {
// not enough data
Expand All @@ -254,10 +254,10 @@ static DecodeStatus _getInstruction(MCInst *MI,
return MCDisassembler_Fail;
}

bool AArch64_getInstruction(csh ud, char *code, uint64_t code_len, MCInst *instr, uint16_t *size, uint64_t address, void *info)
bool AArch64_getInstruction(csh ud, unsigned char *code, size_t code_len, MCInst *instr, uint16_t *size, size_t address, void *info)
{
DecodeStatus status = _getInstruction(instr,
(unsigned char *)code, code_len,
code, code_len,
size,
address, (MCRegisterInfo *)info);

Expand Down
4 changes: 2 additions & 2 deletions arch/AArch64/AArch64Disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

void AArch64_init(MCRegisterInfo *MRI);

bool AArch64_getInstruction(csh ud, char *code, uint64_t code_len,
MCInst *instr, uint16_t *size, uint64_t address, void *info);
bool AArch64_getInstruction(csh ud, unsigned char *code, size_t code_len,
MCInst *instr, uint16_t *size, size_t address, void *info);

#endif

16 changes: 8 additions & 8 deletions arch/ARM/ARMDisassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ void ARM_init(MCRegisterInfo *MRI)
0);
}

static DecodeStatus _ARM_getInstruction(MCInst *MI, char *code, uint64_t code_len,
uint16_t *Size, uint64_t Address)
static DecodeStatus _ARM_getInstruction(MCInst *MI, unsigned char *code, size_t code_len,
uint16_t *Size, size_t Address)
{
uint8_t bytes[4];

Expand Down Expand Up @@ -644,8 +644,8 @@ static void UpdateThumbVFPPredicate(MCInst *MI)
}
}

static DecodeStatus _Thumb_getInstruction(MCInst *MI, char *code, uint64_t code_len,
uint16_t *Size, uint64_t Address)
static DecodeStatus _Thumb_getInstruction(MCInst *MI, unsigned char *code, size_t code_len,
uint16_t *Size, size_t Address)
{
uint8_t bytes[4];

Expand Down Expand Up @@ -821,17 +821,17 @@ static DecodeStatus _Thumb_getInstruction(MCInst *MI, char *code, uint64_t code_
return MCDisassembler_Fail;
}

bool Thumb_getInstruction(csh ud, char *code, uint64_t code_len, MCInst *instr,
uint16_t *size, uint64_t address, void *info)
bool Thumb_getInstruction(csh ud, unsigned char *code, size_t code_len, MCInst *instr,
uint16_t *size, size_t address, void *info)
{
//cs_struct *handle = (cs_struct *)ud;
DecodeStatus status = _Thumb_getInstruction(instr, code, code_len, size, address);

return status == MCDisassembler_Success;
}

bool ARM_getInstruction(csh ud, char *code, uint64_t code_len, MCInst *instr,
uint16_t *size, uint64_t address, void *info)
bool ARM_getInstruction(csh ud, unsigned char *code, size_t code_len, MCInst *instr,
uint16_t *size, size_t address, void *info)
{
//cs_struct *handle = (cs_struct *)ud;
DecodeStatus status = _ARM_getInstruction(instr, code, code_len, size, address);
Expand Down
4 changes: 2 additions & 2 deletions arch/ARM/ARMDisassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

void ARM_init(MCRegisterInfo *MRI);

bool ARM_getInstruction(csh handle, char *code, uint64_t code_len, MCInst *instr, uint16_t *size, uint64_t address, void *info);
bool ARM_getInstruction(csh handle, unsigned char *code, size_t code_len, MCInst *instr, uint16_t *size, size_t address, void *info);

bool Thumb_getInstruction(csh handle, char *code, uint64_t code_len, MCInst *instr, uint16_t *size, uint64_t address, void *info);
bool Thumb_getInstruction(csh handle, unsigned char *code, size_t code_len, MCInst *instr, uint16_t *size, size_t address, void *info);

#endif
2 changes: 1 addition & 1 deletion arch/ARM/ARMGenDisassemblerTables.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13448,7 +13448,7 @@ DecodeToMCInst(decodeToMCInst_4, fieldFromInstruction_4, uint32_t)

#define DecodeInstruction(fname, fieldname, decoder, InsnType) \
static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \
InsnType insn, uint64_t Address, \
InsnType insn, size_t Address, \
int feature) \
{ \
uint64_t Bits = ARM_getFeatureBits(feature); \
Expand Down
6 changes: 3 additions & 3 deletions arch/ARM/ARMInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static void printRegImmShift(MCInst *MI, SStream *O, ARM_AM_ShiftOpc ShOpc,

//assert (!(ShOpc == ARM_AM_ror && !ShImm) && "Cannot have ror #0");
SStream_concat(O, ARM_AM_getShiftOpcStr(ShOpc));
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count - 1].shift.type = ShOpc;
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count - 1].shift.type = (arm_shifter)ShOpc;

if (ShOpc != ARM_AM_rrx) {
SStream_concat(O, " ");
Expand Down Expand Up @@ -342,7 +342,7 @@ void ARM_printInst(MCInst *MI, SStream *O, void *Info)
SStream_concat(O, ", %s", markup("<imm:"));
SStream_concat(O, "#0x%x", translateShiftImm(getSORegOffset(MCOperand_getImm(MO2))));
SStream_concat(O, markup(">"));
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count - 1].shift.type = ARM_AM_getSORegShOp(MCOperand_getImm(MO2));
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count - 1].shift.type = (arm_shifter)ARM_AM_getSORegShOp(MCOperand_getImm(MO2));
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count - 1].shift.value = translateShiftImm(getSORegOffset(MCOperand_getImm(MO2)));
return;
}
Expand Down Expand Up @@ -622,7 +622,7 @@ static void printAM2PreOrOffsetIndexOp(MCInst *MI, unsigned Op, SStream *O)
if (getAM2Offset(MCOperand_getImm(MO3))) { // Don't print +0.
SStream_concat(O, ", %s", markup("<imm:"));
SStream_concat(O, "#%s0x%x", ARM_AM_getAddrOpcStr(getAM2Op(MCOperand_getImm(MO3))), getAM2Offset(MCOperand_getImm(MO3)));
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count].shift.type = getAM2Op(MCOperand_getImm(MO3));
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count].shift.type = (arm_shifter)getAM2Op(MCOperand_getImm(MO3));
MI->pub_insn.arm.operands[MI->pub_insn.arm.op_count].shift.value = getAM2Offset(MCOperand_getImm(MO3));
SStream_concat(O, markup(">"));
}
Expand Down
16 changes: 8 additions & 8 deletions arch/Mips/MipsDisassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ static DecodeStatus readInstruction32(unsigned char *code, uint32_t *insn, bool
}

static DecodeStatus MipsDisassembler_getInstruction(int mode, MCInst *instr,
char *code, uint64_t code_len,
unsigned char *code, size_t code_len,
uint16_t *Size,
uint64_t Address, bool isBigEndian, MCRegisterInfo *MRI,
size_t Address, bool isBigEndian, MCRegisterInfo *MRI,
bool isMicroMips)
{
uint32_t Insn;
Expand Down Expand Up @@ -279,8 +279,8 @@ static DecodeStatus MipsDisassembler_getInstruction(int mode, MCInst *instr,
return MCDisassembler_Fail;
}

bool Mips_getInstruction(csh ud, char *code, uint64_t code_len, MCInst *instr,
uint16_t *size, uint64_t address, void *info)
bool Mips_getInstruction(csh ud, unsigned char *code, size_t code_len, MCInst *instr,
uint16_t *size, size_t address, void *info)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;

Expand All @@ -294,9 +294,9 @@ bool Mips_getInstruction(csh ud, char *code, uint64_t code_len, MCInst *instr,
}

static DecodeStatus Mips64Disassembler_getInstruction(int mode, MCInst *instr,
char *code, uint64_t code_len,
unsigned char *code, size_t code_len,
uint16_t *Size,
uint64_t Address, bool isBigEndian, MCRegisterInfo *MRI)
size_t Address, bool isBigEndian, MCRegisterInfo *MRI)
{
uint32_t Insn;

Expand All @@ -320,8 +320,8 @@ static DecodeStatus Mips64Disassembler_getInstruction(int mode, MCInst *instr,
return MCDisassembler_Fail;
}

bool Mips64_getInstruction(csh ud, char *code, uint64_t code_len, MCInst *instr,
uint16_t *size, uint64_t address, void *info)
bool Mips64_getInstruction(csh ud, unsigned char *code, size_t code_len, MCInst *instr,
uint16_t *size, size_t address, void *info)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;

Expand Down
8 changes: 4 additions & 4 deletions arch/Mips/MipsDisassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

void Mips_init(MCRegisterInfo *MRI);

bool Mips_getInstruction(csh handle, char *code, uint64_t code_len,
MCInst *instr, uint16_t *size, uint64_t address, void *info);
bool Mips_getInstruction(csh handle, unsigned char *code, size_t code_len,
MCInst *instr, uint16_t *size, size_t address, void *info);

bool Mips64_getInstruction(csh handle, char *code, uint64_t code_len,
MCInst *instr, uint16_t *size, uint64_t address, void *info);
bool Mips64_getInstruction(csh handle, unsigned char *code, size_t code_len,
MCInst *instr, uint16_t *size, size_t address, void *info);

#endif
4 changes: 2 additions & 2 deletions arch/X86/X86Disassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "X86GenInstrInfo.inc"

struct reader_info {
char *code;
unsigned char *code;
uint64_t size;
uint64_t offset;
};
Expand Down Expand Up @@ -611,7 +611,7 @@ static void update_pub_insn(cs_insn *pub, InternalInstruction *inter)
}

// Public interface for the disassembler
bool X86_getInstruction(csh ud, char *code, uint64_t code_len, MCInst *instr, uint16_t *size, uint64_t address, void *_info)
bool X86_getInstruction(csh ud, unsigned char *code, size_t code_len, MCInst *instr, uint16_t *size, size_t address, void *_info)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;
InternalInstruction insn;
Expand Down
4 changes: 2 additions & 2 deletions arch/X86/X86Disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
#undef INSTRUCTION_SPECIFIER_FIELDS
#undef INSTRUCTION_IDS

bool X86_getInstruction(csh handle, char *code, uint64_t code_len,
MCInst *instr, uint16_t *size, uint64_t address, void *info);
bool X86_getInstruction(csh handle, unsigned char *code, size_t code_len,
MCInst *instr, uint16_t *size, size_t address, void *info);

#endif
10 changes: 5 additions & 5 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mc
insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
}

uint64_t cs_disasm(csh ud, char *buffer, uint64_t size, uint64_t offset, uint64_t count, cs_insn *insn)
size_t cs_disasm(csh ud, unsigned char *buffer, size_t size, size_t offset, size_t count, cs_insn *insn)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;
MCInst mci;
uint16_t insn_size;
uint64_t c = 0;
size_t c = 0;

if (!handle) {
// FIXME: handle this case?
Expand Down Expand Up @@ -238,15 +238,15 @@ uint64_t cs_disasm(csh ud, char *buffer, uint64_t size, uint64_t offset, uint64_

// dynamicly allocate memory to contain disasm insn
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
uint64_t cs_disasm_dyn(csh ud, char *buffer, uint64_t size, uint64_t offset, uint64_t count, cs_insn **insn)
size_t cs_disasm_dyn(csh ud, unsigned char *buffer, size_t size, size_t offset, size_t count, cs_insn **insn)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;
MCInst mci;
uint16_t insn_size;
uint64_t c = 0, f = 0;
size_t c = 0, f = 0;
cs_insn insn_cache[64];
void *total = NULL;
uint64_t total_size = 0;
size_t total_size = 0;

if (!handle) {
// FIXME: how to handle this case:
Expand Down
2 changes: 1 addition & 1 deletion cs_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef void (*Printer_t)(MCInst *MI, SStream *OS, void *info);
// this is the best time to gather insn's characteristics
typedef void (*PostPrinter_t)(unsigned int insn, cs_insn *, char *mnem);

typedef bool (*Disasm_t)(csh handle, char *code, uint64_t code_len, MCInst *instr, uint16_t *size, uint64_t address, void *info);
typedef bool (*Disasm_t)(csh handle, unsigned char *code, size_t code_len, MCInst *instr, uint16_t *size, size_t address, void *info);

typedef char *(*GetName_t)(unsigned int reg);

Expand Down
4 changes: 2 additions & 2 deletions include/arm.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct arm_op_mem {
unsigned int base; // base register
unsigned int index; // index register
int scale; // scale for index register (can be 1, or -1)
int64_t disp; // displacement/offset value
int disp; // displacement/offset value
} arm_op_mem;

// Instruction operand
Expand All @@ -74,7 +74,7 @@ typedef struct cs_arm_op {
arm_op_type type; // operand type
union {
unsigned int reg; // register value for REG operand
int64_t imm; // immediate value for C-IMM, P-IMM or IMM operand
unsigned int imm; // immediate value for C-IMM, P-IMM or IMM operand
double fp; // floating point value for FP operand
arm_op_mem mem; // base/index/scale/disp value for MEM operand
};
Expand Down
20 changes: 10 additions & 10 deletions include/capstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern "C" {
#include <stdbool.h>

// Handle using with all API
typedef uint64_t csh;
typedef size_t csh;

// Architecture type
typedef enum cs_arch {
Expand Down Expand Up @@ -52,7 +52,7 @@ typedef struct cs_insn {
unsigned int id;

// Offset address of this instruction
uint64_t address;
size_t address;

// Size of this instruction
uint16_t size;
Expand Down Expand Up @@ -144,10 +144,10 @@ cs_err cs_errno(csh handle);
@return: the number of succesfully disassembled instructions,
or 0 if this function failed to disassemble the given code
*/
uint64_t cs_disasm(csh handle,
char *code, uint64_t code_size,
uint64_t offset,
uint64_t count,
size_t cs_disasm(csh handle,
unsigned char *code, size_t code_size,
size_t offset,
size_t count,
cs_insn *insn);

/*
Expand All @@ -168,10 +168,10 @@ uint64_t cs_disasm(csh handle,
@return: the number of succesfully disassembled instructions,
or 0 if this function failed to disassemble the given code
*/
uint64_t cs_disasm_dyn(csh handle,
char *code, uint64_t code_size,
uint64_t offset,
uint64_t count,
size_t cs_disasm_dyn(csh handle,
unsigned char *code, size_t code_size,
size_t offset,
size_t count,
cs_insn **insn);

/*
Expand Down
Loading

0 comments on commit b42a657

Please sign in to comment.