source: trunk/config.tests/mac/crc/main.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 1.8 KB
Line 
1#include <iostream>
2#include <cstdlib>
3#include <cstring>
4
5
6class CCRC32
7{
8public:
9 CCRC32() { initialize(); }
10
11 unsigned long FullCRC(const unsigned char *sData, unsigned long ulDataLength)
12 {
13 unsigned long ulCRC = 0xffffffff;
14 PartialCRC(&ulCRC, sData, ulDataLength);
15 return(ulCRC ^ 0xffffffff);
16 }
17
18 void PartialCRC(unsigned long *ulCRC, const unsigned char *sData, unsigned long ulDataLength)
19 {
20 while(ulDataLength--) {
21 *ulCRC = (*ulCRC >> 8) ^ ulTable[(*ulCRC & 0xFF) ^ *sData++];
22 }
23 }
24
25private:
26 void initialize(void)
27 {
28 unsigned long ulPolynomial = 0x04C11DB7;
29 memset(&ulTable, 0, sizeof(ulTable));
30 for(int iCodes = 0; iCodes <= 0xFF; iCodes++) {
31 ulTable[iCodes] = Reflect(iCodes, 8) << 24;
32 for(int iPos = 0; iPos < 8; iPos++) {
33 ulTable[iCodes] = (ulTable[iCodes] << 1)
34 ^ ((ulTable[iCodes] & (1 << 31)) ? ulPolynomial : 0);
35 }
36
37 ulTable[iCodes] = Reflect(ulTable[iCodes], 32);
38 }
39 }
40 unsigned long Reflect(unsigned long ulReflect, const char cChar)
41 {
42 unsigned long ulValue = 0;
43 // Swap bit 0 for bit 7, bit 1 For bit 6, etc....
44 for(int iPos = 1; iPos < (cChar + 1); iPos++) {
45 if(ulReflect & 1) {
46 ulValue |= (1 << (cChar - iPos));
47 }
48 ulReflect >>= 1;
49 }
50 return ulValue;
51 }
52 unsigned long ulTable[256]; // CRC lookup table array.
53};
54
55
56int main(int argc, char **argv)
57{
58 CCRC32 crc;
59 char *name;
60 if (argc < 2) {
61 std::cerr << "usage: crc <string>\n";
62 return 0;
63 } else {
64 name = argv[1];
65 }
66 std::cout << crc.FullCRC((unsigned char *)name, strlen(name)) << std::endl;
67}
Note: See TracBrowser for help on using the repository browser.