2 * Compute the Adler32 checksum (RFC 1950)
4 * Based on code from RFC 1950 (Chapter 9. Appendix: Sample code)
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <wsutil/adler32.h>
33 #define BASE 65521 /* largest prime smaller than 65536 */
35 /*--- update_adler32 --------------------------------------------------------*/
36 guint32
update_adler32(guint32 adler
, const guint8
*buf
, size_t len
)
38 guint32 s1
= adler
& 0xffff;
39 guint32 s2
= (adler
>> 16) & 0xffff;
42 for (n
= 0; n
< len
; n
++) {
43 s1
= (s1
+ buf
[n
]) % BASE
;
44 s2
= (s2
+ s1
) % BASE
;
46 return (s2
<< 16) + s1
;
49 /*--- adler32 ---------------------------------------------------------------*/
50 guint32
adler32_bytes(const guint8
*buf
, size_t len
)
52 return update_adler32(1, buf
, len
);
55 /*--- adler32_str -----------------------------------------------------------*/
56 guint32
adler32_str(const char *buf
)
58 return update_adler32(1, (const guint8
*)buf
, strlen(buf
));
61 /*---------------------------------------------------------------------------*/