adopt recent style, drop compatibility

This commit is contained in:
Connor Olding 2015-04-11 10:34:48 -07:00
parent 38ec3b57d1
commit ff6ee0e980
6 changed files with 58 additions and 62 deletions

View File

@ -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.

View File

@ -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

7
args.c
View File

@ -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]);
}

1
args.h
View File

@ -7,4 +7,3 @@
void args_parse(int argc, char **argv,
void flagfn(char, char*()), void plainfn(char*));

20
crc32.c
View File

@ -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;

76
main.c
View File

@ -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 <n> 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);