From ff6ee0e9808fb8002cf65a5830e57b4529914ed9 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 10:34:48 -0700 Subject: [PATCH 01/16] adopt recent style, drop compatibility --- LICENSE | 3 +-- Makefile | 13 +++------- args.c | 7 +++--- args.h | 1 - crc32.c | 20 ++++++++------- main.c | 76 ++++++++++++++++++++++++++++---------------------------- 6 files changed, 58 insertions(+), 62 deletions(-) diff --git a/LICENSE b/LICENSE index c34f735..985d160 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (C) 2012 Connor Olding +Copyright (C) 2012, 2015 Connor Olding Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -20,4 +20,3 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/Makefile b/Makefile index a5218e5..bf8026d 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,14 @@ PROGRAM = crc32 .PHONY: all clean install -FILES = main.c args.c crc32.c - -CFLAGS += -Wall -Werror -ansi -pedantic -LDFLAGS += -PREFIX ?= /usr/local all: $(PROGRAM) -$(PROGRAM): $(FILES) - $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(FILES) +$(PROGRAM): main.c + $(CC) -o $@ -Wall -Winline -std=gnu11 $(CFLAGS) $(LDFLAGS) $^ clean: -rm -f $(PROGRAM) install: - cp $(PROGRAM) $(PREFIX)/bin - + install -d 0755 $(PREFIX)/bin + install -m 0755 $(PROGRAM) $(PREFIX)/bin diff --git a/args.c b/args.c index d813194..90d9a55 100644 --- a/args.c +++ b/args.c @@ -11,7 +11,8 @@ static int argc, argi; static char **argv, *flag; -static char *nextarg() +static char * +nextarg() { char *temp = flag; flag = NULL; @@ -22,7 +23,8 @@ static char *nextarg() return argv[argi]; } -void args_parse(int argc_, char **argv_, +void +args_parse(int argc_, char **argv_, void flagfn(char, char*()), void plainfn(char*)) { argc = argc_; @@ -54,4 +56,3 @@ void args_parse(int argc_, char **argv_, if (plainfn) plainfn(argv[argi]); } - diff --git a/args.h b/args.h index e325306..0b002bc 100644 --- a/args.h +++ b/args.h @@ -7,4 +7,3 @@ void args_parse(int argc, char **argv, void flagfn(char, char*()), void plainfn(char*)); - diff --git a/crc32.c b/crc32.c index 0016db7..8dd6cab 100644 --- a/crc32.c +++ b/crc32.c @@ -8,11 +8,11 @@ typedef unsigned long ulong; #include "crc32.h" -ulong crc_reflect(ulong input) +ulong +crc_reflect(ulong input) { ulong reflected = 0; - int i; - for (i = 0; i < 4 * 8; i++) { + for (int i = 0; i < 4 * 8; i++) { reflected <<= 1; reflected |= input & 1; input >>= 1; @@ -20,15 +20,15 @@ ulong crc_reflect(ulong input) return reflected; } -void crc_fill_table(ulong *table, int big, ulong polynomial) +void +crc_fill_table(ulong *table, int big, ulong polynomial) { ulong lsb = (big) ? 1 << 31 : 1; /* least significant bit */ ulong poly = (big) ? polynomial : crc_reflect(polynomial); - int c, i; - for (c = 0; c < CRC_TABLE_SIZE; c++, table++) { + for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) { *table = (big) ? c << 24 : c; - for (i = 0; i < 8; i++) { + for (int i = 0; i < 8; i++) { if (*table & lsb) { *table = (big) ? *table << 1 : *table >> 1; *table ^= poly; @@ -40,13 +40,15 @@ void crc_fill_table(ulong *table, int big, ulong polynomial) } } -void crc_be_cycle(ulong *table, ulong *remainder, char c) +void +crc_be_cycle(ulong *table, ulong *remainder, char c) { ulong byte = table[(((*remainder) >> 24) ^ c) & 0xff]; *remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF; } -void crc_le_cycle(ulong *table, ulong *remainder, char c) +void +crc_le_cycle(ulong *table, ulong *remainder, char c) { ulong byte = table[((*remainder) ^ c) & 0xFF]; *remainder = ((*remainder) >> 8) ^ byte; diff --git a/main.c b/main.c index 4609675..2d809ff 100644 --- a/main.c +++ b/main.c @@ -10,14 +10,8 @@ typedef unsigned long ulong; -#include "crc32.h" -#include "args.h" - -#ifdef _MSC_VER - #define FREOPEN_BLANK ("") -#else - #define FREOPEN_BLANK (NULL) -#endif +#include "crc32.c" +#include "args.c" #ifndef BUFFER_SIZE #define BUFFER_SIZE 4096 @@ -56,7 +50,8 @@ static const char help2[] = "\ numbers may be entered as hexadecimal or octal with prefixes\n\ "; -static char *check_next(char flag, char *next) { +static char +*check_next(char flag, char *next) { if (!next) { fprintf(stderr, "-%c requires another argument\n", flag); exit(1); @@ -64,41 +59,43 @@ static char *check_next(char flag, char *next) { return next; } -static void handle_flag(char flag, char *(*nextarg)()) +static void +handle_flag(char flag, char *(*nextarg)()) { char *next; switch (flag) { - case 'h': + case 'h': { printf(help1); printf(help2); - exit(0); - case 'e': + } exit(0); + case 'e': { big_endian = 1; - return; - case 'b': + } break; + case 'b': { print_binary = 1; - return; - case 'x': + } break; + case 'x': { xor_output = 0; - return; - case 'r': + } break; + case 'r': { reflect_output = 1; - return; - case 's': + } break; + case 's': { next = check_next(flag, nextarg()); starting = strtoul(next, NULL, 0); - break; - case 'p': + } break; + case 'p': { next = check_next(flag, nextarg()); polynomial = strtoul(next, NULL, 0); - break; - default: + } break; + default: { fprintf(stderr, "Unknown flag: -%c\n", flag); - exit(1); + } exit(1); } } -static void add_input(char *arg) +static void +add_input(char *arg) { static string_node *last = NULL; string_node *n = calloc(1, sizeof(string_node)); @@ -115,7 +112,8 @@ static void add_input(char *arg) last = n; } -static FILE *open_stream(char *filename) +static FILE * +open_stream(char *filename) { FILE *stream = NULL; stream = fopen(filename, "rb"); @@ -127,23 +125,23 @@ static FILE *open_stream(char *filename) } -static ulong cycle_file(FILE *stream) +static ulong +cycle_file(FILE *stream) { ulong remainder = starting; void (*cycle)(ulong*, ulong*, char) = (big_endian) ? crc_be_cycle : crc_le_cycle; ulong table[CRC_TABLE_SIZE]; - int i, len; crc_fill_table(table, big_endian, polynomial); do { - len = fread(buff, 1, BUFFER_SIZE, stream); + int len = fread(buff, 1, BUFFER_SIZE, stream); if (ferror(stream)) { perror(NULL); exit(1); } - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) cycle(table, &remainder, buff[i]); } while (!feof(stream)); @@ -154,7 +152,8 @@ static ulong cycle_file(FILE *stream) return remainder; } -static void print_crc(ulong remainder) +static void +print_crc(ulong remainder) { if (print_binary) fwrite(&remainder, sizeof(remainder), 1, stdout); @@ -162,7 +161,8 @@ static void print_crc(ulong remainder) printf("%08X\n", (int) remainder); } -static void free_nodes(string_node *n) +static void +free_nodes(string_node *n) { string_node *next; while (n) { @@ -172,16 +172,16 @@ static void free_nodes(string_node *n) } } -int main(int argc, char **argv) +int +main(int argc, char **argv) { - string_node *n; args_parse(argc, argv, handle_flag, add_input); if (!input_node) { - freopen(FREOPEN_BLANK, "rb", stdin); + freopen(NULL, "rb", stdin); print_crc(cycle_file(stdin)); } - for (n = input_node; n; n = n->next) { + for (string_node *n = input_node; n; n = n->next) { FILE *stream = open_stream(n->s); print_crc(cycle_file(stream)); fclose(stream); From c9b1c760b19189a41d157781c1c11385d4a18883 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 10:38:37 -0700 Subject: [PATCH 02/16] eliminate headers --- args.c | 8 -------- args.h | 9 --------- crc32.c | 10 +--------- crc32.h | 13 ------------- main.c | 7 ------- 5 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 args.h delete mode 100644 crc32.h diff --git a/args.c b/args.c index 90d9a55..452e3cb 100644 --- a/args.c +++ b/args.c @@ -1,12 +1,4 @@ -/* Copyright (C) 2012 Connor Olding - * - * This program is licensed under the terms of the MIT License, and - * is distributed without any warranty. You should have received a - * copy of the license along with this program; see the file LICENSE. - */ - #include -#include "args.h" static int argc, argi; static char **argv, *flag; diff --git a/args.h b/args.h deleted file mode 100644 index 0b002bc..0000000 --- a/args.h +++ /dev/null @@ -1,9 +0,0 @@ -/* Copyright (C) 2012 Connor Olding - * - * This program is licensed under the terms of the MIT License, and - * is distributed without any warranty. You should have received a - * copy of the license along with this program; see the file LICENSE. - */ - -void args_parse(int argc, char **argv, - void flagfn(char, char*()), void plainfn(char*)); diff --git a/crc32.c b/crc32.c index 8dd6cab..fa2d770 100644 --- a/crc32.c +++ b/crc32.c @@ -1,12 +1,4 @@ -/* Copyright (C) 2012 Connor Olding - * - * This program is licensed under the terms of the MIT License, and - * is distributed without any warranty. You should have received a - * copy of the license along with this program; see the file LICENSE. - */ - -typedef unsigned long ulong; -#include "crc32.h" +enum { CRC_TABLE_SIZE = 0x100 }; ulong crc_reflect(ulong input) diff --git a/crc32.h b/crc32.h deleted file mode 100644 index 4e43b4a..0000000 --- a/crc32.h +++ /dev/null @@ -1,13 +0,0 @@ -/* Copyright (C) 2012 Connor Olding - * - * This program is licensed under the terms of the MIT License, and - * is distributed without any warranty. You should have received a - * copy of the license along with this program; see the file LICENSE. - */ - -enum { CRC_TABLE_SIZE = 0x100 }; - -void crc_fill_table(ulong *table, int big, ulong polynomial); -void crc_be_cycle(ulong *table, ulong *remainder, char c); -void crc_le_cycle(ulong *table, ulong *remainder, char c); -ulong crc_reflect(ulong input); diff --git a/main.c b/main.c index 2d809ff..9c10dc4 100644 --- a/main.c +++ b/main.c @@ -1,10 +1,3 @@ -/* Copyright (C) 2012 Connor Olding - * - * This program is licensed under the terms of the MIT License, and - * is distributed without any warranty. You should have received a - * copy of the license along with this program; see the file LICENSE. - */ - #include #include From e2291b22a27b95358bcd75c3a59d1dd01d9a6e3f Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 10:41:52 -0700 Subject: [PATCH 03/16] use exact type --- crc32.c | 20 ++++++++++---------- main.c | 17 ++++++++--------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/crc32.c b/crc32.c index fa2d770..6e0ebb1 100644 --- a/crc32.c +++ b/crc32.c @@ -1,9 +1,9 @@ enum { CRC_TABLE_SIZE = 0x100 }; -ulong -crc_reflect(ulong input) +uint32_t +crc_reflect(uint32_t input) { - ulong reflected = 0; + uint32_t reflected = 0; for (int i = 0; i < 4 * 8; i++) { reflected <<= 1; reflected |= input & 1; @@ -13,10 +13,10 @@ crc_reflect(ulong input) } void -crc_fill_table(ulong *table, int big, ulong polynomial) +crc_fill_table(uint32_t *table, int big, uint32_t polynomial) { - ulong lsb = (big) ? 1 << 31 : 1; /* least significant bit */ - ulong poly = (big) ? polynomial : crc_reflect(polynomial); + uint32_t lsb = (big) ? 1 << 31 : 1; /* least significant bit */ + uint32_t poly = (big) ? polynomial : crc_reflect(polynomial); for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) { *table = (big) ? c << 24 : c; @@ -33,15 +33,15 @@ crc_fill_table(ulong *table, int big, ulong polynomial) } void -crc_be_cycle(ulong *table, ulong *remainder, char c) +crc_be_cycle(uint32_t *table, uint32_t *remainder, char c) { - ulong byte = table[(((*remainder) >> 24) ^ c) & 0xff]; + uint32_t byte = table[(((*remainder) >> 24) ^ c) & 0xff]; *remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF; } void -crc_le_cycle(ulong *table, ulong *remainder, char c) +crc_le_cycle(uint32_t *table, uint32_t *remainder, char c) { - ulong byte = table[((*remainder) ^ c) & 0xFF]; + uint32_t byte = table[((*remainder) ^ c) & 0xFF]; *remainder = ((*remainder) >> 8) ^ byte; } diff --git a/main.c b/main.c index 9c10dc4..6ca39b4 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,6 @@ #include #include - -typedef unsigned long ulong; +#include #include "crc32.c" #include "args.c" @@ -19,9 +18,9 @@ struct string_node_s { static string_node *input_node = NULL; -static ulong starting = 0xFFFFFFFF; +static uint32_t starting = 0xFFFFFFFF; static char big_endian = 0; -static ulong polynomial = 0x04C11DB7; +static uint32_t polynomial = 0x04C11DB7; static char print_binary = 0; static char xor_output = 1; static char reflect_output = 0; @@ -118,13 +117,13 @@ open_stream(char *filename) } -static ulong +static uint32_t cycle_file(FILE *stream) { - ulong remainder = starting; - void (*cycle)(ulong*, ulong*, char) = + uint32_t remainder = starting; + void (*cycle)(uint32_t*, uint32_t*, char) = (big_endian) ? crc_be_cycle : crc_le_cycle; - ulong table[CRC_TABLE_SIZE]; + uint32_t table[CRC_TABLE_SIZE]; crc_fill_table(table, big_endian, polynomial); do { @@ -146,7 +145,7 @@ cycle_file(FILE *stream) } static void -print_crc(ulong remainder) +print_crc(uint32_t remainder) { if (print_binary) fwrite(&remainder, sizeof(remainder), 1, stdout); From a83a83600748f2b9242e3ecf1315b69537ef03b7 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 10:48:23 -0700 Subject: [PATCH 04/16] inline cyclic functions --- args.c | 2 +- crc32.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/args.c b/args.c index 452e3cb..bbcea06 100644 --- a/args.c +++ b/args.c @@ -15,7 +15,7 @@ nextarg() return argv[argi]; } -void +static void args_parse(int argc_, char **argv_, void flagfn(char, char*()), void plainfn(char*)) { diff --git a/crc32.c b/crc32.c index 6e0ebb1..e4a9110 100644 --- a/crc32.c +++ b/crc32.c @@ -1,6 +1,6 @@ enum { CRC_TABLE_SIZE = 0x100 }; -uint32_t +static inline uint32_t crc_reflect(uint32_t input) { uint32_t reflected = 0; @@ -12,7 +12,7 @@ crc_reflect(uint32_t input) return reflected; } -void +static inline void crc_fill_table(uint32_t *table, int big, uint32_t polynomial) { uint32_t lsb = (big) ? 1 << 31 : 1; /* least significant bit */ @@ -32,14 +32,14 @@ crc_fill_table(uint32_t *table, int big, uint32_t polynomial) } } -void +static inline void crc_be_cycle(uint32_t *table, uint32_t *remainder, char c) { uint32_t byte = table[(((*remainder) >> 24) ^ c) & 0xff]; *remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF; } -void +static inline void crc_le_cycle(uint32_t *table, uint32_t *remainder, char c) { uint32_t byte = table[((*remainder) ^ c) & 0xFF]; From 4524ecba1afcd8efa2b4ac1d148dd9765551bd49 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 10:53:07 -0700 Subject: [PATCH 05/16] specify booleans --- main.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 6ca39b4..e438e54 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,8 @@ #include "crc32.c" #include "args.c" +typedef int bool; + #ifndef BUFFER_SIZE #define BUFFER_SIZE 4096 #endif @@ -19,11 +21,11 @@ struct string_node_s { static string_node *input_node = NULL; static uint32_t starting = 0xFFFFFFFF; -static char big_endian = 0; +static bool big_endian = 0; static uint32_t polynomial = 0x04C11DB7; -static char print_binary = 0; -static char xor_output = 1; -static char reflect_output = 0; +static bool print_binary = 0; +static bool xor_output = 1; +static bool reflect_output = 0; static const char help1[] = "\ crc32 - a 32-bit cyclic rendundancy check calculator\n\ From 2ce38461bbd23f15480ce7e1f9211ff96a855676 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 11:04:03 -0700 Subject: [PATCH 06/16] split fill table endians, mark consts --- crc32.c | 42 +++++++++++++++++++++++++++++++----------- main.c | 5 ++++- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/crc32.c b/crc32.c index e4a9110..e44ac87 100644 --- a/crc32.c +++ b/crc32.c @@ -13,35 +13,55 @@ crc_reflect(uint32_t input) } static inline void -crc_fill_table(uint32_t *table, int big, uint32_t polynomial) +crc_be_fill_table(uint32_t *table, uint32_t polynomial) { - uint32_t lsb = (big) ? 1 << 31 : 1; /* least significant bit */ - uint32_t poly = (big) ? polynomial : crc_reflect(polynomial); + const uint32_t lsb = 1 << 31; + uint32_t poly = polynomial; for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) { - *table = (big) ? c << 24 : c; + uint32_t v = c << 24; for (int i = 0; i < 8; i++) { - if (*table & lsb) { - *table = (big) ? *table << 1 : *table >> 1; - *table ^= poly; + if (v & lsb) { + v <<= 1; + v ^= poly; } else { - *table = (big) ? *table << 1 : *table >> 1; + v <<= 1; } - *table &= 0xFFFFFFFF; } + *table = v; + } +} + +static inline void +crc_le_fill_table(uint32_t *table, uint32_t polynomial) +{ + const uint32_t lsb = 1; + uint32_t poly = crc_reflect(polynomial); + + for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) { + uint32_t v = c; + for (int i = 0; i < 8; i++) { + if (v & lsb) { + v >>= 1; + v ^= poly; + } else { + v >>= 1; + } + } + *table = v; } } static inline void crc_be_cycle(uint32_t *table, uint32_t *remainder, char c) { - uint32_t byte = table[(((*remainder) >> 24) ^ c) & 0xff]; + const uint32_t byte = table[(((*remainder) >> 24) ^ c) & 0xFF]; *remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF; } static inline void crc_le_cycle(uint32_t *table, uint32_t *remainder, char c) { - uint32_t byte = table[((*remainder) ^ c) & 0xFF]; + const uint32_t byte = table[((*remainder) ^ c) & 0xFF]; *remainder = ((*remainder) >> 8) ^ byte; } diff --git a/main.c b/main.c index e438e54..8c4905d 100644 --- a/main.c +++ b/main.c @@ -127,7 +127,10 @@ cycle_file(FILE *stream) (big_endian) ? crc_be_cycle : crc_le_cycle; uint32_t table[CRC_TABLE_SIZE]; - crc_fill_table(table, big_endian, polynomial); + if (big_endian) + crc_be_fill_table(table, polynomial); + else + crc_le_fill_table(table, polynomial); do { int len = fread(buff, 1, BUFFER_SIZE, stream); if (ferror(stream)) { From 58f9e4591e0fc7e156bb9f0f476df64410f3ca76 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 11:10:53 -0700 Subject: [PATCH 07/16] more type work --- crc32.c | 8 ++++---- main.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crc32.c b/crc32.c index e44ac87..68594f2 100644 --- a/crc32.c +++ b/crc32.c @@ -53,14 +53,14 @@ crc_le_fill_table(uint32_t *table, uint32_t polynomial) } static inline void -crc_be_cycle(uint32_t *table, uint32_t *remainder, char c) +crc_be_cycle(uint32_t *table, uint32_t *remainder, uint8_t c) { - const uint32_t byte = table[(((*remainder) >> 24) ^ c) & 0xFF]; - *remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF; + const uint32_t byte = table[(((*remainder) >> 24) ^ c)]; + *remainder = (((*remainder) << 8) ^ byte); } static inline void -crc_le_cycle(uint32_t *table, uint32_t *remainder, char c) +crc_le_cycle(uint32_t *table, uint32_t *remainder, uint8_t c) { const uint32_t byte = table[((*remainder) ^ c) & 0xFF]; *remainder = ((*remainder) >> 8) ^ byte; diff --git a/main.c b/main.c index 8c4905d..190f7eb 100644 --- a/main.c +++ b/main.c @@ -123,8 +123,8 @@ static uint32_t cycle_file(FILE *stream) { uint32_t remainder = starting; - void (*cycle)(uint32_t*, uint32_t*, char) = - (big_endian) ? crc_be_cycle : crc_le_cycle; + typeof(&crc_be_cycle) cycle; + cycle = (big_endian) ? crc_be_cycle : crc_le_cycle; uint32_t table[CRC_TABLE_SIZE]; if (big_endian) From 0226973efab8eb416e513c2db1b16f70ed30d059 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 11:21:46 -0700 Subject: [PATCH 08/16] split loops by endian --- main.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 190f7eb..de2eec6 100644 --- a/main.c +++ b/main.c @@ -118,28 +118,36 @@ open_stream(char *filename) return stream; } +static inline size_t +buff_read(FILE *stream) +{ + size_t len = fread(buff, 1, BUFFER_SIZE, stream); + if (ferror(stream)) { + perror(NULL); + exit(1); + } + return len; +} static uint32_t cycle_file(FILE *stream) { uint32_t remainder = starting; - typeof(&crc_be_cycle) cycle; - cycle = (big_endian) ? crc_be_cycle : crc_le_cycle; uint32_t table[CRC_TABLE_SIZE]; if (big_endian) crc_be_fill_table(table, polynomial); else crc_le_fill_table(table, polynomial); - do { - int len = fread(buff, 1, BUFFER_SIZE, stream); - if (ferror(stream)) { - perror(NULL); - exit(1); - } - for (int i = 0; i < len; i++) - cycle(table, &remainder, buff[i]); + if (big_endian) do { + size_t len = buff_read(stream); + for (size_t i = 0; i < len; i++) + crc_be_cycle(table, &remainder, buff[i]); + } while (!feof(stream)); else do { + size_t len = buff_read(stream); + for (size_t i = 0; i < len; i++) + crc_le_cycle(table, &remainder, buff[i]); } while (!feof(stream)); if (xor_output) From b28e961740cdbf261cd4ddeba064b89b54249495 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 11:27:16 -0700 Subject: [PATCH 09/16] split table index to separate line --- crc32.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crc32.c b/crc32.c index 68594f2..8427ea7 100644 --- a/crc32.c +++ b/crc32.c @@ -55,13 +55,15 @@ crc_le_fill_table(uint32_t *table, uint32_t polynomial) static inline void crc_be_cycle(uint32_t *table, uint32_t *remainder, uint8_t c) { - const uint32_t byte = table[(((*remainder) >> 24) ^ c)]; - *remainder = (((*remainder) << 8) ^ byte); + const uint8_t i = ((*remainder) >> 24) ^ c; + const uint32_t next = table[i]; + *remainder = ((*remainder) << 8) ^ next; } static inline void crc_le_cycle(uint32_t *table, uint32_t *remainder, uint8_t c) { - const uint32_t byte = table[((*remainder) ^ c) & 0xFF]; - *remainder = ((*remainder) >> 8) ^ byte; + const uint8_t i = (*remainder) ^ c; + const uint32_t next = table[i]; + *remainder = ((*remainder) >> 8) ^ next; } From 2927f39bb1ee5ec474dc4492b09082635f0ee889 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 11:48:50 -0700 Subject: [PATCH 10/16] fix install command --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bf8026d..00d85ef 100644 --- a/Makefile +++ b/Makefile @@ -10,5 +10,5 @@ clean: -rm -f $(PROGRAM) install: - install -d 0755 $(PREFIX)/bin + install -m 0755 -d $(PREFIX)/bin install -m 0755 $(PROGRAM) $(PREFIX)/bin From 020acb022c7abc4aaa5e3110952fb9016877a42e Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 12:22:20 -0700 Subject: [PATCH 11/16] reword help --- README.md | 2 +- main.c | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ab2783f..04ade52 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ crc32 - a 32-bit cyclic rendundancy check calculator -x NOT the output -r reverse output's bits -numbers may be entered as hexadecimal or octal with prefixes +numbers may be in hexadecimal or octal using proper prefixes ``` Notes diff --git a/main.c b/main.c index de2eec6..9967a4c 100644 --- a/main.c +++ b/main.c @@ -27,7 +27,7 @@ static bool print_binary = 0; static bool xor_output = 1; static bool reflect_output = 0; -static const char help1[] = "\ +static const char help[] = "\ crc32 - a 32-bit cyclic rendundancy check calculator\n\ \n\ open files as inputs\n\ @@ -39,9 +39,7 @@ crc32 - a 32-bit cyclic rendundancy check calculator\n\ -x NOT the output\n\ -r reverse output's bits\n\ \n\ -"; -static const char help2[] = "\ -numbers may be entered as hexadecimal or octal with prefixes\n\ +numbers may be in hexadecimal or octal using proper prefixes\n\ "; static char @@ -59,8 +57,7 @@ handle_flag(char flag, char *(*nextarg)()) char *next; switch (flag) { case 'h': { - printf(help1); - printf(help2); + printf(help); } exit(0); case 'e': { big_endian = 1; From d5e14c8edddde18f31c913380454aff732529d72 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sat, 11 Apr 2015 19:53:28 -0700 Subject: [PATCH 12/16] use globals instead of calloc/free --- main.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/main.c b/main.c index 9967a4c..7d4b2d2 100644 --- a/main.c +++ b/main.c @@ -12,13 +12,10 @@ typedef int bool; #endif char buff[BUFFER_SIZE]; -typedef struct string_node_s string_node; -struct string_node_s { - char *s; - string_node *next; -}; +#define MAX_INPUTS 256 -static string_node *input_node = NULL; +static char *inputs[MAX_INPUTS]; +static unsigned int input_n = 0; static uint32_t starting = 0xFFFFFFFF; static bool big_endian = 0; @@ -88,19 +85,11 @@ handle_flag(char flag, char *(*nextarg)()) static void add_input(char *arg) { - static string_node *last = NULL; - string_node *n = calloc(1, sizeof(string_node)); - if (!n) { - fprintf(stderr, "calloc failed\n"); + if (input_n >= MAX_INPUTS) { + fprintf(stderr, "Too many inputs specified\n"); exit(1); } - - n->s = arg; - if (!input_node) - input_node = n; - else - last->next = n; - last = n; + inputs[input_n++] = arg; } static FILE * @@ -163,31 +152,19 @@ print_crc(uint32_t remainder) printf("%08X\n", (int) remainder); } -static void -free_nodes(string_node *n) -{ - string_node *next; - while (n) { - next = n->next; - free(n); - n = next; - } -} - int main(int argc, char **argv) { args_parse(argc, argv, handle_flag, add_input); - if (!input_node) { + if (!input_n) { freopen(NULL, "rb", stdin); print_crc(cycle_file(stdin)); } - for (string_node *n = input_node; n; n = n->next) { - FILE *stream = open_stream(n->s); + for (int i = 0; i < input_n; i++) { + FILE *stream = open_stream(inputs[i]); print_crc(cycle_file(stream)); fclose(stream); } - free_nodes(input_node); return 0; } From c3921c69e4b8a8bd55f95d89977e2145f5186cde Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 24 Apr 2017 02:23:21 +0000 Subject: [PATCH 13/16] remove notes from readme "not padding input" was a misunderstanding. the algorithm we use sidesteps the need to explicitly pad anything. big endian calculations are fine. --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 04ade52..7595469 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,4 @@ crc32 - a 32-bit cyclic rendundancy check calculator numbers may be in hexadecimal or octal using proper prefixes ``` -Notes ------ - -* Does not pad input. -* Big endian calculations are somewhat untested. - ## [Smaller Still](https://gist.github.com/notwa/5689243) From accb4a3e268d9d0e259ebc6347e8f9b1e0caa617 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 24 Apr 2017 18:11:42 +0000 Subject: [PATCH 14/16] rewrite out some weirdness --- crc32.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/crc32.c b/crc32.c index 8427ea7..eee14f9 100644 --- a/crc32.c +++ b/crc32.c @@ -4,7 +4,7 @@ static inline uint32_t crc_reflect(uint32_t input) { uint32_t reflected = 0; - for (int i = 0; i < 4 * 8; i++) { + for (int i = 0; i < 32; i++) { reflected <<= 1; reflected |= input & 1; input >>= 1; @@ -18,7 +18,7 @@ crc_be_fill_table(uint32_t *table, uint32_t polynomial) const uint32_t lsb = 1 << 31; uint32_t poly = polynomial; - for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) { + for (int c = 0; c < CRC_TABLE_SIZE; c++) { uint32_t v = c << 24; for (int i = 0; i < 8; i++) { if (v & lsb) { @@ -28,7 +28,7 @@ crc_be_fill_table(uint32_t *table, uint32_t polynomial) v <<= 1; } } - *table = v; + *table++ = v; } } @@ -38,7 +38,7 @@ crc_le_fill_table(uint32_t *table, uint32_t polynomial) const uint32_t lsb = 1; uint32_t poly = crc_reflect(polynomial); - for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) { + for (int c = 0; c < CRC_TABLE_SIZE; c++) { uint32_t v = c; for (int i = 0; i < 8; i++) { if (v & lsb) { @@ -48,22 +48,20 @@ crc_le_fill_table(uint32_t *table, uint32_t polynomial) v >>= 1; } } - *table = v; + *table++ = v; } } static inline void crc_be_cycle(uint32_t *table, uint32_t *remainder, uint8_t c) { - const uint8_t i = ((*remainder) >> 24) ^ c; - const uint32_t next = table[i]; - *remainder = ((*remainder) << 8) ^ next; + const uint8_t i = (*remainder >> 24) ^ c; + *remainder = (*remainder << 8) ^ table[i]; } static inline void crc_le_cycle(uint32_t *table, uint32_t *remainder, uint8_t c) { - const uint8_t i = (*remainder) ^ c; - const uint32_t next = table[i]; - *remainder = ((*remainder) >> 8) ^ next; + const uint8_t i = (*remainder & 0xFF) ^ c; + *remainder = (*remainder >> 8) ^ table[i]; } From 81a33946da5cdd0a84a52648c3e89c5599077412 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 24 Apr 2017 18:14:02 +0000 Subject: [PATCH 15/16] use signed ints for iterating --- main.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 7d4b2d2..48bb8d1 100644 --- a/main.c +++ b/main.c @@ -15,7 +15,7 @@ char buff[BUFFER_SIZE]; #define MAX_INPUTS 256 static char *inputs[MAX_INPUTS]; -static unsigned int input_n = 0; +static int input_n = 0; static uint32_t starting = 0xFFFFFFFF; static bool big_endian = 0; @@ -126,13 +126,15 @@ cycle_file(FILE *stream) else crc_le_fill_table(table, polynomial); + // cast len to int to enable potential signedness optimizations. + // this is safe so long as BUFFER_SIZE can fit in an int. if (big_endian) do { - size_t len = buff_read(stream); - for (size_t i = 0; i < len; i++) + int len = (int) buff_read(stream); + for (int i = 0; i < len; i++) crc_be_cycle(table, &remainder, buff[i]); } while (!feof(stream)); else do { - size_t len = buff_read(stream); - for (size_t i = 0; i < len; i++) + int len = (int) buff_read(stream); + for (int i = 0; i < len; i++) crc_le_cycle(table, &remainder, buff[i]); } while (!feof(stream)); From cbd77618039d400027ee868e0424041f32bfd1a1 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 24 Apr 2017 18:21:38 +0000 Subject: [PATCH 16/16] compile as C99 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 00d85ef..36b805b 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROGRAM = crc32 all: $(PROGRAM) $(PROGRAM): main.c - $(CC) -o $@ -Wall -Winline -std=gnu11 $(CFLAGS) $(LDFLAGS) $^ + $(CC) -o $@ -Wall -Winline -std=c99 $(CFLAGS) $(LDFLAGS) $^ clean: -rm -f $(PROGRAM)