×

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind CRC is to divide the data to be sent by a predetermined divisor, and then send the remainder along with the data. The receiver then performs the same operation and compares the remainder with the one that was sent. If they match, the data is considered to be error-free.

Here is an example of a simple implementation of the CRC algorithm in C:

Algorithm

unsigned int crc32(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;
}

Here is a more detailed implementation of the CRC algorithm in C:

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define POLYNOMIAL 0x04C11DB7  // Standard CRC-32 polynomial


unsigned int crcTable[256];   // Table of precalculated values


// Function to generate the table of precalculated values
void generateCRCTable() {
unsigned int remainder;


    // Perform modulo-2 division, a bit at a time
for (int dividend = 0; dividend < 256; dividend++) {
        // Start with the dividend followed by zeros
remainder = dividend << 24;


        // Perform modulo-2 division, a bit at a time
for (int bit = 0; bit < 8; bit++) {
            // Try to divide the current data bit
if (remainder & 0x80000000) {
remainder = (remainder << 1) ^ POLYNOMIAL;
            } else {
remainder = (remainder << 1);
            }
        }


        // Store the result in the table
crcTable[dividend] = remainder;
    }
}


// Function to calculate the CRC of a given data block
unsigned int calculateCRC(unsigned char *data, int length) {
unsigned int remainder = 0xFFFFFFFF;  // Start with all 1's


    // Perform modulo-2 division, a byte at a time
for (int byte = 0; byte < length; byte++) {
        // Bring the next byte into the remainder
remainder = (remainder << 8) ^ crcTable[(remainder >> 24) ^ data[byte]];
    }


    // The final remainder is the CRC result
return remainder;
}


int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <string>\n", argv[0]);
return 1;
    }


generateCRCTable();  // Generate the table of precalculated values


    // Calculate the CRC of the given string
unsigned int crc = calculateCRC((unsigned char *)argv[1], strlen(argv[1]));


printf("CRC: 0x%08X\n", crc);


return 0;
}

Output:

Usage: ./a.out<string>

This implementation uses a table of precalculated values to speed up the calculation of the CRC. The table is generated using the standard CRC-32 polynomial (0x04C11DB7) and the generateCRCTable() function. The calculateCRC() function takes a pointer to the data and its length as arguments and returns the calculated CRC. The main() function takes a string as a command line argument and calculates its CRC.

In this example, the crc is calculated in the little endian format, if you want it in big endian format, you should reverse the byte order of the final remainder before returning it.

Complexity of CRC Program in C

The time complexity of the implementation of the CRC algorithm in C that is provided earlier is O(n), where n is the length of the data block. This is because the algorithm performs a modulo-2 division, a byte (or a bit) at a time, for the entire data block.

The space complexity of the implementation is O(1) because it only uses a fixed-size table (256 entries) of precalculated values to store the intermediate results, regardless of the size of the data block.

The table generation of precalculated values has a time complexity of O(256*8) = O(2048) which is a constant time and can be done before the actual calculation of the crc.

It is important to note that the time complexity will change if the width of the crc or the polynomial is changed.

One way to implement a CRC using bit manipulation is to use a pre-defined "CRC polynomial" and perform a series of bitwise operations on the data, such as XOR and shift, to calculate a fixed-size "CRC value" that can be appended to the data to form a "CRC code." Here's some example pseudo code for a basic implementation of a CRC using bit manipulation:

functioncrc(data, polynomial) {
crc = 0
for each bit in data {
if (crc& 0x8000) {
crc = (crc<< 1) ^ polynomial
    } else {
crc = crc<< 1
    }
  }
returncrc& 0xFFFF
}

In this example, "data" is the raw data that we want to create a CRC code for, and "polynomial" is the pre-defined polynomial that you are using for the calculation. The function first initializes a "crc" variable to 0, then iterates through each bit in the data. For each bit, it checks if the most significant bit of the "crc" variable is set (i.e. if the crc& 0x8000 is true), and if so, it performs an XOR operation with the polynomial and a left shift. If the most significant bit is not set, it simply performs a left shift. Finally, the function returns the "crc" variable, masked with 0xFFFF to return the 16-bit value.


Related Topics

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.