blob: ae86c3836543fb9f0a5a4e5ed4a4d1d7b36dd32b [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001#include <cppunit/extensions/TestFactoryRegistry.h>
2#include <cppunit/CompilerOutputter.h>
3#include <cppunit/ui/text/TestRunner.h>
4#include <string>
5#include <iomanip>
6
7//#include "SHATumblerTest.h"
8
9//CPPUNIT_TEST_SUITE_REGISTRATION(SHATumblerTest);
10
11using namespace std;
12
13#define ULONG unsigned long
14
15ULONG crc32_table[256]; // Lookup table array
16
17// Reflection is a requirement for the official CRC-32 standard.
18// You can create CRCs without it, but they won't conform to the standard.
19ULONG Reflect(ULONG ref, char ch)
20{// Used only by Init_CRC32_Table()
21
22 ULONG value(0);
23
24 // Swap bit 0 for bit 7
25 // bit 1 for bit 6, etc.
26 for(int i = 1; i < (ch + 1); i++) {
27 if(ref & 1)
28 value |= 1 << (ch - i);
29 ref >>= 1;
30 }
31 return value;
32}
33
34// Call this function only once to initialize the CRC table.
35void Init_CRC32_Table()
36{// Called by OnInitDialog()
37
38 // This is the official polynomial used by CRC-32
39 // in PKZip, WinZip and Ethernet.
40 ULONG ulPolynomial = 0x04c11db7;
41
42 // 256 values representing ASCII character codes.
43 for(int i = 0; i <= 0xFF; i++) {
44 crc32_table[i]=Reflect(i, 8) << 24;
45 for (int j = 0; j < 8; j++)
46 crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
47 crc32_table[i] = Reflect(crc32_table[i], 32);
48 //cout << i << ":" << crc32_table[i] << endl;
49 }
50}
51
52// Once the lookup table has been filled in by the two functions above,
53// this function creates all CRCs using only the lookup table.
54int Get_CRC(string &text)
55{// Called by OnChangeText()
56
57 // Be sure to use unsigned variables,
58 // because negative values introduce high bits
59 // where zero bits are required.
60
61 // Start out with all bits set high.
62 ULONG ulCRC(0xffffffff);
63 int len;
64 unsigned char* buffer;
65
66 // Get the length.
67 // Note that if the text contains NULL characters
68 // processing ends at the first NULL and the CRC value is invalid.
69 // See the 32 Bit File Demonstration source code
70 // for a method of dealing with NULL characters in files.
71 len = text.length();
72 // Save the text in the buffer.
73 buffer = (unsigned char*)text.c_str();
74 // Perform the algorithm on each character
75 // in the string, using the lookup table values.
76 while(len--)
77 ulCRC = (ulCRC >> 8) ^ crc32_table[(ulCRC & 0xFF) ^ *buffer++];
78 // Exclusive OR the result with the beginning value.
79 return ulCRC ^ 0xffffffff;
80}
81
82int main(int argc, char * argv[])
83{
84 // if command line contains "-selftest" then this is the post build check
85 // => the output must be in the compiler error format.
86 bool selfTest = (argc > 1) && (std::string("-selftest") == argv[1]);
87
88 // new
89 CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
90 CppUnit::TextUi::TestRunner runner;
91 runner.addTest(registry.makeTest());
92
93 if(selfTest)
94 { // Change the default outputter to a compiler error format outputter
95 // The test runner owns the new outputter.
96 runner.setOutputter(
97 CppUnit::CompilerOutputter::defaultOutputter(&runner.result(), std::cerr)
98 );
99 }
100
101 bool wasSucessful = runner.run("", false);
102
103 //Init_CRC32_Table();
104 //cout << hex << setw(8) << Get_CRC(string("pippo"));
105
106 return !wasSucessful; // inverted as 0 is success 1 is failure
107}