adler32.c 5.1 KB

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