AIM: Implementation of Cyclic Redundancy Check
THEORY:
A Cyclic Redundancy Check (CRC) is an error-detection algorithm widely used in computer networks, data storage systems, and communication protocols. Its primary purpose is to detect errors during data transmission, storage, or retrieval processes.The CRC algorithm involves performing polynomial division on the data and generating a fixed-size checksum, which is appended to the data before transmission or storage. Upon receiving the data, the recipient can independently calculate its CRC checksum and compare it with the received ones. If both checksums match, the data is considered to be error-free. However, if the checksums differ, it indicates that errors have occurred during data transmission, and the data integrity may be compromised. CRC operates by utilizing a predetermined binary polynomial, known as the CRC polynomial or generator polynomial. The choice of the polynomial impacts the algorithm's effectiveness in detecting errors of different types and magnitudes. This error-detection technique provides a reliable means to verify the accuracy of transmitted or stored data. It is essential in various applications, including internet communication, wireless networks, file transfers, and disk storage systems. The use of CRC ensures data integrity, critical for maintaining the overall reliability and efficiency of digital information exchange.
CODE:
#include <stdio.h>
#include <string.h>
#define N strlen(gen_poly) // Length of the generator polynomial
char data[28]; // Data to be transmitted and received
char check_value[28]; // CRC value
char gen_poly[10]; // Generator polynomial
int data_length, i, j; // Variables
void XOR() { // Function that performs XOR operation
for (j = 1; j < N; j++)
check_value[j] = ((check_value[j] == gen_poly[j]) ? '0' : '1');
}
void crc() {
for (i = 0; i < N; i++) // Initializing check_value
check_value[i] = data[i];
do {
if (check_value[0] == '1') // Check if the first bit is 1 and call XOR function
XOR();
for (j = 0; j < N - 1; j++) // Move the bits by 1 position for the next computation
check_value[j] = check_value[j + 1];
check_value[j] = data[i++]; // Appending a bit from data
} while (i <= data_length + N - 1); // Loop until the data ends
}
void receiver() { // Function to check for errors on the receiver side
printf("Enter the received data: ");
scanf("%s", data);
printf("\n------------------------------\n");
printf("Data received: %s", data);
crc(); // Cyclic Redundancy Check
for (i = 0; (i < N - 1) && (check_value[i] != '1'); i++);
if (i < N - 1)
printf("\nError detected\n\n");
else
printf("\nNo error detected\n\n");
}
int main() {
printf("\nEnter data to be transmitted: "); // Get the data to be transmitted
scanf("%s", data);
printf("\nEnter the Generating polynomial: ");
scanf("%s", gen_poly); // Get the generator polynomial
data_length = strlen(data); // Find the length of data
for (i = data_length; i < data_length + N - 1; i++) // Appending n-1 zeros to the data
data[i] = '0';
data[i] = '\0';
printf("\n------------------------------\n");
printf("\nData padded with n-1 zeros: %s", data); // Print the data with padded zeros
crc(); // Cyclic Redundancy Check
printf("\nCRC or Check value is: %s", check_value); // Print the computed check value
for (i = data_length; i < data_length + N - 1; i++) // Append data with check_value (CRC)
data[i] = check_value[i - data_length];
data[i] = '\0';
printf("\n------------------------------\n");
printf("\nFinal data to be sent: %s", data); // Printing the final data to be sent
printf("\n------------------------------\n\n");
receiver(); // Calling the receiver function to check errors
return 0;
}
OUTPUT:
CONCLUSION:
The given program successfully implements Cyclic Redundancy Check (CRC) error detection, a crucial method in digital communication to ensure data integrity. It follows these steps:
Data Transmission: The user enters the data and a generating polynomial, which is used to compute the CRC check value
Appending CRC: The computed CRC value is appended to the original data, forming the final transmitted message.
Error Detection at Receiver: The receiver checks for errors by recalculating the CRC. If the remainder is zero, the data is intact; otherwise, an error is detected.
This implementation provides an efficient and reliable way to detect errors in transmitted data, making it useful for network communication, digital storage, and embedded systems.