Added some useful IDA scripts (NPOTB).
1 /* vim: set ts=4 sw=4 tw=99 et: */
5 #include "sp_file_headers.h"
7 #include "sp_symhash.h"
10 NameHash(const char *str)
12 size_t len = strlen(str);
13 const uint8_t *data = (uint8_t *)str;
15 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
16 || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
17 #define get16bits(d) (*((const uint16_t *) (d)))
19 #if !defined (get16bits)
20 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
21 +(uint32_t)(((const uint8_t *)(d))[0]) )
23 uint32_t hash = len, tmp;
26 if (len <= 0 || data == NULL) return 0;
32 for (;len > 0; len--) {
33 hash += get16bits (data);
34 tmp = (get16bits (data+2) << 11) ^ hash;
35 hash = (hash << 16) ^ tmp;
36 data += 2*sizeof (uint16_t);
40 /* Handle end cases */
42 case 3: hash += get16bits (data);
44 hash ^= data[sizeof (uint16_t)] << 18;
47 case 2: hash += get16bits (data);
51 case 1: hash += *data;
56 /* Force "avalanching" of final 127 bits */
69 SC_FUNC HashTable *NewHashTable()
71 HashTable *ht = (HashTable*)malloc(sizeof(HashTable));
74 ht->buckets = (HashEntry **)calloc(32, sizeof(HashEntry *));
81 ht->bucketmask = 32 - 1;
86 DestroyHashTable(HashTable *ht)
91 for (i = 0; i < ht->nbuckets; i++) {
92 HashEntry *he = ht->buckets[i];
94 HashEntry *next = he->next;
104 FindTaggedInHashTable(HashTable *ht, const char *name, int fnumber,
108 symbol *firstmatch=NULL;
109 uint32_t hash = NameHash(name);
110 uint32_t bucket = hash & ht->bucketmask;
111 HashEntry *he = ht->buckets[bucket];
113 assert(cmptag!=NULL);
116 symbol *sym = he->sym;
117 if ((sym->parent==NULL || sym->ident==iCONSTEXPR) &&
118 (sym->fnumber<0 || sym->fnumber==fnumber) &&
119 (strcmp(sym->name, name) == 0))
121 /* return closest match or first match; count number of matches */
122 if (firstmatch==NULL)
126 if (*cmptag==sym->tag) {
127 *cmptag=1; /* good match found, set number of matches to 1 */
134 if (firstmatch!=NULL)
140 FindInHashTable(HashTable *ht, const char *name, int fnumber)
142 uint32_t hash = NameHash(name);
143 uint32_t bucket = hash & ht->bucketmask;
144 HashEntry *he = ht->buckets[bucket];
147 symbol *sym = he->sym;
148 if ((sym->parent==NULL || sym->ident==iCONSTEXPR) &&
149 (sym->fnumber<0 || sym->fnumber==fnumber) &&
150 (strcmp(sym->name, name) == 0))
161 ResizeHashTable(HashTable *ht)
164 uint32_t xnbuckets = ht->nbuckets * 2;
165 uint32_t xbucketmask = xnbuckets - 1;
166 HashEntry **xbuckets = (HashEntry **)calloc(xnbuckets, sizeof(HashEntry*));
170 for (i = 0; i < ht->nbuckets; i++) {
171 HashEntry *he = ht->buckets[i];
173 HashEntry *next = he->next;
174 uint32_t bucket = he->sym->hash & xbucketmask;
175 he->next = xbuckets[bucket];
176 xbuckets[bucket] = he;
181 ht->buckets = xbuckets;
182 ht->nbuckets = xnbuckets;
183 ht->bucketmask = xbucketmask;
187 AddToHashTable(HashTable *ht, symbol *sym)
189 uint32_t bucket = sym->hash & ht->bucketmask;
190 HashEntry **hep, *he;
192 hep = &ht->buckets[bucket];
194 assert((*hep)->sym != sym);
198 he = (HashEntry *)malloc(sizeof(HashEntry));
206 if (ht->nused > ht->nbuckets && ht->nbuckets <= INT_MAX / 2)
211 RemoveFromHashTable(HashTable *ht, symbol *sym)
213 uint32_t bucket = sym->hash & ht->bucketmask;
214 HashEntry **hep = &ht->buckets[bucket];
215 HashEntry *he = *hep;
218 if (he->sym == sym) {