adler32.c 4.9 KB

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