adler32.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* adler32.c -- compute the Adler-32 checksum of a data stream
  2. * Copyright (C) 1995-2011, 2016 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zbuild.h"
  6. #include "functable.h"
  7. #include "adler32_p.h"
  8. #ifdef ZLIB_COMPAT
  9. unsigned long Z_EXPORT PREFIX(adler32_z)(unsigned long adler, const unsigned char *buf, size_t len) {
  10. return (unsigned long)FUNCTABLE_CALL(adler32)((uint32_t)adler, buf, len);
  11. }
  12. #else
  13. uint32_t Z_EXPORT PREFIX(adler32_z)(uint32_t adler, const unsigned char *buf, size_t len) {
  14. return FUNCTABLE_CALL(adler32)(adler, buf, len);
  15. }
  16. #endif
  17. /* ========================================================================= */
  18. #ifdef ZLIB_COMPAT
  19. unsigned long Z_EXPORT PREFIX(adler32)(unsigned long adler, const unsigned char *buf, unsigned int len) {
  20. return (unsigned long)FUNCTABLE_CALL(adler32)((uint32_t)adler, buf, len);
  21. }
  22. #else
  23. uint32_t Z_EXPORT PREFIX(adler32)(uint32_t adler, const unsigned char *buf, uint32_t len) {
  24. return FUNCTABLE_CALL(adler32)(adler, buf, len);
  25. }
  26. #endif
  27. /* ========================================================================= */
  28. static uint32_t adler32_combine_(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
  29. uint32_t sum1;
  30. uint32_t sum2;
  31. unsigned rem;
  32. /* for negative len, return invalid adler32 as a clue for debugging */
  33. if (len2 < 0)
  34. return 0xffffffff;
  35. /* the derivation of this formula is left as an exercise for the reader */
  36. len2 %= BASE; /* assumes len2 >= 0 */
  37. rem = (unsigned)len2;
  38. sum1 = adler1 & 0xffff;
  39. sum2 = rem * sum1;
  40. sum2 %= BASE;
  41. sum1 += (adler2 & 0xffff) + BASE - 1;
  42. sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
  43. if (sum1 >= BASE) sum1 -= BASE;
  44. if (sum1 >= BASE) sum1 -= BASE;
  45. if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
  46. if (sum2 >= BASE) sum2 -= BASE;
  47. return sum1 | (sum2 << 16);
  48. }
  49. /* ========================================================================= */
  50. #ifdef ZLIB_COMPAT
  51. unsigned long Z_EXPORT PREFIX(adler32_combine)(unsigned long adler1, unsigned long adler2, z_off_t len2) {
  52. return (unsigned long)adler32_combine_((uint32_t)adler1, (uint32_t)adler2, len2);
  53. }
  54. unsigned long Z_EXPORT PREFIX4(adler32_combine)(unsigned long adler1, unsigned long adler2, z_off64_t len2) {
  55. return (unsigned long)adler32_combine_((uint32_t)adler1, (uint32_t)adler2, len2);
  56. }
  57. #else
  58. uint32_t Z_EXPORT PREFIX4(adler32_combine)(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
  59. return adler32_combine_(adler1, adler2, len2);
  60. }
  61. #endif