檔案Crc32校正追加以及檔案Crc32校正(0xEDB88320)

來源:互聯網
上載者:User

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) ;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.