1 /* Copyright (C) 2012 Connor Olding
3 * This program is licensed under the terms of the MIT License, and
4 * is distributed without any warranty. You should have received a
5 * copy of the license along with this program; see the file LICENSE.
8 typedef unsigned long ulong
;
11 ulong
crc_reflect(ulong input
)
15 for (i
= 0; i
< 4 * 8; i
++) {
17 reflected
|= input
& 1;
23 void crc_fill_table(ulong
*table
, int big
, ulong polynomial
)
25 ulong lsb
= (big
) ? 1 << 31 : 1; /* least significant bit */
26 ulong poly
= (big
) ? polynomial
: crc_reflect(polynomial
);
29 for (c
= 0; c
< CRC_TABLE_SIZE
; c
++, table
++) {
30 *table
= (big
) ? c
<< 24 : c
;
31 for (i
= 0; i
< 8; i
++) {
33 *table
= (big
) ? *table
<< 1 : *table
>> 1;
36 *table
= (big
) ? *table
<< 1 : *table
>> 1;
43 void crc_be_cycle(ulong
*table
, ulong
*remainder
, char c
)
45 ulong byte
= table
[(((*remainder
) >> 24) ^ c
) & 0xff];
46 *remainder
= (((*remainder
) << 8) ^ byte
) & 0xFFFFFFFF;
49 void crc_le_cycle(ulong
*table
, ulong
*remainder
, char c
)
51 ulong byte
= table
[((*remainder
) ^ c
) & 0xFF];
52 *remainder
= ((*remainder
) >> 8) ^ byte
;