3 * $Id: crc-mktab.c,v 1.3 2001/01/20 12:06:01 mdw Exp $
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: crc-mktab.c,v $
33 * Revision 1.3 2001/01/20 12:06:01 mdw
34 * Define flags with macros, to ensure unsignedness.
36 * Revision 1.2 2000/10/08 11:07:21 mdw
37 * With no arguments, give an error rather than spewing a big table at the
40 * Revision 1.1 2000/07/21 19:01:33 mdw
41 * Generate the CRC table rather than hardcoding it.
45 /*----- Header files ------------------------------------------------------*/
59 /*----- Static variables --------------------------------------------------*/
61 static unsigned long poly = 0;
62 static const char *guard = 0;
63 static unsigned bits = 0;
64 static unsigned chunk = 8;
65 static const char *file = 0;
66 static unsigned flags = 0;
67 static const char *sym = 0;
68 static const char *type = 0;
69 static const char *inc = 0;
78 /*----- Main code ---------------------------------------------------------*/
80 static unsigned long getint(const char *p, unsigned long max,
84 unsigned long x = strtoul(p, &pp, 0);
85 if (*pp || (max && x > max))
86 die(EXIT_FAILURE, "bad %s `%s'", what, p);
90 static void version(FILE *fp)
92 pquis(fp, "$, mLib version " VERSION "\n");
95 static void usage(FILE *fp)
97 pquis(fp, "Usage: $ [-cr] [-g guard] [-o file] [-b bits] [-p poly]\n");
100 static void help(FILE *fp)
106 Emits a table containing precomuted values for CRC algorithms. A number\n\
107 of options are provided:\n\
109 -h, --help Show this help text.\n\
110 -v, --version Show the program's version number.\n\
111 -u, --usage Show a terse usage message.\n\
113 -c, --c-source Emit a C source file rather than a header.\n\
114 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
115 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
116 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
117 -r, --reverse Create a `reversed' CRC table.\n\
118 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
119 -s, --symbol=SYM Name the generated table SYM\n\
120 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
121 -o, --output=FILE Write the output to FILE.\n\
125 unsigned long reflect(unsigned long x, unsigned b)
128 unsigned long xm, ym;
130 if (!(flags & f_reverse))
134 for (i = 0; i < b; i++) {
143 int main(int argc, char *argv[])
152 static struct option opts[] = {
153 { "help", 0, 0, 'h' },
154 { "version", 0, 0, 'v' },
155 { "usage", 0, 0, 'u' },
157 { "output", OPTF_ARGREQ, 0, 'o' },
158 { "c-source", 0, 0, 'c' },
159 { "symbol", OPTF_ARGREQ, 0, 's' },
160 { "type", OPTF_ARGREQ, 0, 't' },
161 { "include", OPTF_ARGREQ, 0, 'i' },
162 { "guard", OPTF_ARGREQ, 0, 'g' },
164 { "bits", OPTF_ARGREQ, 0, 'b' },
165 { "bit-chunk", OPTF_ARGREQ, 0, 'B' },
166 { "polynomial", OPTF_ARGREQ, 0, 'p' },
167 { "reverse", 0, 0, 'r' },
171 int i = mdwopt(argc, argv, "hvu o:cs:t:i:g: b:B:p:r", opts, 0, 0, 0);
206 bits = getint(optarg, 32, "CRC width");
209 chunk = getint(optarg, 32, "chunk size");
212 poly = getint(optarg, 0xffffffff, "CRC poly");
223 if ((flags & f_bogus) || optind != argc) {
228 /* --- Sort out the various parameters --- */
233 if (flags & f_reverse)
244 die(EXIT_FAILURE, "no polynomial or bit width set");
247 die(EXIT_FAILURE, "no standard polynomials for %u bits", bits);
253 unsigned long x = poly;
261 if ((flags & f_ctab) && !type)
262 type = bits > 16 ? "unsigned long" : "unsigned short";
264 /* --- Start output --- */
269 if (!(flags & f_ctab) && !guard) {
272 if ((p = malloc(strlen(file) + 1)) == 0)
273 die(EXIT_FAILURE, "not enough memory");
275 for (q = file; *q; p++, q++) {
276 if (isalnum((unsigned char)*q))
283 if ((fp = fopen(file, "w")) == 0)
284 die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno));
288 sym = (flags & f_ctab) ? "crctab" : "CRC_TAB";
290 /* --- Dump out the first chunk of the file --- */
295 * CRC table (poly = %0*lx%s) [generated]\n\
298 nw, poly, flags & f_reverse ? "; reversed" : "");
300 if (flags & f_ctab) {
302 fprintf(fp, "#include \"%s\"\n\n", inc);
303 fprintf(fp, "%s %s[] = {\n", type, sym);
307 fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard);
308 n = fprintf(fp, "#define %s {", sym);
316 /* --- Sort out the line width --- */
319 while (2 + n * (nw + 4) < BSCOL)
323 while (((1 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
326 /* --- Start grinding --- */
329 mask = 0xffffffff >> (32 - bits);
331 for (i = 0; i < max; i++) {
335 x = reflect(i, chunk) << (bits - chunk);
336 for (j = 0; j < chunk; j++)
337 x = ((x << 1) ^ (x & (1 << (bits - 1)) ? poly : 0)) & mask;
338 x = reflect(x, bits);
340 fprintf(fp, " 0x%0*lx", nw, x);
342 if (!(flags & f_ctab) && ((2 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
346 if (i + 1 == max || (i + 1)%n == 0) {
347 if (!(flags & f_ctab)) {
348 for (j = 0; j < t; j++)
360 fputs(flags & f_ctab ? "};\n" : "}\n", fp);
361 if (!(flags & f_ctab) && guard)
362 fputs("\n#endif\n", fp);
367 /*----- That's all, folks -------------------------------------------------*/