uint32 -> ulong, partial 64bit compiler support

This commit is contained in:
Connor Olding 2012-04-13 22:20:39 -07:00
parent 1b4d604e0d
commit a068f910f3
2 changed files with 23 additions and 25 deletions

42
crc32.h
View File

@ -22,28 +22,28 @@
/* interface */ /* interface */
typedef unsigned long uint32; typedef unsigned long ulong;
void crc_cycle(uint32 *remainder, char c); void crc_cycle(ulong *remainder, char c);
uint32 crc_reflect(uint32 input); ulong crc_reflect(ulong input);
void crc_set_big_endian(); void crc_set_big_endian();
void crc_set_little_endian(); void crc_set_little_endian();
void crc_set_polynomial(uint32 p); void crc_set_polynomial(ulong p);
/* implementation */ /* implementation */
static char crc_big_endian = 0; static char crc_big_endian = 0;
static const int crc_table_size = 0x100; static const int crc_table_size = 0x100;
static uint32* crc_be_table = NULL; /* big endian */ static ulong* crc_be_table = NULL; /* big endian */
static uint32* crc_le_table = NULL; /* little endian */ static ulong* crc_le_table = NULL; /* little endian */
static uint32 crc_polynomial = 0x04C11DB7; static ulong crc_polynomial = 0x04C11DB7;
uint32 crc_reflect(uint32 input) ulong crc_reflect(ulong input)
{ {
uint32 reflected = 0; ulong reflected = 0;
unsigned int i; int i;
for (i = 0; i < sizeof(uint32) * 8; i++) { for (i = 0; i < 4 * 8; i++) {
reflected <<= 1; reflected <<= 1;
reflected |= input & 1; reflected |= input & 1;
input >>= 1; input >>= 1;
@ -51,9 +51,9 @@ uint32 crc_reflect(uint32 input)
return reflected; return reflected;
} }
static uint32* crc_alloc(size_t size) static ulong* crc_alloc(size_t size)
{ {
uint32* p = (uint32*) malloc(size * sizeof(uint32)); ulong* p = (ulong*) malloc(size * sizeof(ulong));
if (p == NULL) if (p == NULL)
exit(1); exit(1);
return p; return p;
@ -73,7 +73,7 @@ static void crc_allocate_tables_once()
static void crc_fill_big_endian_table() static void crc_fill_big_endian_table()
{ {
const uint32 least_significant_bit = 1 << 31; const ulong least_significant_bit = 1 << 31;
int c, i; int c, i;
crc_allocate_tables_once(); crc_allocate_tables_once();
@ -88,13 +88,14 @@ static void crc_fill_big_endian_table()
crc_be_table[c] <<= 1; crc_be_table[c] <<= 1;
} }
} }
crc_be_table[c] &= 0xFFFFFFFF;
} }
} }
static void crc_fill_little_endian_table() static void crc_fill_little_endian_table()
{ {
const uint32 least_significant_bit = 1; const ulong least_significant_bit = 1;
const uint32 reflected_polynomial = crc_reflect(crc_polynomial); const ulong reflected_polynomial = crc_reflect(crc_polynomial);
int c, i; int c, i;
crc_allocate_tables_once(); crc_allocate_tables_once();
@ -135,22 +136,23 @@ void crc_set_little_endian()
crc_big_endian = 0; crc_big_endian = 0;
} }
void crc_set_polynomial(uint32 p) void crc_set_polynomial(ulong p)
{ {
crc_polynomial = p; crc_polynomial = p;
} }
void crc_cycle(uint32 *remainder, char c) void crc_cycle(ulong *remainder, char c)
{ {
crc_fill_tables_once(); crc_fill_tables_once();
if (crc_big_endian) { if (crc_big_endian) {
const uint32 newByte = crc_be_table[ const ulong newByte = crc_be_table[
((*remainder) >> 24) ^ c]; ((*remainder) >> 24) ^ c];
/* printf("%08X00 ^ %08X =\n", /* printf("%08X00 ^ %08X =\n",
(int)(*remainder), (int)newByte); */ (int)(*remainder), (int)newByte); */
*remainder = ((*remainder) << 8) ^ newByte; *remainder = ((*remainder) << 8) ^ newByte;
*remainder &= 0xFFFFFFFF;
} else { } else {
const uint32 newByte = crc_le_table[ const ulong newByte = crc_le_table[
((*remainder) ^ c) & 0xFF]; ((*remainder) ^ c) & 0xFF];
/* printf(" %08X ^ %08X =\n", /* printf(" %08X ^ %08X =\n",
(int)((*remainder) >> 8), (int)newByte); */ (int)((*remainder) >> 8), (int)newByte); */

6
main.c
View File

@ -27,7 +27,7 @@
#define FREOPEN_BLANK (NULL) #define FREOPEN_BLANK (NULL)
#endif #endif
static uint32 remainder = 0xFFFFFFFF; static ulong remainder = 0xFFFFFFFF;
static FILE* input_stream = NULL; static FILE* input_stream = NULL;
static char* input_filename = "-"; static char* input_filename = "-";
@ -135,10 +135,6 @@ static void cycle_input()
remainder ^= 0xFFFFFFFF; remainder ^= 0xFFFFFFFF;
if (reflect_output) if (reflect_output)
remainder = crc_reflect(remainder); remainder = crc_reflect(remainder);
/* if remainder has unnecessary bits from
left shifting, remove them
remainder &= 0xFFFFFFFF;
*/
} }
static void print_crc() static void print_crc()