Skip to content

Commit 540728d

Browse files
authored
Update PyinstallerRegister.cpp
fixed bug
1 parent f5e379b commit 540728d

File tree

1 file changed

+18
-51
lines changed

1 file changed

+18
-51
lines changed

CPP/7zip/Archive/PyInstaller/PyinstallerRegister.cpp

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@
1010
#include "../../Common/LimitedStreams.h"
1111
#include "../../Common/StreamUtils.h"
1212
#include "PyInstallerHandler.h"
13-
#include <cstdint>
13+
1414

1515
using namespace NWindows;
1616
namespace NArchive {
1717
namespace NZzz {
18-
const uint8_t pyinstallerMagic[] = { 'M', 'E', 'I', 0x0C, 0x0B, 0x0A, 0x0B, 0x0E };
1918

2019
// Define properties used by the handler
2120
static const Byte kProps[] =
2221
{
2322
kpidPath, // Path property identifier
2423
kpidSize, // Size property identifier
2524
};
26-
25+
const uint8_t pyinstallerMagic[] = { 'M', 'E', 'I', 0x0C, 0x0B, 0x0A, 0x0B, 0x0E };
2726
// CHandler class implements IInArchive and IInArchiveGetStream interfaces
2827
class CHandler : public IInArchive, public IInArchiveGetStream, public CMyUnknownImp {
2928
public:
@@ -74,29 +73,17 @@ namespace NArchive {
7473
* @param callback Callback interface for reporting progress and status.
7574
* @return HRESULT indicating success or failure.
7675
*/
77-
STDMETHODIMP CHandler::Open(IInStream* stream, const UInt64*, IArchiveOpenCallback* callback) {
76+
STDMETHODIMP CHandler::Open(IInStream* stream, const UInt64*, IArchiveOpenCallback* callback) {
7877
Close(); // Close any existing archive state
7978

8079
// Validate input parameters
8180
if (!callback || !stream) {
8281
return S_FALSE; // Invalid arguments
8382
}
8483

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-
}
9284

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);
85+
// Attempt to open the PyInstaller handler
86+
HRESULT result = pyHandler.Open(stream, nullptr, callback);
10087
if (FAILED(result)) {
10188
return result; // Return if the handler fails to open
10289
}
@@ -108,14 +95,12 @@ namespace NArchive {
10895
#ifdef _DEBUG
10996
MessageBox(NULL, msg.c_str(), L"Debug - CHandler::Open", MB_OK);
11097
#endif
111-
11298
UInt64 fileSize = 0; // Declare file size variable
11399
result = stream->Seek(0, STREAM_SEEK_END, &fileSize); // Seek to the end to get the file size
114100
if (FAILED(result) || fileSize == 0) {
115101
return S_FALSE; // Ensure the file size is valid
116102
}
117103
stream->Seek(0, STREAM_SEEK_SET, nullptr); // Seek back to the start of the stream
118-
119104
// Get the name of the file from the callback
120105
CMyComPtr<IArchiveOpenVolumeCallback> volumeCallback;
121106
result = callback->QueryInterface(IID_IArchiveOpenVolumeCallback, (void**)&volumeCallback);
@@ -238,7 +223,7 @@ namespace NArchive {
238223
}
239224

240225
RINOK(extractCallback->PrepareOperation(askMode));
241-
#ifdef _DEBUG
226+
242227
std::wstringstream debugMsg;
243228
debugMsg << L"[Debug] Extracting file index " << index << L":\n"
244229
<< L" Name: " << item.name.c_str() << L"\n"
@@ -256,21 +241,19 @@ namespace NArchive {
256241
contentMsg << byte;
257242
}
258243
MessageBox(NULL, contentMsg.str().c_str(), L"Debug Info", MB_OK);
259-
#endif
244+
260245
HRESULT writeResult = realOutStream->Write(item.decompressedData.data(), (UINT32)item.decompressedData.size(), NULL);
261246
if (writeResult != S_OK) {
262247
std::wstringstream errMsg;
263-
#ifdef _DEBUG
264248
errMsg << L"[Debug] Failed to write data for file index " << index << L". Data size: " << item.decompressedData.size();
265249
MessageBox(NULL, errMsg.str().c_str(), L"Debug Info", MB_OK);
266-
#endif
267250
return writeResult;
268251
}
269-
#ifdef _DEBUG
252+
270253
std::wstringstream successMsg;
271254
successMsg << L"[Debug] Successfully wrote data for file index " << index << L". Data size: " << item.decompressedData.size();
272255
MessageBox(NULL, successMsg.str().c_str(), L"Debug Info", MB_OK);
273-
#endif
256+
274257
realOutStream.Release();
275258
RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kOK));
276259
}
@@ -305,31 +288,15 @@ namespace NArchive {
305288
return S_OK; // Indicate success
306289
}
307290

308-
/**
309-
* @brief Registers an archive handler for .exe files containing PyInstaller archives.
310-
*
311-
* This function registers a handler that detects .exe files with a specific magic number
312-
* and processes them as PyInstaller archives. The handler is triggered when an .exe file
313-
* containing the defined magic sequence is encountered.
314-
*
315-
* @param "exe" The file extension that this handler applies to. In this case, it is for .exe files.
316-
* @param "exe" The file type this handler is associated with, which is also '.exe'.
317-
* @param 0 Flags for the handler, currently set to 0 (no flags).
318-
* @param 0xAA Unique ID for this archive handler, ensuring it's distinguishable.
319-
* @param pyinstallerMagic Array containing the magic number (byte sequence) used to identify PyInstaller archives.
320-
* @param 0 Reserved field, currently set to 0.
321-
* @param NArcInfoFlags::kStartOpen Indicates that the archive should be opened immediately when detected.
322-
* @return None.
323-
*/
291+
// Register the archive handler for .exe files
324292
REGISTER_ARC_I(
325-
"exe",
326-
"exe",
327-
0,
328-
0xAA,
329-
pyinstallerMagic,
330-
0,
331-
NArcInfoFlags::kStartOpen
332-
)
333-
293+
"exe",
294+
"exe",
295+
0,
296+
0xAA,
297+
pyinstallerMagic,
298+
0,
299+
NULL
300+
)
334301
}
335302
}

0 commit comments

Comments
 (0)