title: CRC32 - wrong checksum using TABLE algorithm and 04C11DB7 polynomial
body: <p>I am following a painless guide to code correction algorithms. (<a href="https://zlib.net/crc_v3.txt" rel="nofollow noreferrer">https://zlib.net/crc_v3.txt</a>) I've managed to write a TABLE algorithm, using extra loop for augmented part (I hope so). I am trying to write a most widely used CRC32 version (with 0x04C11DB7 polynomial), but I can not get the right CRC value.</p>
  <p>I've achieved the correct table for CRC32 values with mentioned polynomial.
  My code for generating CRC32 (chapter 9 and 10):</p>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define CRC32_BYTE_POSSIBLE_VALUES 255
  #define CRC32_LAST_BIT_MASK 0x80000000
  
  #define CRC32_POLYNOMIAL 0x04C11DB7
  
  uint32_t __crc32_table[CRC32_BYTE_POSSIBLE_VALUES] = { 0 };
  
  void __crc32_fill_crc_table() {
      uint32_t reg;
      uint8_t byte = 0;
  
      for (;;) {
          reg = (byte &lt;&lt; 24);
  
          for (uint8_t byte_size = 0; byte_size &lt; 8; byte_size++) {
              if (reg &amp; CRC32_LAST_BIT_MASK) {
                  reg &lt;&lt;= 1;
                  reg ^= CRC32_POLYNOMIAL;
              } else {
                  reg &lt;&lt;= 1;
              }
          }
  
          __crc32_table[byte] = reg;
          if (byte == 255)
              break;
          else
              byte++;
      }
  
  }
  
  void __crc32_print_table(uint32_t *arr) {
      printf(&quot; 0x%08X &quot;, arr[0]);
  
      for (uint32_t i = 1; i &lt; 256; i++) {
          if (!(i % 8))
              printf(&quot;\n&quot;);
  
          printf(&quot; 0x%08X &quot;, arr[i]);
  
      }
  
      printf(&quot;\n&quot;);
  }
  
  uint8_t inverse_byte(uint8_t byte) {
      uint8_t reflected_byte = 0;
  
      for (uint8_t i = 0; i &lt; 8; i++) {
          if (byte &amp; (1 &lt;&lt; i))
              reflected_byte |= (1 &lt;&lt; (7 - i));
      }
  
      return reflected_byte;
  }
  
  uint32_t inverse(uint32_t src) {
  
      uint32_t toret;
  
      for (uint8_t i = 0; i &lt; 32; i++) {
          if (src &amp; (1 &lt;&lt; i))
              toret |= (1 &lt;&lt; (31 - i));
      }
  
      return toret;
  }
  
  
  uint32_t __crc32_table_approach( unsigned char *data, size_t size) {
      uint32_t reg = -1;
      uint8_t top_byte;
  
      for (size_t i = 0; i &lt; size; i++) {
          top_byte = (uint8_t)(reg &gt;&gt; 24);
          reg = (reg &lt;&lt; 8) | inverse_byte(data[i]);
          reg ^= __crc32_table[top_byte];
      }
  
      for (size_t i = 0; i &lt; 4; i++) {
          top_byte = (uint8_t) (reg &gt;&gt; 24);
          reg = (reg &lt;&lt; 8) ;
          reg ^= __crc32_table[top_byte];
      }
  
      return inverse(reg) ^ -1;
  }
  
  uint32_t calc_crc32(unsigned char *data, size_t size) {
      if (!__crc32_table[1])
          __crc32_fill_crc_table();
  
      __crc32_print_table(__crc32_table);
  
      return __crc32_table_approach(data, size);
  }
  
  
  int main( int argc, char** argv )
  {
      
      unsigned char* test = &quot;123456789&quot;;
      size_t test_len = strlen(test);
  
      uint32_t crc = calc_crc32(test, test_len);
      printf(&quot;CRC32: 0x%08X&quot;, crc);
      return 0;
  }
  </code></pre>
  <p>The inverse function reverses bits of UINT32 value, and function inverse_byte inverses bits of UINT8 value.</p>
  <p>But for the '123456789' string I get the wrong checksum.
  Could someone help me? Or give some advice?</p>
  <hr />
  <p>Input string: '123456789'</p>
  <p>Outputted CRC: CRC32: 0x22016B0A</p>
  <p>Desired CRC: CRC32: 0xCBF43926</p>