Commit | Line | Data |
---|---|---|
b7580524 | 1 | /* -*-c-*- |
2 | * | |
b7580524 | 3 | * Build CRC tables |
4 | * | |
5 | * (c) 2000 Straylight/Edgeware | |
6 | */ | |
7 | ||
d4efbcd9 | 8 | /*----- Licensing notice --------------------------------------------------* |
b7580524 | 9 | * |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
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. | |
d4efbcd9 | 16 | * |
b7580524 | 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. | |
d4efbcd9 | 21 | * |
b7580524 | 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, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
b7580524 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
a0150d8d MW |
30 | #include "config.h" |
31 | ||
b7580524 | 32 | #include <ctype.h> |
33 | #include <errno.h> | |
34 | #include <limits.h> | |
35 | #include <stdarg.h> | |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | ||
40 | #include "mdwopt.h" | |
41 | #include "quis.h" | |
42 | #include "report.h" | |
43 | ||
44 | /*----- Static variables --------------------------------------------------*/ | |
45 | ||
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; | |
55 | static FILE *fp; | |
56 | ||
393cf1d9 | 57 | #define f_bogus 1u |
58 | #define f_ctab 2u | |
59 | #define f_reverse 4u | |
b7580524 | 60 | |
61 | #define BSCOL 72 | |
62 | ||
63 | /*----- Main code ---------------------------------------------------------*/ | |
64 | ||
65 | static unsigned long getint(const char *p, unsigned long max, | |
66 | const char *what) | |
67 | { | |
68 | char *pp; | |
69 | unsigned long x = strtoul(p, &pp, 0); | |
70 | if (*pp || (max && x > max)) | |
71 | die(EXIT_FAILURE, "bad %s `%s'", what, p); | |
72 | return (x); | |
73 | } | |
74 | ||
75 | static void version(FILE *fp) | |
76 | { | |
77 | pquis(fp, "$, mLib version " VERSION "\n"); | |
78 | } | |
79 | ||
80 | static void usage(FILE *fp) | |
81 | { | |
6f444bda | 82 | pquis(fp, "Usage: $ [-cr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\ |
d4efbcd9 | 83 | [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n"); |
b7580524 | 84 | } |
85 | ||
86 | static void help(FILE *fp) | |
87 | { | |
88 | version(fp); | |
89 | putc('\n', stdout); | |
90 | usage(fp); | |
91 | fputs("\n\ | |
92 | Emits a table containing precomuted values for CRC algorithms. A number\n\ | |
93 | of options are provided:\n\ | |
94 | \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\ | |
98 | \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\ | |
6f444bda | 105 | -i, --include=HEADER Include HEADER at top of C source file.\n\ |
106 | -s, --symbol=SYM Name the generated table SYM.\n\ | |
b7580524 | 107 | -t, --type=TYPE Give the table entries type TYPE in C source.\n\ |
108 | -o, --output=FILE Write the output to FILE.\n\ | |
109 | ", stdout); | |
110 | } | |
111 | ||
112 | unsigned long reflect(unsigned long x, unsigned b) | |
113 | { | |
114 | unsigned long y = 0; | |
115 | unsigned long xm, ym; | |
116 | unsigned i; | |
117 | if (!(flags & f_reverse)) | |
118 | return (x); | |
119 | xm = 1; | |
c9507812 | 120 | ym = 1u << (b - 1); |
b7580524 | 121 | for (i = 0; i < b; i++) { |
122 | if (x & xm) | |
123 | y |= ym; | |
124 | xm <<= 1; | |
125 | ym >>= 1; | |
126 | } | |
127 | return (y); | |
128 | } | |
129 | ||
130 | int main(int argc, char *argv[]) | |
131 | { | |
132 | unsigned n, t, nw; | |
133 | unsigned max, i; | |
134 | unsigned long mask; | |
135 | ||
136 | ego(argv[0]); | |
137 | ||
138 | for (;;) { | |
139 | static struct option opts[] = { | |
140 | { "help", 0, 0, 'h' }, | |
141 | { "version", 0, 0, 'v' }, | |
142 | { "usage", 0, 0, 'u' }, | |
143 | ||
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' }, | |
150 | ||
151 | { "bits", OPTF_ARGREQ, 0, 'b' }, | |
152 | { "bit-chunk", OPTF_ARGREQ, 0, 'B' }, | |
153 | { "polynomial", OPTF_ARGREQ, 0, 'p' }, | |
154 | { "reverse", 0, 0, 'r' }, | |
155 | ||
156 | { 0, 0, 0, 0 } | |
157 | }; | |
158 | int i = mdwopt(argc, argv, "hvu o:cs:t:i:g: b:B:p:r", opts, 0, 0, 0); | |
159 | ||
160 | if (i < 0) | |
161 | break; | |
162 | switch (i) { | |
163 | case 'h': | |
164 | help(stdout); | |
165 | exit(0); | |
166 | case 'v': | |
167 | version(stdout); | |
168 | exit(0); | |
169 | case 'u': | |
170 | usage(stdout); | |
171 | exit(0); | |
172 | ||
173 | case 'o': | |
174 | file = optarg; | |
175 | break; | |
176 | case 'c': | |
177 | flags |= f_ctab; | |
178 | break; | |
179 | case 's': | |
180 | sym = optarg; | |
181 | break; | |
182 | case 't': | |
183 | type = optarg; | |
184 | break; | |
185 | case 'i': | |
186 | inc = optarg; | |
187 | break; | |
188 | case 'g': | |
189 | guard = optarg; | |
190 | break; | |
191 | ||
192 | case 'b': | |
193 | bits = getint(optarg, 32, "CRC width"); | |
194 | break; | |
195 | case 'B': | |
196 | chunk = getint(optarg, 32, "chunk size"); | |
197 | break; | |
198 | case 'p': | |
199 | poly = getint(optarg, 0xffffffff, "CRC poly"); | |
200 | break; | |
201 | case 'r': | |
202 | flags |= f_reverse; | |
203 | break; | |
204 | ||
205 | default: | |
206 | flags |= f_bogus; | |
207 | break; | |
208 | } | |
209 | } | |
210 | if ((flags & f_bogus) || optind != argc) { | |
211 | usage(stderr); | |
212 | exit(EXIT_FAILURE); | |
213 | } | |
214 | ||
215 | /* --- Sort out the various parameters --- */ | |
216 | ||
217 | if (!poly) { | |
218 | switch (bits) { | |
219 | case 16: | |
220 | if (flags & f_reverse) | |
221 | poly = 0x8408; | |
222 | else | |
223 | poly = 0x1021; | |
224 | break; | |
225 | case 32: | |
b7580524 | 226 | poly = 0x04c11db7; |
227 | flags |= f_reverse; | |
228 | bits = 32; | |
229 | break; | |
000ba0de | 230 | case 0: |
231 | die(EXIT_FAILURE, "no polynomial or bit width set"); | |
232 | break; | |
b7580524 | 233 | default: |
234 | die(EXIT_FAILURE, "no standard polynomials for %u bits", bits); | |
235 | break; | |
236 | } | |
237 | } | |
238 | ||
239 | if (!bits) { | |
240 | unsigned long x = poly; | |
241 | while (x > 1) { | |
242 | x >>= 8; | |
243 | bits += 8; | |
244 | } | |
245 | } | |
0521f473 | 246 | nw = (bits + 3)/4; |
b7580524 | 247 | |
248 | if ((flags & f_ctab) && !type) | |
249 | type = bits > 16 ? "unsigned long" : "unsigned short"; | |
250 | ||
251 | /* --- Start output --- */ | |
252 | ||
253 | if (!file) | |
254 | fp = stdout; | |
255 | else { | |
256 | if (!(flags & f_ctab) && !guard) { | |
257 | char *p; | |
258 | const char *q; | |
259 | if ((p = malloc(strlen(file) + 1)) == 0) | |
260 | die(EXIT_FAILURE, "not enough memory"); | |
261 | guard = p; | |
262 | for (q = file; *q; p++, q++) { | |
263 | if (isalnum((unsigned char)*q)) | |
0521f473 | 264 | *p = toupper((unsigned char)*q); |
b7580524 | 265 | else |
266 | *p = '_'; | |
267 | } | |
268 | *p++ = 0; | |
269 | } | |
270 | if ((fp = fopen(file, "w")) == 0) | |
271 | die(EXIT_FAILURE, "couldn't write `%s': %s", file, strerror(errno)); | |
272 | } | |
273 | ||
274 | if (!sym) | |
275 | sym = (flags & f_ctab) ? "crctab" : "CRC_TAB"; | |
276 | ||
277 | /* --- Dump out the first chunk of the file --- */ | |
278 | ||
279 | fprintf(fp, "\ | |
280 | /* -*-c-*-\n\ | |
281 | *\n\ | |
282 | * CRC table (poly = %0*lx%s) [generated]\n\ | |
283 | */\n\ | |
284 | \n", | |
285 | nw, poly, flags & f_reverse ? "; reversed" : ""); | |
286 | ||
287 | if (flags & f_ctab) { | |
288 | if (inc) | |
289 | fprintf(fp, "#include \"%s\"\n\n", inc); | |
290 | fprintf(fp, "%s %s[] = {\n", type, sym); | |
291 | } else { | |
292 | int n; | |
293 | if (guard) | |
294 | fprintf(fp, "#ifndef %s\n#define %s\n\n", guard, guard); | |
295 | n = fprintf(fp, "#define %s {", sym); | |
296 | while (n < BSCOL) { | |
297 | fputc('\t', fp); | |
298 | n = (n + 8) & ~7; | |
299 | } | |
300 | fputc('\n', fp); | |
301 | } | |
302 | ||
303 | /* --- Sort out the line width --- */ | |
304 | ||
305 | n = 1; | |
306 | while (2 + n * (nw + 4) < BSCOL) | |
307 | n <<= 1; | |
308 | n >>= 1; | |
0521f473 | 309 | max = 1 << chunk; |
310 | if (n > max) | |
311 | n = max; | |
b7580524 | 312 | t = 0; |
313 | while (((1 + n * (nw + 4)) & ~7) + 8 * t < BSCOL) | |
314 | t++; | |
315 | ||
316 | /* --- Start grinding --- */ | |
317 | ||
b7580524 | 318 | mask = 0xffffffff >> (32 - bits); |
319 | fputc(' ', fp); | |
320 | for (i = 0; i < max; i++) { | |
0521f473 | 321 | unsigned long x, y, z; |
b7580524 | 322 | unsigned j; |
0521f473 | 323 | unsigned ni, nn; |
324 | ||
325 | x = reflect(i, chunk); | |
326 | y = 0; | |
327 | nn = chunk; | |
328 | while (nn) { | |
329 | ni = bits; | |
330 | if (ni > nn) | |
331 | ni = nn; | |
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; | |
336 | nn -= ni; | |
337 | } | |
338 | x = reflect(y, bits); | |
b7580524 | 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 -------------------------------------------------*/ |