blob: 62cbc95efe64247ab9d4b0190887910c84e0c43a [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
vit96965e1b9282017-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>
pancake9c10ace2015-02-24 04:55:55 +010011#include "include/capstone/capstone.h"
vit96965e1b9282017-03-08 14:40:22 +030012#endif
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 Quynhefffe782015-03-25 15:02:13 +080023 uint16_t regs_use[12]; // list of implicit registers used by this instruction
24 uint16_t regs_mod[20]; // list of implicit registers modified by this instruction
Nguyen Anh Quynh18103e42013-12-20 17:35:15 +080025 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 Quynh1182d252015-04-27 12:13:34 +080045// map ID to a name
46// return NULL if not found
47char *id2name(name_map* map, int max, const unsigned int id);
48
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080049// count number of positive members in a list.
50// NOTE: list must be guaranteed to end in 0
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +080051unsigned int count_positive(uint16_t *list);
52unsigned int count_positive8(unsigned char *list);
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080053
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080054#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))
55
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080056char *cs_strdup(const char *str);
57
Nguyen Anh Quynha88c1162014-04-27 13:38:04 +080058#define MIN(x, y) ((x) < (y) ? (x) : (y))
59
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +080060// we need this since Windows doesnt have snprintf()
61int cs_snprintf(char *buffer, size_t size, const char *fmt, ...);
62
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +080063#define CS_AC_IGNORE (1 << 7)
64
Nguyen Anh Quynh58eb0732015-04-02 15:18:33 +080065// check if an id is existent in an array
66bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id);
67
68bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id);
69
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080070#endif
71