blob: 60ed8221d345e770631412bec954ad101303c911 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
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>
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +08008#include "include/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 Quynh18103e42013-12-20 17:35:15 +080019 unsigned char regs_use[12]; // list of implicit registers used by this instruction
20 unsigned char regs_mod[20]; // list of implicit registers modified by this instruction
21 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 Quynhf35e2ad2013-12-03 11:10:26 +080041// count number of positive members in a list.
42// NOTE: list must be guaranteed to end in 0
Nguyen Anh Quynh18103e42013-12-20 17:35:15 +080043unsigned int count_positive(unsigned char *list);
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080044
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080045#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))
46
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080047char *cs_strdup(const char *str);
48
Nguyen Anh Quynha88c1162014-04-27 13:38:04 +080049#define MIN(x, y) ((x) < (y) ? (x) : (y))
50
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +080051// we need this since Windows doesnt have snprintf()
52int cs_snprintf(char *buffer, size_t size, const char *fmt, ...);
53
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080054#endif
55