5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
44 /*----- Static variables --------------------------------------------------*/
46 static unsigned long poly
= 0;
47 static const char *guard
= 0;
48 static unsigned bits
= 0;
49 static unsigned chunk
= 8;
50 static const char *file
= 0;
51 static unsigned flags
= 0;
52 static const char *sym
= 0;
53 static const char *type
= 0;
54 static const char *inc
= 0;
64 /*----- Main code ---------------------------------------------------------*/
66 static unsigned long getint(const char *p
, unsigned long max
,
70 unsigned long x
= strtoul(p
, &pp
, 0);
71 if (*pp
|| (max
&& x
> max
))
72 die(EXIT_FAILURE
, "bad %s `%s'", what
, p
);
76 static void version(FILE *fp
)
78 pquis(fp
, "$, mLib version " VERSION
"\n");
81 static void usage(FILE *fp
)
83 pquis(fp
, "Usage: $ [-Ccr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\
84 [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n");
87 static void help(FILE *fp
)
93 Emits a table containing precomuted values for CRC algorithms. A number\n\
94 of options are provided:\n\
96 -h, --help Show this help text.\n\
97 -v, --version Show the program's version number.\n\
98 -u, --usage Show a terse usage message.\n\
100 -c, --c-source Emit a C source file rather than a header.\n\
101 -C, --const Declare table to be `const' (only useful with `-c').\n\
102 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
103 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
104 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
105 -r, --reverse Create a `reversed' CRC table.\n\
106 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
107 -i, --include=HEADER Include HEADER at top of C source file.\n\
108 -s, --symbol=SYM Name the generated table SYM.\n\
109 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
110 -o, --output=FILE Write the output to FILE.\n\
114 unsigned long reflect(unsigned long x
, unsigned b
)
117 unsigned long xm
, ym
;
119 if (!(flags
& f_reverse
))
123 for (i
= 0; i
< b
; i
++) {
132 int main(int argc
, char *argv
[])
141 static struct option opts
[] = {
142 { "help", 0, 0, 'h' },
143 { "version", 0, 0, 'v' },
144 { "usage", 0, 0, 'u' },
146 { "output", OPTF_ARGREQ
, 0, 'o' },
147 { "c-source", 0, 0, 'c' },
148 { "const", 0, 0, 'C' },
149 { "symbol", OPTF_ARGREQ
, 0, 's' },
150 { "type", OPTF_ARGREQ
, 0, 't' },
151 { "include", OPTF_ARGREQ
, 0, 'i' },
152 { "guard", OPTF_ARGREQ
, 0, 'g' },
154 { "bits", OPTF_ARGREQ
, 0, 'b' },
155 { "bit-chunk", OPTF_ARGREQ
, 0, 'B' },
156 { "polynomial", OPTF_ARGREQ
, 0, 'p' },
157 { "reverse", 0, 0, 'r' },
161 int i
= mdwopt(argc
, argv
, "hvu o:cCs:t:i:g: b:B:p:r", opts
, 0, 0, 0);
199 bits
= getint(optarg
, 32, "CRC width");
202 chunk
= getint(optarg
, 32, "chunk size");
205 poly
= getint(optarg
, 0xffffffff, "CRC poly");
216 if ((flags
& f_bogus
) || optind
!= argc
) {
221 /* --- Sort out the various parameters --- */
226 if (flags
& f_reverse
)
237 die(EXIT_FAILURE
, "no polynomial or bit width set");
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
))
270 *p
= toupper((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 %s[] = {\n",
297 (flags
&f_const
) ?
"const " : "", type
, sym
);
301 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
302 n
= fprintf(fp
, "#define %s {", sym
);
310 /* --- Sort out the line width --- */
313 while (2 + n
* (nw
+ 4) < BSCOL
)
320 while (((1 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
323 /* --- Start grinding --- */
325 mask
= 0xffffffff >> (32 - bits
);
327 for (i
= 0; i
< max
; i
++) {
328 unsigned long x
, y
, z
;
332 x
= reflect(i
, chunk
);
339 z
= (x
>> (nn
- ni
)) & (0xffffffff >> (32 - ni
));
340 y
^= z
<< (bits
- ni
);
341 for (j
= 0; j
< ni
; j
++)
342 y
= ((y
<< 1) ^ (y
& (1 << (bits
- 1)) ? poly
: 0)) & mask
;
345 x
= reflect(y
, bits
);
347 fprintf(fp
, " 0x%0*lx", nw
, x
);
349 if (!(flags
& f_ctab
) && ((2 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
353 if (i
+ 1 == max
|| (i
+ 1)%n
== 0) {
354 if (!(flags
& f_ctab
)) {
355 for (j
= 0; j
< t
; j
++)
367 fputs(flags
& f_ctab ?
"};\n" : "}\n", fp
);
368 if (!(flags
& f_ctab
) && guard
)
369 fputs("\n#endif\n", fp
);
374 /*----- That's all, folks -------------------------------------------------*/