Fix TGA reader regression for TGA files smaller than 26 bytes

This commit is contained in:
Chuck Walbourn 2021-03-26 21:43:56 -07:00
parent 77f1a6fc86
commit eda513ceba

View File

@ -1424,11 +1424,8 @@ HRESULT DirectX::GetMetadataFromTGAFile(const wchar_t* szFile, TGA_FLAGS flags,
TGA_FOOTER footer = {}; TGA_FOOTER footer = {};
#ifdef WIN32 #ifdef WIN32
if (SetFilePointer(hFile.get(), -static_cast<int>(sizeof(TGA_FOOTER)), nullptr, FILE_END) == INVALID_SET_FILE_POINTER) if (SetFilePointer(hFile.get(), -static_cast<int>(sizeof(TGA_FOOTER)), nullptr, FILE_END) != INVALID_SET_FILE_POINTER)
{ {
return HRESULT_FROM_WIN32(GetLastError());
}
if (!ReadFile(hFile.get(), &footer, sizeof(TGA_FOOTER), &bytesRead, nullptr)) if (!ReadFile(hFile.get(), &footer, sizeof(TGA_FOOTER), &bytesRead, nullptr))
{ {
return HRESULT_FROM_WIN32(GetLastError()); return HRESULT_FROM_WIN32(GetLastError());
@ -1438,14 +1435,15 @@ HRESULT DirectX::GetMetadataFromTGAFile(const wchar_t* szFile, TGA_FLAGS flags,
{ {
return E_FAIL; return E_FAIL;
} }
}
#else #else
inFile.seekg(-static_cast<int>(sizeof(TGA_FOOTER)), std::ios::end); inFile.seekg(-static_cast<int>(sizeof(TGA_FOOTER)), std::ios::end);
if (!inFile) if (inFile)
return E_FAIL; {
inFile.read(reinterpret_cast<char*>(&footer), sizeof(TGA_FOOTER)); inFile.read(reinterpret_cast<char*>(&footer), sizeof(TGA_FOOTER));
if (!inFile) if (!inFile)
return E_FAIL; return E_FAIL;
}
#endif #endif
if (memcmp(footer.Signature, g_Signature, sizeof(g_Signature)) == 0) if (memcmp(footer.Signature, g_Signature, sizeof(g_Signature)) == 0)
@ -1980,11 +1978,8 @@ HRESULT DirectX::LoadFromTGAFile(
TGA_FOOTER footer = {}; TGA_FOOTER footer = {};
#ifdef WIN32 #ifdef WIN32
if (SetFilePointer(hFile.get(), -static_cast<int>(sizeof(TGA_FOOTER)), nullptr, FILE_END) == INVALID_SET_FILE_POINTER) if (SetFilePointer(hFile.get(), -static_cast<int>(sizeof(TGA_FOOTER)), nullptr, FILE_END) != INVALID_SET_FILE_POINTER)
{ {
return HRESULT_FROM_WIN32(GetLastError());
}
if (!ReadFile(hFile.get(), &footer, sizeof(TGA_FOOTER), &bytesRead, nullptr)) if (!ReadFile(hFile.get(), &footer, sizeof(TGA_FOOTER), &bytesRead, nullptr))
{ {
image.Release(); image.Release();
@ -1996,20 +1991,18 @@ HRESULT DirectX::LoadFromTGAFile(
image.Release(); image.Release();
return E_FAIL; return E_FAIL;
} }
}
#else // !WIN32 #else // !WIN32
inFile.seekg(-static_cast<int>(sizeof(TGA_FOOTER)), std::ios::end); inFile.seekg(-static_cast<int>(sizeof(TGA_FOOTER)), std::ios::end);
if (!inFile) if (inFile)
{ {
image.Release();
return E_FAIL;
}
inFile.read(reinterpret_cast<char*>(&footer), sizeof(TGA_FOOTER)); inFile.read(reinterpret_cast<char*>(&footer), sizeof(TGA_FOOTER));
if (!inFile) if (!inFile)
{ {
image.Release(); image.Release();
return E_FAIL; return E_FAIL;
} }
}
#endif #endif
if (memcmp(footer.Signature, g_Signature, sizeof(g_Signature)) == 0) if (memcmp(footer.Signature, g_Signature, sizeof(g_Signature)) == 0)
@ -2310,7 +2303,7 @@ HRESULT DirectX::SaveToTGAFile(
extOffset = SetFilePointer(hFile.get(), 0, nullptr, FILE_CURRENT); extOffset = SetFilePointer(hFile.get(), 0, nullptr, FILE_CURRENT);
if (extOffset == INVALID_SET_FILE_POINTER) if (extOffset == INVALID_SET_FILE_POINTER)
{ {
return HRESULT_FROM_WIN32(GetLastError()); return E_FAIL;
} }
if (!WriteFile(hFile.get(), &ext, sizeof(TGA_EXTENSION), &bytesWritten, nullptr)) if (!WriteFile(hFile.get(), &ext, sizeof(TGA_EXTENSION), &bytesWritten, nullptr))
@ -2322,6 +2315,8 @@ HRESULT DirectX::SaveToTGAFile(
return E_FAIL; return E_FAIL;
#else #else
extOffset = static_cast<uint32_t>(outFile.tellp()); extOffset = static_cast<uint32_t>(outFile.tellp());
if (!outFile)
return E_FAIL;
outFile.write(reinterpret_cast<char*>(&ext), sizeof(TGA_EXTENSION)); outFile.write(reinterpret_cast<char*>(&ext), sizeof(TGA_EXTENSION));
if (!outFile) if (!outFile)