chiark / gitweb /
Add adns support in background resolver.
[mLib] / crc-mktab.c
index 887315b67f7355a47503afdb821627b66f987237..2439e20f4bc610ff04c7bc9e897f397b44f834a9 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: crc-mktab.c,v 1.1 2000/07/21 19:01:33 mdw Exp $
+ * $Id: crc-mktab.c,v 1.4 2001/03/10 10:59:21 mdw Exp $
  *
  * Build CRC tables
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: crc-mktab.c,v $
+ * Revision 1.4  2001/03/10 10:59:21  mdw
+ * Fix generating tables where the chunk size is longer than the
+ * polynomial.  Also fix output formatting when there aren't enough entries
+ * to fill a line.
+ *
+ * Revision 1.3  2001/01/20 12:06:01  mdw
+ * Define flags with macros, to ensure unsignedness.
+ *
+ * Revision 1.2  2000/10/08 11:07:21  mdw
+ * With no arguments, give an error rather than spewing a big table at the
+ * user.
+ *
  * Revision 1.1  2000/07/21 19:01:33  mdw
  * Generate the CRC table rather than hardcoding it.
  *
@@ -62,11 +74,9 @@ static const char *type = 0;
 static const char *inc = 0;
 static FILE *fp;
 
-enum {
-  f_bogus = 1,
-  f_ctab = 2,
-  f_reverse = 4
-};
+#define f_bogus 1u
+#define f_ctab 2u
+#define f_reverse 4u
 
 #define BSCOL 72
 
@@ -231,11 +241,13 @@ int main(int argc, char *argv[])
          poly = 0x1021;
        break;
       case 32:
-      case 0:
        poly = 0x04c11db7;
        flags |= f_reverse;
        bits = 32;
        break;
+      case 0:
+       die(EXIT_FAILURE, "no polynomial or bit width set");
+       break;
       default:
        die(EXIT_FAILURE, "no standard polynomials for %u bits", bits);
        break;
@@ -249,7 +261,7 @@ int main(int argc, char *argv[])
       bits += 8;
     }
   }
-  nw = bits/4;
+  nw = (bits + 3)/4;
 
   if ((flags & f_ctab) && !type)
     type = bits > 16 ? "unsigned long" : "unsigned short";
@@ -267,7 +279,7 @@ int main(int argc, char *argv[])
       guard = p;
       for (q = file; *q; p++, q++) {
        if (isalnum((unsigned char)*q))
-         *p = *q;
+         *p = toupper((unsigned char)*q);
        else
          *p = '_';
       }
@@ -312,23 +324,36 @@ int main(int argc, char *argv[])
   while (2 + n * (nw + 4) < BSCOL)
     n <<= 1;
   n >>= 1;
+  max = 1 << chunk;
+  if (n > max)
+    n = max;
   t = 0;
   while (((1 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
     t++;
 
   /* --- Start grinding --- */
 
-  max = 1 << chunk;
   mask = 0xffffffff >> (32 - bits);
   fputc(' ', fp);
   for (i = 0; i < max; i++) {
-    unsigned long x;
+    unsigned long x, y, z;
     unsigned j;
-
-    x = reflect(i, chunk) << (bits - chunk);
-    for (j = 0; j < chunk; j++)
-      x = ((x << 1) ^ (x & (1 << (bits - 1)) ? poly : 0)) & mask;
-    x = reflect(x, bits);
+    unsigned ni, nn;
+
+    x = reflect(i, chunk);
+    y = 0;
+    nn = chunk;
+    while (nn) {
+      ni = bits;
+      if (ni > nn)
+       ni = nn;
+      z = (x >> (nn - ni)) & (0xffffffff >> (32 - ni));
+      y ^= z << (bits - ni);
+      for (j = 0; j < ni; j++)
+       y = ((y << 1) ^ (y & (1 << (bits - 1)) ? poly : 0)) & mask;
+      nn -= ni;
+    }
+    x = reflect(y, bits);
 
     fprintf(fp, " 0x%0*lx", nw, x);
     if (i == max - 1) {