hash.c 3.5 KB

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