CRC checking of files or data

Source: Internet
Author: User
Tags crc32

Used to perform a CRC check on a file to ensure the correctness of file data transfer.

Nonsense not much to say, directly on the code!

Crc32.h

#ifndef crc_32_h#define crc_32_h#ifdef __cplusplusextern "C" {#endifvoid init_crc_table (void); unsigned int crc32 ( unsigned int crc,unsigned char *buffer, unsigned int size);  int CALC_IMG_CRC (const char *in_file, unsigned int *img_crc);  #ifdef __cplusplus} #endif #endif


Crc32.c

#include <stdlib.h>/***************************************************** * * name:crc32.c * * Author : * * version:1.0 * * Date: * * 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 S  tatic unsigned int crc_table[256];  const static char * program_name = "CRC32";      /* * * Initialize CRC table, generate 32-bit CRC table */void init_crc_table (void) {unsigned int c;      unsigned int i, J;          for (i = 0; i < i++) {c = (unsigned int) i;              for (j = 0; J < 8; J + +) {if (C & 1) c = 0xedb88320l ^ (c >> 1);          else C = C >> 1;      } Crc_table[i] = C; }/* Calculate the CRC check code for buffer */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]) & 0xFF] ^ (CRC >> 8);  } return CRC; }/* * To calculate the CRC check code for large files: crc32 function, is processing a buffer, * * But if a file is relatively large, obviously cannot read directly into the memory * * So only the file fragment read out for CRC check, * * Then the loop will pass the last CRC checksum to the new buffer check function, * * to the end, the generated CRC check code is the CRC check code of the file.      */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 needs to be fixed, if the sender uses this value to calculate the CRC checksum, * * Then the receiving end also needs to use this value for calculation */unsigned int CRC = 0xFFFFFFFF;      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) {exit (1);      } init_crc_table ();      ret = CALC_IMG_CRC (In_file, &AMP;IMG_CRC);      if (Ret < 0) {exit (1);      } printf ("The CRC of%s is:%u\n", In_file, IMG_CRC);  return 0; }*/

To verify a file:

         Init_crc_table ();         unsigned int bin_crc;         CALC_IMG_CRC (ARGV[1],&BIN_CRC);

To verify the data in the BUF:

     Init_crc_table ();     unsigned int bincrcnew = 0xFFFFFFFF;     Bincrcnew = CRC32 (bincrcnew, (unsigned char*) fwbuff, Binlen);

The simplest makefile Test example:

All:encryptBINencryptBIN:encryptBIN.cpp crc32.cgcc-c crc32.c-o crc32.og++-C encryptbin.cpp-o ENCRYPTBIN.OGCC CRC32.O Encryptbin.o-lstdc++-o encryptbinclean:rm-rf *.o encryptbin


CRC checking of files or data

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.