adler32.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* adler32.c -- compute the Adler-32 checksum of a data stream
  2. * Copyright (C) 1995-2004 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /* @(#) $Id$ */
  6. #define ZLIB_INTERNAL
  7. #include "zlib.h"
  8. #define BASE 65521UL /* 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. #ifdef NO_DIVIDE
  18. # define MOD(a) \
  19. do { \
  20. if (a >= (BASE << 16)) a -= (BASE << 16); \
  21. if (a >= (BASE << 15)) a -= (BASE << 15); \
  22. if (a >= (BASE << 14)) a -= (BASE << 14); \
  23. if (a >= (BASE << 13)) a -= (BASE << 13); \
  24. if (a >= (BASE << 12)) a -= (BASE << 12); \
  25. if (a >= (BASE << 11)) a -= (BASE << 11); \
  26. if (a >= (BASE << 10)) a -= (BASE << 10); \
  27. if (a >= (BASE << 9)) a -= (BASE << 9); \
  28. if (a >= (BASE << 8)) a -= (BASE << 8); \
  29. if (a >= (BASE << 7)) a -= (BASE << 7); \
  30. if (a >= (BASE << 6)) a -= (BASE << 6); \
  31. if (a >= (BASE << 5)) a -= (BASE << 5); \
  32. if (a >= (BASE << 4)) a -= (BASE << 4); \
  33. if (a >= (BASE << 3)) a -= (BASE << 3); \
  34. if (a >= (BASE << 2)) a -= (BASE << 2); \
  35. if (a >= (BASE << 1)) a -= (BASE << 1); \
  36. if (a >= BASE) a -= BASE; \
  37. } while (0)
  38. # define MOD4(a) \
  39. do { \
  40. if (a >= (BASE << 4)) a -= (BASE << 4); \
  41. if (a >= (BASE << 3)) a -= (BASE << 3); \
  42. if (a >= (BASE << 2)) a -= (BASE << 2); \
  43. if (a >= (BASE << 1)) a -= (BASE << 1); \
  44. if (a >= BASE) a -= BASE; \
  45. } while (0)
  46. #else
  47. # define MOD(a) a %= BASE
  48. # define MOD4(a) a %= BASE
  49. #endif
  50. /* ========================================================================= */
  51. uLong ZEXPORT adler32(adler, buf, len)
  52. uLong adler;
  53. const Bytef *buf;
  54. uInt len;
  55. {
  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. MOD4(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_combine(adler1, adler2, len2)
  115. uLong adler1;
  116. uLong adler2;
  117. z_off_t len2;
  118. {
  119. unsigned long sum1;
  120. unsigned long sum2;
  121. unsigned rem;
  122. /* the derivation of this formula is left as an exercise for the reader */
  123. rem = (unsigned)(len2 % BASE);
  124. sum1 = adler1 & 0xffff;
  125. sum2 = rem * sum1;
  126. MOD(sum2);
  127. sum1 += (adler2 & 0xffff) + BASE - 1;
  128. sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
  129. if (sum1 > BASE) sum1 -= BASE;
  130. if (sum1 > BASE) sum1 -= BASE;
  131. if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
  132. if (sum2 > BASE) sum2 -= BASE;
  133. return sum1 | (sum2 << 16);
  134. }