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