int CPacker::CompressData()
SIZE_T CompressedBufferSize;
PBYTE InputBuffer = NULL;
HANDLE InputFile = INVALID_HANDLE_VALUE;
HANDLE CompressedFile = INVALID_HANDLE_VALUE;
BOOL DeleteTargetFile = TRUE;
InputBuffer = (PBYTE)m_LoadedImage.MappedAddress + m_LoadedImage.Sections[0].PointerToRawData;
for (ULONG i = 0; i < m_LoadedImage.NumberOfSections; i++) {
InputFileSize += m_LoadedImage.Sections[i].SizeOfRawData;
m_DataSize = InputFileSize;
Success = CreateCompressor(
printf("Cannot create a compressor %d.\n", GetLastError());
m_Compressor, // Compressor Handle
InputBuffer, // Input buffer, Uncompressed data
InputFileSize, // Uncompressed data size
NULL, // Compressed Buffer
0, // Compressed Buffer size
&CompressedBufferSize); // Compressed Data size
DWORD ErrorCode = GetLastError();
if (ErrorCode != ERROR_INSUFFICIENT_BUFFER) {
printf("Cannot compress data: %d.\n", ErrorCode);
m_CompressedBuffer = (PBYTE)malloc(CompressedBufferSize);
if (!m_CompressedBuffer) {
printf("Cannot allocate memory for compressed buffer.\n");
m_Compressor, // Compressor Handle
InputBuffer, // Input buffer, Uncompressed data
InputFileSize, // Uncompressed data size
m_CompressedBuffer, // Compressed Buffer
CompressedBufferSize, // Compressed Buffer size
&m_CompressedDataSize); // Compressed Data size
printf("Cannot compress data: %d\n", GetLastError());
printf("Input file size: %d; Compressed Size: %d\n",
DeleteTargetFile = FALSE;
InputBuffer = m_LoadedImage.MappedAddress;
InputFileSize = m_LoadedImage.FileHeader->OptionalHeader.SizeOfHeaders;
m_PeHeaderDataSize = InputFileSize;
m_Compressor, // Compressor Handle
InputBuffer, // Input buffer, Uncompressed data
InputFileSize, // Uncompressed data size
NULL, // Compressed Buffer
0, // Compressed Buffer size
&m_PeHeaderComDataSize); // Compressed Data size
DWORD ErrorCode = GetLastError();
if (ErrorCode != ERROR_INSUFFICIENT_BUFFER) {
printf("Cannot compress data: %d.\n", ErrorCode);
m_PeHeaderBuffer = (PBYTE)malloc(m_PeHeaderComDataSize);
printf("Cannot allocate memory for compressed buffer.\n");
m_Compressor, // Compressor Handle
InputBuffer, // Input buffer, Uncompressed data
InputFileSize, // Uncompressed data size
m_PeHeaderBuffer, // Compressed Buffer
m_PeHeaderDataSize, // Compressed Buffer size
&m_PeHeaderComDataSize); // Compressed Data size
printf("Cannot compress data: %d\n", GetLastError());
printf("Input file size: %d; Compressed Size: %d\n",
DeleteTargetFile = FALSE;
if (m_Compressor != NULL) {
CloseCompressor(m_Compressor);
if (m_CompressedBuffer) {
free(m_CompressedBuffer);