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;
63 /*----- Main code ---------------------------------------------------------*/
65 static unsigned long getint(const char *p
, unsigned long max
,
69 unsigned long x
= strtoul(p
, &pp
, 0);
70 if (*pp
|| (max
&& x
> max
))
71 die(EXIT_FAILURE
, "bad %s `%s'", what
, p
);
75 static void version(FILE *fp
)
77 pquis(fp
, "$, mLib version " VERSION
"\n");
80 static void usage(FILE *fp
)
82 pquis(fp
, "Usage: $ [-cr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\
83 [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n");
86 static void help(FILE *fp
)
92 Emits a table containing precomuted values for CRC algorithms. A number\n\
93 of options are provided:\n\
95 -h, --help Show this help text.\n\
96 -v, --version Show the program's version number.\n\
97 -u, --usage Show a terse usage message.\n\
99 -c, --c-source Emit a C source file rather than a header.\n\
100 -b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
101 -B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
102 -p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
103 -r, --reverse Create a `reversed' CRC table.\n\
104 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
105 -i, --include=HEADER Include HEADER at top of C source file.\n\
106 -s, --symbol=SYM Name the generated table SYM.\n\
107 -t, --type=TYPE Give the table entries type TYPE in C source.\n\
108 -o, --output=FILE Write the output to FILE.\n\
112 unsigned long reflect(unsigned long x
, unsigned b
)
115 unsigned long xm
, ym
;
117 if (!(flags
& f_reverse
))
121 for (i
= 0; i
< b
; i
++) {
130 int main(int argc
, char *argv
[])
139 static struct option opts
[] = {
140 { "help", 0, 0, 'h' },
141 { "version", 0, 0, 'v' },
142 { "usage", 0, 0, 'u' },
144 { "output", OPTF_ARGREQ
, 0, 'o' },
145 { "c-source", 0, 0, 'c' },
146 { "symbol", OPTF_ARGREQ
, 0, 's' },
147 { "type", OPTF_ARGREQ
, 0, 't' },
148 { "include", OPTF_ARGREQ
, 0, 'i' },
149 { "guard", OPTF_ARGREQ
, 0, 'g' },
151 { "bits", OPTF_ARGREQ
, 0, 'b' },
152 { "bit-chunk", OPTF_ARGREQ
, 0, 'B' },
153 { "polynomial", OPTF_ARGREQ
, 0, 'p' },
154 { "reverse", 0, 0, 'r' },
158 int i
= mdwopt(argc
, argv
, "hvu o:cs:t:i:g: b:B:p:r", opts
, 0, 0, 0);
193 bits
= getint(optarg
, 32, "CRC width");
196 chunk
= getint(optarg
, 32, "chunk size");
199 poly
= getint(optarg
, 0xffffffff, "CRC poly");
210 if ((flags
& f_bogus
) || optind
!= argc
) {
215 /* --- Sort out the various parameters --- */
220 if (flags
& f_reverse
)
231 die(EXIT_FAILURE
, "no polynomial or bit width set");
234 die(EXIT_FAILURE
, "no standard polynomials for %u bits", bits
);
240 unsigned long x
= poly
;
248 if ((flags
& f_ctab
) && !type
)
249 type
= bits
> 16 ?
"unsigned long" : "unsigned short";
251 /* --- Start output --- */
256 if (!(flags
& f_ctab
) && !guard
) {
259 if ((p
= malloc(strlen(file
) + 1)) == 0)
260 die(EXIT_FAILURE
, "not enough memory");
262 for (q
= file
; *q
; p
++, q
++) {
263 if (isalnum((unsigned char)*q
))
264 *p
= toupper((unsigned char)*q
);
270 if ((fp
= fopen(file
, "w")) == 0)
271 die(EXIT_FAILURE
, "couldn't write `%s': %s", file
, strerror(errno
));
275 sym
= (flags
& f_ctab
) ?
"crctab" : "CRC_TAB";
277 /* --- Dump out the first chunk of the file --- */
282 * CRC table (poly = %0*lx%s) [generated]\n\
285 nw
, poly
, flags
& f_reverse ?
"; reversed" : "");
287 if (flags
& f_ctab
) {
289 fprintf(fp
, "#include \"%s\"\n\n", inc
);
290 fprintf(fp
, "%s %s[] = {\n", type
, sym
);
294 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
295 n
= fprintf(fp
, "#define %s {", sym
);
303 /* --- Sort out the line width --- */
306 while (2 + n
* (nw
+ 4) < BSCOL
)
313 while (((1 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
316 /* --- Start grinding --- */
318 mask
= 0xffffffff >> (32 - bits
);
320 for (i
= 0; i
< max
; i
++) {
321 unsigned long x
, y
, z
;
325 x
= reflect(i
, chunk
);
332 z
= (x
>> (nn
- ni
)) & (0xffffffff >> (32 - ni
));
333 y
^= z
<< (bits
- ni
);
334 for (j
= 0; j
< ni
; j
++)
335 y
= ((y
<< 1) ^ (y
& (1 << (bits
- 1)) ? poly
: 0)) & mask
;
338 x
= reflect(y
, bits
);
340 fprintf(fp
, " 0x%0*lx", nw
, x
);
342 if (!(flags
& f_ctab
) && ((2 + n
* (nw
+ 4)) & ~7) + 8 * t
< BSCOL
)
346 if (i
+ 1 == max
|| (i
+ 1)%n
== 0) {
347 if (!(flags
& f_ctab
)) {
348 for (j
= 0; j
< t
; j
++)
360 fputs(flags
& f_ctab ?
"};\n" : "}\n", fp
);
361 if (!(flags
& f_ctab
) && guard
)
362 fputs("\n#endif\n", fp
);
367 /*----- That's all, folks -------------------------------------------------*/