文件Crc32校验追加以及文件Crc32校验(0xEDB88320)

unsigned char AppengCrc32(unsigned char *FileName)
{
  unsigned long CRC = 0xFFFFFFFF;
  unsigned int  i;
  unsigned char Res=0;
  unsigned char Buff[513];
  unsigned int  RealRead;
  FILE          *Fp;
  unsigned short FileSize;


  Fp=fopen((const char *)FileName,"r+");
  fread(&FileSize,1,2,Fp);
  FileSize+=4;
  fseek(Fp,0,SEEK_SET);
  fwrite(&FileSize,2,1,Fp);

  fseek(Fp,0,SEEK_SET);
  if(Fp!=NULL)
  {
    for(;;)
    {
     
      RealRead=fread(Buff,1,512,Fp);
   if(RealRead!=0)
   {
     for(i = 0;i < RealRead;i++)
       CRC = CRC32[((CRC & 0xFF) ^ Buff[i])] ^ (CRC >> 8);
   }
   else
     break;
 }
  }
  CRC = ~CRC;
  fseek(Fp,0,SEEK_END);
  fwrite(&CRC,4,1,Fp);
  fclose(Fp);
  return 1;
}

unsigned char WinCheckCrc32(unsigned char *FileName)
{
  typedef struct
  {
    signed int   FileSize;
    signed int   AccFp   ;
    unsigned long  CrcRead;
  }TCrc32File;
 

  unsigned long CRC = 0xFFFFFFFF;
  signed int  i;
  unsigned char Res=0;
  unsigned char Buff[513];
  signed int  RealRead;

  TCrc32File  Crc32File;
  FILE          *Fp;

 

  Crc32File.AccFp=0;
  Fp=fopen(FileName,"rb");
  fseek(Fp,0,SEEK_END);                   
  Crc32File.FileSize=ftell(Fp);           

  fseek(Fp,SEEK_SET,0);                   

  if(Fp!=NULL)
  {
    for(;;)
    {
      RealRead=fread(Buff,1,512,Fp);      
      if(RealRead>0)
   {
     for(i = 0;i<RealRead;i++)
     {
         Crc32File.AccFp++;
         if(Crc32File.AccFp>Crc32File.FileSize-4) break;
         CRC = CRC32[((CRC & 0xFF) ^ Buff[i])] ^ (CRC >> 8);
     }
   }
   else
      break;
 }
  }
  CRC = ~CRC;
  fclose(Fp);

  Fp=fopen(FileName,"rb");
  fseek(Fp,-4,SEEK_END); 
  fread(&Crc32File.CrcRead,4,1,Fp);
  fclose(Fp); 
  return (Crc32File.CrcRead==CRC) ;
}

 

你可能感兴趣的:(文件Crc32校验追加以及文件Crc32校验(0xEDB88320))