-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
261 lines (230 loc) · 6.92 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
#include <System.IOUtils.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Caption = "Ultimate Packer for eXecutables Launcher";
Dialog->Title = "Choose file to compress";
Dialog->Filter = "Execute file (*.exe)|*.exe";
Dialog->Options.Clear();
Dialog->Options << TOpenOption::ofFileMustExist << TOpenOption::ofNoChangeDir << TOpenOption::ofHideReadOnly;
rbBestNRV2D->Checked = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cmdBrowseClick(TObject *Sender)
{
if(Dialog->Execute() == true)
{
txtSelected->Text = Dialog->FileName;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cmdCompressClick(TObject *Sender)
{
try
{
EnableControls(false);
CompressFile(txtSelected->Text, chkBackup->Checked);
}
__finally
{
EnableControls(true);
}
}
//---------------------------------------------------------------------------
String __fastcall TForm1::GetCmdLine(const String APath)
{
String Result;
if(rbBestNRV2D->Checked == true)
{
Result = "--best --nrv2d --crp-ms=999999";
}
else if(rbBestNRV2B->Checked == true)
{
Result = "--best --nrv2b --crp-ms=999999";
}
else if(rbBrute->Checked == true)
{
Result = "--brute --crp-ms=999999";
}
else if(rbUltraBrute->Checked == true)
{
Result = "--ultra-brute --crp-ms=999999";
}
return "upx.exe " + Result + " \"" + APath + "\"";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CompressFile(const String APath, bool ABackup)
{
int OldSize;
int NewSize;
if(APath.IsEmpty() == true)
{
return;
}
FileSize(APath, OldSize);
// Create a backup
if(ABackup == true)
{
try
{
TFile::Copy(APath, APath + ".bak", true);
}
catch(...)
{
MessageBoxW(Handle, L"Backup failed", L"Error", MB_OK);
}
}
// Launch UPX.exe
if(FileExists(APath) == false)
{
MessageBeep(0);
MessageDlg("The program must be in the same folder as upx.exe."
"\r\n" \
"Program will not be compressed.",
TMsgDlgType::mtError, TMsgDlgButtons() << TMsgDlgBtn::mbOK, 0);
}
else
{
const String CmdLine = GetCmdLine(APath);
HANDLE LHandle = ExecuteProgramEx(CmdLine);
Wait(LHandle);
if(LHandle == NULL)
{
MessageBeep(0);
MessageDlg("Error opening upx.exe."
"\r\n" \
"Program will not be compressed.",
TMsgDlgType::mtError, TMsgDlgButtons() << TMsgDlgBtn::mbOK, 0);
}
}
FileSize(APath, NewSize);
const float ratio = (float)(100.0f * NewSize / OldSize);
MessageDlg("Original file size: " + System::Sysutils::IntToStr(OldSize) + " bytes"
"\r\n" \
"Compress file size: " + System::Sysutils::IntToStr(NewSize) + " bytes"
"\r\n" \
"Ratio: " + FormatFloat("0.00", ratio) + "%",
TMsgDlgType::mtInformation, TMsgDlgButtons() << TMsgDlgBtn::mbOK, 0);
}
//---------------------------------------------------------------------------
HANDLE __fastcall TForm1::ExecuteProgramEx(const String ACmd)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
si.cb = sizeof(si);
si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = const_cast<LPWSTR>(L"UPX");
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.cbReserved2 = 0;
si.lpReserved2 = NULL;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = NULL;
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = true;
//Pour mettre l'output dans un fichier
/*
si.hStdOutput = CreateFile(
"log.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
&sa,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
*/
bool ProcResult = CreateProcess(NULL, ACmd.c_str(), NULL, NULL, true, 0, NULL, NULL, &si, &pi);
if(ProcResult == true)
{
return pi.hProcess;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(si.hStdOutput);
return NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Wait(HANDLE AHandle)
{
DWORD ExitCode;
if(AHandle == NULL || AHandle == INVALID_HANDLE_VALUE)
{
return;
}
// In this wait procedure, we set the cursor to the hourglass.
// Then we repeat a loop: Then restore the cursor.
TCursor Oldcursor = Screen->Cursor;
Screen->Cursor = crHourGlass;
bool Done = false;
while(Done == false)
{
GetExitCodeProcess(
AHandle, // handle to the process
&ExitCode // address to receive termination status
);
if(ExitCode == STILL_ACTIVE)
{
Application->ProcessMessages();
}
else
{
Done = true;
}
/*
DWORD res = WaitForSingleObject(AHandle,50);
if(res==WAIT_TIMEOUT)
{
Application->ProcessMessages();
}
else
{
Done = true;
}
*/
}
CloseHandle(AHandle);
Screen->Cursor = Oldcursor;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EnableControls(bool AEnabled)
{
cmdCompress->Enabled = AEnabled;
Label1->Enabled = AEnabled;
txtSelected->Enabled = AEnabled;
cmdBrowse->Enabled = AEnabled;
rbBestNRV2D->Enabled = AEnabled;
rbBestNRV2B->Enabled = AEnabled;
rbBrute->Enabled = AEnabled;
rbUltraBrute->Enabled = AEnabled;
chkBackup->Enabled = AEnabled;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::FileSize(const String AName, int& ASize)
{
bool Result = false;
NativeUInt FileHandle = Sysutils::FileOpen(AName, fmOpenRead);
if(FileHandle > 0)
{
ASize = Sysutils::FileSeek(FileHandle, 0, 2);
if(ASize > 0)
{
Result = true;
}
Sysutils::FileClose(FileHandle);
}
return Result;
}
//---------------------------------------------------------------------------