From da30e938b5cc9244a5c6f273627b99025592db12 Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Mon, 5 Jan 2009 09:57:37 -0600 Subject: [PATCH] Initial import --- Makefile | 29 +++++++++++++++++++++ crc32.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ crc32.h | 20 +++++++++++++++ main.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 Makefile create mode 100644 crc32.c create mode 100644 crc32.h create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b6e48b9 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +default: depend all + +crc32: crc32.o main.o + $(CC) $(CFLAGS) crc32.o main.o -o crc32 + +crc32.o: crc32.c crc32.h + $(CC) $(CFLAGS) -c crc32.c + +main.o: main.c + $(CC) $(CFLAGS) -c main.c + +depend: .depend + +.depend: *.c *.h + $(CC) -E -MM *.c > .depend + +all: crc32 + +test: crc32 + ./crc32 crc32.h crc32.c main.c + cat crc32.h | ./crc32 - crc32.h + cat crc32.c | ./crc32 crc32.c - + cat main.c | ./crc32 - main.c + ./crc32 -x main.c + ./crc32 -X main.c + ./crc32 -d main.c + +clean: + - rm -f crc32 *.o .depend diff --git a/crc32.c b/crc32.c new file mode 100644 index 0000000..b761418 --- /dev/null +++ b/crc32.c @@ -0,0 +1,74 @@ +/* + * efone - Distributed internet phone system. + * + * (c) 1999,2000 Krzysztof Dabrowski + * (c) 1999,2000 ElysiuM deeZine + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Hacked by CyberLeo to make it capable of digesting long streams + * one buffer at a time. - 20090105 + */ + +/* based on implementation by Finn Yannick Jacobs */ + +#include +#include +#include + +/* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab(). + * so make sure, you call it before using the other + * functions! + */ +uint32_t chksum_crc_tab[256]; + +/* chksum_crc() -- to a given block, this one calculates the + * crc32-checksum until the length is + * reached. the crc32-checksum will be + * the result. + */ +uint32_t chksum_crc32 (unsigned char *block, unsigned int length, uint32_t *chksum_crc) +{ + register unsigned long crc; + unsigned long i; + + crc = *chksum_crc ^ 0xFFFFFFFF; + for (i = 0; i < length; i++) + { + crc = ((crc >> 8) & 0x00FFFFFF) ^ chksum_crc_tab[(crc ^ *block++) & 0xFF]; + } + *chksum_crc = crc ^ 0xFFFFFFFF; + return *chksum_crc; +} + +/* chksum_crc32_gentab() -- to a global crc_tab[256], this one will + * calculate the crcTable for crc32-checksums. + * it is generated to the polynom [..] + */ + +void chksum_crc32_gentab () +{ + unsigned long crc, poly; + int i, j; + + poly = 0xEDB88320L; + for (i = 0; i < 256; i++) + { + crc = i; + for (j = 8; j > 0; j--) + { + if (crc & 1) + { + crc = (crc >> 1) ^ poly; + } + else + { + crc >>= 1; + } + } + chksum_crc_tab[i] = crc; + } +} diff --git a/crc32.h b/crc32.h new file mode 100644 index 0000000..4b2913e --- /dev/null +++ b/crc32.h @@ -0,0 +1,20 @@ +/* + * efone - Distributed internet phone system. + * + * (c) 1999,2000 Krzysztof Dabrowski + * (c) 1999,2000 ElysiuM deeZine + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + */ + +/* based on implementation by Finn Yannick Jacobs. */ + +#include + +void chksum_crc32_gentab (); +uint32_t chksum_crc32 (unsigned char *block, unsigned int length, uint32_t *chksum_crc); +extern uint32_t chksum_crc_tab[256]; diff --git a/main.c b/main.c new file mode 100644 index 0000000..1b4d7b5 --- /dev/null +++ b/main.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include "crc32.h" + +void help() { + printf("-x -X -d -h\n"); + exit(EXIT_FAILURE); +} + +int main (int argc, char *argv[]) { + int c; + FILE * fp; + uint32_t crc; + unsigned char *fmt = "%08x\t%s\n"; + int prog_return = EXIT_SUCCESS; + + int blksz = 4096; + unsigned char bufr[blksz]; + + while (( c = getopt(argc, argv, "hXxd")) != -1) { + switch (c) { + case 'x': + fmt = "%08x\t%s\n"; + break; + case 'X': + fmt = "%08X\t%s\n"; + break; + case 'd': + fmt = "%d\t%s\n"; + break; + case 'h': + case '?': + help(); + break; + } + } + + chksum_crc32_gentab(); + + if (optind == argc) { + crc = 0; + while (!feof(stdin)) { + if ((c = fread(bufr, 1, blksz, stdin)) > 0) { + chksum_crc32(bufr, c, &crc); + } + } + printf(fmt, crc, "-"); + } else { + for ( ; optind < argc; optind++) { + if (0 == strcmp("-", argv[optind])) { + crc = 0; + while (!feof(stdin)) { + if ((c = fread(bufr, 1, blksz, stdin)) > 0) { + chksum_crc32(bufr, c, &crc); + } + } + printf(fmt, crc, "-"); + } else if (access(argv[optind], R_OK) == 0) { + crc = 0; + if (fp = fopen(argv[optind], "r")) { + while (!feof(fp)) + if ((c = fread(bufr, 1, blksz, fp)) > 0) + chksum_crc32(bufr, c, &crc); + } else { + strerror(); + } + printf(fmt, crc, argv[optind]); + } else { + fprintf(stderr, "NG: %s\n", argv[optind]); + prog_return++; + } + } + } + exit(prog_return); +} -- 2.42.0