X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/b7580524f33ee21d3ddc49fe36a71e107e87df1b..ff1e93acf2626d2531a97de3b54bce9b5a37bcb5:/crc-mktab.c diff --git a/crc-mktab.c b/crc-mktab.c index 887315b..200ed32 100644 --- a/crc-mktab.c +++ b/crc-mktab.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: crc-mktab.c,v 1.1 2000/07/21 19:01:33 mdw Exp $ + * $Id: crc-mktab.c,v 1.6 2004/04/08 01:36:11 mdw Exp $ * * Build CRC tables * * (c) 2000 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,28 +15,22 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with mLib; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: crc-mktab.c,v $ - * Revision 1.1 2000/07/21 19:01:33 mdw - * Generate the CRC table rather than hardcoding it. - * - */ - /*----- Header files ------------------------------------------------------*/ +#include "config.h" + #include #include #include @@ -62,11 +56,9 @@ static const char *type = 0; static const char *inc = 0; static FILE *fp; -enum { - f_bogus = 1, - f_ctab = 2, - f_reverse = 4 -}; +#define f_bogus 1u +#define f_ctab 2u +#define f_reverse 4u #define BSCOL 72 @@ -89,7 +81,8 @@ static void version(FILE *fp) static void usage(FILE *fp) { - pquis(fp, "Usage: $ [-cr] [-g guard] [-o file] [-b bits] [-p poly]\n"); + pquis(fp, "Usage: $ [-cr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\ + [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n"); } static void help(FILE *fp) @@ -111,7 +104,8 @@ of options are provided:\n\ -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\ -r, --reverse Create a `reversed' CRC table.\n\ -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\ --s, --symbol=SYM Name the generated table SYM\n\ +-i, --include=HEADER Include HEADER at top of C source file.\n\ +-s, --symbol=SYM Name the generated table SYM.\n\ -t, --type=TYPE Give the table entries type TYPE in C source.\n\ -o, --output=FILE Write the output to FILE.\n\ ", stdout); @@ -231,11 +225,13 @@ int main(int argc, char *argv[]) poly = 0x1021; break; case 32: - case 0: poly = 0x04c11db7; flags |= f_reverse; bits = 32; break; + case 0: + die(EXIT_FAILURE, "no polynomial or bit width set"); + break; default: die(EXIT_FAILURE, "no standard polynomials for %u bits", bits); break; @@ -249,7 +245,7 @@ int main(int argc, char *argv[]) bits += 8; } } - nw = bits/4; + nw = (bits + 3)/4; if ((flags & f_ctab) && !type) type = bits > 16 ? "unsigned long" : "unsigned short"; @@ -267,7 +263,7 @@ int main(int argc, char *argv[]) guard = p; for (q = file; *q; p++, q++) { if (isalnum((unsigned char)*q)) - *p = *q; + *p = toupper((unsigned char)*q); else *p = '_'; } @@ -312,23 +308,36 @@ int main(int argc, char *argv[]) while (2 + n * (nw + 4) < BSCOL) n <<= 1; n >>= 1; + max = 1 << chunk; + if (n > max) + n = max; t = 0; while (((1 + n * (nw + 4)) & ~7) + 8 * t < BSCOL) t++; /* --- Start grinding --- */ - max = 1 << chunk; mask = 0xffffffff >> (32 - bits); fputc(' ', fp); for (i = 0; i < max; i++) { - unsigned long x; + unsigned long x, y, z; unsigned j; - - x = reflect(i, chunk) << (bits - chunk); - for (j = 0; j < chunk; j++) - x = ((x << 1) ^ (x & (1 << (bits - 1)) ? poly : 0)) & mask; - x = reflect(x, bits); + unsigned ni, nn; + + x = reflect(i, chunk); + y = 0; + nn = chunk; + while (nn) { + ni = bits; + if (ni > nn) + ni = nn; + z = (x >> (nn - ni)) & (0xffffffff >> (32 - ni)); + y ^= z << (bits - ni); + for (j = 0; j < ni; j++) + y = ((y << 1) ^ (y & (1 << (bits - 1)) ? poly : 0)) & mask; + nn -= ni; + } + x = reflect(y, bits); fprintf(fp, " 0x%0*lx", nw, x); if (i == max - 1) {