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.

144 lines
3.9 KiB

  1. /*
  2. * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
  3. *
  4. * @(#) $Revision: 5.1 $
  5. * @(#) $Id: hash_32a.c,v 5.1 2009/06/30 09:13:32 chongo Exp $
  6. * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
  7. *
  8. ***
  9. *
  10. * Fowler/Noll/Vo hash
  11. *
  12. * The basis of this hash algorithm was taken from an idea sent
  13. * as reviewer comments to the IEEE POSIX P1003.2 committee by:
  14. *
  15. * Phong Vo (http://www.research.att.com/info/kpv/)
  16. * Glenn Fowler (http://www.research.att.com/~gsf/)
  17. *
  18. * In a subsequent ballot round:
  19. *
  20. * Landon Curt Noll (http://www.isthe.com/chongo/)
  21. *
  22. * improved on their algorithm. Some people tried this hash
  23. * and found that it worked rather well. In an EMail message
  24. * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
  25. *
  26. * FNV hashes are designed to be fast while maintaining a low
  27. * collision rate. The FNV speed allows one to quickly hash lots
  28. * of data while maintaining a reasonable collision rate. See:
  29. *
  30. * http://www.isthe.com/chongo/tech/comp/fnv/index.html
  31. *
  32. * for more details as well as other forms of the FNV hash.
  33. ***
  34. *
  35. * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
  36. * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
  37. *
  38. ***
  39. *
  40. * Please do not copyright this code. This code is in the public domain.
  41. *
  42. * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  43. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
  44. * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  45. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  46. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  47. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  48. * PERFORMANCE OF THIS SOFTWARE.
  49. *
  50. * By:
  51. * chongo <Landon Curt Noll> /\oo/\
  52. * http://www.isthe.com/chongo/
  53. *
  54. * Share and Enjoy! :-)
  55. */
  56. #include <stdlib.h>
  57. #include "fnv.h"
  58. /*
  59. * 32 bit magic FNV-1a prime
  60. */
  61. #define FNV_32_PRIME ((Fnv32_t)0x01000193)
  62. /*
  63. * fnv_32a_buf - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a buffer
  64. *
  65. * input:
  66. * buf - start of buffer to hash
  67. * len - length of buffer in octets
  68. * hval - previous hash value or 0 if first call
  69. *
  70. * returns:
  71. * 32 bit hash as a static hash type
  72. *
  73. * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
  74. * hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
  75. */
  76. Fnv32_t
  77. fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
  78. {
  79. unsigned char *bp = (unsigned char *)buf; /* start of buffer */
  80. unsigned char *be = bp + len; /* beyond end of buffer */
  81. /*
  82. * FNV-1a hash each octet in the buffer
  83. */
  84. while (bp < be) {
  85. /* xor the bottom with the current octet */
  86. hval ^= (Fnv32_t)*bp++;
  87. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  88. #if defined(NO_FNV_GCC_OPTIMIZATION)
  89. hval *= FNV_32_PRIME;
  90. #else
  91. hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
  92. #endif
  93. }
  94. /* return our new hash value */
  95. return hval;
  96. }
  97. /*
  98. * fnv_32a_str - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a string
  99. *
  100. * input:
  101. * str - string to hash
  102. * hval - previous hash value or 0 if first call
  103. *
  104. * returns:
  105. * 32 bit hash as a static hash type
  106. *
  107. * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
  108. * hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
  109. */
  110. Fnv32_t
  111. fnv_32a_str(char *str, Fnv32_t hval)
  112. {
  113. unsigned char *s = (unsigned char *)str; /* unsigned string */
  114. /*
  115. * FNV-1a hash each octet in the buffer
  116. */
  117. while (*s) {
  118. /* xor the bottom with the current octet */
  119. hval ^= (Fnv32_t)*s++;
  120. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  121. #if defined(NO_FNV_GCC_OPTIMIZATION)
  122. hval *= FNV_32_PRIME;
  123. #else
  124. hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
  125. #endif
  126. }
  127. /* return our new hash value */
  128. return hval;
  129. }