Whamcloud - gitweb
LU-1373 ptlrpc: add flow control extension to ptlrpc req set
[fs/lustre-release.git] / libcfs / libcfs / posix / posix-adler.c
1 /*   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
2
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.
6
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:
10
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.
18
19   Jean-loup Gailly      Mark Adler
20   jloup@gzip.org          madler@alumni.caltech.edu
21
22
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).
26 */
27 #include <libcfs/libcfs.h>
28
29
30 #define BASE 65521L /* largest prime smaller than 65536 */
31 #define NMAX 5552
32 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
33
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);
39
40 /* ========================================================================= */
41 /*
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:
47
48          uLong adler = adler32(0L, NULL, 0);
49
50          while (read_buffer(buffer, length) != EOF) {
51            adler = adler32(adler, buffer, length);
52          }
53          if (adler != original_adler) error();
54 */
55 unsigned long zlib_adler32(unsigned long adler,
56                            const unsigned char *buf,
57                            unsigned int len)
58 {
59         unsigned long s1 = adler & 0xffff;
60         unsigned long s2 = (adler >> 16) & 0xffff;
61         int k;
62
63         if (buf == NULL)
64                 return 1L;
65
66         while (len > 0) {
67                 k = len < NMAX ? len : NMAX;
68                 len -= k;
69                 while (k >= 16) {
70                         DO16(buf);
71                         buf += 16;
72                         k -= 16;
73                 }
74                 if (k != 0)
75                         do {
76                                 s1 += *buf++;
77                                 s2 += s1;
78                         } while (--k);
79                 s1 %= BASE;
80                 s2 %= BASE;
81         }
82         return (s2 << 16) | s1;
83 }