1 /* $Id$
2  *  Provides crc calculation routines
3  *  Collected & written by Stas Degteff <g@grumbler.org> 2:5080/102
4  *  (c) Stas Degteff
5  *  (c) HUSKY Developers Team
6  *
7  * Latest version may be foind on http://husky.sourceforge.net
8  *
9  *
10  * HUSKYLIB: common defines, types and functions for HUSKY
11  *
12  * This is part of The HUSKY Fidonet Software project:
13  * see http://husky.sourceforge.net for details
14  *
15  *
16  * HUSKYLIB is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2 of the License, or (at your option) any later version.
20  *
21  * HUSKYLIB is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; see file COPYING. If not, write to the
28  * Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  *
30  * See also http://www.gnu.org, license may be found here.
31  */
32 
33 #ifndef HUSKY_CRC_H
34 #define HUSKY_CRC_H
35 
36 #include "compiler.h"
37 #include "huskyext.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 
44 /* CRC32 initial value */
45 #define CRC32INIT ((dword)0xFFFFFFFFUL)
46 /* CRC16 initial value */
47 #define CRC16INIT ((word)0)
48 
49 
50 
51 /* Calculate CRC32 for memory array
52    str: array
53    size: array size
54    initcrc: initial value (start from 0xFFFFFFFFUL)
55  */
56 HUSKYEXT   dword memcrc32(const char *str, int size, dword initcrc);
57 
58 /* Alias for memcrc32() */
59 #define crc32(x,y,z) memcrc32(x,y,z)
60 
61 /* Calculate CRC32 for ASCIIZ string
62    str: string
63    initcrc: initial value (start from 0xFFFFFFFFUL)
64  */
65 HUSKYEXT   dword strcrc32(const char *str, dword initcrc);
66 
67 /* Calculate CRC32 for file
68    filename: file name for calculate CRC32
69  */
70 HUSKYEXT   dword filecrc32(const char *filename);
71 
72 /* Calculate CRC16 for memory array
73    str: array
74    size: array size
75    initcrc: initial value (start from 0x0000)
76  */
77 HUSKYEXT   hUINT16 memcrc16(const char *str, int size, hUINT16 initcrc);
78 
79 /* Alias for memcrc16() */
80 #define crc16(x,y,z) memcrc32(x,y,z)
81 
82 /* Calculate CRC16 for ASCIIZ string
83    str: string
84    initcrc: initial value (start from 0x0000)
85  */
86 HUSKYEXT   hUINT16 strcrc16(const char *str, hUINT16 initcrc);
87 
88 /* Calculate CRC16 for file
89    filename: file name for calculate CRC16
90  */
91 HUSKYEXT   word filecrc16(const char *filename);
92 
93 
94 /*=======================================================================
95  * Calculating 16-bit checksum, rotating right before each addition;
96  * overflow is discarded.
97  * (This algorithm is the algorithm used by historic BSD UNIX systems as
98  * the `sum` algorithm and by historic AT&T System V UNIX systems
99  * as the `sum -r` algorithm.)
100  *=======================================================================
101  */
102 
103 /* 16-bit checksum (sum -r) for ASCIIZ string */
104 HUSKYEXT word strsum16( const char *str );
105 #define strsumr strsum16
106 
107 /* 16-bit checksum (sum -r) for array of bytes */
108 HUSKYEXT word memsum16( const char *str, unsigned size );
109 #define memsumr memsum16
110 
111 /* 16-bit checksum (sum -r) for file */
112 HUSKYEXT word filesum16(const char *filename);
113 #define filesumr(fn) filesum16((fn),NULL)
114 
115 
116 /*=======================================================================
117  * Calculating 32-bit checksum described in Draft 8 POSIX 1003.2
118  * (This algorithm is the algorithm used by historic AT&T System V UNIX
119  * systems as the `sum` algorithm.)
120  *=======================================================================
121  */
122 
123 /* 32bit checksum for ASCIIZ string */
124 HUSKYEXT dword strsum32( const char *str );
125 
126 /* 32bit checksum for array of bytes */
127 HUSKYEXT dword memsum32( const char *str, unsigned size );
128 
129 /* 32bit checksum for file
130  * plen: pointer to return file lenght, unuse if plen is NULL
131  */
132 HUSKYEXT dword filesum32( const char *filename, unsigned long *plen );
133 
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif
140