3 * $Id: crc-mktab.c,v 1.5 2003/12/15 20:53:47 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.5 2003/12/15 20:53:47 mdw
34 * Add global unihash table; use universal hashing instead of CRC.
36 * Revision 1.4 2001/03/10 10:59:21 mdw
37 * Fix generating tables where the chunk size is longer than the
38 * polynomial. Also fix output formatting when there aren't enough entries
41 * Revision 1.3 2001/01/20 12:06:01 mdw
42 * Define flags with macros, to ensure unsignedness.
44 * Revision 1.2 2000/10/08 11:07:21 mdw
45 * With no arguments, give an error rather than spewing a big table at the
48 * Revision 1.1 2000/07/21 19:01:33 mdw
49 * Generate the CRC table rather than hardcoding it.
53 /*----- Header files ------------------------------------------------------*/
67 /*----- Static variables --------------------------------------------------*/
69 static unsigned long poly = 0;
70 static const char *guard = 0;
71 static unsigned bits = 0;
72 static unsigned chunk = 8;
73 static const char *file = 0;
74 static unsigned flags = 0;
75 static const char *sym = 0;
76 static const char *type = 0;
77 static const char *inc = 0;
86 /*----- Main code ---------------------------------------------------------*/
88 static unsigned long getint(const char *p, unsigned long max,
92 unsigned long x = strtoul(p, &pp, 0);
93 if (*pp || (max && x > max))
94 die(EXIT_FAILURE, "bad %s `%s'", what, p);
98 static void version(FILE *fp)
100 pquis(fp, "$, mLib version " VERSION "\n");
103 static void usage(FILE *fp)
105 pquis(fp, "Usage: $ [-cr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\
106 [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n");
109 static void help(FILE *fp)
115 Emits a table containing precomuted values for CRC algorithms. A number\n\
116 of options are provided:\n\
118 -h, --help Show this help text.\n\
119 -v, --version Show the program's version number.\n\
120 -u, --usage Show a terse usage message.\n\
122 -c, --c-source Emit a C source file rather than a header.\n\
123 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
124 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
125 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
126 -r, --reverse Create a `reversed' CRC table.\n\
127 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
128 -i, --include=HEADER Include HEADER at top of C source file.\n\
129 -s, --symbol=SYM Name the generated table SYM.\n\
130 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
131 -o, --output=FILE Write the output to FILE.\n\
135 unsigned long reflect(unsigned long x, unsigned b)
138 unsigned long xm, ym;
140 if (!(flags & f_reverse))
144 for (i = 0; i < b; i++) {
153 int main(int argc, char *argv[])
162 static struct option opts[] = {
163 { "help", 0, 0, 'h' },
164 { "version", 0, 0, 'v' },
165 { "usage", 0, 0, 'u' },
167 { "output", OPTF_ARGREQ, 0, 'o' },
168 { "c-source", 0, 0, 'c' },
169 { "symbol", OPTF_ARGREQ, 0, 's' },
170 { "type", OPTF_ARGREQ, 0, 't' },
171 { "include", OPTF_ARGREQ, 0, 'i' },
172 { "guard", OPTF_ARGREQ, 0, 'g' },
174 { "bits", OPTF_ARGREQ, 0, 'b' },
175 { "bit-chunk", OPTF_ARGREQ, 0, 'B' },
176 { "polynomial", OPTF_ARGREQ, 0, 'p' },
177 { "reverse", 0, 0, 'r' },
181 int i = mdwopt(argc, argv, "hvu o:cs:t:i:g: b:B:p:r", opts, 0, 0, 0);
216 bits = getint(optarg, 32, "CRC width");
219 chunk = getint(optarg, 32, "chunk size");
222 poly = getint(optarg, 0xffffffff, "CRC poly");
233 if ((flags & f_bogus) || optind != argc) {
238 /* --- Sort out the various parameters --- */
243 if (flags & f_reverse)
254 die(EXIT_FAILURE, "no polynomial or bit width set");
257 die(EXIT_FAILURE, "no standard polynomials for %u bits", bits);
263 unsigned long x = poly;
271 if ((flags & f_ctab) && !type)
272 type = bits > 16 ? "unsigned long" : "unsigned short";
274 /* --- Start output --- */
279 if (!(flags & f_ctab) && !guard) {
282 if ((p = malloc(strlen(file) + 1)) == 0)
283 die(EXIT_FAILURE, "not enough memory");
285 for (q = file; *q; p++, q++) {
286 if (isalnum((unsigned char)*q))
287 *p = toupper((unsigned char)*q);
293 if ((fp = fopen(file, "w")) == 0)
294 die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno));
298 sym = (flags & f_ctab) ? "crctab" : "CRC_TAB";
300 /* --- Dump out the first chunk of the file --- */
305 * CRC table (poly = %0*lx%s) [generated]\n\
308 nw, poly, flags & f_reverse ? "; reversed" : "");
310 if (flags & f_ctab) {
312 fprintf(fp, "#include \"%s\"\n\n", inc);
313 fprintf(fp, "%s %s[] = {\n", type, sym);
317 fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard);
318 n = fprintf(fp, "#define %s {", sym);
326 /* --- Sort out the line width --- */
329 while (2 + n * (nw + 4) < BSCOL)
336 while (((1 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
339 /* --- Start grinding --- */
341 mask = 0xffffffff >> (32 - bits);
343 for (i = 0; i < max; i++) {
344 unsigned long x, y, z;
348 x = reflect(i, chunk);
355 z = (x >> (nn - ni)) & (0xffffffff >> (32 - ni));
356 y ^= z << (bits - ni);
357 for (j = 0; j < ni; j++)
358 y = ((y << 1) ^ (y & (1 << (bits - 1)) ? poly : 0)) & mask;
361 x = reflect(y, bits);
363 fprintf(fp, " 0x%0*lx", nw, x);
365 if (!(flags & f_ctab) && ((2 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
369 if (i + 1 == max || (i + 1)%n == 0) {
370 if (!(flags & f_ctab)) {
371 for (j = 0; j < t; j++)
383 fputs(flags & f_ctab ? "};\n" : "}\n", fp);
384 if (!(flags & f_ctab) && guard)
385 fputs("\n#endif\n", fp);
390 /*----- That's all, folks -------------------------------------------------*/