hash_fnv.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Michael Maclean <mgdm@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /* Based on the public domain algorithm found at
  17. http://www.isthe.com/chongo/tech/comp/fnv/index.html */
  18. #include "php_hash.h"
  19. #include "php_hash_fnv.h"
  20. const php_hash_ops php_hash_fnv132_ops = {
  21. "fnv132",
  22. (php_hash_init_func_t) PHP_FNV132Init,
  23. (php_hash_update_func_t) PHP_FNV132Update,
  24. (php_hash_final_func_t) PHP_FNV132Final,
  25. php_hash_copy,
  26. php_hash_serialize,
  27. php_hash_unserialize,
  28. PHP_FNV132_SPEC,
  29. 4,
  30. 4,
  31. sizeof(PHP_FNV132_CTX),
  32. 0
  33. };
  34. const php_hash_ops php_hash_fnv1a32_ops = {
  35. "fnv1a32",
  36. (php_hash_init_func_t) PHP_FNV132Init,
  37. (php_hash_update_func_t) PHP_FNV1a32Update,
  38. (php_hash_final_func_t) PHP_FNV132Final,
  39. php_hash_copy,
  40. php_hash_serialize,
  41. php_hash_unserialize,
  42. PHP_FNV132_SPEC,
  43. 4,
  44. 4,
  45. sizeof(PHP_FNV132_CTX),
  46. 0
  47. };
  48. const php_hash_ops php_hash_fnv164_ops = {
  49. "fnv164",
  50. (php_hash_init_func_t) PHP_FNV164Init,
  51. (php_hash_update_func_t) PHP_FNV164Update,
  52. (php_hash_final_func_t) PHP_FNV164Final,
  53. php_hash_copy,
  54. php_hash_serialize,
  55. php_hash_unserialize,
  56. PHP_FNV164_SPEC,
  57. 8,
  58. 4,
  59. sizeof(PHP_FNV164_CTX),
  60. 0
  61. };
  62. const php_hash_ops php_hash_fnv1a64_ops = {
  63. "fnv1a64",
  64. (php_hash_init_func_t) PHP_FNV164Init,
  65. (php_hash_update_func_t) PHP_FNV1a64Update,
  66. (php_hash_final_func_t) PHP_FNV164Final,
  67. php_hash_copy,
  68. php_hash_serialize,
  69. php_hash_unserialize,
  70. PHP_FNV164_SPEC,
  71. 8,
  72. 4,
  73. sizeof(PHP_FNV164_CTX),
  74. 0
  75. };
  76. /* {{{ PHP_FNV132Init
  77. * 32-bit FNV-1 hash initialisation
  78. */
  79. PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  80. {
  81. context->state = PHP_FNV1_32_INIT;
  82. }
  83. /* }}} */
  84. PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input,
  85. size_t inputLen)
  86. {
  87. context->state = fnv_32_buf((void *)input, inputLen, context->state, 0);
  88. }
  89. PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input,
  90. size_t inputLen)
  91. {
  92. context->state = fnv_32_buf((void *)input, inputLen, context->state, 1);
  93. }
  94. PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX * context)
  95. {
  96. #ifdef WORDS_BIGENDIAN
  97. memcpy(digest, &context->state, 4);
  98. #else
  99. int i = 0;
  100. unsigned char *c = (unsigned char *) &context->state;
  101. for (i = 0; i < 4; i++) {
  102. digest[i] = c[3 - i];
  103. }
  104. #endif
  105. }
  106. /* {{{ PHP_FNV164Init
  107. * 64-bit FNV-1 hash initialisation
  108. */
  109. PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  110. {
  111. context->state = PHP_FNV1_64_INIT;
  112. }
  113. /* }}} */
  114. PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input,
  115. size_t inputLen)
  116. {
  117. context->state = fnv_64_buf((void *)input, inputLen, context->state, 0);
  118. }
  119. PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input,
  120. size_t inputLen)
  121. {
  122. context->state = fnv_64_buf((void *)input, inputLen, context->state, 1);
  123. }
  124. PHP_HASH_API void PHP_FNV164Final(unsigned char digest[8], PHP_FNV164_CTX * context)
  125. {
  126. #ifdef WORDS_BIGENDIAN
  127. memcpy(digest, &context->state, 8);
  128. #else
  129. int i = 0;
  130. unsigned char *c = (unsigned char *) &context->state;
  131. for (i = 0; i < 8; i++) {
  132. digest[i] = c[7 - i];
  133. }
  134. #endif
  135. }
  136. /*
  137. * fnv_32_buf - perform a 32 bit Fowler/Noll/Vo hash on a buffer
  138. *
  139. * input:
  140. * buf - start of buffer to hash
  141. * len - length of buffer in octets
  142. * hval - previous hash value or 0 if first call
  143. * alternate - if > 0 use the alternate version
  144. *
  145. * returns:
  146. * 32 bit hash as a static hash type
  147. */
  148. static uint32_t
  149. fnv_32_buf(void *buf, size_t len, uint32_t hval, int alternate)
  150. {
  151. unsigned char *bp = (unsigned char *)buf; /* start of buffer */
  152. unsigned char *be = bp + len; /* beyond end of buffer */
  153. /*
  154. * FNV-1 hash each octet in the buffer
  155. */
  156. if (alternate == 0) {
  157. while (bp < be) {
  158. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  159. hval *= PHP_FNV_32_PRIME;
  160. /* xor the bottom with the current octet */
  161. hval ^= (uint32_t)*bp++;
  162. }
  163. } else {
  164. while (bp < be) {
  165. /* xor the bottom with the current octet */
  166. hval ^= (uint32_t)*bp++;
  167. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  168. hval *= PHP_FNV_32_PRIME;
  169. }
  170. }
  171. /* return our new hash value */
  172. return hval;
  173. }
  174. /*
  175. * fnv_64_buf - perform a 64 bit Fowler/Noll/Vo hash on a buffer
  176. *
  177. * input:
  178. * buf - start of buffer to hash
  179. * len - length of buffer in octets
  180. * hval - previous hash value or 0 if first call
  181. * alternate - if > 0 use the alternate version
  182. *
  183. * returns:
  184. * 64 bit hash as a static hash type
  185. */
  186. static uint64_t
  187. fnv_64_buf(void *buf, size_t len, uint64_t hval, int alternate)
  188. {
  189. unsigned char *bp = (unsigned char *)buf; /* start of buffer */
  190. unsigned char *be = bp + len; /* beyond end of buffer */
  191. /*
  192. * FNV-1 hash each octet of the buffer
  193. */
  194. if (alternate == 0) {
  195. while (bp < be) {
  196. /* multiply by the 64 bit FNV magic prime mod 2^64 */
  197. hval *= PHP_FNV_64_PRIME;
  198. /* xor the bottom with the current octet */
  199. hval ^= (uint64_t)*bp++;
  200. }
  201. } else {
  202. while (bp < be) {
  203. /* xor the bottom with the current octet */
  204. hval ^= (uint64_t)*bp++;
  205. /* multiply by the 64 bit FNV magic prime mod 2^64 */
  206. hval *= PHP_FNV_64_PRIME;
  207. }
  208. }
  209. /* return our new hash value */
  210. return hval;
  211. }