blob: cece416d269872e840a792509c109e074ba70c9f [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 Quynhb2654062014-01-03 17:08:58 +08004#include <stdlib.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <string.h>
6
7#include "utils.h"
8
Nguyen Anh Quynhbed90912013-12-13 18:28:38 +08009// return the position of a string in a list of strings
10// or -1 if given string is not in the list
11int str_in_list(char **list, char *s)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012{
13 char **l;
14
Nguyen Anh Quynhbed90912013-12-13 18:28:38 +080015 int c = 0;
16 for(l = list; *l; c++, l++) {
Nguyen Anh Quynh19146e92014-05-28 12:41:31 +080017 if (!strcmp(*l, s))
Nguyen Anh Quynhbed90912013-12-13 18:28:38 +080018 return c;
19 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
Nguyen Anh Quynhbed90912013-12-13 18:28:38 +080021 return -1;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022}
23
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080024// create a cache for fast id lookup
25static unsigned short *make_id2insn(insn_map *insns, unsigned int size)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080026{
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080027 // NOTE: assume that the max id is always put at the end of insns array
28 unsigned short max_id = insns[size - 1].id;
Alex Ionescu46018db2014-01-22 09:45:00 -080029 unsigned short i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080030
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080031 unsigned short *cache = (unsigned short *)cs_mem_calloc(sizeof(*cache), max_id + 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080032
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080033 for (i = 1; i < size; i++)
34 cache[insns[i].id] = i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080035
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080036 return cache;
37}
38
39// look for @id in @insns, given its size in @max. first time call will update @cache.
40// return 0 if not found
41unsigned short insn_find(insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache)
42{
43 if (id > insns[max - 1].id)
44 return 0;
45
46 if (*cache == NULL)
47 *cache = make_id2insn(insns, max);
48
49 return (*cache)[id];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050}
51
pancakef0e4eed2013-12-11 22:14:42 +010052int name2id(name_map* map, int max, const char *name)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080053{
54 int i;
55
56 for (i = 0; i < max; i++) {
Nguyen Anh Quynh19146e92014-05-28 12:41:31 +080057 if (!strcmp(map[i].name, name)) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080058 return map[i].id;
59 }
60 }
61
62 // nothing match
63 return -1;
64}
65
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080066// count number of positive members in a list.
67// NOTE: list must be guaranteed to end in 0
Nguyen Anh Quynh18103e42013-12-20 17:35:15 +080068unsigned int count_positive(unsigned char *list)
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080069{
70 unsigned int c;
71
72 for (c = 0; list[c] > 0; c++);
73
74 return c;
75}
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080076
77char *cs_strdup(const char *str)
78{
79 size_t len = strlen(str)+ 1;
80 void *new = cs_mem_malloc(len);
81
82 if (new == NULL)
83 return NULL;
84
85 return (char *)memmove(new, str, len);
86}