handle mnemonic customization better. issue #1514

This commit is contained in:
Nguyen Anh Quynh 2019-07-10 23:54:15 +08:00
parent bb838aa7d8
commit 02ba83fadc

28
cs.c
View File

@ -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)
@ -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;