Slightly different fix for https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9117 :
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include <string.h>
28
29 #include <glib.h>
30
31 #include <epan/adler32.h>
32
33 #define BASE 65521 /* largest prime smaller than 65536 */
34
35 /*--- update_adler32 --------------------------------------------------------*/
36 guint32 update_adler32(guint32 adler, const guint8 *buf, size_t len)
37 {
38   guint32 s1 = adler & 0xffff;
39   guint32 s2 = (adler >> 16) & 0xffff;
40   size_t n;
41
42   for (n = 0; n < len; n++) {
43     s1 = (s1 + buf[n]) % BASE;
44     s2 = (s2 + s1)     % BASE;
45   }
46   return (s2 << 16) + s1;
47 }
48
49 /*--- adler32 ---------------------------------------------------------------*/
50 guint32 adler32_bytes(const guint8 *buf, size_t len)
51 {
52   return update_adler32(1, buf, len);
53 }
54
55 /*--- adler32_str -----------------------------------------------------------*/
56 guint32 adler32_str(const char *buf)
57 {
58   return update_adler32(1, (const guint8*)buf, strlen(buf));
59 }
60
61 /*---------------------------------------------------------------------------*/