• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# CRC32 Calculation

liquidspark Apr 7th, 2019 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. const int order = 32;
2. const unsigned long polynom = 0x4c11db7;
3. const int direct = 1;
4. const unsigned long crcinit = 0xffffffff;
5. const unsigned long crcxor = 0xffffffff;
6. const int refin = 1;
7. const int refout = 1;
9. unsigned long crchighbit;
10. unsigned long crcinit_direct;
11. unsigned long crcinit_nondirect;
12. unsigned long crctab[256];
13.
14. unsigned long halo_crc32_reflect (unsigned long crc, int bitnum) {
15.
16.     // reflects the lower 'bitnum' bits of 'crc'
17.
18.     unsigned long i, j=1, crcout=0;
19.
20.     for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
21.         if (crc & i) crcout|=j;
22.         j<<= 1;
23.     }
24.     return (crcout);
25. }
26.
27. void halo_crc32_generate_crc_table() {
28.
29.     // make CRC lookup table used by table algorithms
30.
31.     int i, j;
32.     unsigned long bit, crc;
33.
34.     for (i=0; i<256; i++) {
35.
36.         crc=(unsigned long)i;
37.         if (refin) crc=halo_crc32_reflect(crc, 8);
38.         crc<<= order-8;
39.
40.         for (j=0; j<8; j++) {
41.
42.             bit = crc & crchighbit;
43.             crc<<= 1;
44.             if (bit) crc^= polynom;
45.         }
46.
47.         if (refin) crc = halo_crc32_reflect(crc, order);
49.         crctab[i]= crc;
50.     }
51. }
52.
53. unsigned long halo_crctablefast (unsigned char* p, unsigned long len) {
54.
55.     // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
56.     // only usable with polynom orders of 8, 16, 24 or 32.
57.
58.     unsigned long crc = crcinit_direct;
59.
60.     if (refin) crc = halo_crc32_reflect(crc, order);
61.
62.     if (!refin) while (len--) crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++];
63.     else while (len--) crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++];
64.
65.     if (refout^refin) crc = halo_crc32_reflect(crc, order);
66.     crc^= crcxor;
68.
69.     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
70.
71.     return(crc);
72. }
73.
74. uint32_t halo_crc32 (const void* p, unsigned long len)
75. {
76.     int i;
77.     unsigned long bit, crc;
78.
79.     // at first, compute constant bit masks for whole CRC and CRC high bit
80.
82.     crchighbit = (unsigned long)1<<(order-1);
83.
84.     // check parameters
85.
86.     if (order < 1 || order > 32) {
87.         printf("ERROR, invalid order, it must be between 1..32.\n");
88.         return(0);
89.     }
90.
91.     if (polynom != (polynom & crcmask)) {
92.         printf("ERROR, invalid polynom.\n");
93.         return(0);
94.     }
95.
96.     if (crcinit != (crcinit & crcmask)) {
97.         printf("ERROR, invalid crcinit.\n");
98.         return(0);
99.     }
100.
101.     if (crcxor != (crcxor & crcmask)) {
102.         printf("ERROR, invalid crcxor.\n");
103.         return(0);
104.     }
105.
106.     // generate lookup table
107.
108.     halo_crc32_generate_crc_table();
109.
110.     // compute missing initial CRC value
111.
112.     if (!direct) {
113.
114.         crcinit_nondirect = crcinit;
115.         crc = crcinit;
116.         for (i=0; i<order; i++) {
117.
118.             bit = crc & crchighbit;
119.             crc<<= 1;
120.             if (bit) crc^= polynom;
121.         }
123.         crcinit_direct = crc;
124.     }
125.
126.     else {
127.
128.         crcinit_direct = crcinit;
129.         crc = crcinit;
130.         for (i=0; i<order; i++) {
131.
132.             bit = crc & 1;
133.             if (bit) crc^= polynom;
134.             crc >>= 1;
135.             if (bit) crc|= crchighbit;
136.         }
137.         crcinit_nondirect = crc;
138.     }
139.
140.     uint32_t result = (uint32_t)halo_crctablefast((unsigned char *)p, len);
141.     return result;
142. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top