chiark / gitweb /
Typo fixes.
[mLib] / crc-mktab.c
CommitLineData
b7580524 1/* -*-c-*-
2 *
000ba0de 3 * $Id: crc-mktab.c,v 1.2 2000/10/08 11:07:21 mdw Exp $
b7580524 4 *
5 * Build CRC tables
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: crc-mktab.c,v $
000ba0de 33 * Revision 1.2 2000/10/08 11:07:21 mdw
34 * With no arguments, give an error rather than spewing a big table at the
35 * user.
36 *
b7580524 37 * Revision 1.1 2000/07/21 19:01:33 mdw
38 * Generate the CRC table rather than hardcoding it.
39 *
40 */
41
42/*----- Header files ------------------------------------------------------*/
43
44#include <ctype.h>
45#include <errno.h>
46#include <limits.h>
47#include <stdarg.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51
52#include "mdwopt.h"
53#include "quis.h"
54#include "report.h"
55
56/*----- Static variables --------------------------------------------------*/
57
58static unsigned long poly = 0;
59static const char *guard = 0;
60static unsigned bits = 0;
61static unsigned chunk = 8;
62static const char *file = 0;
63static unsigned flags = 0;
64static const char *sym = 0;
65static const char *type = 0;
66static const char *inc = 0;
67static FILE *fp;
68
69enum {
70 f_bogus = 1,
71 f_ctab = 2,
72 f_reverse = 4
73};
74
75#define BSCOL 72
76
77/*----- Main code ---------------------------------------------------------*/
78
79static unsigned long getint(const char *p, unsigned long max,
80 const char *what)
81{
82 char *pp;
83 unsigned long x = strtoul(p, &pp, 0);
84 if (*pp || (max && x > max))
85 die(EXIT_FAILURE, "bad %s `%s'", what, p);
86 return (x);
87}
88
89static void version(FILE *fp)
90{
91 pquis(fp, "$, mLib version " VERSION "\n");
92}
93
94static void usage(FILE *fp)
95{
96 pquis(fp, "Usage: $ [-cr] [-g guard] [-o file] [-b bits] [-p poly]\n");
97}
98
99static void help(FILE *fp)
100{
101 version(fp);
102 putc('\n', stdout);
103 usage(fp);
104 fputs("\n\
105Emits a table containing precomuted values for CRC algorithms. A number\n\
106of options are provided:\n\
107\n\
108-h, --help Show this help text.\n\
109-v, --version Show the program's version number.\n\
110-u, --usage Show a terse usage message.\n\
111\n\
112-c, --c-source Emit a C source file rather than a header.\n\
113-b, --bits=BITS Emit a table for a BITS bits-wide CRC.\n\
114-B, --bit-chunk=BITS Emit a table to process BITS bits at a time.\n\
115-p, --polynomial=POLY Use the POLY as the dividing polynomial.\n\
116-r, --reverse Create a `reversed' CRC table.\n\
117-g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
118-s, --symbol=SYM Name the generated table SYM\n\
119-t, --type=TYPE Give the table entries type TYPE in C source.\n\
120-o, --output=FILE Write the output to FILE.\n\
121", stdout);
122}
123
124unsigned long reflect(unsigned long x, unsigned b)
125{
126 unsigned long y = 0;
127 unsigned long xm, ym;
128 unsigned i;
129 if (!(flags & f_reverse))
130 return (x);
131 xm = 1;
132 ym = 1 << (b - 1);
133 for (i = 0; i < b; i++) {
134 if (x & xm)
135 y |= ym;
136 xm <<= 1;
137 ym >>= 1;
138 }
139 return (y);
140}
141
142int main(int argc, char *argv[])
143{
144 unsigned n, t, nw;
145 unsigned max, i;
146 unsigned long mask;
147
148 ego(argv[0]);
149
150 for (;;) {
151 static struct option opts[] = {
152 { "help", 0, 0, 'h' },
153 { "version", 0, 0, 'v' },
154 { "usage", 0, 0, 'u' },
155
156 { "output", OPTF_ARGREQ, 0, 'o' },
157 { "c-source", 0, 0, 'c' },
158 { "symbol", OPTF_ARGREQ, 0, 's' },
159 { "type", OPTF_ARGREQ, 0, 't' },
160 { "include", OPTF_ARGREQ, 0, 'i' },
161 { "guard", OPTF_ARGREQ, 0, 'g' },
162
163 { "bits", OPTF_ARGREQ, 0, 'b' },
164 { "bit-chunk", OPTF_ARGREQ, 0, 'B' },
165 { "polynomial", OPTF_ARGREQ, 0, 'p' },
166 { "reverse", 0, 0, 'r' },
167
168 { 0, 0, 0, 0 }
169 };
170 int i = mdwopt(argc, argv, "hvu o:cs:t:i:g: b:B:p:r", opts, 0, 0, 0);
171
172 if (i < 0)
173 break;
174 switch (i) {
175 case 'h':
176 help(stdout);
177 exit(0);
178 case 'v':
179 version(stdout);
180 exit(0);
181 case 'u':
182 usage(stdout);
183 exit(0);
184
185 case 'o':
186 file = optarg;
187 break;
188 case 'c':
189 flags |= f_ctab;
190 break;
191 case 's':
192 sym = optarg;
193 break;
194 case 't':
195 type = optarg;
196 break;
197 case 'i':
198 inc = optarg;
199 break;
200 case 'g':
201 guard = optarg;
202 break;
203
204 case 'b':
205 bits = getint(optarg, 32, "CRC width");
206 break;
207 case 'B':
208 chunk = getint(optarg, 32, "chunk size");
209 break;
210 case 'p':
211 poly = getint(optarg, 0xffffffff, "CRC poly");
212 break;
213 case 'r':
214 flags |= f_reverse;
215 break;
216
217 default:
218 flags |= f_bogus;
219 break;
220 }
221 }
222 if ((flags & f_bogus) || optind != argc) {
223 usage(stderr);
224 exit(EXIT_FAILURE);
225 }
226
227 /* --- Sort out the various parameters --- */
228
229 if (!poly) {
230 switch (bits) {
231 case 16:
232 if (flags & f_reverse)
233 poly = 0x8408;
234 else
235 poly = 0x1021;
236 break;
237 case 32:
b7580524 238 poly = 0x04c11db7;
239 flags |= f_reverse;
240 bits = 32;
241 break;
000ba0de 242 case 0:
243 die(EXIT_FAILURE, "no polynomial or bit width set");
244 break;
b7580524 245 default:
246 die(EXIT_FAILURE, "no standard polynomials for %u bits", bits);
247 break;
248 }
249 }
250
251 if (!bits) {
252 unsigned long x = poly;
253 while (x > 1) {
254 x >>= 8;
255 bits += 8;
256 }
257 }
258 nw = bits/4;
259
260 if ((flags & f_ctab) && !type)
261 type = bits > 16 ? "unsigned long" : "unsigned short";
262
263 /* --- Start output --- */
264
265 if (!file)
266 fp = stdout;
267 else {
268 if (!(flags & f_ctab) && !guard) {
269 char *p;
270 const char *q;
271 if ((p = malloc(strlen(file) + 1)) == 0)
272 die(EXIT_FAILURE, "not enough memory");
273 guard = p;
274 for (q = file; *q; p++, q++) {
275 if (isalnum((unsigned char)*q))
276 *p = *q;
277 else
278 *p = '_';
279 }
280 *p++ = 0;
281 }
282 if ((fp = fopen(file, "w")) == 0)
283 die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno));
284 }
285
286 if (!sym)
287 sym = (flags & f_ctab) ? "crctab" : "CRC_TAB";
288
289 /* --- Dump out the first chunk of the file --- */
290
291 fprintf(fp, "\
292/* -*-c-*-\n\
293 *\n\
294 * CRC table (poly = %0*lx%s) [generated]\n\
295 */\n\
296\n",
297 nw, poly, flags & f_reverse ? "; reversed" : "");
298
299 if (flags & f_ctab) {
300 if (inc)
301 fprintf(fp, "#include \"%s\"\n\n", inc);
302 fprintf(fp, "%s %s[] = {\n", type, sym);
303 } else {
304 int n;
305 if (guard)
306 fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard);
307 n = fprintf(fp, "#define %s {", sym);
308 while (n < BSCOL) {
309 fputc('\t', fp);
310 n = (n + 8) & ~7;
311 }
312 fputc('\n', fp);
313 }
314
315 /* --- Sort out the line width --- */
316
317 n = 1;
318 while (2 + n * (nw + 4) < BSCOL)
319 n <<= 1;
320 n >>= 1;
321 t = 0;
322 while (((1 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
323 t++;
324
325 /* --- Start grinding --- */
326
327 max = 1 << chunk;
328 mask = 0xffffffff >> (32 - bits);
329 fputc(' ', fp);
330 for (i = 0; i < max; i++) {
331 unsigned long x;
332 unsigned j;
333
334 x = reflect(i, chunk) << (bits - chunk);
335 for (j = 0; j < chunk; j++)
336 x = ((x << 1) ^ (x & (1 << (bits - 1)) ? poly : 0)) & mask;
337 x = reflect(x, bits);
338
339 fprintf(fp, " 0x%0*lx", nw, x);
340 if (i == max - 1) {
341 if (!(flags & f_ctab) && ((2 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
342 fputc('\t', fp);
343 } else
344 fputc(',', fp);
345 if (i + 1 == max || (i + 1)%n == 0) {
346 if (!(flags & f_ctab)) {
347 for (j = 0; j < t; j++)
348 fputc('\t', fp);
349 fputc('\\', fp);
350 }
351 fputc('\n', fp);
352 if (i + 1 != max)
353 fputc(' ', fp);
354 }
355 }
356
357 /* --- Done --- */
358
359 fputs(flags & f_ctab ? "};\n" : "}\n", fp);
360 if (!(flags & f_ctab) && guard)
361 fputs("\n#endif\n", fp);
362
363 return (0);
364}
365
366/*----- That's all, folks -------------------------------------------------*/