Hardware authentication for Linux using ordinary USB Flash Drives.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

605 lines
38 KiB

13 years ago
  1. /*
  2. Copyright (c) 2003-2006, Troy Hanson http://uthash.sourceforge.net
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of the copyright holder nor the names of its
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  19. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <string.h> /* memcmp,strlen */
  28. #ifndef UTHASH_H
  29. #define UTHASH_H
  30. #define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */
  31. #define uthash_bkt_malloc(sz) malloc(sz) /* malloc fcn for UT_hash_bucket's */
  32. #define uthash_bkt_free(ptr) free(ptr) /* free fcn for UT_hash_bucket's */
  33. #define uthash_tbl_malloc(sz) malloc(sz) /* malloc fcn for UT_hash_table */
  34. #define uthash_tbl_free(ptr) free(ptr) /* free fcn for UT_hash_table */
  35. #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */
  36. #define uthash_expand_fyi(tbl) /* can be defined to log expands */
  37. /* initial number of buckets */
  38. #define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */
  39. #define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches */
  40. #define HASH_FIND(hh,head,keyptr,keylen_in,out) \
  41. do { \
  42. out=head; \
  43. if (head) { \
  44. (head)->hh.tbl->key = (char*)(keyptr); \
  45. (head)->hh.tbl->keylen = keylen_in; \
  46. HASH_FCN((head)->hh.tbl->key,(head)->hh.tbl->keylen, \
  47. (head)->hh.tbl->num_buckets,(head)->hh.tbl->bkt, \
  48. (head)->hh.tbl->i, (head)->hh.tbl->j,(head)->hh.tbl->k); \
  49. HASH_FIND_IN_BKT(hh, (head)->hh.tbl->buckets[ (head)->hh.tbl->bkt], \
  50. keyptr,keylen_in,out); \
  51. } \
  52. } while (0)
  53. #define HASH_ADD(hh,head,fieldname,keylen_in,add) \
  54. HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add)
  55. #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \
  56. do { \
  57. add->hh.next = NULL; \
  58. add->hh.key = (char*)keyptr; \
  59. add->hh.keylen = keylen_in; \
  60. add->hh.elmt = add; \
  61. if (!(head)) { \
  62. head = add; \
  63. (head)->hh.prev = NULL; \
  64. (head)->hh.tbl = (UT_hash_table*)uthash_tbl_malloc( \
  65. sizeof(UT_hash_table)); \
  66. if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \
  67. (head)->hh.tbl->name = #head; \
  68. (head)->hh.tbl->tail = &(add->hh); \
  69. (head)->hh.tbl->noexpand = 0; \
  70. (head)->hh.tbl->hash_q = 1<<16; \
  71. (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
  72. (head)->hh.tbl->num_items = 0; \
  73. (head)->hh.tbl->hho = ((long)(&add->hh) - (long)(add)); \
  74. (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_bkt_malloc( \
  75. HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
  76. if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \
  77. memset((head)->hh.tbl->buckets, 0, \
  78. HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
  79. } else { \
  80. (head)->hh.tbl->tail->next = add; \
  81. add->hh.prev = (head)->hh.tbl->tail->elmt; \
  82. (head)->hh.tbl->tail = &(add->hh); \
  83. } \
  84. (head)->hh.tbl->num_items++; \
  85. add->hh.tbl = (head)->hh.tbl; \
  86. (head)->hh.tbl->key = (char*)keyptr; \
  87. (head)->hh.tbl->keylen = keylen_in; \
  88. HASH_FCN((head)->hh.tbl->key,(head)->hh.tbl->keylen, \
  89. (head)->hh.tbl->num_buckets, \
  90. (head)->hh.tbl->bkt, \
  91. (head)->hh.tbl->i, (head)->hh.tbl->j, (head)->hh.tbl->k ); \
  92. HASH_ADD_TO_BKT(hh,(head)->hh.tbl->buckets[(head)->hh.tbl->bkt],add); \
  93. HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \
  94. HASH_FSCK(head); \
  95. } while(0)
  96. #define HASH_DELETE(hh,head,delptr) \
  97. do { \
  98. if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \
  99. uthash_bkt_free((head)->hh.tbl->buckets ); \
  100. uthash_tbl_free((head)->hh.tbl); \
  101. head = NULL; \
  102. } else { \
  103. if ((delptr) == (head)->hh.tbl->tail->elmt) { \
  104. (head)->hh.tbl->tail = (void*)(((long)((delptr)->hh.prev)) + \
  105. (head)->hh.tbl->hho); \
  106. } \
  107. if ((delptr)->hh.prev) { \
  108. ((UT_hash_handle*)(((long)((delptr)->hh.prev)) + \
  109. (head)->hh.tbl->hho))->next = (delptr)->hh.next; \
  110. } else { \
  111. head = (delptr)->hh.next; \
  112. } \
  113. if ((delptr)->hh.next) { \
  114. ((UT_hash_handle*)(((long)((delptr)->hh.next)) + \
  115. (head)->hh.tbl->hho))->prev = (delptr)->hh.prev; \
  116. } \
  117. (head)->hh.tbl->key = (char*)((delptr)->hh.key); \
  118. (head)->hh.tbl->keylen = (delptr)->hh.keylen; \
  119. HASH_FCN((head)->hh.tbl->key,(head)->hh.tbl->keylen, \
  120. (head)->hh.tbl->num_buckets,(head)->hh.tbl->bkt, \
  121. (head)->hh.tbl->i,(head)->hh.tbl->j,(head)->hh.tbl->k ); \
  122. HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[(head)->hh.tbl->bkt], \
  123. delptr); \
  124. (head)->hh.tbl->num_items--; \
  125. } \
  126. HASH_FSCK(head); \
  127. } while (0)
  128. /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */
  129. #define HASH_FIND_STR(head,findstr,out) \
  130. HASH_FIND(hh,head,findstr,strlen(findstr),out)
  131. #define HASH_ADD_STR(head,strfield,add) \
  132. HASH_ADD(hh,head,strfield,strlen(add->strfield),add)
  133. #define HASH_FIND_INT(head,findint,out) \
  134. HASH_FIND(hh,head,findint,sizeof(int),out)
  135. #define HASH_ADD_INT(head,intfield,add) \
  136. HASH_ADD(hh,head,intfield,sizeof(int),add)
  137. #define HASH_DEL(head,delptr) \
  138. HASH_DELETE(hh,head,delptr)
  139. /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined.
  140. * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined.
  141. * This function misuses fields in UT_hash_table for its bookkeeping variables.
  142. */
  143. #ifdef HASH_DEBUG
  144. #define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0)
  145. #define HASH_FSCK(head) \
  146. do { \
  147. if (head) { \
  148. (head)->hh.tbl->keylen = 0; /* item counter */ \
  149. for( (head)->hh.tbl->bkt_i = 0; \
  150. (head)->hh.tbl->bkt_i < (head)->hh.tbl->num_buckets; \
  151. (head)->hh.tbl->bkt_i++) \
  152. { \
  153. (head)->hh.tbl->bkt_ideal = 0; /* bkt item counter */ \
  154. (head)->hh.tbl->hh = \
  155. (head)->hh.tbl->buckets[(head)->hh.tbl->bkt_i].hh_head; \
  156. (head)->hh.tbl->key = NULL; /* hh_prev */ \
  157. while ((head)->hh.tbl->hh) { \
  158. if ((head)->hh.tbl->key != \
  159. (char*)((head)->hh.tbl->hh->hh_prev)) { \
  160. HASH_OOPS("invalid hh_prev %x, actual %x\n", \
  161. (head)->hh.tbl->hh->hh_prev, \
  162. (head)->hh.tbl->key ); \
  163. } \
  164. (head)->hh.tbl->bkt_ideal++; \
  165. (head)->hh.tbl->key = (char*)((head)->hh.tbl->hh); \
  166. (head)->hh.tbl->hh = (head)->hh.tbl->hh->hh_next; \
  167. } \
  168. (head)->hh.tbl->keylen += (head)->hh.tbl->bkt_ideal; \
  169. if ((head)->hh.tbl->buckets[(head)->hh.tbl->bkt_i].count \
  170. != (head)->hh.tbl->bkt_ideal) { \
  171. HASH_OOPS("invalid bucket count %d, actual %d\n", \
  172. (head)->hh.tbl->buckets[(head)->hh.tbl->bkt_i].count, \
  173. (head)->hh.tbl->bkt_ideal); \
  174. } \
  175. } \
  176. if ((head)->hh.tbl->keylen != (head)->hh.tbl->num_items) { \
  177. HASH_OOPS("invalid hh item count %d, actual %d\n", \
  178. (head)->hh.tbl->num_items, (head)->hh.tbl->keylen ); \
  179. } \
  180. /* traverse hh in app order; check next/prev integrity, count */ \
  181. (head)->hh.tbl->keylen = 0; /* item counter */ \
  182. (head)->hh.tbl->key = NULL; /* app prev */ \
  183. (head)->hh.tbl->hh = &(head)->hh; \
  184. while ((head)->hh.tbl->hh) { \
  185. (head)->hh.tbl->keylen++; \
  186. if ((head)->hh.tbl->key !=(char*)((head)->hh.tbl->hh->prev)) {\
  187. HASH_OOPS("invalid prev %x, actual %x\n", \
  188. (head)->hh.tbl->hh->prev, \
  189. (head)->hh.tbl->key ); \
  190. } \
  191. (head)->hh.tbl->key = (head)->hh.tbl->hh->elmt; \
  192. (head)->hh.tbl->hh = ( (head)->hh.tbl->hh->next ? \
  193. (UT_hash_handle*)((long)((head)->hh.tbl->hh->next) + \
  194. (head)->hh.tbl->hho) \
  195. : NULL ); \
  196. } \
  197. if ((head)->hh.tbl->keylen != (head)->hh.tbl->num_items) { \
  198. HASH_OOPS("invalid app item count %d, actual %d\n", \
  199. (head)->hh.tbl->num_items, (head)->hh.tbl->keylen ); \
  200. } \
  201. } \
  202. } while (0)
  203. #else
  204. #define HASH_FSCK(head)
  205. #endif
  206. /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to
  207. * the descriptor to which this macro is defined for tuning the hash function.
  208. * The app can #include <unistd.h> to get the prototype for write(2). */
  209. #ifdef HASH_EMIT_KEYS
  210. #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \
  211. (head)->hh.tbl->keylen = fieldlen; \
  212. write(HASH_EMIT_KEYS, &((head)->hh.tbl->keylen), sizeof(int)); \
  213. write(HASH_EMIT_KEYS, keyptr, fieldlen);
  214. #else
  215. #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen)
  216. #endif
  217. /* default to Jenkins unless specified e.g. DHASH_FUNCTION=HASH_SAX */
  218. #ifdef HASH_FUNCTION
  219. #define HASH_FCN HASH_FUNCTION
  220. #else
  221. #define HASH_FCN HASH_JEN
  222. #endif
  223. /* The Bernstein hash function, used in Perl prior to v5.6 */
  224. #define HASH_BER(key,keylen,num_bkts,bkt,i,j,k) \
  225. bkt = 0; \
  226. while (keylen--) bkt = (bkt * 33) + *key++; \
  227. bkt &= (num_bkts-1);
  228. /* SAX/FNV/OAT/JEN/JSW hash functions are macro variants of those listed at
  229. * http://eternallyconfuzzled.com/tuts/hashing.html (thanks Julienne Walker) */
  230. #define HASH_SAX(key,keylen,num_bkts,bkt,i,j,k) \
  231. bkt = 0; \
  232. for(i=0; i < keylen; i++) \
  233. bkt ^= (bkt << 5) + (bkt >> 2) + key[i]; \
  234. bkt &= (num_bkts-1);
  235. #define HASH_FNV(key,keylen,num_bkts,bkt,i,j,k) \
  236. bkt = 2166136261UL; \
  237. for(i=0; i < keylen; i++) \
  238. bkt = (bkt * 16777619) ^ key[i]; \
  239. bkt &= (num_bkts-1);
  240. #define HASH_OAT(key,keylen,num_bkts,bkt,i,j,k) \
  241. bkt = 0; \
  242. for(i=0; i < keylen; i++) { \
  243. bkt += key[i]; \
  244. bkt += (bkt << 10); \
  245. bkt ^= (bkt >> 6); \
  246. } \
  247. bkt += (bkt << 3); \
  248. bkt ^= (bkt >> 11); \
  249. bkt += (bkt << 15); \
  250. bkt &= (num_bkts-1);
  251. #define HASH_JEN_MIX(a,b,c) \
  252. { \
  253. a -= b; a -= c; a ^= ( c >> 13 ); \
  254. b -= c; b -= a; b ^= ( a << 8 ); \
  255. c -= a; c -= b; c ^= ( b >> 13 ); \
  256. a -= b; a -= c; a ^= ( c >> 12 ); \
  257. b -= c; b -= a; b ^= ( a << 16 ); \
  258. c -= a; c -= b; c ^= ( b >> 5 ); \
  259. a -= b; a -= c; a ^= ( c >> 3 ); \
  260. b -= c; b -= a; b ^= ( a << 10 ); \
  261. c -= a; c -= b; c ^= ( b >> 15 ); \
  262. }
  263. #define HASH_JEN(key,keylen,num_bkts,bkt,i,j,k) \
  264. bkt = 0xfeedbeef; \
  265. i = j = 0x9e3779b9; \
  266. k = keylen; \
  267. while (k >= 12) { \
  268. i += (key[0] + ( (unsigned)key[1] << 8 ) \
  269. + ( (unsigned)key[2] << 16 ) \
  270. + ( (unsigned)key[3] << 24 ) ); \
  271. j += (key[4] + ( (unsigned)key[5] << 8 ) \
  272. + ( (unsigned)key[6] << 16 ) \
  273. + ( (unsigned)key[7] << 24 ) ); \
  274. bkt += (key[8] + ( (unsigned)key[9] << 8 ) \
  275. + ( (unsigned)key[10] << 16 ) \
  276. + ( (unsigned)key[11] << 24 ) ); \
  277. \
  278. HASH_JEN_MIX(i, j, bkt); \
  279. \
  280. key += 12; \
  281. k -= 12; \
  282. } \
  283. bkt += keylen; \
  284. switch ( k ) { \
  285. case 11: bkt += ( (unsigned)key[10] << 24 ); \
  286. case 10: bkt += ( (unsigned)key[9] << 16 ); \
  287. case 9: bkt += ( (unsigned)key[8] << 8 ); \
  288. case 8: j += ( (unsigned)key[7] << 24 ); \
  289. case 7: j += ( (unsigned)key[6] << 16 ); \
  290. case 6: j += ( (unsigned)key[5] << 8 ); \
  291. case 5: j += key[4]; \
  292. case 4: i += ( (unsigned)key[3] << 24 ); \
  293. case 3: i += ( (unsigned)key[2] << 16 ); \
  294. case 2: i += ( (unsigned)key[1] << 8 ); \
  295. case 1: i += key[0]; \
  296. } \
  297. HASH_JEN_MIX(i, j, bkt); \
  298. bkt &= (num_bkts-1);
  299. #define HASH_JSW(key,keylen,num_bkts,bkt,i,j,k) \
  300. bkt = 16777551; \
  301. for(i=0; i < keylen; i++) { \
  302. bkt = (bkt << 1 | bkt >> 31) ^ \
  303. *(int*)((long)( \
  304. "\xe9\x81\x51\xe4\x84\x9d\x32\xd9\x2d\xda\xca\x94\xa7\x85\x1e" \
  305. "\x28\xfe\xa3\x18\x60\x28\x45\xa6\x48\x67\xdb\xd5\xa2\x91\x4d" \
  306. "\x1a\x2f\x97\x37\x82\xd8\xe9\x1c\xb7\x7b\x3c\xa5\x4c\x23\x2" \
  307. "\x42\x85\x20\x78\x6c\x6\x67\x6f\xa5\xcb\x53\x8c\xe1\x1f\x12" \
  308. "\x66\xcb\xa0\xbe\x47\x59\x8\x20\xd5\x31\xd9\xdc\xcc\x27\xc3" \
  309. "\x4d\x8\x9f\xb3\x50\x8\x90\x4f\x1f\x20\x60\xb8\xe2\x7b\x63" \
  310. "\x49\xc0\x64\xc7\xaf\xc9\x81\x9c\x5f\x7d\x45\xc5\xe4\xe4\x86" \
  311. "\xaf\x1a\x15\x6c\x9b\xc3\x7c\xc5\x88\x2b\xf3\xd9\x72\x76\x47" \
  312. "\x56\xe6\x8c\xd1\x6c\x94\x41\x59\x4d\xe2\xd7\x44\x9a\x55\x5e" \
  313. "\xee\x9d\x7c\x8f\x21\x57\x10\x77\xf7\x4b\xd8\x7e\xc0\x4d\xba" \
  314. "\x1f\x96\x2a\x60\x13\xae\xab\x58\x70\xe5\x23\x62\x2b\x63\xb6" \
  315. "\x42\x8e\x8f\x57\xf2\xfa\x47\x37\x91\xac\x11\x3d\x9a\x85\x73" \
  316. "\x9e\x39\x65\xc8\xd4\x5b\xaa\x35\x72\x5f\x40\x31\x9a\xb0\xdd" \
  317. "\xa9\x2c\x16\xa3\x32\xef\xcb\x8c\x80\x33\x60\xd\x85\xce\x22" \
  318. "\x8c\x28\x6\x7f\xff\xf6\x8a\x5f\x21\x8e\xf2\xd0\xd9\x63\x66" \
  319. "\x22\xe8\xe6\x3\x39\xfd\x10\x69\xce\x6c\xc4\xde\xf3\x87\x56" \
  320. "\xc8\x4a\x31\x51\x58\xc5\x62\x30\x8e\xd\xd5\x2f\x7c\x24\xca" \
  321. "\xd1\x12\x1b\x3a\x3e\x95\x99\xa\x7\xc1\x83\xd0\x4f\x97\x8c" \
  322. "\xf1\xb0\x9c\xd8\xb9\x72\xd7\x3e\x6b\x66\x83\x8e\xe9\x86\xad" \
  323. "\xfa\xc2\xe\x4\xb5\x7b\x5d\x0\xbc\x47\xbe\x4\x69\xfa\xd1" \
  324. "\x29\x5c\x77\x38\xfc\x88\xeb\xd5\xe1\x17\x54\xf6\xe5\xb3\xae" \
  325. "\xc7\x14\xb6\x4b\xa6\x42\x4b\xa3\xdf\xa5\xcf\xdb\xad\xcd\x2c" \
  326. "\xa3\x3\x13\xc0\x42\x5d\x6e\x3c\xfe\xd8\xeb\xa7\x96\x47\x2b" \
  327. "\x61\xb3\x70\xc9\x6d\xff\x1a\x82\x65\xdc\x92\x4b\x1a\x52\x75" \
  328. "\xa5\x61\x55\x2b\xe\x7\xde\x1e\x71\xc5\x12\x34\x59\x4f\x19" \
  329. "\x2\x9\xb6\x5\xe6\x7b\xad\xb6\x92\xfb\x84\x32\xf1\x45\x6c" \
  330. "\xec\x1a\xcb\x39\x32\x2\x47\x51\xd6\xc8\x9d\xd0\xb1\xdb\xa8" \
  331. "\x90\x4c\x65\x5a\x77\x1f\xca\x74\x8e\x3b\xce\x76\x55\x8b\x78" \
  332. "\x3c\xf3\x19\x8f\xe1\xc3\xa9\x8a\xc8\xf3\x14\x30\x4e\x77\xe9" \
  333. "\xd5\x6a\xcb\x96\x2f\x31\x35\xff\x6b\x10\x92\xf7\xc4\x33\xb8" \
  334. "\x76\x35\x6\xf\x82\x1c\xfa\x1f\x92\x47\xa1\xf9\x7e\xe5\x51" \
  335. "\xee\x63\xaa\x9a\x38\xa3\xa1\x86\xbf\xf0\xe8\x29\xe1\x19\x83" \
  336. "\xff\x36\x3c\x26\x15\x89\x36\x22\x93\x41\x3e\x63\x36\x34\x4c" \
  337. "\xda\x18\xd4\x18\xd8\xc8\x8a\x10\x1f\x14\x4c\x7f\x79\xfc\x46" \
  338. "\xbb\xc8\x24\x51\xc7\xe4\xfb\xc0\x78\xb1\xe9\xac\xf1\x3d\x55" \
  339. "\x51\x9c\x8\xf0\xa6\x3\xcb\x91\xc6\xf4\xe2\xd4\xe5\x18\x61" \
  340. "\xfc\x8f\x8a\xce\x89\x33\xcd\xf\x7d\x50\xa0\x7d\x3f\xac\x49" \
  341. "\xe1\x71\x92\xc7\x8d\xc0\xd0\x6e\xe4\xf7\xcd\xc1\x47\x9f\x99" \
  342. "\xd5\x7\x20\xad\x64\xdb\xab\x44\xd4\x8\xc6\x9a\xa4\xa7\x7c" \
  343. "\x9b\x13\xe4\x9c\x88\xec\xc4\xcb\xe1\x3f\x5\x5\xf\xd\x3a" \
  344. "\x75\xed\xfa\xc0\x23\x34\x74\xfd\xca\x1c\x74\x77\x29\xc8\xb6" \
  345. "\xe2\xbb\xa1\xa\x2e\xae\x65\x3e\xcb\xf5\x5e\xe0\x29\x4c\xfa" \
  346. "\xab\x35\xea\x7\x9f\xb3\x3b\x9c\x4e\x86\xe8\x5b\x76\x11\xf1" \
  347. "\xbf\x7f\x73\x34\x71\x9\x2d\x2a\x60\x8f\x14\x12\xba\x26\x84" \
  348. "\xb9\x94\xa9\x59\x38\x25\xfd\x77\xc3\xe5\x86\xc4\x3\xda\x32" \
  349. "\x30\xd8\x84\x81\x83\x14\x8c\x24\xee\x51\xa9\x92\x61\xb2\xeb" \
  350. "\xce\xac\x34\xc1\xad\x24\x74\xce\xf9\xce\x5c\xfd\x45\x69\x1d" \
  351. "\xc6\xc2\xaf\x7c\x8d\x5\x52\xb5\x88\x2f\x9f\xee\x6b\x5f\xbd" \
  352. "\xfe\x22\x6\x47\xa2\xc8\x25\x37\x67\x44\x4c\xe\xfe\x7e\x5a" \
  353. "\x36\x7f\x18\x83\x8f\x82\x87\x3b\xbf\xb8\xd2\x37\xff\x52\x60" \
  354. "\xb5\xf3\xd\x20\x80\xcc\xb2\x7a\xdd\xc2\x94\xbc\xe3\xb1\x87" \
  355. "\x3e\x49\x57\xcc\xe9\x5a\xea\xb4\xe\xdf\xa6\x8f\x70\x60\x32" \
  356. "\xb\x7d\x74\xf5\x46\xb6\x93\xc2\x5\x92\x72\xfc\xd9\xd2\xe5" \
  357. "\x90\x36\x2a\xd4\xf9\x50\x33\x52\xa5\xcc\xcf\x14\x9e\xdc\x4f" \
  358. "\xb7\x7d\xcf\x25\xdb\xc0\x46\xdb\xea\xe\x27\xc8\x18\x40\x39" \
  359. "\xbd\xec\x48\xa3\xfa\x87\xa3\x18\x68\xfc\x7a\x44\xa8\xc5\x8c" \
  360. "\x45\x81\x70\x72\x14\x70\xf9\x40\xc8\xe7\x41\xcb\xde\xd\x4e" \
  361. "\x35\x4d\xcd\xe2\x40\xa3\x2e\xbb\xb7\x50\x6c\x26\xb8\xbe\x2a" \
  362. "\x36\x8e\x23\xb\xa\xfe\xed\xa\xe7\xa0\x16\x73\xad\x24\x51" \
  363. "\x7f\xda\x9d\xd7\x9f\x18\xe6\xa8\xe4\x98\xbc\x62\x77\x55\x60" \
  364. "\x88\x16\x25\xbf\x95\xad\xea\xe1\x87\x18\x35\x9e\x7c\x51\xee" \
  365. "\xc0\x80\x8b\xb8\x37\xfd\x95\xfe\x87\x15\xf4\x97\xd5\x61\x4f" \
  366. "\x97\xfa\xaf\x48\xd\x5b\x84\x2d\xdb\x15\xf2\xb4\x17\x4f\x41" \
  367. "\x31\x58\x32\x93\xc1\x52\x34\xa6\x17\xd\x56\x5\xee\xfb\xfb" \
  368. "\x2d\x69\x14\xbe\x24\x94\x8\xb0\xfc\x9f\x2\x95\x88\x7d\xd6" \
  369. "\xe7\xa4\x5b\xbb\xf2\x7d\xd8\xa5\xd2\x7c\x9\x62\x22\x5\x53" \
  370. "\xd0\x67\xeb\x68\xfc\x82\x80\xf\xc9\x73\x76\x36\xb8\x13\x9f" \
  371. "\xb1\xf1\xee\x61\x12\xe7\x5d\x75\x65\xb8\x84\x17\xb\x7b\x28" \
  372. "\x4c\xb7\xda\xbb" ) \
  373. + ( (unsigned char)key[i] * sizeof(int) )); \
  374. } \
  375. bkt &= (num_bkts-1);
  376. /* key comparison function; return 0 if keys equal */
  377. #define HASH_KEYCMP(a,b,len) memcmp(a,b,len)
  378. /* iterate over items in a known bucket to find desired item */
  379. #define HASH_FIND_IN_BKT(hh,head,keyptr,keylen_in,out) \
  380. out = (head.hh_head) ? (head.hh_head->elmt) : NULL; \
  381. while (out) { \
  382. if (out->hh.keylen == keylen_in) { \
  383. if ((HASH_KEYCMP(out->hh.key,keyptr,keylen_in)) == 0) break; \
  384. } \
  385. out= (out->hh.hh_next) ? (out->hh.hh_next->elmt) : NULL; \
  386. }
  387. /* add an item to a bucket */
  388. #define HASH_ADD_TO_BKT(hh,head,add) \
  389. head.count++; \
  390. add->hh.hh_next = head.hh_head; \
  391. add->hh.hh_prev = NULL; \
  392. if (head.hh_head) head.hh_head->hh_prev = &add->hh; \
  393. head.hh_head=&add->hh; \
  394. if (head.count >= ((head.expand_mult+1) * HASH_BKT_CAPACITY_THRESH) \
  395. && add->hh.tbl->noexpand != 1) { \
  396. HASH_EXPAND_BUCKETS(add->hh.tbl) \
  397. }
  398. /* remove an item from a given bucket */
  399. #define HASH_DEL_IN_BKT(hh,head,delptr) \
  400. (head).count--; \
  401. if ((head).hh_head->elmt == delptr) { \
  402. (head).hh_head = delptr->hh.hh_next; \
  403. } \
  404. if (delptr->hh.hh_prev) { \
  405. delptr->hh.hh_prev->hh_next = delptr->hh.hh_next; \
  406. } \
  407. if (delptr->hh.hh_next) { \
  408. delptr->hh.hh_next->hh_prev = delptr->hh.hh_prev; \
  409. }
  410. #define HASH_EXPAND_BUCKETS(tbl) \
  411. tbl->new_buckets = (UT_hash_bucket*)uthash_bkt_malloc( \
  412. 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
  413. if (!tbl->new_buckets) { uthash_fatal( "out of memory"); } \
  414. memset(tbl->new_buckets, 0, \
  415. 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
  416. tbl->bkt_ideal= (tbl->num_items / tbl->num_buckets*2) + \
  417. ((tbl->num_items % (tbl->num_buckets*2)) ? 1 : 0);\
  418. tbl->sum_of_deltas = 0; \
  419. for(tbl->bkt_i = 0; tbl->bkt_i < tbl->num_buckets; tbl->bkt_i++) \
  420. { \
  421. tbl->hh = tbl->buckets[ tbl->bkt_i ].hh_head; \
  422. while (tbl->hh) { \
  423. tbl->hh_nxt = tbl->hh->hh_next; \
  424. tbl->key = tbl->hh->key; \
  425. tbl->keylen = tbl->hh->keylen; \
  426. HASH_FCN(tbl->key,tbl->keylen,tbl->num_buckets*2,tbl->bkt,\
  427. tbl->i,tbl->j,tbl->k); \
  428. tbl->newbkt = &(tbl->new_buckets[ tbl->bkt ]); \
  429. if (++(tbl->newbkt->count) > tbl->bkt_ideal) { \
  430. tbl->sum_of_deltas++; \
  431. tbl->newbkt->expand_mult = tbl->newbkt->count / \
  432. tbl->bkt_ideal; \
  433. } \
  434. tbl->hh->hh_prev = NULL; \
  435. tbl->hh->hh_next = tbl->newbkt->hh_head; \
  436. if (tbl->newbkt->hh_head) tbl->newbkt->hh_head->hh_prev = \
  437. tbl->hh; \
  438. tbl->newbkt->hh_head = tbl->hh; \
  439. tbl->hh = tbl->hh_nxt; \
  440. } \
  441. } \
  442. tbl->num_buckets *= 2; \
  443. uthash_bkt_free( tbl->buckets ); \
  444. tbl->buckets = tbl->new_buckets; \
  445. tbl->new_hash_q = (1<<16) - ((tbl->sum_of_deltas << 16) / tbl->num_items); \
  446. if (tbl->hash_q < (1<<15) && tbl->new_hash_q < (1<<15)) { \
  447. tbl->noexpand=1; \
  448. uthash_noexpand_fyi(tbl); \
  449. } \
  450. tbl->hash_q = tbl->new_hash_q; \
  451. uthash_expand_fyi(tbl);
  452. /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */
  453. #define HASH_SORT(head,cmpfcn) \
  454. if (head) { \
  455. (head)->hh.tbl->insize = 1; \
  456. (head)->hh.tbl->looping = 1; \
  457. (head)->hh.tbl->list = &((head)->hh); \
  458. while ((head)->hh.tbl->looping) { \
  459. (head)->hh.tbl->p = (head)->hh.tbl->list; \
  460. (head)->hh.tbl->list = NULL; \
  461. (head)->hh.tbl->tale = NULL; \
  462. (head)->hh.tbl->nmerges = 0; \
  463. while ((head)->hh.tbl->p) { \
  464. (head)->hh.tbl->nmerges++; \
  465. (head)->hh.tbl->q = (head)->hh.tbl->p; \
  466. (head)->hh.tbl->psize = 0; \
  467. for ( (head)->hh.tbl->i = 0; \
  468. (head)->hh.tbl->i < (head)->hh.tbl->insize; \
  469. (head)->hh.tbl->i++ ) { \
  470. (head)->hh.tbl->psize++; \
  471. (head)->hh.tbl->q = (((head)->hh.tbl->q->next) ? \
  472. ((void*)(((long)((head)->hh.tbl->q->next)) + \
  473. (head)->hh.tbl->hho)) : NULL); \
  474. if (! ((head)->hh.tbl->q) ) break; \
  475. } \
  476. (head)->hh.tbl->qsize = (head)->hh.tbl->insize; \
  477. while (((head)->hh.tbl->psize > 0) || \
  478. (((head)->hh.tbl->qsize > 0) && (head)->hh.tbl->q )) { \
  479. if ((head)->hh.tbl->psize == 0) { \
  480. (head)->hh.tbl->e = (head)->hh.tbl->q; \
  481. (head)->hh.tbl->q = (((head)->hh.tbl->q->next) ? \
  482. ((void*)(((long)((head)->hh.tbl->q->next)) + \
  483. (head)->hh.tbl->hho)) : NULL); \
  484. (head)->hh.tbl->qsize--; \
  485. } else if ( ((head)->hh.tbl->qsize == 0) || \
  486. !((head)->hh.tbl->q) ) { \
  487. (head)->hh.tbl->e = (head)->hh.tbl->p; \
  488. (head)->hh.tbl->p = (((head)->hh.tbl->p->next) ? \
  489. ((void*)(((long)((head)->hh.tbl->p->next)) + \
  490. (head)->hh.tbl->hho)) : NULL); \
  491. (head)->hh.tbl->psize--; \
  492. } else if (( \
  493. cmpfcn((head)->hh.tbl->p->elmt,(head)->hh.tbl->q->elmt)) \
  494. <= 0) { \
  495. (head)->hh.tbl->e = (head)->hh.tbl->p; \
  496. (head)->hh.tbl->p = (((head)->hh.tbl->p->next) ? \
  497. ((void*)(((long)((head)->hh.tbl->p->next)) + \
  498. (head)->hh.tbl->hho)) : NULL); \
  499. (head)->hh.tbl->psize--; \
  500. } else { \
  501. (head)->hh.tbl->e = (head)->hh.tbl->q; \
  502. (head)->hh.tbl->q = (((head)->hh.tbl->q->next) ? \
  503. ((void*)(((long)((head)->hh.tbl->q->next)) + \
  504. (head)->hh.tbl->hho)) : NULL); \
  505. (head)->hh.tbl->qsize--; \
  506. } \
  507. if ( (head)->hh.tbl->tale ) { \
  508. (head)->hh.tbl->tale->next = (((head)->hh.tbl->e) ? \
  509. ((head)->hh.tbl->e->elmt) : NULL); \
  510. } else { \
  511. (head)->hh.tbl->list = (head)->hh.tbl->e; \
  512. } \
  513. (head)->hh.tbl->e->prev = (((head)->hh.tbl->tale) ? \
  514. ((head)->hh.tbl->tale->elmt) : NULL); \
  515. (head)->hh.tbl->tale = (head)->hh.tbl->e; \
  516. } \
  517. (head)->hh.tbl->p = (head)->hh.tbl->q; \
  518. } \
  519. (head)->hh.tbl->tale->next = NULL; \
  520. if ( (head)->hh.tbl->nmerges <= 1 ) { \
  521. (head)->hh.tbl->looping=0; \
  522. (head)->hh.tbl->tail = (head)->hh.tbl->tale; \
  523. (head) = (head)->hh.tbl->list->elmt; \
  524. } \
  525. (head)->hh.tbl->insize *= 2; \
  526. } \
  527. HASH_FSCK(head); \
  528. }
  529. typedef struct UT_hash_bucket {
  530. struct UT_hash_handle *hh_head;
  531. unsigned count;
  532. unsigned expand_mult;
  533. } UT_hash_bucket;
  534. typedef struct UT_hash_table {
  535. UT_hash_bucket *buckets;
  536. unsigned num_buckets;
  537. unsigned num_items;
  538. int noexpand; /* when set, inhibits expansion of buckets for this hash */
  539. int hash_q; /* measures the evenness of the items among buckets (0-1) */
  540. struct UT_hash_handle *tail; /* tail hh in app order, for fast append */
  541. char *name; /* macro-stringified name of list head, used by libut */
  542. int hho;
  543. /* scratch */
  544. unsigned bkt;
  545. char *key;
  546. size_t keylen;
  547. int i,j,k;
  548. /* scratch for bucket expansion */
  549. UT_hash_bucket *new_buckets, *newbkt;
  550. struct UT_hash_handle *hh, *hh_nxt;
  551. unsigned bkt_i, bkt_ideal, sum_of_deltas;
  552. int new_hash_q;
  553. /* scratch for sort */
  554. int looping,nmerges,insize,psize,qsize;
  555. struct UT_hash_handle *p, *q, *e, *list, *tale;
  556. } UT_hash_table;
  557. typedef struct UT_hash_handle {
  558. struct UT_hash_table *tbl;
  559. void *elmt; /* ptr to enclosing element */
  560. void *prev; /* prev element in app order */
  561. void *next; /* next element in app order */
  562. struct UT_hash_handle *hh_prev; /* previous hh in bucket order */
  563. struct UT_hash_handle *hh_next; /* next hh in bucket order */
  564. void *key; /* ptr to enclosing struct's key */
  565. size_t keylen; /* enclosing struct's key len */
  566. } UT_hash_handle;
  567. #endif /* UTHASH_H */