Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int order = 32;
- const unsigned long polynom = 0x4c11db7;
- const int direct = 1;
- const unsigned long crcinit = 0xffffffff;
- const unsigned long crcxor = 0xffffffff;
- const int refin = 1;
- const int refout = 1;
- unsigned long crcmask;
- unsigned long crchighbit;
- unsigned long crcinit_direct;
- unsigned long crcinit_nondirect;
- unsigned long crctab[256];
- unsigned long halo_crc32_reflect (unsigned long crc, int bitnum) {
- // reflects the lower 'bitnum' bits of 'crc'
- unsigned long i, j=1, crcout=0;
- for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
- if (crc & i) crcout|=j;
- j<<= 1;
- }
- return (crcout);
- }
- void halo_crc32_generate_crc_table() {
- // make CRC lookup table used by table algorithms
- int i, j;
- unsigned long bit, crc;
- for (i=0; i<256; i++) {
- crc=(unsigned long)i;
- if (refin) crc=halo_crc32_reflect(crc, 8);
- crc<<= order-8;
- for (j=0; j<8; j++) {
- bit = crc & crchighbit;
- crc<<= 1;
- if (bit) crc^= polynom;
- }
- if (refin) crc = halo_crc32_reflect(crc, order);
- crc&= crcmask;
- crctab[i]= crc;
- }
- }
- unsigned long halo_crctablefast (unsigned char* p, unsigned long len) {
- // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
- // only usable with polynom orders of 8, 16, 24 or 32.
- unsigned long crc = crcinit_direct;
- if (refin) crc = halo_crc32_reflect(crc, order);
- if (!refin) while (len--) crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++];
- else while (len--) crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++];
- if (refout^refin) crc = halo_crc32_reflect(crc, order);
- crc^= crcxor;
- crc&= crcmask;
- crc = ~crc & 0xffffffff; // Halo has an inverted CRC32 value and we also want to mask to strip unusable bytes from unsigned long to unsigned int
- return(crc);
- }
- uint32_t halo_crc32 (const void* p, unsigned long len)
- {
- int i;
- unsigned long bit, crc;
- // at first, compute constant bit masks for whole CRC and CRC high bit
- crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
- crchighbit = (unsigned long)1<<(order-1);
- // check parameters
- if (order < 1 || order > 32) {
- printf("ERROR, invalid order, it must be between 1..32.\n");
- return(0);
- }
- if (polynom != (polynom & crcmask)) {
- printf("ERROR, invalid polynom.\n");
- return(0);
- }
- if (crcinit != (crcinit & crcmask)) {
- printf("ERROR, invalid crcinit.\n");
- return(0);
- }
- if (crcxor != (crcxor & crcmask)) {
- printf("ERROR, invalid crcxor.\n");
- return(0);
- }
- // generate lookup table
- halo_crc32_generate_crc_table();
- // compute missing initial CRC value
- if (!direct) {
- crcinit_nondirect = crcinit;
- crc = crcinit;
- for (i=0; i<order; i++) {
- bit = crc & crchighbit;
- crc<<= 1;
- if (bit) crc^= polynom;
- }
- crc&= crcmask;
- crcinit_direct = crc;
- }
- else {
- crcinit_direct = crcinit;
- crc = crcinit;
- for (i=0; i<order; i++) {
- bit = crc & 1;
- if (bit) crc^= polynom;
- crc >>= 1;
- if (bit) crc|= crchighbit;
- }
- crcinit_nondirect = crc;
- }
- uint32_t result = (uint32_t)halo_crctablefast((unsigned char *)p, len);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement