55#include < vector>
66#include < algorithm>
77#include < array>
8- #include < fstream>
9-
108#include " RenderEngine.h"
119#define CURRENT_MODULE " RenderEngine"
1210#include " QuickViewETW.h"
@@ -224,21 +222,27 @@ bool BuildSrgbProfileBytes(std::vector<uint8_t> *outBytes) {
224222 }
225223 profilePath.resize (wcsnlen (profilePath.c_str (), pathLen));
226224
227- std::ifstream stream (profilePath, std::ios::binary );
228- if (!stream ) {
225+ HANDLE hFile = CreateFileW (profilePath. c_str (), GENERIC_READ , FILE_SHARE_READ , nullptr , OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , nullptr );
226+ if (hFile == INVALID_HANDLE_VALUE ) {
229227 return false ;
230228 }
231229
232- stream. seekg ( 0 , std::ios::end) ;
233- const std::streamoff size = stream. tellg ();
234- if (size <= 0 ) {
230+ LARGE_INTEGER fileSize ;
231+ if (! GetFileSizeEx (hFile, &fileSize) || fileSize. QuadPart <= 0 ) {
232+ CloseHandle (hFile);
235233 return false ;
236234 }
237235
238- outBytes->resize (static_cast <size_t >(size));
239- stream.seekg (0 , std::ios::beg);
240- stream.read (reinterpret_cast <char *>(outBytes->data ()), size);
241- return stream.good () || stream.eof ();
236+ outBytes->resize (static_cast <size_t >(fileSize.QuadPart ));
237+ DWORD bytesRead = 0 ;
238+ BOOL success = ReadFile (hFile, outBytes->data (), static_cast <DWORD >(fileSize.QuadPart ), &bytesRead, nullptr );
239+ CloseHandle (hFile);
240+
241+ if (!success || bytesRead != fileSize.QuadPart ) {
242+ outBytes->clear ();
243+ return false ;
244+ }
245+ return true ;
242246}
243247
244248bool TryLoadProfileBytesForPrimaries (QuickView::ColorPrimaries primaries,
@@ -1536,14 +1540,21 @@ bool ResolveTargetProfileBlob(const CRenderEngine::GamutWarningAnalysisOptions&
15361540
15371541 if (options.targetKind == CRenderEngine::GamutTargetKind::ProofTarget &&
15381542 options.enableSoftProofing && !options.softProofProfilePath .empty ()) {
1539- std::ifstream stream (options.softProofProfilePath , std::ios::binary);
1540- if (!stream) return false ;
1541- stream.seekg (0 , std::ios::end);
1542- const std::streamoff size = stream.tellg ();
1543- if (size <= 0 ) return false ;
1544- outBlob->bytes .resize (static_cast <size_t >(size));
1545- stream.seekg (0 , std::ios::beg);
1546- stream.read (reinterpret_cast <char *>(outBlob->bytes .data ()), size);
1543+ HANDLE hFile = CreateFileW (options.softProofProfilePath .c_str (), GENERIC_READ , FILE_SHARE_READ , nullptr , OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , nullptr );
1544+ if (hFile == INVALID_HANDLE_VALUE ) return false ;
1545+ LARGE_INTEGER fileSize;
1546+ if (!GetFileSizeEx (hFile, &fileSize) || fileSize.QuadPart <= 0 ) {
1547+ CloseHandle (hFile);
1548+ return false ;
1549+ }
1550+ outBlob->bytes .resize (static_cast <size_t >(fileSize.QuadPart ));
1551+ DWORD bytesRead = 0 ;
1552+ BOOL success = ReadFile (hFile, outBlob->bytes .data (), static_cast <DWORD >(fileSize.QuadPart ), &bytesRead, nullptr );
1553+ CloseHandle (hFile);
1554+ if (!success || bytesRead != fileSize.QuadPart ) {
1555+ outBlob->bytes .clear ();
1556+ return false ;
1557+ }
15471558 outBlob->name = options.softProofProfilePath .substr (
15481559 options.softProofProfilePath .find_last_of (L" \\ /" ) == std::wstring::npos
15491560 ? 0
@@ -1560,19 +1571,25 @@ bool ResolveTargetProfileBlob(const CRenderEngine::GamutWarningAnalysisOptions&
15601571 TryGetMonitorProfilePath (options.displayState , &monitorProfilePath) &&
15611572 !monitorProfilePath.empty () &&
15621573 !IsGenericSrgbProfilePath (monitorProfilePath)) {
1563- std::ifstream stream (monitorProfilePath, std::ios::binary);
1564- if (stream) {
1565- stream.seekg (0 , std::ios::end);
1566- const std::streamoff size = stream.tellg ();
1567- if (size > 0 ) {
1568- outBlob->bytes .resize (static_cast <size_t >(size));
1569- stream.seekg (0 , std::ios::beg);
1570- stream.read (reinterpret_cast <char *>(outBlob->bytes .data ()), size);
1571- outBlob->name = monitorProfilePath.substr (
1572- monitorProfilePath.find_last_of (L" \\ /" ) == std::wstring::npos
1573- ? 0
1574- : monitorProfilePath.find_last_of (L" \\ /" ) + 1 );
1575- return !outBlob->bytes .empty ();
1574+ HANDLE hFile = CreateFileW (monitorProfilePath.c_str (), GENERIC_READ , FILE_SHARE_READ , nullptr , OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , nullptr );
1575+ if (hFile != INVALID_HANDLE_VALUE ) {
1576+ LARGE_INTEGER fileSize;
1577+ if (GetFileSizeEx (hFile, &fileSize) && fileSize.QuadPart > 0 ) {
1578+ outBlob->bytes .resize (static_cast <size_t >(fileSize.QuadPart ));
1579+ DWORD bytesRead = 0 ;
1580+ BOOL success = ReadFile (hFile, outBlob->bytes .data (), static_cast <DWORD >(fileSize.QuadPart ), &bytesRead, nullptr );
1581+ CloseHandle (hFile);
1582+ if (success && bytesRead == fileSize.QuadPart ) {
1583+ outBlob->name = monitorProfilePath.substr (
1584+ monitorProfilePath.find_last_of (L" \\ /" ) == std::wstring::npos
1585+ ? 0
1586+ : monitorProfilePath.find_last_of (L" \\ /" ) + 1 );
1587+ return !outBlob->bytes .empty ();
1588+ } else {
1589+ outBlob->bytes .clear ();
1590+ }
1591+ } else {
1592+ CloseHandle (hFile);
15761593 }
15771594 }
15781595 }
0 commit comments