adler32.c 4.5 KB

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