title: CRC table algorithm in C, width limitation
body: <p>I found an easy to use implementation of CRC algorithms <a href="http://www.zorc.breitbandkatze.de/crctester.c" rel="nofollow">here</a>. It includes table based and bit-wise algorithms. The code seems to work fine but there is an important limitation of table based algorithms. This is the relevant code:</p>
  
  <pre><code>unsigned long reflect (unsigned long crc, int bitnum) {
      unsigned long i, j=1, crcout=0;
  
      for (i=(unsigned long)1&lt;&lt;(bitnum-1); i; i&gt;&gt;=1) {
          if (crc &amp; i) crcout|=j;
          j&lt;&lt;= 1;
      }
      return (crcout);
  }
  
  void generate_crc_table() {
      // make CRC lookup table used by table algorithms
  
      int i, j;
      unsigned long bit, crc;
  
      for (i=0; i&lt;256; i++) {
          crc=(unsigned long)i;
          if (refin) crc=reflect(crc, 8);
          crc&lt;&lt;= order-8;
  
          for (j=0; j&lt;8; j++) {
              bit = crc &amp; crchighbit;
              crc&lt;&lt;= 1;
              if (bit) crc^= polynom;
          }           
  
          if (refin) crc = reflect(crc, order);
          crc&amp;= crcmask;
          crctab[i]= crc;
      }
  }  
  
  unsigned long crctablefast (unsigned char* p, unsigned long len) {
  
      // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
      // only usable with polynom orders of 8, 16, 24 or 32.
  
      unsigned long crc = crcinit_direct;
  
      if (refin) crc = reflect(crc, order);
  
      if (!refin) while (len--) crc = (crc &lt;&lt; 8) ^ crctab[ ((crc &gt;&gt; (order-8)) &amp; 0xff) ^ *p++];
      else while (len--) crc = (crc &gt;&gt; 8) ^ crctab[ (crc &amp; 0xff) ^ *p++];
  
      if (refout^refin) crc = reflect(crc, order);
      crc^= crcxor;
      crc&amp;= crcmask;
  
      return(crc);
  }
  </code></pre>
  
  <p>Please note code comments for table functions say:</p>
  
  <blockquote>
    <p>only usable with polynom orders of 8, 16, 24 or 32.</p>
  </blockquote>
  
  <p>Are table based algorithms generally limited to widths that are multiples of eight (especially table algorithms that use 16 and 32 bit tables)?</p>
  
  <p>Is it possible to implement a table based CRC algorithm that accepts <em>any</em> CRC widths (not only multiples of 8)? How?</p>