hash.c 3.3 KB

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