blob: 79c5a0fa6ad0de0984d618b3c4970e1b2ad9a1ce [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 Quynhb2654062014-01-03 17:08:58 +08009// create a cache for fast id lookup
10static unsigned short *make_id2insn(insn_map *insns, unsigned int size)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080011{
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080012 // NOTE: assume that the max id is always put at the end of insns array
13 unsigned short max_id = insns[size - 1].id;
Alex Ionescu46018db2014-01-22 09:45:00 -080014 unsigned short i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015
Nguyen Anh Quynhaaddb252014-06-17 13:32:37 +080016 unsigned short *cache = (unsigned short *)cs_mem_malloc(sizeof(*cache) * (max_id + 1));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080018 for (i = 1; i < size; i++)
19 cache[insns[i].id] = i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080021 return cache;
22}
23
24// look for @id in @insns, given its size in @max. first time call will update @cache.
25// return 0 if not found
26unsigned short insn_find(insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache)
27{
28 if (id > insns[max - 1].id)
29 return 0;
30
31 if (*cache == NULL)
32 *cache = make_id2insn(insns, max);
33
34 return (*cache)[id];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080035}
36
pancakef0e4eed2013-12-11 22:14:42 +010037int name2id(name_map* map, int max, const char *name)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080038{
39 int i;
40
41 for (i = 0; i < max; i++) {
Nguyen Anh Quynh19146e92014-05-28 12:41:31 +080042 if (!strcmp(map[i].name, name)) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080043 return map[i].id;
44 }
45 }
46
47 // nothing match
48 return -1;
49}
50
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080051// count number of positive members in a list.
52// NOTE: list must be guaranteed to end in 0
Nguyen Anh Quynh18103e42013-12-20 17:35:15 +080053unsigned int count_positive(unsigned char *list)
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080054{
55 unsigned int c;
56
57 for (c = 0; list[c] > 0; c++);
58
59 return c;
60}
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080061
62char *cs_strdup(const char *str)
63{
64 size_t len = strlen(str)+ 1;
65 void *new = cs_mem_malloc(len);
66
67 if (new == NULL)
68 return NULL;
69
70 return (char *)memmove(new, str, len);
71}
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +080072
73// we need this since Windows doesnt have snprintf()
74int cs_snprintf(char *buffer, size_t size, const char *fmt, ...)
75{
76 int ret;
77
78 va_list ap;
79 va_start(ap, fmt);
80 ret = cs_vsnprintf(buffer, size, fmt, ap);
81 va_end(ap);
82
83 return ret;
84}