blob: 3df23b7dff94762b12ec28fcdce3fceec740b562 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08003
Nguyen Anh Quynhae3649f2014-01-02 13:15:07 +08004#ifndef CS_UTILS_H
5#define CS_UTILS_H
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08006
Nguyen Anh Quynhcae09bf2014-06-17 14:58:39 +08007#include <stddef.h>
pancake9c10ace2015-02-24 04:55:55 +01008#include "include/capstone/capstone.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +08009#include "cs_priv.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010
Nguyen Anh Quynh462f2912013-12-11 17:35:27 +080011// threshold number, so above this number will be printed in hexa mode
12#define HEX_THRESHOLD 9
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014// map instruction to its characteristics
15typedef struct insn_map {
Nguyen Anh Quynhf4af98c2014-01-01 00:02:42 +080016 unsigned short id;
17 unsigned short mapid;
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080018#ifndef CAPSTONE_DIET
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +080019 uint16_t regs_use[12]; // list of implicit registers used by this instruction
20 uint16_t regs_mod[20]; // list of implicit registers modified by this instruction
Nguyen Anh Quynh18103e42013-12-20 17:35:15 +080021 unsigned char groups[8]; // list of group this instruction belong to
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022 bool branch; // branch instruction?
23 bool indirect_branch; // indirect branch instruction?
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080024#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080025} insn_map;
26
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080027// look for @id in @m, given its size in @max. first time call will update @cache.
28// return 0 if not found
29unsigned short insn_find(insn_map *m, unsigned int max, unsigned int id, unsigned short **cache);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080030
31// map id to string
32typedef struct name_map {
33 unsigned int id;
34 char *name;
35} name_map;
36
37// map a name to its ID
38// return 0 if not found
pancakef0e4eed2013-12-11 22:14:42 +010039int name2id(name_map* map, int max, const char *name);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080040
Nguyen Anh Quynh1182d252015-04-27 12:13:34 +080041// map ID to a name
42// return NULL if not found
43char *id2name(name_map* map, int max, const unsigned int id);
44
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080045// count number of positive members in a list.
46// NOTE: list must be guaranteed to end in 0
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +080047unsigned int count_positive(uint16_t *list);
48unsigned int count_positive8(unsigned char *list);
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080049
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))
51
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080052char *cs_strdup(const char *str);
53
Nguyen Anh Quynha88c1162014-04-27 13:38:04 +080054#define MIN(x, y) ((x) < (y) ? (x) : (y))
55
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +080056// we need this since Windows doesnt have snprintf()
57int cs_snprintf(char *buffer, size_t size, const char *fmt, ...);
58
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +080059#define CS_AC_IGNORE (1 << 7)
60
Nguyen Anh Quynh58eb0732015-04-02 15:18:33 +080061// check if an id is existent in an array
62bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id);
63
64bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id);
65
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080066#endif
67