adler32.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(uLong adler, const Bytef *buf, uInt len)
  52. {
  53. unsigned long sum2;
  54. unsigned n;
  55. /* split Adler-32 into component sums */
  56. sum2 = (adler >> 16) & 0xffff;
  57. adler &= 0xffff;
  58. /* in case user likes doing a byte at a time, keep it fast */
  59. if (len == 1) {
  60. adler += buf[0];
  61. if (adler >= BASE)
  62. adler -= BASE;
  63. sum2 += adler;
  64. if (sum2 >= BASE)
  65. sum2 -= BASE;
  66. return adler | (sum2 << 16);
  67. }
  68. /* initial Adler-32 value (deferred check for len == 1 speed) */
  69. if (buf == Z_NULL)
  70. return 1L;
  71. /* in case short lengths are provided, keep it somewhat fast */
  72. if (len < 16) {
  73. while (len--) {
  74. adler += *buf++;
  75. sum2 += adler;
  76. }
  77. if (adler >= BASE)
  78. adler -= BASE;
  79. MOD4(sum2); /* only added so many BASE's */
  80. return adler | (sum2 << 16);
  81. }
  82. /* do length NMAX blocks -- requires just one modulo operation */
  83. while (len >= NMAX) {
  84. len -= NMAX;
  85. n = NMAX / 16; /* NMAX is divisible by 16 */
  86. do {
  87. DO16(buf); /* 16 sums unrolled */
  88. buf += 16;
  89. } while (--n);
  90. MOD(adler);
  91. MOD(sum2);
  92. }
  93. /* do remaining bytes (less than NMAX, still just one modulo) */
  94. if (len) { /* avoid modulos if none remaining */
  95. while (len >= 16) {
  96. len -= 16;
  97. DO16(buf);
  98. buf += 16;
  99. }
  100. while (len--) {
  101. adler += *buf++;
  102. sum2 += adler;
  103. }
  104. MOD(adler);
  105. MOD(sum2);
  106. }
  107. /* return recombined sums */
  108. return adler | (sum2 << 16);
  109. }