3 * $Id: crc-mktab.c,v 1.4 2001/03/10 10:59: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.4 2001/03/10 10:59:21 mdw
34 * Fix generating tables where the chunk size is longer than the
35 * polynomial. Also fix output formatting when there aren't enough entries
38 * Revision 1.3 2001/01/20 12:06:01 mdw
39 * Define flags with macros, to ensure unsignedness.
41 * Revision 1.2 2000/10/08 11:07:21 mdw
42 * With no arguments, give an error rather than spewing a big table at the
45 * Revision 1.1 2000/07/21 19:01:33 mdw
46 * Generate the CRC table rather than hardcoding it.
50 /*----- Header files ------------------------------------------------------*/
64 /*----- Static variables --------------------------------------------------*/
66 static unsigned long poly
= 0;
67 static const char *guard
= 0;
68 static unsigned bits
= 0;
69 static unsigned chunk
= 8;
70 static const char *file
= 0;
71 static unsigned flags
= 0;
72 static const char *sym
= 0;
73 static const char *type
= 0;
74 static const char *inc
= 0;
83 /*----- Main code ---------------------------------------------------------*/
85 static unsigned long getint(const char *p
, unsigned long max
,
89 unsigned long x
= strtoul(p
, &pp
, 0);
90 if (*pp
|| (max
&& x
> max
))
91 die(EXIT_FAILURE
, "bad %s `%s'", what
, p
);
95 static void version(FILE *fp
)
97 pquis(fp
, "$, mLib version " VERSION
"\n");
100 static void usage(FILE *fp
)
102 pquis(fp
, "Usage: $ [-cr] [-g guard] [-o file] [-b bits] [-p poly]\n");
105 static void help(FILE *fp
)
111 Emits a table containing precomuted values for CRC algorithms. A number\n\
112 of options are provided:\n\
114 -h, --help Show this help text.\n\
115 -v, --version Show the program's version number.\n\
116 -u, --usage Show a terse usage message.\n\
118 -c, --c-source Emit a C source file rather than a header.\n\
119 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
120 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
121 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
122 -r, --reverse Create a `reversed' CRC table.\n\
123 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
124 -s, --symbol=SYM Name the generated table SYM\n\
125 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
126 -o, --output=FILE Write the output to FILE.\n\
130 unsigned long reflect(unsigned long x
, unsigned b
)
133 unsigned long xm
, ym
;
135 if (!(flags
& f_reverse
))
139 for (i
= 0; i
< b
; i
++) {
148 int main(int argc
, char *argv
[])
157 static struct option opts
[] = {
158 { "help", 0, 0, 'h' },
159 { "version", 0, 0, 'v' },
160 { "usage", 0, 0, 'u' },
162 { "output", OPTF_ARGREQ
, 0, 'o' },
163 { "c-source", 0, 0, 'c' },
164 { "symbol", OPTF_ARGREQ
, 0, 's' },
165 { "type", OPTF_ARGREQ
, 0, 't' },
166 { "include", OPTF_ARGREQ
, 0, 'i' },
167 { "guard", OPTF_ARGREQ
, 0, 'g' },
169 { "bits", OPTF_ARGREQ
, 0, 'b' },
170 { "bit-chunk", OPTF_ARGREQ
, 0, 'B' },
171 { "polynomial", OPTF_ARGREQ
, 0, 'p' },
172 { "reverse", 0, 0, 'r' },
176 int i
= mdwopt(argc
, argv
, "hvu o:cs:t:i:g: b:B:p:r", opts
, 0, 0, 0);
211 bits
= getint(optarg
, 32, "CRC width");
214 chunk
= getint(optarg
, 32, "chunk size");
217 poly
= getint(optarg
, 0xffffffff, "CRC poly");
228 if ((flags
& f_bogus
) || optind
!= argc
) {
233 /* --- Sort out the various parameters --- */
238 if (flags
& f_reverse
)
249 die(EXIT_FAILURE
, "no polynomial or bit width set");
252 die(EXIT_FAILURE
, "no standard polynomials for %u bits", bits
);
258 unsigned long x
= poly
;
266 if ((flags
& f_ctab
) && !type
)
267 type
= bits
> 16 ?
"unsigned long" : "unsigned short";
269 /* --- Start output --- */
274 if (!(flags
& f_ctab
) && !guard
) {
277 if ((p
= malloc(strlen(file
) + 1)) == 0)
278 die(EXIT_FAILURE
, "not enough memory");
280 for (q
= file
; *q
; p
++, q
++) {
281 if (isalnum((unsigned char)*q
))
282 *p
= toupper((unsigned char)*q
);
288 if ((fp
= fopen(file
, "w")) == 0)
289 die(EXIT_FAILURE
, "couldn't write `%s': %s", file
, strerror(errno
));
293 sym
= (flags
& f_ctab
) ?
"crctab" : "CRC_TAB";
295 /* --- Dump out the first chunk of the file --- */
300 * CRC table (poly = %0*lx%s) [generated]\n\
303 nw
, poly
, flags
& f_reverse ?
"; reversed" : "");
305 if (flags
& f_ctab
) {
307 fprintf(fp
, "#include \"%s\"\n\n", inc
);
308 fprintf(fp
, "%s %s[] = {\n", type
, sym
);
312 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
313 n
= fprintf(fp
, "#define %s {", sym
);
321 /* --- Sort out the line width --- */
324 while (2 + n
* (nw
+ 4) < BSCOL
)
331 while (((1 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
334 /* --- Start grinding --- */
336 mask
= 0xffffffff >> (32 - bits
);
338 for (i
= 0; i
< max
; i
++) {
339 unsigned long x
, y
, z
;
343 x
= reflect(i
, chunk
);
350 z
= (x
>> (nn
- ni
)) & (0xffffffff >> (32 - ni
));
351 y
^= z
<< (bits
- ni
);
352 for (j
= 0; j
< ni
; j
++)
353 y
= ((y
<< 1) ^ (y
& (1 << (bits
- 1)) ? poly
: 0)) & mask
;
356 x
= reflect(y
, bits
);
358 fprintf(fp
, " 0x%0*lx", nw
, x
);
360 if (!(flags
& f_ctab
) && ((2 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
364 if (i
+ 1 == max
|| (i
+ 1)%n
== 0) {
365 if (!(flags
& f_ctab
)) {
366 for (j
= 0; j
< t
; j
++)
378 fputs(flags
& f_ctab ?
"};\n" : "}\n", fp
);
379 if (!(flags
& f_ctab
) && guard
)
380 fputs("\n#endif\n", fp
);
385 /*----- That's all, folks -------------------------------------------------*/