2 * Compute the Adler32 checksum (RFC 1950)
4 * Based on code from RFC 1950 (Chapter 9. Appendix: Sample code)
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
29 #include <epan/adler32.h>
31 #define BASE 65521 /* largest prime smaller than 65536 */
33 /*--- update_adler32 --------------------------------------------------------*/
34 unsigned long update_adler32(unsigned long adler, const unsigned char *buf, int len)
36 unsigned long s1 = adler & 0xffff;
37 unsigned long s2 = (adler >> 16) & 0xffff;
40 for (n = 0; n < len; n++) {
41 s1 = (s1 + buf[n]) % BASE;
42 s2 = (s2 + s1) % BASE;
44 return (s2 << 16) + s1;
47 /*--- adler32 ---------------------------------------------------------------*/
48 unsigned long adler32_bytes(const unsigned char *buf, int len)
50 return update_adler32(1L, buf, len);
53 /*--- adler32_str -----------------------------------------------------------*/
54 unsigned long adler32_str(const char *buf)
56 return update_adler32(1L, (const unsigned char*)buf, strlen(buf));
59 /*---------------------------------------------------------------------------*/