home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d23456 / NSIS.EXE / Source / zlib / ADLER32.C next >
C/C++ Source or Header  |  2001-05-19  |  1KB  |  51 lines

  1. /* adler32.c -- compute the Adler-32 checksum of a data stream
  2.  * Copyright (C) 1995-1998 Mark Adler
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /* @(#) $Id$ */
  7.  
  8. #include "zlib.h"
  9.  
  10. #define BASE 65521L /* largest prime smaller than 65536 */
  11. #define NMAX 5552
  12. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  13.  
  14.  
  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.  
  25.     if (buf == Z_NULL) return 1L;
  26.  
  27.     while (len > 0) 
  28.     {
  29.         k = len < NMAX ? len : NMAX;
  30.         len -= k;
  31.         while (k >= 16) {
  32.             int r=0;
  33.             while (r < 16)
  34.             {
  35.               s1+=buf[r];
  36.               s2+=s1;
  37.               r++;
  38.             }
  39.                   buf += 16;
  40.             k -= 16;
  41.         }
  42.         if (k != 0) do {
  43.             s1 += *buf++;
  44.               s2 += s1;
  45.         } while (--k);
  46.         s1 %= BASE;
  47.         s2 %= BASE;
  48.     }
  49.     return (s2 << 16) | s1;
  50. }
  51.