1 /* Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
3 This software is provided 'as-is', without any express or implied
4 warranty. In no event will the authors be held liable for any damages
5 arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it
9 freely, subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not
12 claim that you wrote the original software. If you use this software
13 in a product, an acknowledgment in the product documentation would be
14 appreciated but is not required.
15 2. Altered source versions must be plainly marked as such, and must not be
16 misrepresented as being the original software.
17 3. This notice may not be removed or altered from any source distribution.
19 Jean-loup Gailly Mark Adler
20 jloup@gzip.org madler@alumni.caltech.edu
23 The data format used by the zlib library is described by RFCs (Request for
24 Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
25 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
27 #include <libcfs/libcfs.h>
30 #define BASE 65521L /* largest prime smaller than 65536 */
32 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
34 #define DO1(buf, i) {s1 += buf[i]; s2 += s1; }
35 #define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1);
36 #define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2);
37 #define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4);
38 #define DO16(buf) DO8(buf, 0); DO8(buf, 8);
40 /* ========================================================================= */
42 Update a running Adler-32 checksum with the bytes buf[0..len-1] and
43 return the updated checksum. If buf is NULL, this function returns
44 the required initial value for the checksum.
45 An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
46 much faster. Usage example:
48 uLong adler = adler32(0L, NULL, 0);
50 while (read_buffer(buffer, length) != EOF) {
51 adler = adler32(adler, buffer, length);
53 if (adler != original_adler) error();
55 unsigned long zlib_adler32(unsigned long adler,
56 const unsigned char *buf,
59 unsigned long s1 = adler & 0xffff;
60 unsigned long s2 = (adler >> 16) & 0xffff;
67 k = len < NMAX ? len : NMAX;
82 return (s2 << 16) | s1;