hash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2009 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. * Hash calculation
  12. */
  13. #include "portab.h"
  14. static char UNUSED id[] = "$Id: hash.c,v 1.13 2006/10/06 21:23:47 fw Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <string.h>
  18. #include "defines.h"
  19. #include "tool.h"
  20. #include "exp.h"
  21. #include "hash.h"
  22. static UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval ));
  23. GLOBAL UINT32
  24. Hash( const char *String )
  25. {
  26. /* Hash-Wert ueber String berechnen */
  27. char buffer[LINE_LEN];
  28. strlcpy(buffer, String, sizeof(buffer));
  29. return jenkins_hash((UINT8 *)ngt_LowerStr(buffer),
  30. (UINT32)strlen(buffer), 42);
  31. } /* Hash */
  32. /*
  33. * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
  34. * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
  35. * --------------------------------------------------------------------
  36. * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
  37. * hash(), hash2(), hash3, and mix() are externally useful functions.
  38. * Routines to test the hash are included if SELF_TEST is defined.
  39. * You can use this free for any purpose. It has no warranty.
  40. * --------------------------------------------------------------------
  41. * nicht alle seiner Funktionen werden hier genutzt.
  42. */
  43. #define hashsize(n) ((UINT32)1<<(n))
  44. #define hashmask(n) (hashsize(n)-1)
  45. #define mix(a,b,c) \
  46. { \
  47. a -= b; a -= c; a ^= (c>>13); \
  48. b -= c; b -= a; b ^= (a<<8); \
  49. c -= a; c -= b; c ^= (b>>13); \
  50. a -= b; a -= c; a ^= (c>>12); \
  51. b -= c; b -= a; b ^= (a<<16); \
  52. c -= a; c -= b; c ^= (b>>5); \
  53. a -= b; a -= c; a ^= (c>>3); \
  54. b -= c; b -= a; b ^= (a<<10); \
  55. c -= a; c -= b; c ^= (b>>15); \
  56. } /* mix */
  57. static UINT32
  58. jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
  59. {
  60. /* k: the key
  61. * length: length of the key
  62. * initval: the previous hash, or an arbitrary value
  63. */
  64. register UINT32 a,b,c,len;
  65. /* Set up the internal state */
  66. len = length;
  67. a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
  68. c = initval; /* the previous hash value */
  69. /* handle most of the key */
  70. while (len >= 12)
  71. {
  72. a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
  73. b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
  74. c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
  75. mix(a,b,c);
  76. k += 12; len -= 12;
  77. }
  78. /* handle the last 11 bytes */
  79. c += length;
  80. switch( (int)len ) /* all the case statements fall through */
  81. {
  82. case 11: c+=((UINT32)k[10]<<24);
  83. case 10: c+=((UINT32)k[9]<<16);
  84. case 9 : c+=((UINT32)k[8]<<8);
  85. /* the first byte of c is reserved for the length */
  86. case 8 : b+=((UINT32)k[7]<<24);
  87. case 7 : b+=((UINT32)k[6]<<16);
  88. case 6 : b+=((UINT32)k[5]<<8);
  89. case 5 : b+=k[4];
  90. case 4 : a+=((UINT32)k[3]<<24);
  91. case 3 : a+=((UINT32)k[2]<<16);
  92. case 2 : a+=((UINT32)k[1]<<8);
  93. case 1 : a+=k[0];
  94. /* case 0: nothing left to add */
  95. }
  96. mix(a,b,c);
  97. /* report the result */
  98. return c;
  99. } /* jenkins_hash */
  100. /* -eof- */