split crc_cycle by endianness

This commit is contained in:
Connor Olding 2012-08-05 19:24:31 -07:00
parent 34dc1a8df2
commit a215f38d6d
3 changed files with 20 additions and 23 deletions

29
crc32.c
View File

@ -53,27 +53,26 @@ static void crc_fill_table(int big)
} }
} }
static void crc_fill_tables_once() void crc_be_cycle(ulong *remainder, char c)
{ {
static char filled = 0; static char filled = 0;
if (crc_big_endian && !(filled & 1)) { ulong byte;
if (!filled) {
crc_fill_table(1); crc_fill_table(1);
filled |= 1; filled = 1;
} else if (!crc_big_endian && !(filled & 2)) {
crc_fill_table(0);
filled |= 2;
} }
byte = crc_be_table[((*remainder) >> 24) ^ c];
*remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF;
} }
void crc_cycle(ulong *remainder, char c) void crc_le_cycle(ulong *remainder, char c)
{ {
crc_fill_tables_once(); static char filled = 0;
if (crc_big_endian) { ulong byte;
const ulong byte = crc_be_table[((*remainder) >> 24) ^ c]; if (!filled) {
*remainder = ((*remainder) << 8) ^ byte; crc_fill_table(0);
*remainder &= 0xFFFFFFFF; filled = 1;
} else {
const ulong byte = crc_le_table[((*remainder) ^ c) & 0xFF];
*remainder = ((*remainder) >> 8) ^ byte;
} }
byte = crc_le_table[((*remainder) ^ c) & 0xFF];
*remainder = ((*remainder) >> 8) ^ byte;
} }

View File

@ -5,9 +5,8 @@
* copy of the license along with this program; see the file LICENSE. * copy of the license along with this program; see the file LICENSE.
*/ */
int crc_big_endian;
ulong crc_polynomial; ulong crc_polynomial;
void crc_cycle(ulong *remainder, char c); void crc_be_cycle(ulong *remainder, char c);
void crc_le_cycle(ulong *remainder, char c);
ulong crc_reflect(ulong input); ulong crc_reflect(ulong input);

9
main.c
View File

@ -34,6 +34,7 @@ struct string_node_s {
static string_node *input_node = NULL; static string_node *input_node = NULL;
static ulong starting = 0xFFFFFFFF; static ulong starting = 0xFFFFFFFF;
static char big_endian = 0;
static char print_binary = 0; static char print_binary = 0;
static char xor_output = 1; static char xor_output = 1;
static char reflect_output = 0; static char reflect_output = 0;
@ -72,7 +73,7 @@ static void handle_flag(char flag, char *(*nextarg)())
printf(help2); printf(help2);
exit(0); exit(0);
case 'e': case 'e':
crc_big_endian = 1; big_endian = 1;
return; return;
case 'b': case 'b':
print_binary = 1; print_binary = 1;
@ -83,9 +84,6 @@ static void handle_flag(char flag, char *(*nextarg)())
case 'r': case 'r':
reflect_output = 1; reflect_output = 1;
return; return;
}
switch (flag) {
case 's': case 's':
next = check_next(flag, nextarg()); next = check_next(flag, nextarg());
starting = strtoul(next, NULL, 0); starting = strtoul(next, NULL, 0);
@ -132,6 +130,7 @@ static FILE *open_stream(char *filename)
static ulong cycle_file(FILE *stream) static ulong cycle_file(FILE *stream)
{ {
ulong remainder = starting; ulong remainder = starting;
void (*cycle)(ulong*, char) = (big_endian) ? crc_be_cycle : crc_le_cycle;
int i, len; int i, len;
do { do {
@ -142,7 +141,7 @@ static ulong cycle_file(FILE *stream)
} }
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
crc_cycle(&remainder, buff[i]); cycle(&remainder, buff[i]);
} while (!feof(stream)); } while (!feof(stream));
if (xor_output) if (xor_output)