1 // This may look like C code, but it is really -*- C++ -*-
2
3 // ------------------------------------------------------------------
4 // The Goldware Library
5 // Copyright (C) 1990-1999 Odinn Sorensen
6 // Copyright (C) 2000 Alexander S. Aganichev
7 // ------------------------------------------------------------------
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this program; if not, write to the Free
20 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 // MA 02111-1307, USA
22 // ------------------------------------------------------------------
23 // $Id: gcrcall.h,v 1.5 2006/05/06 09:13:21 ssianky Exp $
24 // ------------------------------------------------------------------
25 // CRC-16, CRC-32 and Hashing.
26 // ------------------------------------------------------------------
27
28 #ifndef __gcrcall_h
29 #define __gcrcall_h
30
31
32 // ------------------------------------------------------------------
33
34 #include <gdefs.h>
35
36
37 // ------------------------------------------------------------------
38 // Declare the CRC tables
39
40 extern word __crc16_table[];
41 extern dword __crc32_table[];
42
43 // ------------------------------------------------------------------
44 // Generate/update a CRC-16 or CRC-32 value
45 //
46 // These inline functions were derived from various #define macro
47 // implementations. Some of those implementations masked the right
48 // shifts to ensure correct zero fill. But this is C++, where zero
49 // fill is guaranteed for unsigned operands, and besides we use
50 // prototyped unsigned parameters anyway, so we have no problem here.
51
updCrc16(byte ch,word crc)52 inline word updCrc16(byte ch, word crc) { return (word)(__crc16_table[crc >> 8] ^ ((crc << 8) & 0xFFFF) ^ ch); }
updCrc16c(byte ch,word crc)53 inline word updCrc16c(byte ch, word crc) { return (word)(__crc16_table[(crc >> 8) ^ ch] ^ ((crc << 8) & 0xFFFF)); }
updCrc32(byte ch,dword crc)54 inline dword updCrc32(byte ch, dword crc) { return (dword)(__crc32_table[(crc & 0xFF) ^ ch] ^ (crc >> 8)); }
55
56
57 // ------------------------------------------------------------------
58 // Define CRC masks in the "normal" and "CCITT" variants
59
60 const word CRC16_MASK_NORMAL = 0;
61 const word CRC16_MASK_CCITT = 0xFFFFU;
62 const dword CRC32_MASK_NORMAL = 0;
63 const dword CRC32_MASK_CCITT = 0xFFFFFFFFUL;
64
65
66 // ------------------------------------------------------------------
67 // Prototypes
68
69 word strCrc16(const char* s, bool nocase=true, word mask=CRC16_MASK_NORMAL);
70 word strCrc16c(const char* s, bool nocase=true, word mask=CRC16_MASK_NORMAL);
71 dword strCrc32(const char* s, bool nocase=true, dword mask=CRC32_MASK_NORMAL);
72
73 dword strHash32(const char* s, bool nocase=true);
74
75 word memCrc16(const void* m, long l, bool nocase=true, word mask=CRC16_MASK_NORMAL);
76 dword memCrc32(const void* m, long l, bool nocase=true, dword mask=CRC32_MASK_NORMAL);
77 inline dword memCrc32(dword crc, const void* m, long l, bool nocase=true, dword mask=CRC32_MASK_NORMAL)
78 {
79 return memCrc32(m, l, nocase, crc ^ mask) ^ mask;
80 }
81
82 // ------------------------------------------------------------------
83 // Get keyword/value pairs and crc
84
85 void getkeyval(char** key, char** val);
86 void getkeyvaleql(char** key, char** val, bool eql);
87 word getkeyvalcrc(char** key, char** val);
88
89
90 // ------------------------------------------------------------------
91
92 #endif
93
94 // ------------------------------------------------------------------
95