adler32.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /* @(#) $Id$ */
  6. #include "zutil.h"
  7. #define BASE 65521U /* largest prime smaller than 65536 */
  8. #define NMAX 5552
  9. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  10. #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
  11. #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  12. #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  13. #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  14. #define DO16(buf) DO8(buf,0); DO8(buf,8);
  15. /* use NO_DIVIDE if your processor does not do division in hardware --
  16. try it both ways to see which is faster */
  17. #ifdef NO_DIVIDE
  18. /* note that this assumes BASE is 65521, where 65536 % 65521 == 15
  19. (thank you to John Reiser for pointing this out) */
  20. # define CHOP(a) \
  21. do { \
  22. unsigned long tmp = a >> 16; \
  23. a &= 0xffffUL; \
  24. a += (tmp << 4) - tmp; \
  25. } while (0)
  26. # define MOD28(a) \
  27. do { \
  28. CHOP(a); \
  29. if (a >= BASE) a -= BASE; \
  30. } while (0)
  31. # define MOD(a) \
  32. do { \
  33. CHOP(a); \
  34. MOD28(a); \
  35. } while (0)
  36. # define MOD63(a) \
  37. do { /* this assumes a is not negative */ \
  38. z_off64_t tmp = a >> 32; \
  39. a &= 0xffffffffL; \
  40. a += (tmp << 8) - (tmp << 5) + tmp; \
  41. tmp = a >> 16; \
  42. a &= 0xffffL; \
  43. a += (tmp << 4) - tmp; \
  44. tmp = a >> 16; \
  45. a &= 0xffffL; \
  46. a += (tmp << 4) - tmp; \
  47. if (a >= BASE) a -= BASE; \
  48. } while (0)
  49. #else
  50. # define MOD(a) a %= BASE
  51. # define MOD28(a) a %= BASE
  52. # define MOD63(a) a %= BASE
  53. #endif
  54. /* ========================================================================= */
  55. uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf, z_size_t len) {
  56. unsigned long sum2;
  57. unsigned n;
  58. /* split Adler-32 into component sums */
  59. sum2 = (adler >> 16) & 0xffff;
  60. adler &= 0xffff;
  61. /* in case user likes doing a byte at a time, keep it fast */
  62. if (len == 1) {
  63. adler += buf[0];
  64. if (adler >= BASE)
  65. adler -= BASE;
  66. sum2 += adler;
  67. if (sum2 >= BASE)
  68. sum2 -= BASE;
  69. return adler | (sum2 << 16);
  70. }
  71. /* initial Adler-32 value (deferred check for len == 1 speed) */
  72. if (buf == Z_NULL)
  73. return 1L;
  74. /* in case short lengths are provided, keep it somewhat fast */
  75. if (len < 16) {
  76. while (len--) {
  77. adler += *buf++;
  78. sum2 += adler;
  79. }
  80. if (adler >= BASE)
  81. adler -= BASE;
  82. MOD28(sum2); /* only added so many BASE's */
  83. return adler | (sum2 << 16);
  84. }
  85. /* do length NMAX blocks -- requires just one modulo operation */
  86. while (len >= NMAX) {
  87. len -= NMAX;
  88. n = NMAX / 16; /* NMAX is divisible by 16 */
  89. do {
  90. DO16(buf); /* 16 sums unrolled */
  91. buf += 16;
  92. } while (--n);
  93. MOD(adler);
  94. MOD(sum2);
  95. }
  96. /* do remaining bytes (less than NMAX, still just one modulo) */
  97. if (len) { /* avoid modulos if none remaining */
  98. while (len >= 16) {
  99. len -= 16;
  100. DO16(buf);
  101. buf += 16;
  102. }
  103. while (len--) {
  104. adler += *buf++;
  105. sum2 += adler;
  106. }
  107. MOD(adler);
  108. MOD(sum2);
  109. }
  110. /* return recombined sums */
  111. return adler | (sum2 << 16);
  112. }
  113. /* ========================================================================= */
  114. uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len) {
  115. return adler32_z(adler, buf, len);
  116. }
  117. /* ========================================================================= */
  118. local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2) {
  119. unsigned long sum1;
  120. unsigned long sum2;
  121. unsigned rem;
  122. /* for negative len, return invalid adler32 as a clue for debugging */
  123. if (len2 < 0)
  124. return 0xffffffffUL;
  125. /* the derivation of this formula is left as an exercise for the reader */
  126. MOD63(len2); /* assumes len2 >= 0 */
  127. rem = (unsigned)len2;
  128. sum1 = adler1 & 0xffff;
  129. sum2 = rem * sum1;
  130. MOD(sum2);
  131. sum1 += (adler2 & 0xffff) + BASE - 1;
  132. sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
  133. if (sum1 >= BASE) sum1 -= BASE;
  134. if (sum1 >= BASE) sum1 -= BASE;
  135. if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
  136. if (sum2 >= BASE) sum2 -= BASE;
  137. return sum1 | (sum2 << 16);
  138. }
  139. /* ========================================================================= */
  140. uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, z_off_t len2) {
  141. return adler32_combine_(adler1, adler2, len2);
  142. }
  143. uLong ZEXPORT adler32_combine64(uLong adler1, uLong adler2, z_off64_t len2) {
  144. return adler32_combine_(adler1, adler2, len2);
  145. }