From a215f38d6dded1dec29767fbb47a0b5c0c0ec486 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sun, 5 Aug 2012 19:24:31 -0700 Subject: [PATCH] split crc_cycle by endianness --- crc32.c | 29 ++++++++++++++--------------- crc32.h | 5 ++--- main.c | 9 ++++----- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/crc32.c b/crc32.c index 015463d..94680c4 100644 --- a/crc32.c +++ b/crc32.c @@ -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; - if (crc_big_endian && !(filled & 1)) { + ulong byte; + if (!filled) { crc_fill_table(1); - filled |= 1; - } else if (!crc_big_endian && !(filled & 2)) { - crc_fill_table(0); - filled |= 2; + filled = 1; } + 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(); - if (crc_big_endian) { - const ulong byte = crc_be_table[((*remainder) >> 24) ^ c]; - *remainder = ((*remainder) << 8) ^ byte; - *remainder &= 0xFFFFFFFF; - } else { - const ulong byte = crc_le_table[((*remainder) ^ c) & 0xFF]; - *remainder = ((*remainder) >> 8) ^ byte; + static char filled = 0; + ulong byte; + if (!filled) { + crc_fill_table(0); + filled = 1; } + byte = crc_le_table[((*remainder) ^ c) & 0xFF]; + *remainder = ((*remainder) >> 8) ^ byte; } diff --git a/crc32.h b/crc32.h index b939d95..aa4dc99 100644 --- a/crc32.h +++ b/crc32.h @@ -5,9 +5,8 @@ * copy of the license along with this program; see the file LICENSE. */ -int crc_big_endian; 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); - diff --git a/main.c b/main.c index 0639436..7da9a78 100644 --- a/main.c +++ b/main.c @@ -34,6 +34,7 @@ struct string_node_s { static string_node *input_node = NULL; static ulong starting = 0xFFFFFFFF; +static char big_endian = 0; static char print_binary = 0; static char xor_output = 1; static char reflect_output = 0; @@ -72,7 +73,7 @@ static void handle_flag(char flag, char *(*nextarg)()) printf(help2); exit(0); case 'e': - crc_big_endian = 1; + big_endian = 1; return; case 'b': print_binary = 1; @@ -83,9 +84,6 @@ static void handle_flag(char flag, char *(*nextarg)()) case 'r': reflect_output = 1; return; - } - - switch (flag) { case 's': next = check_next(flag, nextarg()); starting = strtoul(next, NULL, 0); @@ -132,6 +130,7 @@ static FILE *open_stream(char *filename) static ulong cycle_file(FILE *stream) { ulong remainder = starting; + void (*cycle)(ulong*, char) = (big_endian) ? crc_be_cycle : crc_le_cycle; int i, len; do { @@ -142,7 +141,7 @@ static ulong cycle_file(FILE *stream) } for (i = 0; i < len; i++) - crc_cycle(&remainder, buff[i]); + cycle(&remainder, buff[i]); } while (!feof(stream)); if (xor_output)