limit fstBC to 30bp in Python3 ver.
[GalaxyCodeBases.git] / tools / torrent / mktorrent_crc / crc32.c
blob0016db7307d15206684f9380e17db81ccb5f2f1b
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.
6 */
8 typedef unsigned long ulong;
9 #include "crc32.h"
11 ulong crc_reflect(ulong input)
13 ulong reflected = 0;
14 int i;
15 for (i = 0; i < 4 * 8; i++) {
16 reflected <<= 1;
17 reflected |= input & 1;
18 input >>= 1;
20 return reflected;
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);
27 int c, i;
29 for (c = 0; c < CRC_TABLE_SIZE; c++, table++) {
30 *table = (big) ? c << 24 : c;
31 for (i = 0; i < 8; i++) {
32 if (*table & lsb) {
33 *table = (big) ? *table << 1 : *table >> 1;
34 *table ^= poly;
35 } else {
36 *table = (big) ? *table << 1 : *table >> 1;
38 *table &= 0xFFFFFFFF;
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;