Во! У себя в проекте, что подобное нашел
.H| Код | /***************************************************************************/ /* FUNCTION: CRC_File */ /* PURPOSE: Returns CRC32 for file FName */ /* PARAMETERS: FName - File Name */ /* Crc - CRC32 reference */ /* RETURN VALUE: 0 - error CRC32 detection */ /* 1 - CRC32 detected OK */ /***************************************************************************/ int CRC_File(unsigned char *FName,unsigned long &Crc); // Get CRC32 for file
/***************************************************************************/ /* FUNCTION: CRC_FileHandler */ /* PURPOSE: Returns CRC32 for file FName */ /* PARAMETERS: F - File Handler */ /* Crc - CRC32 reference */ /* Start - Start position in file */ /* Len - Data length (0L - all file) */ /* RETURN VALUE: 0 - error CRC32 detection */ /* 1 - CRC32 detected OK */ /***************************************************************************/ int CRC_FileHandler(int F,unsigned long &Crc,long Start,// Get CRC32 FileHand long Len);
void Gen_crc_table(void); // Generate CRC32 Tbl unsigned long Update_crc(DWORD ulCrc_accum, // Previous CRC32 BYTE *pchData_blk_ptr, // Block address DWORD lData_blk_size); // Size of Block
|
.CPP
| Код | /***************************************************************************/ /* PROGRAM: CRC32.cpp */ /* FUNCTIONS: Crc_table, Update_crc, CRC_File */ /* COMMENTS: Unit for calculating of CRC remainder. */ /***************************************************************************/ #include <stdio.h> // Include file #include <io.h> // section
#define POLYNOMIAL 0x04C11DB7 // Polinom const
static DWORD aulCrc_table[256]; // CRC32 Table
/***************************************************************************/ /* FUNCTION: Gen_crc_table */ /* PURPOSE: Generates a 256 word table containing CRC remainders for */ /* every possible 8-bit byte */ /***************************************************************************/ void Gen_crc_table(void) // Generate CRC32 Tbl { register DWORD iI,iJ; // Cycle variables register DWORD ulCrc_accum; // CRC32 accumulator
for(iI=0;iI < 256;iI++) // For all bytes { ulCrc_accum=iI << 24; // Init accumulator for(iJ=0;iJ < 8;iJ++) // For each bit { if (ulCrc_accum & 0x80000000L) // If high bit set ulCrc_accum=(ulCrc_accum << 1) ^ POLYNOMIAL; // Shift and XOR Poly else // Else ulCrc_accum=(ulCrc_accum << 1); // Shift only } aulCrc_table[iI]=ulCrc_accum; // Setup table value } } // End of initialize
/***************************************************************************/ /* FUNCTION: Update_crc */ /* PURPOSE: Returns the updated value of the CRC after processing each */ /* byte in the adressed block of data */ /* PARAMETERS: ulCrc_accum - present CRC value */ /* pchData_blk_ptr - data for updating CRC */ /* iData_blk_size - data size */ /***************************************************************************/ unsigned long Update_crc(DWORD ulCrc_accum, // Previous CRC32 BYTE *pchData_blk_ptr, // Block address DWORD lData_blk_size) // Size of Block { __asm { mov esi,pchData_blk_ptr lea edi,aulCrc_table mov ecx,lData_blk_size mov eax,ulCrc_accum ucrc_loop: mov dl,[esi] mov ebx,eax inc esi shr ebx,24 shl eax,8 xor edx,ebx and edx,0xFF xor eax,[edi+4*edx] loop ucrc_loop mov ulCrc_accum,eax; }; return ulCrc_accum; // Return value }
//#ifndef WIN32 /***************************************************************************/ /* FUNCTION: CRC_File */ /* PURPOSE: Returns CRC32 for file FName */ /* PARAMETERS: FName - File Name */ /* Crc - CRC32 reference */ /* RETURN VALUE: 0 - error CRC32 detection */ /* 1 - CRC32 detected OK */ /***************************************************************************/ int CRC_File(char *FName,unsigned long &Crc) // Get CRC32 for file { FILE *hStream; // File stream char achBuff1[1000]; // Read buffer unsigned long ulCrc=0L,ulFileLen; // CRC32 & File len int iJ; // Return value unsigned int Siz; // Size to read unsigned long ulI=0; // Bytes counter
Gen_crc_table(); // Generate CRC Tbl if((hStream=fopen(FName,"rb")) == NULL) return(0); // If not opened-err fseek(hStream,0,SEEK_END); // Seek to end ulFileLen=ftell(hStream); // Get file length fseek(hStream,0,SEEK_SET); // Return to start
while(ulI < ulFileLen) // For all bytes { if((ulI+1000) <= ulFileLen) Siz=1000; // Calculate possible else Siz=(unsigned int)(ulFileLen-ulI); // block size if((iJ=fread(achBuff1,1,Siz,hStream)) != (int)Siz) // If read error { fclose(hStream); // Close file return(0); // Return error } ulI += iJ; // Update byte cnt ulCrc=Update_crc(ulCrc,(BYTE*)achBuff1,iJ); // Update CRC32 } fclose(hStream); // Close file Crc=ulCrc; // Setup result return(1); // Return OK }
/***************************************************************************/ /* FUNCTION: CRC_FileHandler */ /* PURPOSE: Returns CRC32 for file FName */ /* PARAMETERS: F - File Handler */ /* Crc - CRC32 reference */ /* Start - Start position in file */ /* Len - Data length (0L - all file) */ /* RETURN VALUE: 0 - error CRC32 detection */ /* 1 - CRC32 detected OK */ /***************************************************************************/ int CRC_FileHandler(int F,unsigned long &Crc,long Start,// Get CRC32 FileHand long Len) { char achBuff1[1000]; // Read buffer unsigned long ulCrc=0L,ulFileLen; // CRC32 & File Len long ulFPos; // File position int iJ; // Return value unsigned int Siz; // Size to read unsigned long ulI=0; // Bytes counter
if((ulFPos=tell(F)) == -1) return(0); // No get position Gen_crc_table(); // Generate CRC Tbl if(Len == 0L) ulFileLen=filelength(F)-Start; // Get file length else ulFileLen=Len; // Or set it from par if(lseek(F,Start,SEEK_SET) == -1L) return(0); // No seek to start
while(ulI < ulFileLen) // For all bytes { if((ulI+1000) <= ulFileLen) Siz=1000; // Calculate possible else Siz=(unsigned int)(ulFileLen-ulI); // block size if((iJ=_read(F,achBuff1,Siz)) != (int)Siz) // If read error { lseek(F,ulFPos,SEEK_SET); // Return file pos return(0); // Return error } ulI += iJ; // Update byte cnt ulCrc=Update_crc(ulCrc,(BYTE*)achBuff1,iJ); // Update CRC32 } lseek(F,ulFPos,SEEK_SET); // Return file pos Crc=ulCrc; // Setup result return(1); // Return OK } //#endif /***************************** End of Module ******************************/
| |