Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
Nguyen Anh Quynh | bfcaba5 | 2015-03-04 17:45:23 +0800 | [diff] [blame] | 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 3 | |
Nguyen Anh Quynh | ae3649f | 2014-01-02 13:15:07 +0800 | [diff] [blame] | 4 | #ifndef CS_UTILS_H |
| 5 | #define CS_UTILS_H |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 6 | |
vit9696 | 5e1b928 | 2017-03-08 14:40:22 +0300 | [diff] [blame] | 7 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 8 | #include <libkern/libkern.h> |
| 9 | #else |
Nguyen Anh Quynh | cae09bf | 2014-06-17 14:58:39 +0800 | [diff] [blame] | 10 | #include <stddef.h> |
pancake | 9c10ace | 2015-02-24 04:55:55 +0100 | [diff] [blame] | 11 | #include "include/capstone/capstone.h" |
vit9696 | 5e1b928 | 2017-03-08 14:40:22 +0300 | [diff] [blame] | 12 | #endif |
Nguyen Anh Quynh | c740407 | 2014-01-05 11:35:47 +0800 | [diff] [blame] | 13 | #include "cs_priv.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 14 | |
Nguyen Anh Quynh | 462f291 | 2013-12-11 17:35:27 +0800 | [diff] [blame] | 15 | // threshold number, so above this number will be printed in hexa mode |
| 16 | #define HEX_THRESHOLD 9 |
| 17 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 18 | // map instruction to its characteristics |
| 19 | typedef struct insn_map { |
Nguyen Anh Quynh | f4af98c | 2014-01-01 00:02:42 +0800 | [diff] [blame] | 20 | unsigned short id; |
| 21 | unsigned short mapid; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 22 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | efffe78 | 2015-03-25 15:02:13 +0800 | [diff] [blame] | 23 | 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 Quynh | 18103e4 | 2013-12-20 17:35:15 +0800 | [diff] [blame] | 25 | unsigned char groups[8]; // list of group this instruction belong to |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 26 | bool branch; // branch instruction? |
| 27 | bool indirect_branch; // indirect branch instruction? |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 28 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 29 | } insn_map; |
| 30 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 31 | // look for @id in @m, given its size in @max. first time call will update @cache. |
| 32 | // return 0 if not found |
| 33 | unsigned short insn_find(insn_map *m, unsigned int max, unsigned int id, unsigned short **cache); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 34 | |
| 35 | // map id to string |
| 36 | typedef 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 |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 43 | int name2id(name_map* map, int max, const char *name); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 44 | |
Nguyen Anh Quynh | 1182d25 | 2015-04-27 12:13:34 +0800 | [diff] [blame] | 45 | // map ID to a name |
| 46 | // return NULL if not found |
| 47 | char *id2name(name_map* map, int max, const unsigned int id); |
| 48 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 49 | // count number of positive members in a list. |
| 50 | // NOTE: list must be guaranteed to end in 0 |
Nguyen Anh Quynh | efffe78 | 2015-03-25 15:02:13 +0800 | [diff] [blame] | 51 | unsigned int count_positive(uint16_t *list); |
| 52 | unsigned int count_positive8(unsigned char *list); |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 53 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 54 | #define ARR_SIZE(a) (sizeof(a)/sizeof(a[0])) |
Wolfgang Schwotzer | 22b4d0e | 2017-10-21 15:44:36 +0200 | [diff] [blame] | 55 | #define MATRIX_SIZE(a) (sizeof(a[0])/sizeof(a[0][0])) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 56 | |
Nguyen Anh Quynh | a9ffb44 | 2014-01-15 18:27:01 +0800 | [diff] [blame] | 57 | char *cs_strdup(const char *str); |
| 58 | |
Nguyen Anh Quynh | a88c116 | 2014-04-27 13:38:04 +0800 | [diff] [blame] | 59 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 60 | |
Nguyen Anh Quynh | 4b6b15f | 2014-08-26 15:57:04 +0800 | [diff] [blame] | 61 | // we need this since Windows doesnt have snprintf() |
| 62 | int cs_snprintf(char *buffer, size_t size, const char *fmt, ...); |
| 63 | |
Nguyen Anh Quynh | efffe78 | 2015-03-25 15:02:13 +0800 | [diff] [blame] | 64 | #define CS_AC_IGNORE (1 << 7) |
| 65 | |
Nguyen Anh Quynh | 58eb073 | 2015-04-02 15:18:33 +0800 | [diff] [blame] | 66 | // check if an id is existent in an array |
| 67 | bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id); |
| 68 | |
| 69 | bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id); |
| 70 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 71 | #endif |
| 72 | |