summaryrefslogtreecommitdiff
path: root/hash/crc32.cpp
diff options
context:
space:
mode:
authorzyp <zyp@localhost>2006-01-26 20:07:15 +0100
committerzyp <zyp@localhost>2006-01-26 20:07:15 +0100
commit21559e9f4bd58b1504cc0fda7b0dceee0c833862 (patch)
tree119240a883b5bda7e4c11f60dc39206e87b1e04f /hash/crc32.cpp
[project @ zyp-20060126190715-557e941315671b81]
[project @ 18] Moved disccat and pyanidb to the right location. Added pyqtmpc and ophidia.
Diffstat (limited to 'hash/crc32.cpp')
-rw-r--r--hash/crc32.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/hash/crc32.cpp b/hash/crc32.cpp
new file mode 100644
index 0000000..55d6dd2
--- /dev/null
+++ b/hash/crc32.cpp
@@ -0,0 +1,41 @@
+#include "crc32.h"
+
+int* CRC32::crc_table;
+
+int* CRC32::generate_table() {
+ int crc;
+ int* table = new int[256];
+ for(int i = 0; i < 256; i++) {
+ crc = i << 24;
+ for(int j = 0; j < 8; j++) {
+ if(crc & 0x80000000) {
+ crc = (crc << 1) ^ 0x04c11db7;
+ } else {
+ crc = crc << 1;
+ }
+ }
+ table[i] = crc;
+ }
+ return table;
+}
+
+int CRC32::reflect(int data, int bits) {
+ int x = 0;
+ for(int i = 0; i < bits; i++) {
+ x = x << 1;
+ x |= data & 1;
+ data = data >> 1;
+ }
+ return x;
+}
+
+int CRC32::crc32(int crc, const char* data, int length) {
+ crc = ~reflect(crc, 32);
+ if(!crc_table) {
+ crc_table = generate_table();
+ }
+ for (int i = 0; i < length; i++) {
+ crc = (crc << 8) ^ crc_table[((crc >> 24) ^ reflect(data[i], 8)) & 0xff];
+ }
+ return ~reflect(crc, 32);
+}