1515using namespace NWindows ;
1616namespace NArchive {
1717 namespace NZzz {
18+ const uint8_t pyinstallerMagic[] = { ' M' , ' E' , ' I' , 0x0C , 0x0B , 0x0A , 0x0B , 0x0E };
1819
1920 // Define properties used by the handler
2021 static const Byte kProps [] =
@@ -73,16 +74,29 @@ namespace NArchive {
7374 * @param callback Callback interface for reporting progress and status.
7475 * @return HRESULT indicating success or failure.
7576 */
76- STDMETHODIMP CHandler::Open (IInStream* stream, const UInt64*, IArchiveOpenCallback* callback) {
77+ STDMETHODIMP CHandler::Open (IInStream* stream, const UInt64*, IArchiveOpenCallback* callback) {
7778 Close (); // Close any existing archive state
7879
7980 // Validate input parameters
8081 if (!callback || !stream) {
8182 return S_FALSE; // Invalid arguments
8283 }
8384
84- // Attempt to open the PyInstaller handler
85- HRESULT result = pyHandler.Open (stream, nullptr , callback);
85+ // Read the first few bytes to check for PyInstaller signature
86+ const size_t magicSize = sizeof (pyinstallerMagic);
87+ char magic[magicSize];
88+ HRESULT result = stream->Read (magic, magicSize, nullptr );
89+ if (FAILED (result)) {
90+ return result; // Return if the magic signature cannot be read
91+ }
92+
93+ // Check if the magic signature matches PyInstaller
94+ if (memcmp (magic, pyinstallerMagic, magicSize) != 0 ) {
95+ return S_FALSE; // If signature doesn't match, return S_FALSE
96+ }
97+
98+ // Proceed with opening the PyInstaller handler
99+ result = pyHandler.Open (stream, nullptr , callback);
86100 if (FAILED (result)) {
87101 return result; // Return if the handler fails to open
88102 }
@@ -94,12 +108,14 @@ namespace NArchive {
94108#ifdef _DEBUG
95109 MessageBox (NULL , msg.c_str (), L" Debug - CHandler::Open" , MB_OK);
96110#endif
111+
97112 UInt64 fileSize = 0 ; // Declare file size variable
98113 result = stream->Seek (0 , STREAM_SEEK_END, &fileSize); // Seek to the end to get the file size
99114 if (FAILED (result) || fileSize == 0 ) {
100115 return S_FALSE; // Ensure the file size is valid
101116 }
102117 stream->Seek (0 , STREAM_SEEK_SET, nullptr ); // Seek back to the start of the stream
118+
103119 // Get the name of the file from the callback
104120 CMyComPtr<IArchiveOpenVolumeCallback> volumeCallback;
105121 result = callback->QueryInterface (IID_IArchiveOpenVolumeCallback, (void **)&volumeCallback);
@@ -289,7 +305,6 @@ namespace NArchive {
289305 return S_OK; // Indicate success
290306 }
291307
292- const uint8_t pyinstallerMagic[] = { ' M' , ' E' , ' I' , 0x0C , 0x0B , 0x0A , 0x0B , 0x0E };
293308 /* *
294309 * @brief Registers an archive handler for .exe files containing PyInstaller archives.
295310 *
@@ -317,4 +332,4 @@ namespace NArchive {
317332 )
318333
319334 }
320- }
335+ }
0 commit comments