CRC32 verification algorithm-C implementation

Source: Internet
Author: User
Tags crc32

CRC is the Cyclic Redundancy checksum (Cyclic)
Redundancy check): it is the most common error verification code in the field of data communication. It is characterized by the length of information fields and verification fields that can be selected at will.

In the field of data storage and data communication, the CRC verification utility library has to adopt the method of checking errors to ensure data correctness.

The following is the C language implementation of CRC32, which can be correctly run after testing:

 

 
/*************************************** * *************** Name: crc32.c ** Author: gzshun ** version: 1.0 ** Date: 2011-12 ** description: CRC32 checking ************************************** * ***************/# include <stdio. h> # include <stdlib. h> # include <string. h> # include <errno. h> # include <unistd. h> # include <fcntl. h> # include <sys/STAT. h> # define bufsize 1024*4 static unsigned int crc_table [256]; con St static char * program_name = "CRC32"; static void usage (void); static void init_crc_table (void); static unsigned int CRC32 (unsigned int CRC, unsigned char * buffer, unsigned int size); static int calc_img_crc (const char * in_file, unsigned int * img_crc); static void usage (void) {fprintf (stderr, "Usage: % s input_file \ n ", program_name);}/*** initialize the CRC table to generate a 32-bit CRC table. ** you can also define a CRC table and directly look up the table, ** however, there are a total of 256, which are dazzled by the generated ratio. Convenient. */static void init_crc_table (void) {unsigned int C; unsigned int I, j; for (I = 0; I <256; I ++) {c = (unsigned INT) i; for (j = 0; j <8; j ++) {If (C & 1) C = 0xedb88320l ^ (C> 1 ); elsec = C> 1;} crc_table [I] = C;}/* calculate the CRC check code of the buffer */static unsigned int CRC32 (unsigned int CRC, unsigned char * buffer, unsigned int size) {unsigned int I; for (I = 0; I <size; I ++) {CRC = crc_table [(CRC ^ buffer [I]) & 0 XFF] ^ (CRC> 8);} return CRC;}/*** calculate the CRC check code of a large file: The CRC32 function, which processes a buffer, ** however, if a file is relatively large, it cannot be directly read into the memory. ** therefore, you can only read the file in segments for CRC verification, ** the last CRC check code is passed to the new buffer check function cyclically. ** Finally, the generated CRC check code is the CRC check code of the file. (tested) */static int calc_img_crc (const char * in_file, unsigned int * img_crc) {int FD; int nread; int ret; unsigned char Buf [bufsize]; /* The first incoming value must be fixed. If the sender uses this value to calculate the CRC verification code, ** the receiver also needs to use this value for calculation */unsigned int CRC = 0 xffffffff; FD = Open (in_file, o_rdonly); If (FD <0) {printf ("% d: Open % S. \ n ", _ line __, strerror (errno); Return-1;} while (nread = read (FD, Buf, bufsize)> 0) {CRC = CRC32 (CRC, Buf, nread);} * img_crc = CRC; close (FD); If (nread <0) {printf ("% d: Read % S. \ n ", _ line __, strerror (errno); Return-1;} return 0;} int main (INT argc, char ** argv) {int ret; unsigned int img_crc; const char * in_file = argv [1]; If (argc <2) {usag E (); exit (1) ;}init_crc_table (); ret = calc_img_crc (in_file, & img_crc); If (Ret <0) {exit (1 );} printf ("the CRC of % s is: % u \ n", in_file, img_crc); Return 0 ;}/ *** test program ** environment: ** Linux Ubuntu 2.6.32-24-generic-pae # 39-ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux ** GCC version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ** gzshun @ Ubuntu :~ /Apue/CRC32 $ lscrc32.cgzshun @ Ubuntu :~ /Apue/CRC32 $ GCC crc32.c-O crc32gzshun @ Ubuntu :~ /Apue/CRC32 $./CRC32 crc32.cthe CRC of crc32.c is: 3892136086 */

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.