Skip to content

Commit

Permalink
Add crnd_validate_header to check it without data CRC
Browse files Browse the repository at this point in the history
The stuff in the header like image size or format is really important,
so let us validate that without having to CRC the entire file. The CRC
implementation is slow and who cares if 1 pixel is wrong?
  • Loading branch information
slipher committed Jun 20, 2024
1 parent 923dcb1 commit e471bf6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
22 changes: 17 additions & 5 deletions inc/crn_decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ const crn_header* crnd_get_header(const void* pData, uint32 data_size) {
return &file_header;
}

bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info) {
bool crnd_validate_header(const void* pData, uint32 data_size, crn_file_info* pFile_info) {
if (pFile_info) {
if (pFile_info->m_struct_size != sizeof(crn_file_info))
return false;
Expand All @@ -2223,10 +2223,8 @@ bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFil
if (header_crc != pHeader->m_header_crc16)
return false;

const uint32 data_crc = crc16((const uint8*)pData + pHeader->m_header_size, pHeader->m_data_size - pHeader->m_header_size);
if (data_crc != pHeader->m_data_crc16)
return false;

if (pHeader->m_data_size < pHeader->m_header_size)
return false;
if ((pHeader->m_faces != 1) && (pHeader->m_faces != 6))
return false;
if ((pHeader->m_width < 1) || (pHeader->m_width > cCRNMaxLevelResolution))
Expand Down Expand Up @@ -2268,6 +2266,20 @@ bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFil
return true;
}

bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info) {
if (!crnd_validate_header(pData, data_size, pFile_info))
return false;

// was already validated by crnd_get_header
const crn_header* pHeader = static_cast<const crn_header*>(pData);

if (pHeader->m_data_size > data_size)
return false;

const uint32 data_crc = crc16((const uint8*)pData + pHeader->m_header_size, pHeader->m_data_size - pHeader->m_header_size);
return data_crc == pHeader->m_data_crc16;
}

bool crnd_get_texture_info(const void* pData, uint32 data_size, crn_texture_info* pInfo) {
if ((!pData) || (data_size < sizeof(crn_header)) || (!pInfo))
return false;
Expand Down
9 changes: 8 additions & 1 deletion inc/crn_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ uint32 crnd_get_crn_format_bits_per_texel(crn_format fmt);
// Returns the number of bytes per DXTn block (8 or 16).
uint32 crnd_get_bytes_per_dxt_block(crn_format fmt);

// Validates the entire file by checking the header and data CRC's.
// Validates the file header, performing a CRC check on it and verifying that there is
// e.g. a sane number of levels.
// pData/data_size need only include the header, but may be the whole file.
// The crn_file_info.m_struct_size field must be set before calling this function.
bool crnd_validate_header(const void* pData, uint32 data_size, crn_file_info* pFile_info);

// Validates the entire file by calling crnd_validate_header, then checking the data CRC
// for the whole file.
// This is not something you want to be doing much!
// The crn_file_info.m_struct_size field must be set before calling this function.
bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info);
Expand Down

0 comments on commit e471bf6

Please sign in to comment.