diff --git a/arch/AArch64/AArch64Mapping.c b/arch/AArch64/AArch64Mapping.c index 58c14ef983..1233668df8 100644 --- a/arch/AArch64/AArch64Mapping.c +++ b/arch/AArch64/AArch64Mapping.c @@ -840,28 +840,20 @@ static name_map group_name_maps[] = { { ARM64_GRP_JUMP, "jump" }, { ARM64_GRP_CALL, "call" }, { ARM64_GRP_RET, "return" }, + { ARM64_GRP_PRIVILEGE, "privilege" }, // architecture-specific groups { ARM64_GRP_CRYPTO, "crypto" }, { ARM64_GRP_FPARMV8, "fparmv8" }, { ARM64_GRP_NEON, "neon" }, { ARM64_GRP_CRC, "crc" }, - }; #endif const char *AArch64_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - // verify group id - if (id >= ARM64_GRP_ENDING || (id > ARM64_GRP_RET && id < ARM64_GRP_CRYPTO)) - return NULL; - - // NOTE: when new generic groups are added, 4 must be changed accordingly - if (id >= ARM64_GRP_CRYPTO) - return group_name_maps[id - ARM64_GRP_CRYPTO + 4].name; - else - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/arch/ARM/ARMMapping.c b/arch/ARM/ARMMapping.c index 37895bb984..1050e04e50 100644 --- a/arch/ARM/ARMMapping.c +++ b/arch/ARM/ARMMapping.c @@ -768,6 +768,7 @@ static name_map group_name_maps[] = { // generic groups { ARM_GRP_INVALID, NULL }, { ARM_GRP_JUMP, "jump" }, + { ARM_GRP_PRIVILEGE, "privilege" }, // architecture-specific groups { ARM_GRP_CRYPTO, "crypto" }, @@ -808,15 +809,7 @@ static name_map group_name_maps[] = { const char *ARM_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - // verify group id - if (id >= ARM_GRP_ENDING || (id > ARM_GRP_JUMP && id < ARM_GRP_CRYPTO)) - return NULL; - - // NOTE: when new generic groups are added, 2 must be changed accordingly - if (id >= ARM_GRP_CRYPTO) - return group_name_maps[id - ARM_GRP_CRYPTO + 2].name; - else - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/arch/Mips/MipsMapping.c b/arch/Mips/MipsMapping.c index 2fed993f5f..f9005eee0d 100644 --- a/arch/Mips/MipsMapping.c +++ b/arch/Mips/MipsMapping.c @@ -991,15 +991,7 @@ static name_map group_name_maps[] = { const char *Mips_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - // verify group id - if (id >= MIPS_GRP_ENDING || (id > MIPS_GRP_JUMP && id < MIPS_GRP_BITCOUNT)) - return NULL; - - // NOTE: when new generic groups are added, 2 must be changed accordingly - if (id >= 128) - return group_name_maps[id - 128 + 2].name; - else - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/arch/PowerPC/PPCMapping.c b/arch/PowerPC/PPCMapping.c index 56439d2b23..085d85f6bc 100644 --- a/arch/PowerPC/PPCMapping.c +++ b/arch/PowerPC/PPCMapping.c @@ -1462,15 +1462,7 @@ static name_map group_name_maps[] = { const char *PPC_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - // verify group id - if (id >= PPC_GRP_ENDING || (id > PPC_GRP_JUMP && id < PPC_GRP_ALTIVEC)) - return NULL; - - // NOTE: when new generic groups are added, 2 must be changed accordingly - if (id >= PPC_GRP_ALTIVEC) - return group_name_maps[id - PPC_GRP_ALTIVEC + 2].name; - else - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/arch/Sparc/SparcMapping.c b/arch/Sparc/SparcMapping.c index eeaf2c9a68..0f70ff0c8e 100644 --- a/arch/Sparc/SparcMapping.c +++ b/arch/Sparc/SparcMapping.c @@ -526,15 +526,7 @@ static name_map group_name_maps[] = { const char *Sparc_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - // verify group id - if (id >= SPARC_GRP_ENDING || (id > SPARC_GRP_JUMP && id < SPARC_GRP_HARDQUAD)) - return NULL; - - // NOTE: when new generic groups are added, 2 must be changed accordingly - if (id >= 128) - return group_name_maps[id - 128 + 2].name; - else - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/arch/SystemZ/SystemZMapping.c b/arch/SystemZ/SystemZMapping.c index f841c14ade..c32ec1a3dd 100644 --- a/arch/SystemZ/SystemZMapping.c +++ b/arch/SystemZ/SystemZMapping.c @@ -839,15 +839,7 @@ static name_map group_name_maps[] = { const char *SystemZ_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - // verify group id - if (id >= SYSZ_GRP_ENDING || (id > SYSZ_GRP_JUMP && id < SYSZ_GRP_DISTINCTOPS)) - return NULL; - - // NOTE: when new generic groups are added, 2 must be changed accordingly - if (id >= 128) - return group_name_maps[id - 128 + 2].name; - else - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/arch/X86/X86Mapping.c b/arch/X86/X86Mapping.c index 6dee30f119..f766d151a7 100644 --- a/arch/X86/X86Mapping.c +++ b/arch/X86/X86Mapping.c @@ -2200,6 +2200,7 @@ static name_map group_name_maps[] = { { X86_GRP_RET, "ret" }, { X86_GRP_INT, "int" }, { X86_GRP_IRET, "iret" }, + { X86_GRP_PRIVILEGE, "privilege" }, // architecture-specific groups { X86_GRP_VM, "vm" }, @@ -2249,15 +2250,7 @@ static name_map group_name_maps[] = { const char *X86_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - // verify group id - if (id >= X86_GRP_ENDING || (id > X86_GRP_IRET && id < X86_GRP_VM)) - return NULL; - - // NOTE: when new generic groups are added, 6 must be changed accordingly - if (id >= 128) - return group_name_maps[id - 128 + 6].name; - else - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/arch/XCore/XCoreMapping.c b/arch/XCore/XCoreMapping.c index 11c09ecbb2..8c86eb8a80 100644 --- a/arch/XCore/XCoreMapping.c +++ b/arch/XCore/XCoreMapping.c @@ -275,10 +275,7 @@ static name_map group_name_maps[] = { const char *XCore_group_name(csh handle, unsigned int id) { #ifndef CAPSTONE_DIET - if (id >= XCORE_GRP_ENDING) - return NULL; - - return group_name_maps[id].name; + return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); #else return NULL; #endif diff --git a/suite/test_group_name.py b/suite/test_group_name.py index 0bccd32835..61c32c97c2 100755 --- a/suite/test_group_name.py +++ b/suite/test_group_name.py @@ -29,7 +29,7 @@ def run(self): name = self.data.get(group_id) res = _cs.cs_group_name(cap.csh, group_id) if res != name: - print("ERROR: expected '%s', but got '%s'" %(name, res)) + print("ERROR: id = %u expected '%s', but got '%s'" %(group_id, name, res)) print("") arm_dict = { diff --git a/utils.c b/utils.c index d2f5fdcb20..ca2a7ecf47 100644 --- a/utils.c +++ b/utils.c @@ -52,6 +52,20 @@ int name2id(name_map* map, int max, const char *name) return -1; } +char *id2name(name_map* map, int max, const unsigned int id) +{ + int i; + + for (i = 0; i < max; i++) { + if (map[i].id == id) { + return map[i].name; + } + } + + // nothing match + return NULL; +} + // count number of positive members in a list. // NOTE: list must be guaranteed to end in 0 unsigned int count_positive(uint16_t *list) diff --git a/utils.h b/utils.h index f52fdb5dad..3df23b7dff 100644 --- a/utils.h +++ b/utils.h @@ -38,6 +38,10 @@ typedef struct name_map { // return 0 if not found int name2id(name_map* map, int max, const char *name); +// map ID to a name +// return NULL if not found +char *id2name(name_map* map, int max, const unsigned int id); + // count number of positive members in a list. // NOTE: list must be guaranteed to end in 0 unsigned int count_positive(uint16_t *list);