adler32.c 4.2 KB

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