From Harald Welte:
[obnox/wireshark/wip.git] / epan / adler32.c
1 /* adler32.c
2  * Compute the Adler32 checksum (RFC 1950)
3  * 2003 Tomas Kukosa
4  * Based on code from RFC 1950 (Chapter 9. Appendix: Sample code)
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #include <string.h>
28
29 #include <epan/adler32.h>
30
31 #define BASE 65521 /* largest prime smaller than 65536 */
32
33 /*--- update_adler32 --------------------------------------------------------*/
34 unsigned long update_adler32(unsigned long adler, const unsigned char *buf, int len)
35 {
36   unsigned long s1 = adler & 0xffff;
37   unsigned long s2 = (adler >> 16) & 0xffff;
38   int n;
39
40   for (n = 0; n < len; n++) {
41     s1 = (s1 + buf[n]) % BASE;
42     s2 = (s2 + s1)     % BASE;
43   }
44   return (s2 << 16) + s1;
45 }
46
47 /*--- adler32 ---------------------------------------------------------------*/
48 unsigned long adler32_bytes(const unsigned char *buf, int len)
49 {
50   return update_adler32(1L, buf, len);
51 }
52
53 /*--- adler32_str -----------------------------------------------------------*/
54 unsigned long adler32_str(const char *buf)
55 {
56   return update_adler32(1L, (const unsigned char*)buf, (int)strlen(buf));
57 }
58
59 /*---------------------------------------------------------------------------*/