hash.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Hash calculation
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "defines.h"
  20. #include "tool.h"
  21. #include "exp.h"
  22. #include "hash.h"
  23. static UINT32 jenkins_hash PARAMS((UINT8 *k, UINT32 length, UINT32 initval));
  24. /**
  25. * Calculate hash value for a given string.
  26. *
  27. * @param String Input string
  28. * @return 32 bit hash value
  29. */
  30. GLOBAL UINT32
  31. Hash( const char *String )
  32. {
  33. char buffer[LINE_LEN];
  34. strlcpy(buffer, String, sizeof(buffer));
  35. return jenkins_hash((UINT8 *)ngt_LowerStr(buffer),
  36. (UINT32)strlen(buffer), 42);
  37. } /* Hash */
  38. /*
  39. * This hash function originates from lookup3.c of Bob Jenkins
  40. * (URL: <http://burtleburtle.net/bob/c/lookup3.c>):
  41. * --------------------------------------------------------------------
  42. * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
  43. * These are functions for producing 32-bit hashes for hash table lookup.
  44. * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
  45. * are externally useful functions. Routines to test the hash are included
  46. * if SELF_TEST is defined. You can use this free for any purpose. It's in
  47. * the public domain. It has no warranty.
  48. * --------------------------------------------------------------------
  49. * Not all of his functions are used here.
  50. */
  51. #define hashsize(n) ((UINT32)1<<(n))
  52. #define hashmask(n) (hashsize(n)-1)
  53. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  54. #define mix(a,b,c) \
  55. { \
  56. a -= c; a ^= rot(c, 4); c += b; \
  57. b -= a; b ^= rot(a, 6); a += c; \
  58. c -= b; c ^= rot(b, 8); b += a; \
  59. a -= c; a ^= rot(c,16); c += b; \
  60. b -= a; b ^= rot(a,19); a += c; \
  61. c -= b; c ^= rot(b, 4); b += a; \
  62. } /* mix */
  63. #define final(a,b,c) \
  64. { \
  65. c ^= b; c -= rot(b,14); \
  66. a ^= c; a -= rot(c,11); \
  67. b ^= a; b -= rot(a,25); \
  68. c ^= b; c -= rot(b,16); \
  69. a ^= c; a -= rot(c,4); \
  70. b ^= a; b -= rot(a,14); \
  71. c ^= b; c -= rot(b,24); \
  72. }
  73. static UINT32
  74. jenkins_hash(UINT8 *k, UINT32 length, UINT32 initval)
  75. {
  76. /* k: the key
  77. * length: length of the key
  78. * initval: the previous hash, or an arbitrary value
  79. */
  80. UINT32 a,b,c;
  81. /* Set up the internal state */
  82. a = b = c = 0xdeadbeef + length + initval;
  83. /* handle most of the key */
  84. while (length > 12) {
  85. a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
  86. b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
  87. c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
  88. mix(a,b,c);
  89. length -= 12;
  90. k += 12;
  91. }
  92. /*-------------------------------- last block: affect all 32 bits of (c) */
  93. switch(length) /* all the case statements fall through */
  94. {
  95. case 12: c+=((UINT32)k[11])<<24;
  96. case 11: c+=((UINT32)k[10]<<16);
  97. case 10: c+=((UINT32)k[9]<<8);
  98. case 9 : c+=k[8];
  99. case 8 : b+=((UINT32)k[7]<<24);
  100. case 7 : b+=((UINT32)k[6]<<16);
  101. case 6 : b+=((UINT32)k[5]<<8);
  102. case 5 : b+=k[4];
  103. case 4 : a+=((UINT32)k[3]<<24);
  104. case 3 : a+=((UINT32)k[2]<<16);
  105. case 2 : a+=((UINT32)k[1]<<8);
  106. case 1 : a+=k[0];
  107. break;
  108. case 0 : return c;
  109. }
  110. final(a,b,c);
  111. return c;
  112. } /* jenkins_hash */
  113. /* -eof- */