chiark / gitweb /
@@@ fltfmt fettling
[mLib] / hash / crc32.c
1 /* -*-c-*-
2  *
3  * Calculating cyclic redundancy values (non-cryptographic!)
4  *
5  * (c) 1998 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * mLib 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 Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 /* --- Local headers --- */
31
32 #include "bits.h"
33 #include "crc32.h"
34
35 /*----- Functionc provided ------------------------------------------------*/
36
37 /* --- @crc32@ --- *
38  *
39  * Arguments:   @uint32 crc@ = carryover from previous call, or zero
40  *              @const void *buf@ = pointer to buffer to check
41  *              @size_t sz@ = size of the buffer
42  *
43  * Returns:     The CRC updated by the new buffer.
44  *
45  * Use:         A restartable CRC calculator.  This is just a function
46  *              wrapper for the macro version.
47  */
48
49 uint32 crc32(uint32 crc, const void *buf, size_t sz)
50 {
51   uint32 c;
52   CRC32(c, crc, buf, sz);
53   return (c);
54 }
55
56 /*----- Test driver -------------------------------------------------------*/
57
58 #ifdef TEST_RIG
59
60 #include <stdio.h>
61
62 int main(void)
63 {
64   uint32 crc = 0;
65   char buf[BUFSIZ];
66   int r;
67
68   do {
69     r = fread(buf, 1, sizeof(buf), stdin);
70     if (r > 0)
71       crc = crc32(crc, buf, r);
72   } while (r == sizeof(buf));
73
74   printf("crc32(stdin) = %08lx\n", (unsigned long)crc);
75   return (0);
76 }
77
78 #endif
79
80 /*----- That's all, folks -------------------------------------------------*/