3 * $Id: crc-mktab.c,v 1.2 2000/10/08 11:07:21 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.2 2000/10/08 11:07:21 mdw
34 * With no arguments, give an error rather than spewing a big table at the
37 * Revision 1.1 2000/07/21 19:01:33 mdw
38 * Generate the CRC table rather than hardcoding it.
42 /*----- Header files ------------------------------------------------------*/
56 /*----- Static variables --------------------------------------------------*/
58 static unsigned long poly
= 0;
59 static const char *guard
= 0;
60 static unsigned bits
= 0;
61 static unsigned chunk
= 8;
62 static const char *file
= 0;
63 static unsigned flags
= 0;
64 static const char *sym
= 0;
65 static const char *type
= 0;
66 static const char *inc
= 0;
77 /*----- Main code ---------------------------------------------------------*/
79 static unsigned long getint(const char *p
, unsigned long max
,
83 unsigned long x
= strtoul(p
, &pp
, 0);
84 if (*pp
|| (max
&& x
> max
))
85 die(EXIT_FAILURE
, "bad %s `%s'", what
, p
);
89 static void version(FILE *fp
)
91 pquis(fp
, "$, mLib version " VERSION
"\n");
94 static void usage(FILE *fp
)
96 pquis(fp
, "Usage: $ [-cr] [-g guard] [-o file] [-b bits] [-p poly]\n");
99 static void help(FILE *fp
)
105 Emits a table containing precomuted values for CRC algorithms. A number\n\
106 of options are provided:\n\
108 -h, --help Show this help text.\n\
109 -v, --version Show the program's version number.\n\
110 -u, --usage Show a terse usage message.\n\
112 -c, --c-source Emit a C source file rather than a header.\n\
113 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
114 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
115 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
116 -r, --reverse Create a `reversed' CRC table.\n\
117 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
118 -s, --symbol=SYM Name the generated table SYM\n\
119 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
120 -o, --output=FILE Write the output to FILE.\n\
124 unsigned long reflect(unsigned long x
, unsigned b
)
127 unsigned long xm
, ym
;
129 if (!(flags
& f_reverse
))
133 for (i
= 0; i
< b
; i
++) {
142 int main(int argc
, char *argv
[])
151 static struct option opts
[] = {
152 { "help", 0, 0, 'h' },
153 { "version", 0, 0, 'v' },
154 { "usage", 0, 0, 'u' },
156 { "output", OPTF_ARGREQ
, 0, 'o' },
157 { "c-source", 0, 0, 'c' },
158 { "symbol", OPTF_ARGREQ
, 0, 's' },
159 { "type", OPTF_ARGREQ
, 0, 't' },
160 { "include", OPTF_ARGREQ
, 0, 'i' },
161 { "guard", OPTF_ARGREQ
, 0, 'g' },
163 { "bits", OPTF_ARGREQ
, 0, 'b' },
164 { "bit-chunk", OPTF_ARGREQ
, 0, 'B' },
165 { "polynomial", OPTF_ARGREQ
, 0, 'p' },
166 { "reverse", 0, 0, 'r' },
170 int i
= mdwopt(argc
, argv
, "hvu o:cs:t:i:g: b:B:p:r", opts
, 0, 0, 0);
205 bits
= getint(optarg
, 32, "CRC width");
208 chunk
= getint(optarg
, 32, "chunk size");
211 poly
= getint(optarg
, 0xffffffff, "CRC poly");
222 if ((flags
& f_bogus
) || optind
!= argc
) {
227 /* --- Sort out the various parameters --- */
232 if (flags
& f_reverse
)
243 die(EXIT_FAILURE
, "no polynomial or bit width set");
246 die(EXIT_FAILURE
, "no standard polynomials for %u bits", bits
);
252 unsigned long x
= poly
;
260 if ((flags
& f_ctab
) && !type
)
261 type
= bits
> 16 ?
"unsigned long" : "unsigned short";
263 /* --- Start output --- */
268 if (!(flags
& f_ctab
) && !guard
) {
271 if ((p
= malloc(strlen(file
) + 1)) == 0)
272 die(EXIT_FAILURE
, "not enough memory");
274 for (q
= file
; *q
; p
++, q
++) {
275 if (isalnum((unsigned char)*q
))
282 if ((fp
= fopen(file
, "w")) == 0)
283 die(EXIT_FAILURE
, "couldn't write `%s': %s", file
, strerror(errno
));
287 sym
= (flags
& f_ctab
) ?
"crctab" : "CRC_TAB";
289 /* --- Dump out the first chunk of the file --- */
294 * CRC table (poly = %0*lx%s) [generated]\n\
297 nw
, poly
, flags
& f_reverse ?
"; reversed" : "");
299 if (flags
& f_ctab
) {
301 fprintf(fp
, "#include \"%s\"\n\n", inc
);
302 fprintf(fp
, "%s %s[] = {\n", type
, sym
);
306 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
307 n
= fprintf(fp
, "#define %s {", sym
);
315 /* --- Sort out the line width --- */
318 while (2 + n
* (nw
+ 4) < BSCOL
)
322 while (((1 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
325 /* --- Start grinding --- */
328 mask
= 0xffffffff >> (32 - bits
);
330 for (i
= 0; i
< max
; i
++) {
334 x
= reflect(i
, chunk
) << (bits
- chunk
);
335 for (j
= 0; j
< chunk
; j
++)
336 x
= ((x
<< 1) ^ (x
& (1 << (bits
- 1)) ? poly
: 0)) & mask
;
337 x
= reflect(x
, bits
);
339 fprintf(fp
, " 0x%0*lx", nw
, x
);
341 if (!(flags
& f_ctab
) && ((2 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
345 if (i
+ 1 == max
|| (i
+ 1)%n
== 0) {
346 if (!(flags
& f_ctab
)) {
347 for (j
= 0; j
< t
; j
++)
359 fputs(flags
& f_ctab ?
"};\n" : "}\n", fp
);
360 if (!(flags
& f_ctab
) && guard
)
361 fputs("\n#endif\n", fp
);
366 /*----- That's all, folks -------------------------------------------------*/