1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-24 13:20:13 +00:00

Minor code review for DDSTextureLoader

This commit is contained in:
Chuck Walbourn 2021-01-05 23:19:37 -08:00
parent be2277523f
commit ab6898cb64
3 changed files with 41 additions and 14 deletions

View File

@ -147,6 +147,8 @@ namespace
return E_POINTER;
}
*bitSize = 0;
if (ddsDataSize > UINT32_MAX)
{
return E_FAIL;
@ -179,7 +181,7 @@ namespace
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
{
return E_FAIL;
}
@ -212,6 +214,8 @@ namespace
return E_POINTER;
}
*bitSize = 0;
// open the file
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(fileName,
@ -261,19 +265,21 @@ namespace
}
// read the data in
DWORD BytesRead = 0;
DWORD bytesRead = 0;
if (!ReadFile(hFile.get(),
ddsData.get(),
fileInfo.EndOfFile.LowPart,
&BytesRead,
&bytesRead,
nullptr
))
{
ddsData.reset();
return HRESULT_FROM_WIN32(GetLastError());
}
if (BytesRead < fileInfo.EndOfFile.LowPart)
if (bytesRead < fileInfo.EndOfFile.LowPart)
{
ddsData.reset();
return E_FAIL;
}
@ -281,6 +287,7 @@ namespace
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
if (dwMagicNumber != DDS_MAGIC)
{
ddsData.reset();
return E_FAIL;
}
@ -290,6 +297,7 @@ namespace
if (hdr->size != sizeof(DDS_HEADER) ||
hdr->ddspf.size != sizeof(DDS_PIXELFORMAT))
{
ddsData.reset();
return E_FAIL;
}
@ -299,8 +307,9 @@ namespace
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
{
ddsData.reset();
return E_FAIL;
}

View File

@ -170,6 +170,8 @@ namespace
return E_POINTER;
}
*bitSize = 0;
if (ddsDataSize > UINT32_MAX)
{
return E_FAIL;
@ -202,7 +204,7 @@ namespace
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
{
return E_FAIL;
}
@ -235,6 +237,8 @@ namespace
return E_POINTER;
}
*bitSize = 0;
// open the file
ScopedHandle hFile(safe_handle(CreateFile2(fileName,
GENERIC_READ,
@ -261,7 +265,7 @@ namespace
}
// Need at least enough data to fill the header and magic number to be a valid DDS
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
{
return E_FAIL;
}
@ -274,19 +278,21 @@ namespace
}
// read the data in
DWORD BytesRead = 0;
DWORD bytesRead = 0;
if (!ReadFile(hFile.get(),
ddsData.get(),
fileInfo.EndOfFile.LowPart,
&BytesRead,
&bytesRead,
nullptr
))
{
ddsData.reset();
return HRESULT_FROM_WIN32(GetLastError());
}
if (BytesRead < fileInfo.EndOfFile.LowPart)
if (bytesRead < fileInfo.EndOfFile.LowPart)
{
ddsData.reset();
return E_FAIL;
}
@ -294,6 +300,7 @@ namespace
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
if (dwMagicNumber != DDS_MAGIC)
{
ddsData.reset();
return E_FAIL;
}
@ -303,6 +310,7 @@ namespace
if (hdr->size != sizeof(DDS_HEADER) ||
hdr->ddspf.size != sizeof(DDS_PIXELFORMAT))
{
ddsData.reset();
return E_FAIL;
}
@ -312,8 +320,9 @@ namespace
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
{
ddsData.reset();
return E_FAIL;
}

View File

@ -127,6 +127,8 @@ namespace
return E_POINTER;
}
*bitSize = 0;
if (ddsDataSize > UINT32_MAX)
{
return E_FAIL;
@ -184,6 +186,8 @@ namespace
return E_POINTER;
}
*bitSize = 0;
// open the file
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(fileName,
@ -233,19 +237,21 @@ namespace
}
// read the data in
DWORD BytesRead = 0;
DWORD bytesRead = 0;
if (!ReadFile(hFile.get(),
ddsData.get(),
fileInfo.EndOfFile.LowPart,
&BytesRead,
&bytesRead,
nullptr
))
{
ddsData.reset();
return HRESULT_FROM_WIN32(GetLastError());
}
if (BytesRead < fileInfo.EndOfFile.LowPart)
if (bytesRead < fileInfo.EndOfFile.LowPart)
{
ddsData.reset();
return E_FAIL;
}
@ -253,6 +259,7 @@ namespace
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
if (dwMagicNumber != DDS_MAGIC)
{
ddsData.reset();
return E_FAIL;
}
@ -262,6 +269,7 @@ namespace
if (hdr->size != sizeof(DDS_HEADER) ||
hdr->ddspf.size != sizeof(DDS_PIXELFORMAT))
{
ddsData.reset();
return E_FAIL;
}
@ -270,6 +278,7 @@ namespace
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
{
// We don't support the new DX10 header for Direct3D 9
ddsData.reset();
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
}