// Verify that the given binary log has the correct checksum int TestBinary( unsigned char* ucBuf_, int iSize_ ) { if( ucBuf_[0] != 0xAA ) return FALSE; // The crc-32 of a buffer containing a valid crc-32 will be 0. return CalculateCRC32(ucBuf_, iSize_) ? FALSE : TRUE; }
// getHashValue - Generate callstack hash value. // // Return Value: // // None. // DWORD CallStack::getHashValue () const { DWORD hashcode = 0xD202EF8D; // Iterate through each frame in the call stack. for (UINT32 frame = 0; frame < m_size; frame++) { UINT_PTR programcounter = (*this)[frame]; hashcode = CalculateCRC32(programcounter, hashcode); } return hashcode; }
// Verify that the given OEM-4 ascii log has the correct checksum int TestASCII( char* ucBuf_ ) { char* szCheckCrc32; // Location of the Crc32 unsigned long ulCrc32; // Log's Crc32 size_t dataSize; // Length of data used to compute crc-32 int bufSize; // Size of buffer including crc-32 int crcSize; unsigned char* willy; // Temporary buffer containing binary crc32 appended to guts of ucBuf_ int crcStatus; if( ucBuf_[0] != '#' ) return( FALSE ); szCheckCrc32 = (char *)strrchr((const char*)ucBuf_, '*'); if ( !szCheckCrc32 ) return( FALSE ); // Convert the ascii crc32 to a binary value if( sscanf( szCheckCrc32+1, "%lx ", &ulCrc32 ) == 0) return( FALSE ); // Make a copy of the log with the '#' and '*' wrapper removed crcSize = sizeof(ulCrc32); dataSize = szCheckCrc32 - &ucBuf_[1]; bufSize = dataSize + crcSize; willy = (unsigned char *) malloc(bufSize); memset(willy,0,bufSize); memcpy(willy, &ucBuf_[1], dataSize); // Append the reported CRC to the data in the buffer memcpy(&willy[dataSize], &ulCrc32, crcSize); // The crc-32 of a buffer containing a valid crc-32 will be 0. crcStatus = CalculateCRC32( willy, bufSize ) ? FALSE : TRUE; free(willy); // ...I had to work too hard for this joke... return crcStatus; }
unsigned char CheckFirmware(fileTYPE *file, char *name) { UPGRADE *pUpgrade = (UPGRADE*)sector_buffer; unsigned long crc; unsigned long size; unsigned long rom_size; unsigned long rom_crc; unsigned long read_size; Error = ERROR_FILE_NOT_FOUND; if (FileOpen(file, name)) { Error = ERROR_INVALID_DATA; iprintf("Upgrade file size : %lu\r", file->size); iprintf("Upgrade header size : %lu\r", (unsigned long)sizeof(UPGRADE)); if (file->size >= sizeof(UPGRADE)) { FileRead(file, sector_buffer); crc = ~CalculateCRC32(-1, sector_buffer, sizeof(UPGRADE) - 4); iprintf("Upgrade ROM size : %lu\r", pUpgrade->rom.size); iprintf("Upgrade header CRC : %08lX\r", pUpgrade->crc); iprintf("Calculated header CRC : %08lX\r", crc); if (pUpgrade->crc == crc) { if (strncmp((const char*)pUpgrade->id, "MNMGUPG", 7) == 0 && pUpgrade->id[7] == 0) { if (pUpgrade->rom.size == file->size - sizeof(UPGRADE)) { rom_size = pUpgrade->rom.size; rom_crc = pUpgrade->rom.crc; crc = -1; // initial CRC32 value size = rom_size; while (size) { if (size > 512) read_size = 512; else read_size = size; FileNextSector(file); FileRead(file, sector_buffer); crc = CalculateCRC32(crc, sector_buffer, read_size); size -= read_size; } iprintf("Calculated ROM CRC : %08lX\r", ~crc); iprintf("ROM CRC from header : %08lX\r", rom_crc); if (~crc == rom_crc) { // upgrade file CRC is OK so go back to the beginning of the file FileSeek(file, 0, SEEK_SET); Error = ERROR_NONE; return 1; } else iprintf("ROM CRC mismatch! from header: %08lX, calculated: %08lX\r", rom_crc, ~crc); } else iprintf("ROM size mismatch! from header: %lu, from file: %lu\r", pUpgrade->rom.size, file->size-sizeof(UPGRADE)); } else iprintf("Invalid upgrade file header!\r"); } else iprintf("Header CRC mismatch! from header: %08lX, calculated: %08lX\r", pUpgrade->crc, crc); } else iprintf("Upgrade file size too small: %lu\r", file->size); } else iprintf("Cannot open firmware file!\r"); return 0; }
bool File::Check() { _Ptr<IO::Stream> stream(GetStream()); uint32 crc = CalculateCRC32(stream, 0xffffffffL) ^ 0xffffffffL; return crc == m_crc_32; }