blob: e9ab70cf853098ad2b47dbee8534cbf6024881a7 [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
vit969643615962017-03-08 14:40:22 +03007#if defined(CAPSTONE_HAS_OSXKERNEL)
8#include <libkern/libkern.h>
9#else
Nguyen Anh Quynhcae09bf2014-06-17 14:58:39 +080010#include <stddef.h>
vit969643615962017-03-08 14:40:22 +030011#endif
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080012#include "include/capstone.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080013#include "cs_priv.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014
Nguyen Anh Quynh462f2912013-12-11 17:35:27 +080015// threshold number, so above this number will be printed in hexa mode
16#define HEX_THRESHOLD 9
17
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018// map instruction to its characteristics
19typedef struct insn_map {
Nguyen Anh Quynhf4af98c2014-01-01 00:02:42 +080020 unsigned short id;
21 unsigned short mapid;
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080022#ifndef CAPSTONE_DIET
Nguyen Anh Quynh18103e42013-12-20 17:35:15 +080023 unsigned char regs_use[12]; // list of implicit registers used by this instruction
24 unsigned char regs_mod[20]; // list of implicit registers modified by this instruction
25 unsigned char groups[8]; // list of group this instruction belong to
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080026 bool branch; // branch instruction?
27 bool indirect_branch; // indirect branch instruction?
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080028#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080029} insn_map;
30
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080031// look for @id in @m, given its size in @max. first time call will update @cache.
32// return 0 if not found
33unsigned short insn_find(insn_map *m, unsigned int max, unsigned int id, unsigned short **cache);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080034
35// map id to string
36typedef struct name_map {
37 unsigned int id;
38 char *name;
39} name_map;
40
41// map a name to its ID
42// return 0 if not found
pancakef0e4eed2013-12-11 22:14:42 +010043int name2id(name_map* map, int max, const char *name);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080044
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 Quynh18103e42013-12-20 17:35:15 +080047unsigned int count_positive(unsigned char *list);
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080048
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080049#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))
50
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080051char *cs_strdup(const char *str);
52
Nguyen Anh Quynha88c1162014-04-27 13:38:04 +080053#define MIN(x, y) ((x) < (y) ? (x) : (y))
54
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +080055// we need this since Windows doesnt have snprintf()
56int cs_snprintf(char *buffer, size_t size, const char *fmt, ...);
57
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080058#endif
59