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 ------------------------------------------------------*/
45 /*----- Static variables --------------------------------------------------*/
47 static unsigned long poly
= 0;
48 static const char *guard
= 0;
49 static unsigned bits
= 0;
50 static unsigned chunk
= 8;
51 static const char *file
= 0;
52 static unsigned flags
= 0;
53 static const char *sym
= 0;
54 static const char *type
= 0;
55 static const char *inc
= 0;
65 /*----- Main code ---------------------------------------------------------*/
67 static unsigned long getint(const char *p
, unsigned long max
,
71 unsigned long x
= strtoul(p
, &pp
, 0);
72 if (*pp
|| (max
&& x
> max
))
73 die(EXIT_FAILURE
, "bad %s `%s'", what
, p
);
77 static void version(FILE *fp
)
79 pquis(fp
, "$, mLib version " VERSION
"\n");
82 static void usage(FILE *fp
)
84 pquis(fp
, "Usage: $ [-Ccr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\
85 [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n");
88 static void help(FILE *fp
)
94 Emits a table containing precomuted values for CRC algorithms. A number\n\
95 of options are provided:\n\
97 -h, --help Show this help text.\n\
98 -v, --version Show the program's version number.\n\
99 -u, --usage Show a terse usage message.\n\
101 -c, --c-source Emit a C source file rather than a header.\n\
102 -C, --const Declare table to be `const' (only useful with `-c').\n\
103 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
104 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
105 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
106 -r, --reverse Create a `reversed' CRC table.\n\
107 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
108 -i, --include=HEADER Include HEADER at top of C source file.\n\
109 -s, --symbol=SYM Name the generated table SYM.\n\
110 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
111 -o, --output=FILE Write the output to FILE.\n\
115 unsigned long reflect(unsigned long x
, unsigned b
)
118 unsigned long xm
, ym
;
120 if (!(flags
& f_reverse
))
124 for (i
= 0; i
< b
; i
++) {
133 int main(int argc
, char *argv
[])
142 static struct option opts
[] = {
143 { "help", 0, 0, 'h' },
144 { "version", 0, 0, 'v' },
145 { "usage", 0, 0, 'u' },
147 { "output", OPTF_ARGREQ
, 0, 'o' },
148 { "c-source", 0, 0, 'c' },
149 { "const", 0, 0, 'C' },
150 { "symbol", OPTF_ARGREQ
, 0, 's' },
151 { "type", OPTF_ARGREQ
, 0, 't' },
152 { "include", OPTF_ARGREQ
, 0, 'i' },
153 { "guard", OPTF_ARGREQ
, 0, 'g' },
155 { "bits", OPTF_ARGREQ
, 0, 'b' },
156 { "bit-chunk", OPTF_ARGREQ
, 0, 'B' },
157 { "polynomial", OPTF_ARGREQ
, 0, 'p' },
158 { "reverse", 0, 0, 'r' },
162 int i
= mdwopt(argc
, argv
, "hvu o:cCs:t:i:g: b:B:p:r", opts
, 0, 0, 0);
200 bits
= getint(optarg
, 32, "CRC width");
203 chunk
= getint(optarg
, 32, "chunk size");
206 poly
= getint(optarg
, 0xffffffff, "CRC poly");
217 if ((flags
& f_bogus
) || optind
!= argc
) {
222 /* --- Sort out the various parameters --- */
227 if (flags
& f_reverse
)
238 die(EXIT_FAILURE
, "no polynomial or bit width set");
241 die(EXIT_FAILURE
, "no standard polynomials for %u bits", bits
);
247 unsigned long x
= poly
;
255 if ((flags
& f_ctab
) && !type
)
256 type
= bits
> 16 ?
"unsigned long" : "unsigned short";
258 /* --- Start output --- */
263 if (!(flags
& f_ctab
) && !guard
) {
266 if ((p
= malloc(strlen(file
) + 1)) == 0)
267 die(EXIT_FAILURE
, "not enough memory");
269 for (q
= file
; *q
; p
++, q
++) {
277 if ((fp
= fopen(file
, "w")) == 0)
278 die(EXIT_FAILURE
, "couldn't write `%s': %s", file
, strerror(errno
));
282 sym
= (flags
& f_ctab
) ?
"crctab" : "CRC_TAB";
284 /* --- Dump out the first chunk of the file --- */
289 * CRC table (poly = %0*lx%s) [generated]\n\
292 nw
, poly
, flags
& f_reverse ?
"; reversed" : "");
294 if (flags
& f_ctab
) {
296 fprintf(fp
, "#include \"%s\"\n\n", inc
);
297 fprintf(fp
, "%s%s %s[] = {\n",
298 (flags
&f_const
) ?
"const " : "", type
, sym
);
302 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
303 n
= fprintf(fp
, "#define %s {", sym
);
311 /* --- Sort out the line width --- */
314 while (2 + n
* (nw
+ 4) < BSCOL
)
321 while (((1 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
324 /* --- Start grinding --- */
326 mask
= 0xffffffff >> (32 - bits
);
328 for (i
= 0; i
< max
; i
++) {
329 unsigned long x
, y
, z
;
333 x
= reflect(i
, chunk
);
340 z
= (x
>> (nn
- ni
)) & (0xffffffff >> (32 - ni
));
341 y
^= z
<< (bits
- ni
);
342 for (j
= 0; j
< ni
; j
++)
343 y
= ((y
<< 1) ^ (y
& (1 << (bits
- 1)) ? poly
: 0)) & mask
;
346 x
= reflect(y
, bits
);
348 fprintf(fp
, " 0x%0*lx", nw
, x
);
350 if (!(flags
& f_ctab
) && ((2 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
354 if (i
+ 1 == max
|| (i
+ 1)%n
== 0) {
355 if (!(flags
& f_ctab
)) {
356 for (j
= 0; j
< t
; j
++)
368 fputs(flags
& f_ctab ?
"};\n" : "}\n", fp
);
369 if (!(flags
& f_ctab
) && guard
)
370 fputs("\n#endif\n", fp
);
375 /*----- That's all, folks -------------------------------------------------*/