adopt recent style, drop compatibility
This commit is contained in:
parent
38ec3b57d1
commit
ff6ee0e980
6 changed files with 58 additions and 62 deletions
3
LICENSE
3
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
|
Permission is hereby granted, free of charge, to any person
|
||||||
obtaining a copy of this software and associated documentation
|
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
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
OTHER DEALINGS IN THE SOFTWARE.
|
OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
|
13
Makefile
13
Makefile
|
@ -1,19 +1,14 @@
|
||||||
PROGRAM = crc32
|
PROGRAM = crc32
|
||||||
.PHONY: all clean install
|
.PHONY: all clean install
|
||||||
FILES = main.c args.c crc32.c
|
|
||||||
|
|
||||||
CFLAGS += -Wall -Werror -ansi -pedantic
|
|
||||||
LDFLAGS +=
|
|
||||||
PREFIX ?= /usr/local
|
|
||||||
|
|
||||||
all: $(PROGRAM)
|
all: $(PROGRAM)
|
||||||
|
|
||||||
$(PROGRAM): $(FILES)
|
$(PROGRAM): main.c
|
||||||
$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(FILES)
|
$(CC) -o $@ -Wall -Winline -std=gnu11 $(CFLAGS) $(LDFLAGS) $^
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
-rm -f $(PROGRAM)
|
-rm -f $(PROGRAM)
|
||||||
|
|
||||||
install:
|
install:
|
||||||
cp $(PROGRAM) $(PREFIX)/bin
|
install -d 0755 $(PREFIX)/bin
|
||||||
|
install -m 0755 $(PROGRAM) $(PREFIX)/bin
|
||||||
|
|
7
args.c
7
args.c
|
@ -11,7 +11,8 @@
|
||||||
static int argc, argi;
|
static int argc, argi;
|
||||||
static char **argv, *flag;
|
static char **argv, *flag;
|
||||||
|
|
||||||
static char *nextarg()
|
static char *
|
||||||
|
nextarg()
|
||||||
{
|
{
|
||||||
char *temp = flag;
|
char *temp = flag;
|
||||||
flag = NULL;
|
flag = NULL;
|
||||||
|
@ -22,7 +23,8 @@ static char *nextarg()
|
||||||
return argv[argi];
|
return argv[argi];
|
||||||
}
|
}
|
||||||
|
|
||||||
void args_parse(int argc_, char **argv_,
|
void
|
||||||
|
args_parse(int argc_, char **argv_,
|
||||||
void flagfn(char, char*()), void plainfn(char*))
|
void flagfn(char, char*()), void plainfn(char*))
|
||||||
{
|
{
|
||||||
argc = argc_;
|
argc = argc_;
|
||||||
|
@ -54,4 +56,3 @@ void args_parse(int argc_, char **argv_,
|
||||||
if (plainfn)
|
if (plainfn)
|
||||||
plainfn(argv[argi]);
|
plainfn(argv[argi]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
args.h
1
args.h
|
@ -7,4 +7,3 @@
|
||||||
|
|
||||||
void args_parse(int argc, char **argv,
|
void args_parse(int argc, char **argv,
|
||||||
void flagfn(char, char*()), void plainfn(char*));
|
void flagfn(char, char*()), void plainfn(char*));
|
||||||
|
|
||||||
|
|
20
crc32.c
20
crc32.c
|
@ -8,11 +8,11 @@
|
||||||
typedef unsigned long ulong;
|
typedef unsigned long ulong;
|
||||||
#include "crc32.h"
|
#include "crc32.h"
|
||||||
|
|
||||||
ulong crc_reflect(ulong input)
|
ulong
|
||||||
|
crc_reflect(ulong input)
|
||||||
{
|
{
|
||||||
ulong reflected = 0;
|
ulong reflected = 0;
|
||||||
int i;
|
for (int i = 0; i < 4 * 8; i++) {
|
||||||
for (i = 0; i < 4 * 8; i++) {
|
|
||||||
reflected <<= 1;
|
reflected <<= 1;
|
||||||
reflected |= input & 1;
|
reflected |= input & 1;
|
||||||
input >>= 1;
|
input >>= 1;
|
||||||
|
@ -20,15 +20,15 @@ ulong crc_reflect(ulong input)
|
||||||
return reflected;
|
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 lsb = (big) ? 1 << 31 : 1; /* least significant bit */
|
||||||
ulong poly = (big) ? polynomial : crc_reflect(polynomial);
|
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;
|
*table = (big) ? c << 24 : c;
|
||||||
for (i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
if (*table & lsb) {
|
if (*table & lsb) {
|
||||||
*table = (big) ? *table << 1 : *table >> 1;
|
*table = (big) ? *table << 1 : *table >> 1;
|
||||||
*table ^= poly;
|
*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];
|
ulong byte = table[(((*remainder) >> 24) ^ c) & 0xff];
|
||||||
*remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF;
|
*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];
|
ulong byte = table[((*remainder) ^ c) & 0xFF];
|
||||||
*remainder = ((*remainder) >> 8) ^ byte;
|
*remainder = ((*remainder) >> 8) ^ byte;
|
||||||
|
|
76
main.c
76
main.c
|
@ -10,14 +10,8 @@
|
||||||
|
|
||||||
typedef unsigned long ulong;
|
typedef unsigned long ulong;
|
||||||
|
|
||||||
#include "crc32.h"
|
#include "crc32.c"
|
||||||
#include "args.h"
|
#include "args.c"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#define FREOPEN_BLANK ("")
|
|
||||||
#else
|
|
||||||
#define FREOPEN_BLANK (NULL)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef BUFFER_SIZE
|
#ifndef BUFFER_SIZE
|
||||||
#define BUFFER_SIZE 4096
|
#define BUFFER_SIZE 4096
|
||||||
|
@ -56,7 +50,8 @@ static const char help2[] = "\
|
||||||
numbers <n> may be entered as hexadecimal or octal with prefixes\n\
|
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) {
|
if (!next) {
|
||||||
fprintf(stderr, "-%c requires another argument\n", flag);
|
fprintf(stderr, "-%c requires another argument\n", flag);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -64,41 +59,43 @@ static char *check_next(char flag, char *next) {
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_flag(char flag, char *(*nextarg)())
|
static void
|
||||||
|
handle_flag(char flag, char *(*nextarg)())
|
||||||
{
|
{
|
||||||
char *next;
|
char *next;
|
||||||
switch (flag) {
|
switch (flag) {
|
||||||
case 'h':
|
case 'h': {
|
||||||
printf(help1);
|
printf(help1);
|
||||||
printf(help2);
|
printf(help2);
|
||||||
exit(0);
|
} exit(0);
|
||||||
case 'e':
|
case 'e': {
|
||||||
big_endian = 1;
|
big_endian = 1;
|
||||||
return;
|
} break;
|
||||||
case 'b':
|
case 'b': {
|
||||||
print_binary = 1;
|
print_binary = 1;
|
||||||
return;
|
} break;
|
||||||
case 'x':
|
case 'x': {
|
||||||
xor_output = 0;
|
xor_output = 0;
|
||||||
return;
|
} break;
|
||||||
case 'r':
|
case 'r': {
|
||||||
reflect_output = 1;
|
reflect_output = 1;
|
||||||
return;
|
} break;
|
||||||
case 's':
|
case 's': {
|
||||||
next = check_next(flag, nextarg());
|
next = check_next(flag, nextarg());
|
||||||
starting = strtoul(next, NULL, 0);
|
starting = strtoul(next, NULL, 0);
|
||||||
break;
|
} break;
|
||||||
case 'p':
|
case 'p': {
|
||||||
next = check_next(flag, nextarg());
|
next = check_next(flag, nextarg());
|
||||||
polynomial = strtoul(next, NULL, 0);
|
polynomial = strtoul(next, NULL, 0);
|
||||||
break;
|
} break;
|
||||||
default:
|
default: {
|
||||||
fprintf(stderr, "Unknown flag: -%c\n", flag);
|
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;
|
static string_node *last = NULL;
|
||||||
string_node *n = calloc(1, sizeof(string_node));
|
string_node *n = calloc(1, sizeof(string_node));
|
||||||
|
@ -115,7 +112,8 @@ static void add_input(char *arg)
|
||||||
last = n;
|
last = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static FILE *open_stream(char *filename)
|
static FILE *
|
||||||
|
open_stream(char *filename)
|
||||||
{
|
{
|
||||||
FILE *stream = NULL;
|
FILE *stream = NULL;
|
||||||
stream = fopen(filename, "rb");
|
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;
|
ulong remainder = starting;
|
||||||
void (*cycle)(ulong*, ulong*, char) =
|
void (*cycle)(ulong*, ulong*, char) =
|
||||||
(big_endian) ? crc_be_cycle : crc_le_cycle;
|
(big_endian) ? crc_be_cycle : crc_le_cycle;
|
||||||
ulong table[CRC_TABLE_SIZE];
|
ulong table[CRC_TABLE_SIZE];
|
||||||
int i, len;
|
|
||||||
|
|
||||||
crc_fill_table(table, big_endian, polynomial);
|
crc_fill_table(table, big_endian, polynomial);
|
||||||
do {
|
do {
|
||||||
len = fread(buff, 1, BUFFER_SIZE, stream);
|
int len = fread(buff, 1, BUFFER_SIZE, stream);
|
||||||
if (ferror(stream)) {
|
if (ferror(stream)) {
|
||||||
perror(NULL);
|
perror(NULL);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
cycle(table, &remainder, buff[i]);
|
cycle(table, &remainder, buff[i]);
|
||||||
} while (!feof(stream));
|
} while (!feof(stream));
|
||||||
|
|
||||||
|
@ -154,7 +152,8 @@ static ulong cycle_file(FILE *stream)
|
||||||
return remainder;
|
return remainder;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_crc(ulong remainder)
|
static void
|
||||||
|
print_crc(ulong remainder)
|
||||||
{
|
{
|
||||||
if (print_binary)
|
if (print_binary)
|
||||||
fwrite(&remainder, sizeof(remainder), 1, stdout);
|
fwrite(&remainder, sizeof(remainder), 1, stdout);
|
||||||
|
@ -162,7 +161,8 @@ static void print_crc(ulong remainder)
|
||||||
printf("%08X\n", (int) remainder);
|
printf("%08X\n", (int) remainder);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void free_nodes(string_node *n)
|
static void
|
||||||
|
free_nodes(string_node *n)
|
||||||
{
|
{
|
||||||
string_node *next;
|
string_node *next;
|
||||||
while (n) {
|
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);
|
args_parse(argc, argv, handle_flag, add_input);
|
||||||
|
|
||||||
if (!input_node) {
|
if (!input_node) {
|
||||||
freopen(FREOPEN_BLANK, "rb", stdin);
|
freopen(NULL, "rb", stdin);
|
||||||
print_crc(cycle_file(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);
|
FILE *stream = open_stream(n->s);
|
||||||
print_crc(cycle_file(stream));
|
print_crc(cycle_file(stream));
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
|
|
Loading…
Add table
Reference in a new issue