1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the config.tests of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <iostream>
|
---|
43 | #include <cstdlib>
|
---|
44 | #include <cstring>
|
---|
45 |
|
---|
46 |
|
---|
47 | class CCRC32
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | CCRC32() { initialize(); }
|
---|
51 |
|
---|
52 | unsigned long FullCRC(const unsigned char *sData, unsigned long ulDataLength)
|
---|
53 | {
|
---|
54 | unsigned long ulCRC = 0xffffffff;
|
---|
55 | PartialCRC(&ulCRC, sData, ulDataLength);
|
---|
56 | return(ulCRC ^ 0xffffffff);
|
---|
57 | }
|
---|
58 |
|
---|
59 | void PartialCRC(unsigned long *ulCRC, const unsigned char *sData, unsigned long ulDataLength)
|
---|
60 | {
|
---|
61 | while(ulDataLength--) {
|
---|
62 | *ulCRC = (*ulCRC >> 8) ^ ulTable[(*ulCRC & 0xFF) ^ *sData++];
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | private:
|
---|
67 | void initialize(void)
|
---|
68 | {
|
---|
69 | unsigned long ulPolynomial = 0x04C11DB7;
|
---|
70 | memset(&ulTable, 0, sizeof(ulTable));
|
---|
71 | for(int iCodes = 0; iCodes <= 0xFF; iCodes++) {
|
---|
72 | ulTable[iCodes] = Reflect(iCodes, 8) << 24;
|
---|
73 | for(int iPos = 0; iPos < 8; iPos++) {
|
---|
74 | ulTable[iCodes] = ((ulTable[iCodes] << 1) & 0xffffffff)
|
---|
75 | ^ ((ulTable[iCodes] & (1 << 31)) ? ulPolynomial : 0);
|
---|
76 | }
|
---|
77 |
|
---|
78 | ulTable[iCodes] = Reflect(ulTable[iCodes], 32);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | unsigned long Reflect(unsigned long ulReflect, const char cChar)
|
---|
82 | {
|
---|
83 | unsigned long ulValue = 0;
|
---|
84 | // Swap bit 0 for bit 7, bit 1 For bit 6, etc....
|
---|
85 | for(int iPos = 1; iPos < (cChar + 1); iPos++) {
|
---|
86 | if(ulReflect & 1) {
|
---|
87 | ulValue |= (1ul << (cChar - iPos));
|
---|
88 | }
|
---|
89 | ulReflect >>= 1;
|
---|
90 | }
|
---|
91 | return ulValue;
|
---|
92 | }
|
---|
93 | unsigned long ulTable[256]; // CRC lookup table array.
|
---|
94 | };
|
---|
95 |
|
---|
96 |
|
---|
97 | int main(int argc, char **argv)
|
---|
98 | {
|
---|
99 | CCRC32 crc;
|
---|
100 | char *name;
|
---|
101 | if (argc < 2) {
|
---|
102 | std::cerr << "usage: crc <string>\n";
|
---|
103 | return 0;
|
---|
104 | } else {
|
---|
105 | name = argv[1];
|
---|
106 | }
|
---|
107 | std::cout << crc.FullCRC((unsigned char *)name, strlen(name)) << std::endl;
|
---|
108 | }
|
---|