crc32/main.c

144 lines
2.9 KiB
C
Raw Normal View History

2012-08-04 05:23:29 -07:00
/* 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.
*/
2012-04-13 02:43:39 -07:00
#include <stdio.h>
#include <stdlib.h>
2012-08-04 07:03:08 -07:00
#include <string.h>
2012-04-13 02:43:39 -07:00
#include "crc32.h"
#include "args.h"
#ifdef _MSC_VER
#define FREOPEN_BLANK ("")
#else
#define FREOPEN_BLANK (NULL)
#endif
static ulong remainder = 0xFFFFFFFF;
2012-04-13 02:43:39 -07:00
2012-08-04 07:24:04 -07:00
static FILE *input_stream = NULL;
static char *input_filename = "-";
2012-04-13 02:43:39 -07:00
static char print_binary = 0;
static char xor_output = 1;
static char reflect_output = 0;
2012-08-04 07:52:09 -07:00
static const char help1[] = "\
2012-08-04 07:03:08 -07:00
crc32 - a 32-bit cyclic rendundancy check calculator\n\
\n\
-h display this text\n\
-i <file> open file for reading (default: stdin)\n\
-s <n> start cycle with n (default: 0xFFFFFFFF)\n\
-p <n> use n as the crc divisor (default: 0x04C11DB7)\n\
-e use big endian calculations\n\
-b output as binary\n\
2012-08-04 07:52:09 -07:00
-x NOT the output\n\
-r reverse output's bits\n\
2012-08-04 07:03:08 -07:00
\n\
";
2012-08-04 07:52:09 -07:00
static const char help2[] = "\
2012-08-04 07:03:08 -07:00
numbers <n> may be entered as hexadecimal or octal with prefixes\n\
";
2012-08-04 07:52:09 -07:00
static void handle_flag(char flag, char *(*nextarg)()) {
char *next;
2012-08-04 07:03:08 -07:00
switch (flag) {
case 'h':
printf(help1);
printf(help2);
exit(0);
case 'e':
crc_set_big_endian();
2012-08-04 07:52:09 -07:00
return;
2012-08-04 07:03:08 -07:00
case 'b':
print_binary = 1;
2012-08-04 07:52:09 -07:00
return;
2012-08-04 07:03:08 -07:00
case 'x':
xor_output = 0;
2012-08-04 07:52:09 -07:00
return;
2012-08-04 07:03:08 -07:00
case 'r':
reflect_output = 1;
2012-08-04 07:52:09 -07:00
return;
}
if (!(next = nextarg())) {
fprintf(stderr, "-%c requires another argument\n", flag);
exit(1);
}
switch (flag) {
case 's':
remainder = strtoul(next, NULL, 0);
break;
case 'i':
input_filename = next;
break;
case 'p':
crc_set_polynomial(strtoul(next, NULL, 0));
2012-08-04 07:03:08 -07:00
break;
default:
2012-08-04 07:52:09 -07:00
fprintf(stderr, "Unknown flag: -%c\n", flag);
2012-08-04 07:03:08 -07:00
exit(1);
}
2012-04-13 02:43:39 -07:00
}
2012-08-04 07:52:09 -07:00
static void complain(char *arg)
{
fprintf(stderr, "Unhandled argument: %s\n", arg);
exit(1);
}
2012-04-13 02:43:39 -07:00
static void open_stream()
{
const char mode[] = "rb";
2012-04-13 12:54:53 -07:00
if (!strcmp(input_filename, "-")) {
2012-04-13 02:43:39 -07:00
freopen(FREOPEN_BLANK, mode, stdin);
input_stream = stdin;
} else {
2012-04-13 12:54:53 -07:00
input_stream = fopen(input_filename, mode);
2012-04-13 02:43:39 -07:00
}
if (input_stream == NULL) {
fprintf(stderr, "Could not open file for reading: %s\n",
2012-08-04 07:24:04 -07:00
input_filename);
2012-04-13 02:43:39 -07:00
exit(1);
}
}
static void close_stream()
{
fclose(input_stream);
}
static void cycle_input()
{
int c;
open_stream();
2012-04-15 00:18:44 -07:00
while ((c = getc(input_stream)) != EOF)
2012-04-13 02:43:39 -07:00
crc_cycle(&remainder, c);
close_stream();
if (xor_output)
remainder ^= 0xFFFFFFFF;
if (reflect_output)
remainder = crc_reflect(remainder);
}
static void print_crc()
{
if (print_binary)
fwrite(&remainder, sizeof(remainder), 1, stdout);
else
printf("%08X\n", (int) remainder);
}
2012-08-04 07:24:04 -07:00
int main(int argc, char **argv)
2012-04-13 02:43:39 -07:00
{
crc_set_little_endian();
2012-08-04 07:52:09 -07:00
args_parse(argc, argv, handle_flag, complain);
2012-04-13 02:43:39 -07:00
cycle_input();
print_crc();
return 0;
}