This is a disaster, SCons is annoying the shit out of me, I guess I am going to build...
[ail.git] / source / crc32.cpp
blob674763f0236112893bb0278879711e8e9a73ea7d
1 #include <ail/crc32.hpp>
2 #include <ail/array.hpp>
4 namespace ail
6 namespace
8 ulong crc32_table[256];
10 struct initialise_crc32_table
12 initialise_crc32_table()
14 char const polynomial_terms[] =
16 0, 1, 2, 4, 5, 7, 8,
17 10, 11, 12, 16, 22, 23, 26
20 ulong polynomial_pattern = 0;
21 for(std::size_t i = 0; i < sizeof(polynomial_terms); ++i)
23 ulong shifts = 31 - polynomial_terms[i];
24 polynomial_pattern |= 1 << shifts;
27 for(std::size_t i = 0; i < ail::countof(crc32_table); ++i)
29 ulong new_table_entry = static_cast<ulong>(i);
30 for(int j = 0; j < 8; ++j)
32 bool first_bit_was_set = (new_table_entry & 1);
33 new_table_entry >>= 1;
34 if(first_bit_was_set == true)
35 new_table_entry ^= polynomial_pattern;
36 crc32_table[i] = new_table_entry;
41 crc32_initialiser;
44 crc32::crc32():
45 sum(0)
49 crc32::crc32(char const * data, std::size_t size):
50 sum(0)
52 hash(data, size);
55 ulong crc32::hash(char const * data, std::size_t size)
57 sum ^= 0xffffffff;
58 while(size > 0)
60 ulong index = (*data ^ sum) & 0xff;
61 ulong shifted_sum = sum >> 8;
62 sum = crc32_table[index] ^ shifted_sum;
63 --size;
64 ++data;
66 sum ^= 0xffffffff;
67 return sum;
70 ulong crc32::checksum() const
72 return sum;