×

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

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 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.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.