3 * $Id: crc-mktab.c,v 1.1 2000/07/21 19:01:33 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.1 2000/07/21 19:01:33 mdw
34 * Generate the CRC table rather than hardcoding it.
38 /*----- Header files ------------------------------------------------------*/
52 /*----- Static variables --------------------------------------------------*/
54 static unsigned long poly
= 0;
55 static const char *guard
= 0;
56 static unsigned bits
= 0;
57 static unsigned chunk
= 8;
58 static const char *file
= 0;
59 static unsigned flags
= 0;
60 static const char *sym
= 0;
61 static const char *type
= 0;
62 static const char *inc
= 0;
73 /*----- Main code ---------------------------------------------------------*/
75 static unsigned long getint(const char *p
, unsigned long max
,
79 unsigned long x
= strtoul(p
, &pp
, 0);
80 if (*pp
|| (max
&& x
> max
))
81 die(EXIT_FAILURE
, "bad %s `%s'", what
, p
);
85 static void version(FILE *fp
)
87 pquis(fp
, "$, mLib version " VERSION
"\n");
90 static void usage(FILE *fp
)
92 pquis(fp
, "Usage: $ [-cr] [-g guard] [-o file] [-b bits] [-p poly]\n");
95 static void help(FILE *fp
)
101 Emits a table containing precomuted values for CRC algorithms. A number\n\
102 of options are provided:\n\
104 -h, --help Show this help text.\n\
105 -v, --version Show the program's version number.\n\
106 -u, --usage Show a terse usage message.\n\
108 -c, --c-source Emit a C source file rather than a header.\n\
109 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
110 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
111 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
112 -r, --reverse Create a `reversed' CRC table.\n\
113 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
114 -s, --symbol=SYM Name the generated table SYM\n\
115 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
116 -o, --output=FILE Write the output to FILE.\n\
120 unsigned long reflect(unsigned long x
, unsigned b
)
123 unsigned long xm
, ym
;
125 if (!(flags
& f_reverse
))
129 for (i
= 0; i
< b
; i
++) {
138 int main(int argc
, char *argv
[])
147 static struct option opts
[] = {
148 { "help", 0, 0, 'h' },
149 { "version", 0, 0, 'v' },
150 { "usage", 0, 0, 'u' },
152 { "output", OPTF_ARGREQ
, 0, 'o' },
153 { "c-source", 0, 0, 'c' },
154 { "symbol", OPTF_ARGREQ
, 0, 's' },
155 { "type", OPTF_ARGREQ
, 0, 't' },
156 { "include", OPTF_ARGREQ
, 0, 'i' },
157 { "guard", OPTF_ARGREQ
, 0, 'g' },
159 { "bits", OPTF_ARGREQ
, 0, 'b' },
160 { "bit-chunk", OPTF_ARGREQ
, 0, 'B' },
161 { "polynomial", OPTF_ARGREQ
, 0, 'p' },
162 { "reverse", 0, 0, 'r' },
166 int i
= mdwopt(argc
, argv
, "hvu o:cs:t:i:g: b:B:p:r", opts
, 0, 0, 0);
201 bits
= getint(optarg
, 32, "CRC width");
204 chunk
= getint(optarg
, 32, "chunk size");
207 poly
= getint(optarg
, 0xffffffff, "CRC poly");
218 if ((flags
& f_bogus
) || optind
!= argc
) {
223 /* --- Sort out the various parameters --- */
228 if (flags
& f_reverse
)
240 die(EXIT_FAILURE
, "no standard polynomials for %u bits", bits
);
246 unsigned long x
= poly
;
254 if ((flags
& f_ctab
) && !type
)
255 type
= bits
> 16 ?
"unsigned long" : "unsigned short";
257 /* --- Start output --- */
262 if (!(flags
& f_ctab
) && !guard
) {
265 if ((p
= malloc(strlen(file
) + 1)) == 0)
266 die(EXIT_FAILURE
, "not enough memory");
268 for (q
= file
; *q
; p
++, q
++) {
269 if (isalnum((unsigned char)*q
))
276 if ((fp
= fopen(file
, "w")) == 0)
277 die(EXIT_FAILURE
, "couldn't write `%s': %s", file
, strerror(errno
));
281 sym
= (flags
& f_ctab
) ?
"crctab" : "CRC_TAB";
283 /* --- Dump out the first chunk of the file --- */
288 * CRC table (poly = %0*lx%s) [generated]\n\
291 nw
, poly
, flags
& f_reverse ?
"; reversed" : "");
293 if (flags
& f_ctab
) {
295 fprintf(fp
, "#include \"%s\"\n\n", inc
);
296 fprintf(fp
, "%s %s[] = {\n", type
, sym
);
300 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
301 n
= fprintf(fp
, "#define %s {", sym
);
309 /* --- Sort out the line width --- */
312 while (2 + n
* (nw
+ 4) < BSCOL
)
316 while (((1 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
319 /* --- Start grinding --- */
322 mask
= 0xffffffff >> (32 - bits
);
324 for (i
= 0; i
< max
; i
++) {
328 x
= reflect(i
, chunk
) << (bits
- chunk
);
329 for (j
= 0; j
< chunk
; j
++)
330 x
= ((x
<< 1) ^ (x
& (1 << (bits
- 1)) ? poly
: 0)) & mask
;
331 x
= reflect(x
, bits
);
333 fprintf(fp
, " 0x%0*lx", nw
, x
);
335 if (!(flags
& f_ctab
) && ((2 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
339 if (i
+ 1 == max
|| (i
+ 1)%n
== 0) {
340 if (!(flags
& f_ctab
)) {
341 for (j
= 0; j
< t
; j
++)
353 fputs(flags
& f_ctab ?
"};\n" : "}\n", fp
);
354 if (!(flags
& f_ctab
) && guard
)
355 fputs("\n#endif\n", fp
);
360 /*----- That's all, folks -------------------------------------------------*/