Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRC32 algorithm/implementation in C without a look up table and with a public license [closed]

Tags:

c

algorithm

crc32

I am trying to implement a CRC32 algorithm in C that does not use a look up table (I need to use it in a boot loader that doesn't have enough memory available to have one). Is there an available solution to this that has a public license?

like image 362
Oscar_Mariani Avatar asked Jan 08 '14 16:01

Oscar_Mariani


People also ask

What is CRC32 algorithm?

CRC32 is an error-detecting function that uses a CRC32 algorithm to detect changes between source and target data. The CRC32 function converts a variable-length string into an 8-character string that is a text representation of the hexadecimal value of a 32 bit-binary sequence.

What is CRC32 table?

CRC32 is a checksum/hashing algorithm that is very commonly used in kernels and for Internet checksums. It is very similar to the MD5 checksum algorithm.

How is CRC calculated with example?

The theory of a CRC calculation is straight forward. The data is treated by the CRC algorithm as a binary num- ber. This number is divided by another binary number called the polynomial. The rest of the division is the CRC checksum, which is appended to the transmitted message.

Can a CRC32 be reversed?

A CRC32 is only reversible if the original string is 4 bytes or less.


1 Answers

A quick search harvested this webpage. I wasn't able to find the license for these code snippets.

The following should do the job:

// ----------------------------- crc32b --------------------------------

/* This is the basic CRC-32 calculation with some optimization but no
table lookup. The the byte reversal is avoided by shifting the crc reg
right instead of left and by using a reversed 32-bit word to represent
the polynomial.
   When compiled to Cyclops with GCC, this function executes in 8 + 72n
instructions, where n is the number of bytes in the input message. It
should be doable in 4 + 61n instructions.
   If the inner loop is strung out (approx. 5*8 = 40 instructions),
it would take about 6 + 46n instructions. */

unsigned int crc32b(unsigned char *message) {
   int i, j;
   unsigned int byte, crc, mask;

   i = 0;
   crc = 0xFFFFFFFF;
   while (message[i] != 0) {
      byte = message[i];            // Get next byte.
      crc = crc ^ byte;
      for (j = 7; j >= 0; j--) {    // Do eight times.
         mask = -(crc & 1);
         crc = (crc >> 1) ^ (0xEDB88320 & mask);
      }
      i = i + 1;
   }
   return ~crc;
}
like image 172
bblincoe Avatar answered Nov 16 '22 03:11

bblincoe