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