hash_adler32.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. | Authors: Michael Wallner <mike@php.net> |
  14. | Sara Golemon <pollita@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php_hash.h"
  18. #include "php_hash_adler32.h"
  19. PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  20. {
  21. context->state = 1;
  22. }
  23. PHP_HASH_API void PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len)
  24. {
  25. uint32_t i, s[2];
  26. s[0] = context->state & 0xffff;
  27. s[1] = (context->state >> 16) & 0xffff;
  28. for (i = 0; i < len; ++i) {
  29. s[0] += input[i];
  30. s[1] += s[0];
  31. if (s[1]>=0x7fffffff)
  32. {
  33. s[0] = s[0] % 65521;
  34. s[1] = s[1] % 65521;
  35. }
  36. }
  37. s[0] = s[0] % 65521;
  38. s[1] = s[1] % 65521;
  39. context->state = s[0] + (s[1] << 16);
  40. }
  41. PHP_HASH_API void PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context)
  42. {
  43. digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
  44. digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
  45. digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
  46. digest[3] = (unsigned char) (context->state & 0xff);
  47. context->state = 0;
  48. }
  49. PHP_HASH_API int PHP_ADLER32Copy(const php_hash_ops *ops, PHP_ADLER32_CTX *orig_context, PHP_ADLER32_CTX *copy_context)
  50. {
  51. copy_context->state = orig_context->state;
  52. return SUCCESS;
  53. }
  54. const php_hash_ops php_hash_adler32_ops = {
  55. "adler32",
  56. (php_hash_init_func_t) PHP_ADLER32Init,
  57. (php_hash_update_func_t) PHP_ADLER32Update,
  58. (php_hash_final_func_t) PHP_ADLER32Final,
  59. (php_hash_copy_func_t) PHP_ADLER32Copy,
  60. php_hash_serialize,
  61. php_hash_unserialize,
  62. PHP_ADLER32_SPEC,
  63. 4, /* what to say here? */
  64. 4,
  65. sizeof(PHP_ADLER32_CTX),
  66. 0
  67. };