Skip to content

Commit

Permalink
handle mnemonic customization better. issue capstone-engine#1514
Browse files Browse the repository at this point in the history
  • Loading branch information
aquynh committed Jul 10, 2019
1 parent 4304060 commit 6065328
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,23 @@ cs_err CAPSTONE_API cs_close(csh *handle)
return CS_ERR_OK;
}

// replace str1 in target with str2; target starts with str1
// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
// return 0 on success, -1 on failure
static int str_replace(char *result, char *target, const char *str1, char *str2)
{
// only perform replacement if the output fits into result
if (strlen(target) - strlen(str1) + strlen(str2) < CS_MNEMONIC_SIZE - 1) {
// copy str2 to begining of result
strcpy(result, str2);
// skip str1 - already replaced by str2
strcat(result, target + strlen(str1));

return 0;
} else
return -1;
}

// fill insn with mnemonic & operands info
static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
PostPrinter_t postprinter, const uint8_t *code)
Expand Down Expand Up @@ -557,9 +574,14 @@ static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCI
struct insn_mnem *tmp = handle->mnem_list;
while(tmp) {
if (tmp->insn.id == insn->id) {
// found this instruction, so copy its mnemonic
(void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
char str[CS_MNEMONIC_SIZE];

if (!str_replace(str, insn->mnemonic, cs_insn_name((csh)handle, insn->id), tmp->insn.mnemonic)) {
// copy result to mnemonic
(void)strncpy(insn->mnemonic, str, sizeof(insn->mnemonic) - 1);
insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
}

break;
}
tmp = tmp->next;
Expand Down

0 comments on commit 6065328

Please sign in to comment.