hash_adler32.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Michael Wallner <mike@php.net> |
  16. | Sara Golemon <pollita@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php_hash.h"
  20. #include "php_hash_adler32.h"
  21. PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context)
  22. {
  23. context->state = 1;
  24. }
  25. PHP_HASH_API void PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len)
  26. {
  27. uint32_t i, s[2];
  28. s[0] = context->state & 0xffff;
  29. s[1] = (context->state >> 16) & 0xffff;
  30. for (i = 0; i < len; ++i) {
  31. s[0] += input[i];
  32. s[1] += s[0];
  33. if (s[1]>=0x7fffffff)
  34. {
  35. s[0] = s[0] % 65521;
  36. s[1] = s[1] % 65521;
  37. }
  38. }
  39. s[0] = s[0] % 65521;
  40. s[1] = s[1] % 65521;
  41. context->state = s[0] + (s[1] << 16);
  42. }
  43. PHP_HASH_API void PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context)
  44. {
  45. digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
  46. digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
  47. digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
  48. digest[3] = (unsigned char) (context->state & 0xff);
  49. context->state = 0;
  50. }
  51. PHP_HASH_API int PHP_ADLER32Copy(const php_hash_ops *ops, PHP_ADLER32_CTX *orig_context, PHP_ADLER32_CTX *copy_context)
  52. {
  53. copy_context->state = orig_context->state;
  54. return SUCCESS;
  55. }
  56. const php_hash_ops php_hash_adler32_ops = {
  57. (php_hash_init_func_t) PHP_ADLER32Init,
  58. (php_hash_update_func_t) PHP_ADLER32Update,
  59. (php_hash_final_func_t) PHP_ADLER32Final,
  60. (php_hash_copy_func_t) PHP_ADLER32Copy,
  61. 4, /* what to say here? */
  62. 4,
  63. sizeof(PHP_ADLER32_CTX),
  64. 0
  65. };
  66. /*
  67. * Local variables:
  68. * tab-width: 4
  69. * c-basic-offset: 4
  70. * End:
  71. * vim600: sw=4 ts=4 fdm=marker
  72. * vim<600: sw=4 ts=4
  73. */