<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://sarwiki.informatik.hu-berlin.de/index.php?action=history&amp;feed=atom&amp;title=Crc32.c</id>
	<title>Crc32.c - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://sarwiki.informatik.hu-berlin.de/index.php?action=history&amp;feed=atom&amp;title=Crc32.c"/>
	<link rel="alternate" type="text/html" href="https://sarwiki.informatik.hu-berlin.de/index.php?title=Crc32.c&amp;action=history"/>
	<updated>2026-05-14T04:35:56Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://sarwiki.informatik.hu-berlin.de/index.php?title=Crc32.c&amp;diff=6305&amp;oldid=prev</id>
		<title>Grauel at 10:33, 31 October 2006</title>
		<link rel="alternate" type="text/html" href="https://sarwiki.informatik.hu-berlin.de/index.php?title=Crc32.c&amp;diff=6305&amp;oldid=prev"/>
		<updated>2006-10-31T10:33:34Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt; #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define CRCPOLY 0xEDB88320&lt;br /&gt;
 #define CRCINV 0x5B358FD3 // inverse poly of (x^N) mod CRCPOLY&lt;br /&gt;
 #define INITXOR 0xFFFFFFFF&lt;br /&gt;
 #define FINALXOR 0xFFFFFFFF&lt;br /&gt;
 typedef unsigned int uint32;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * Creates the CRC table with 256 32-bit entries. CAUTION: Assumes that &lt;br /&gt;
  * enough space for the resulting table has already been allocated.&lt;br /&gt;
  */&lt;br /&gt;
 void make_crc_table(uint32 *table) {&lt;br /&gt;
     uint32 c;&lt;br /&gt;
     int n, k;&lt;br /&gt;
         &lt;br /&gt;
     for (n = 0; n &amp;lt; 256; n++) {&lt;br /&gt;
         c = n;&lt;br /&gt;
         for (k = 0; k &amp;lt; 8; k++) {&lt;br /&gt;
             if ((c &amp;amp; 1) != 0) {&lt;br /&gt;
                 c = CRCPOLY ^ (c &amp;gt;&amp;gt; 1);&lt;br /&gt;
             } else {&lt;br /&gt;
                 c = c &amp;gt;&amp;gt; 1;&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
         table[n] = c;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * Creates the reverse CRC table with 256 32-bit entries. CAUTION: Assumes &lt;br /&gt;
  * that enough space for the resulting table has already been allocated.&lt;br /&gt;
  */&lt;br /&gt;
 void make_crc_revtable(uint32 *table) {&lt;br /&gt;
     uint32 c;&lt;br /&gt;
     int n, k;&lt;br /&gt;
         &lt;br /&gt;
     for (n = 0; n &amp;lt; 256; n++) {&lt;br /&gt;
         c = n &amp;lt;&amp;lt; 3*8;&lt;br /&gt;
         for (k = 0; k &amp;lt; 8; k++) {&lt;br /&gt;
             if ((c &amp;amp; 0x80000000) != 0) {&lt;br /&gt;
                 c = ((c ^ CRCPOLY) &amp;lt;&amp;lt; 1) | 1;&lt;br /&gt;
             } else {&lt;br /&gt;
                 c &amp;lt;&amp;lt;= 1;&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
         table[n] = c;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * Computes the CRC32 of the buffer of the given length using the&lt;br /&gt;
  * (slow) bit-oriented approach&lt;br /&gt;
  */&lt;br /&gt;
 int crc32_bitoriented(unsigned char *buffer, int length) {&lt;br /&gt;
     int i, j;&lt;br /&gt;
     uint32 crcreg = INITXOR;&lt;br /&gt;
     &lt;br /&gt;
     for (j = 0; j &amp;lt; length; ++j) {&lt;br /&gt;
         unsigned char b = buffer[j];&lt;br /&gt;
         for (i = 0; i &amp;lt; 8; ++i) {&lt;br /&gt;
             if ((crcreg ^ b) &amp;amp; 1) {&lt;br /&gt;
                 crcreg = (crcreg &amp;gt;&amp;gt; 1) ^ CRCPOLY;&lt;br /&gt;
             } else {&lt;br /&gt;
                 crcreg &amp;gt;&amp;gt;= 1;&lt;br /&gt;
             }&lt;br /&gt;
             b &amp;gt;&amp;gt;= 1;&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     return crcreg ^ FINALXOR;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * Computes the CRC32 of the buffer of the given length &lt;br /&gt;
  * using the supplied crc_table&lt;br /&gt;
  */&lt;br /&gt;
 int crc32_tabledriven(unsigned char *buffer, &lt;br /&gt;
                       int length, &lt;br /&gt;
                       uint32 *crc_table) &lt;br /&gt;
 {&lt;br /&gt;
     int i;&lt;br /&gt;
     uint32 crcreg = INITXOR;&lt;br /&gt;
         &lt;br /&gt;
     for (i = 0; i &amp;lt; length; ++i) {&lt;br /&gt;
         crcreg = (crcreg &amp;gt;&amp;gt; 8) ^ crc_table[((crcreg ^ buffer[i]) &amp;amp; 0xFF)];&lt;br /&gt;
     }&lt;br /&gt;
     &lt;br /&gt;
     return crcreg ^ FINALXOR;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * Changes the last 4 bytes of the given buffer so that it afterwards will&lt;br /&gt;
  * compute to the &amp;quot;magic sequence&amp;quot; (usually 0x2144DF1C for CRC32)&lt;br /&gt;
  */&lt;br /&gt;
 void fix_crc_magic(unsigned char *buffer, int length, uint32 *crc_table) &lt;br /&gt;
 {&lt;br /&gt;
     int i;&lt;br /&gt;
 &lt;br /&gt;
     // calculate CRC32 except for the last 4 bytes&lt;br /&gt;
     uint32 crcreg = crc32_tabledriven(buffer, length-4, crc_table);&lt;br /&gt;
 &lt;br /&gt;
     // inject crcreg as content - nothing easier than that!&lt;br /&gt;
     for (i = 0; i &amp;lt; 4; ++i)&lt;br /&gt;
         buffer[length - 4 + i] = (crcreg &amp;gt;&amp;gt; i*8) &amp;amp; 0xFF;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * Changes the last 4 bytes of the given buffer so that it afterwards will&lt;br /&gt;
  * compute to the given tcrcreg using the given crc_table&lt;br /&gt;
  *&lt;br /&gt;
  * This function uses the method of the multiplication with (x^N)^-1.&lt;br /&gt;
  */&lt;br /&gt;
 void fix_crc_end(unsigned char *buffer, &lt;br /&gt;
                  int length, &lt;br /&gt;
                  uint32 tcrcreg, &lt;br /&gt;
                  uint32 *crc_table) &lt;br /&gt;
 {&lt;br /&gt;
     int i;&lt;br /&gt;
     tcrcreg ^= FINALXOR;&lt;br /&gt;
 &lt;br /&gt;
     // calculate crc except for the last 4 bytes; this is essentially crc32()&lt;br /&gt;
     uint32 crcreg = INITXOR;&lt;br /&gt;
     for (i = 0; i &amp;lt; length - 4; ++i) {&lt;br /&gt;
         crcreg = (crcreg &amp;gt;&amp;gt; 8) ^ crc_table[((crcreg ^ buffer[i]) &amp;amp; 0xFF)];&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // calculate new content bits&lt;br /&gt;
     // new_content = tcrcreg * CRCINV mod CRCPOLY&lt;br /&gt;
     uint32 new_content = 0;&lt;br /&gt;
     for (i = 0; i &amp;lt; 32; ++i) {&lt;br /&gt;
         // reduce modulo CRCPOLY&lt;br /&gt;
         if (new_content &amp;amp; 1) {&lt;br /&gt;
              new_content = (new_content &amp;gt;&amp;gt; 1) ^ CRCPOLY;&lt;br /&gt;
         } else {&lt;br /&gt;
              new_content &amp;gt;&amp;gt;=  1;&lt;br /&gt;
         }&lt;br /&gt;
         // add CRCINV if corresponding bit of operand is set&lt;br /&gt;
         if (tcrcreg &amp;amp; 1) {&lt;br /&gt;
             new_content ^= CRCINV;&lt;br /&gt;
         }&lt;br /&gt;
         tcrcreg &amp;gt;&amp;gt;= 1;&lt;br /&gt;
     }&lt;br /&gt;
     // finally add old crc&lt;br /&gt;
     new_content ^= crcreg;&lt;br /&gt;
     &lt;br /&gt;
     // inject new content&lt;br /&gt;
     for (i = 0; i &amp;lt; 4; ++i)&lt;br /&gt;
         buffer[length - 4 + i] = (new_content &amp;gt;&amp;gt; i*8) &amp;amp; 0xFF;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * Changes the 4 bytes of the given buffer at position fix_pos so that &lt;br /&gt;
  * it afterwards will compute to the given tcrcreg using the given crc_table.&lt;br /&gt;
  * A reversed crc table (crc_revtable) must be provided.&lt;br /&gt;
  */&lt;br /&gt;
 void fix_crc_pos(unsigned char *buffer, &lt;br /&gt;
                  int length, &lt;br /&gt;
                  uint32 tcrcreg, &lt;br /&gt;
                  int fix_pos, &lt;br /&gt;
                  uint32 *crc_table, &lt;br /&gt;
                  uint32 *crc_revtable) &lt;br /&gt;
 {&lt;br /&gt;
     int i;&lt;br /&gt;
     // make sure fix_pos is within 0..(length-1)&lt;br /&gt;
     fix_pos = ((fix_pos%length)+length)%length;&lt;br /&gt;
 &lt;br /&gt;
     // calculate crc register at position fix_pos; this is essentially crc32()&lt;br /&gt;
     uint32 crcreg = INITXOR;&lt;br /&gt;
     for (i = 0; i &amp;lt; fix_pos; ++i) {&lt;br /&gt;
         crcreg = (crcreg &amp;gt;&amp;gt; 8) ^ crc_table[((crcreg ^ buffer[i]) &amp;amp; 0xFF)];&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // inject crcreg as content&lt;br /&gt;
     for (i = 0; i &amp;lt; 4; ++i)&lt;br /&gt;
         buffer[fix_pos + i] = (crcreg &amp;gt;&amp;gt; i*8) &amp;amp; 0xFF;&lt;br /&gt;
 &lt;br /&gt;
     // calculate crc backwards to fix_pos, beginning at the end&lt;br /&gt;
     tcrcreg ^= FINALXOR;&lt;br /&gt;
     for (i = length - 1; i &amp;gt;= fix_pos; --i) {&lt;br /&gt;
         tcrcreg = (tcrcreg &amp;lt;&amp;lt; 8) ^ crc_revtable[tcrcreg &amp;gt;&amp;gt; 3*8] ^ buffer[i];&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // inject new content&lt;br /&gt;
     for (i = 0; i &amp;lt; 4; ++i)&lt;br /&gt;
         buffer[fix_pos + i] = (tcrcreg &amp;gt;&amp;gt; i*8) &amp;amp; 0xFF;&lt;br /&gt;
         &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  * this function is obsolete as i used it as a testbed&lt;br /&gt;
  */&lt;br /&gt;
 //       1         2         3         4         5         6         7         8&lt;br /&gt;
 // 45678901234567890123456789012345678901234567890123456789012345678901234567890&lt;br /&gt;
 void fix_crc_old(unsigned char *buffer, int length, uint32 target_crc, int fix_pos, uint32 *crc_table) {&lt;br /&gt;
     int i, j;&lt;br /&gt;
     fix_pos = ((fix_pos%length)+length)%length;&lt;br /&gt;
 &lt;br /&gt;
     // calculate target crc register on position fix_pos+4&lt;br /&gt;
     uint32 tcrcreg = (target_crc &amp;amp; 0xFFFFFFFF) ^ FINALXOR;&lt;br /&gt;
 //    uint32 tcrcreg = (target_crc &amp;amp; 0xFFFFFFFF) ^ 0x2144DF1C;&lt;br /&gt;
     for (j = length - 1; j &amp;gt; fix_pos + 3; --j) {&lt;br /&gt;
 //        printf(&amp;quot;fix_crc_old: \ttcrcreg[%d] = %08X\n&amp;quot;, j, tcrcreg);&lt;br /&gt;
         unsigned char cbyte = buffer[j];&lt;br /&gt;
         for (i = 0; i&amp;lt;=7; ++i) {&lt;br /&gt;
             uint32 xor = (tcrcreg &amp;gt;&amp;gt; 31) &amp;amp; 1 ;&lt;br /&gt;
             tcrcreg = ((tcrcreg ^ (xor*CRCPOLY)) &amp;lt;&amp;lt; 1) &amp;amp; 0xFFFFFFFF | (xor ^ (cbyte &amp;gt;&amp;gt; 7) &amp;amp; 1);&lt;br /&gt;
             cbyte &amp;lt;&amp;lt;= 1;&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 //    printf(&amp;quot;fix_crc_old: \ttcrcreg[%d] = %08X\n&amp;quot;, j, tcrcreg);&lt;br /&gt;
 &lt;br /&gt;
     // calculate positions for XORing the CRCPOLY&lt;br /&gt;
     uint32 xorreg = 0;&lt;br /&gt;
     for (i=0; i&amp;lt;=31; ++i) {&lt;br /&gt;
         xorreg &amp;lt;&amp;lt;= 1;&lt;br /&gt;
         if ((tcrcreg &amp;amp; 0x80000000) != 0) {&lt;br /&gt;
             tcrcreg ^= CRCPOLY;&lt;br /&gt;
             xorreg |= 1;&lt;br /&gt;
         }&lt;br /&gt;
         tcrcreg = (tcrcreg &amp;lt;&amp;lt; 1) &amp;amp; 0xFFFFFFFF;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // calculate crc register on position fix_pos&lt;br /&gt;
     uint32 crcreg = crc32_tabledriven(buffer, fix_pos, crc_table);&lt;br /&gt;
     crcreg ^= 0xFFFFFFFFL;&lt;br /&gt;
 &lt;br /&gt;
     printf(&amp;quot;fix_crc_old: \tcrcreg = %08X\n&amp;quot;, crcreg);&lt;br /&gt;
     &lt;br /&gt;
 //    printf(&amp;quot;fix_crc: \t***  crcreg = %08X&amp;quot;, crcreg);&lt;br /&gt;
     &lt;br /&gt;
     // calculate new content bits&lt;br /&gt;
     uint32 new_content = 0;&lt;br /&gt;
     for (i=0; i&amp;lt;=31; ++i) {&lt;br /&gt;
         new_content &amp;gt;&amp;gt;= 1;&lt;br /&gt;
         new_content |= ((crcreg ^ xorreg) &amp;amp; 1) &amp;lt;&amp;lt; 31;&lt;br /&gt;
         crcreg = (crcreg &amp;gt;&amp;gt; 1) ^ ((xorreg &amp;amp; 1) * CRCPOLY);&lt;br /&gt;
         xorreg &amp;gt;&amp;gt;= 1;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
 //    printf(&amp;quot;, b = %08X\n&amp;quot;, new_content);&lt;br /&gt;
 &lt;br /&gt;
     printf(&amp;quot;fix_crc_old: \tnew_content = %08X\n&amp;quot;, new_content);&lt;br /&gt;
 &lt;br /&gt;
     // inject new content&lt;br /&gt;
     for (i=0; i&amp;lt;=3; ++i)&lt;br /&gt;
         buffer[fix_pos + i] = (new_content &amp;gt;&amp;gt; i*8) &amp;amp; 0xFF;&lt;br /&gt;
         &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 #if 0&lt;br /&gt;
 int main() {&lt;br /&gt;
 &lt;br /&gt;
     int i;&lt;br /&gt;
     &lt;br /&gt;
     uint32 crc_table[256*8];&lt;br /&gt;
     uint32 crc_revtable[256*8];&lt;br /&gt;
     unsigned char foostr1[] = &amp;quot;foobar1234&amp;quot;;&lt;br /&gt;
     unsigned char foostr2[] = &amp;quot;foobar1234&amp;quot;;&lt;br /&gt;
     unsigned char foostr3[] = &amp;quot;foobar1234&amp;quot;;&lt;br /&gt;
     unsigned char foostr4[] = &amp;quot;foobar1234&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
     make_crc_table(crc_table);&lt;br /&gt;
     make_crc_revtable(crc_revtable);&lt;br /&gt;
     &lt;br /&gt;
     unsigned char foostr[] = &amp;quot;bla 1234&amp;quot;;&lt;br /&gt;
     fix_crc_pos(foostr, 8, 0xcafebabe, -4, crc_table, crc_revtable);&lt;br /&gt;
     printf(&amp;quot;crc of foostr: %08X\n&amp;quot;, crc32_tabledriven(foostr, 8, crc_table));&lt;br /&gt;
 &lt;br /&gt;
     uint32 targetcrc = 0xdeadbeef;&lt;br /&gt;
 &lt;br /&gt;
     printf(&amp;quot;crc of foobar1: %08X\n&amp;quot;, crc32_bitoriented(foostr1, 6));&lt;br /&gt;
     printf(&amp;quot;crc of foobar2: %08X\n&amp;quot;, crc32_tabledriven(foostr2, 6, crc_table));&lt;br /&gt;
     printf(&amp;quot;crc of foobar3: %08X\n&amp;quot;, crc32_tabledriven(foostr3, 6, crc_table));&lt;br /&gt;
     printf(&amp;quot;crc of foobar4: %08X\n&amp;quot;, crc32_tabledriven(foostr4, 6, crc_table));&lt;br /&gt;
 &lt;br /&gt;
     fix_crc_old(foostr1, 10, targetcrc, 3, crc_table);&lt;br /&gt;
     fix_crc_pos(foostr2, 10, targetcrc, 3, crc_table, crc_revtable);&lt;br /&gt;
     fix_crc_end(foostr3, 10, targetcrc, crc_table);&lt;br /&gt;
     fix_crc_magic(foostr4, 10, crc_table);&lt;br /&gt;
 &lt;br /&gt;
     printf(&amp;quot;crc after fix old: %08X\n&amp;quot;, crc32_tabledriven(foostr1, 10, crc_table));&lt;br /&gt;
     for (i=0; i&amp;lt;10; ++i) printf(&amp;quot;%02X &amp;quot;, foostr1[i]);printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
     printf(&amp;quot;crc after fix new: %08X\n&amp;quot;, crc32_tabledriven(foostr2, 10, crc_table));&lt;br /&gt;
     for (i=0; i&amp;lt;10; ++i) printf(&amp;quot;%02X &amp;quot;, foostr2[i]);printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
     printf(&amp;quot;crc after fix end: %08X\n&amp;quot;, crc32_tabledriven(foostr3, 10, crc_table));&lt;br /&gt;
     for (i=0; i&amp;lt;10; ++i) printf(&amp;quot;%02X &amp;quot;, foostr3[i]);printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
     printf(&amp;quot;crc after fix magic: %08X\n&amp;quot;, crc32_tabledriven(foostr4, 10, crc_table));&lt;br /&gt;
     for (i=0; i&amp;lt;10; ++i) printf(&amp;quot;%02X &amp;quot;, foostr4[i]);printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 #endif&lt;/div&gt;</summary>
		<author><name>Grauel</name></author>
	</entry>
</feed>