Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.2 KiB

  1. /* adler32.c -- compute the Adler-32 checksum of a data stream
  2. * Copyright (C) 1995-2002 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /* @(#) $Id$ */
  6. #include "zlib.h"
  7. #define BASE 65521L /* largest prime smaller than 65536 */
  8. #define NMAX 5552
  9. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  10. #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  11. #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  12. #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  13. #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  14. #define DO16(buf) DO8(buf,0); DO8(buf,8);
  15. /* ========================================================================= */
  16. uLong ZEXPORT adler32(adler, buf, len)
  17. uLong adler;
  18. const Bytef *buf;
  19. uInt len;
  20. {
  21. unsigned long s1 = adler & 0xffff;
  22. unsigned long s2 = (adler >> 16) & 0xffff;
  23. int k;
  24. if (buf == Z_NULL) return 1L;
  25. while (len > 0) {
  26. k = len < NMAX ? len : NMAX;
  27. len -= k;
  28. while (k >= 16) {
  29. DO16(buf);
  30. buf += 16;
  31. k -= 16;
  32. }
  33. if (k != 0) do {
  34. s1 += *buf++;
  35. s2 += s1;
  36. } while (--k);
  37. s1 %= BASE;
  38. s2 %= BASE;
  39. }
  40. return (s2 << 16) | s1;
  41. }