forked from farbrausch/fr_public
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
184 lines (158 loc) · 4.56 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
// Written by Fabian "ryg" Giesen.
// I hereby place this code in the public domain.
#include "_types.hpp"
#include "_startconsole.hpp"
#include "exepacker.hpp"
#include "debuginfo.hpp"
#include "mapfile.hpp"
#include "pdbfile.hpp"
/****************************************************************************/
void packerCallback(sU32 srcPos,sU32 srcSize,sU32 dstPos)
{
static sInt pos,startTime;
sInt nPos,tdelta;
if(!srcPos)
{
pos = 0;
sSystem->PrintF(" - packing [");
startTime = sSystem->GetTime();
}
else
{
for(nPos=srcPos*32/srcSize;pos<nPos;pos++)
sSystem->PrintF("#");
if(srcPos == srcSize)
{
tdelta = sSystem->GetTime() - startTime;
sSystem->PrintF("] => %d bytes (in %d.%02ds)\n",dstPos,tdelta/1000,(tdelta/10)%100);
}
}
}
sBool loadDebugInfo(sChar *fileName,DebugInfo &info)
{
MAPFileReader mapreader;
PDBFileReader pdbreader;
// go through supported file types in order of preference
if(pdbreader.ReadDebugInfo(fileName,info)) // .pdb file
{
sSystem->PrintF(" - program database successfully loaded\n");
return sTRUE;
}
else if(mapreader.ReadDebugInfo(fileName,info)) // .map file
{
sSystem->PrintF(" - .map file successfully loaded\n");
return sTRUE;
}
else // no match
{
sSystem->PrintF(" - no symbol info present\n");
return sFALSE;
}
}
sInt sAppMain(sInt argc,sChar **argv)
{
sU8 *in,*out,*mapf;
sChar *p,*fileName,*outFileName,fileBuf[260],*warn;
sInt i,packMode,inSize,outSize,refSize;
sBool gotMap;
EXEPacker pack;
DebugInfo info;
sSystem->PrintF(sAPPNAME " " sVERSION " >> radical exe packer (c) f. giesen and contributors 2003-2013\n\n");
// parser command line
fileName = 0;
outFileName = 0;
packMode = 0; // default to good
refSize = 0;
for(i=1;i<argc;i++)
{
if(!sCmpString(argv[i],"--good"))
packMode = 0;
else if(!sCmpString(argv[i],"--best"))
packMode = 1;
else if(!sCmpString(argv[i],"--new"))
packMode = 2;
else if(!sCmpString(argv[i],"--out") && i+1<argc)
outFileName = argv[++i];
else if(!sCmpString(argv[i],"--refsize") && i+1<argc)
{
p = argv[++i];
refSize = sScanInt(p);
}
else
fileName = argv[i];
}
if(!outFileName)
outFileName = fileName;
info.Init();
if(!fileName)
{
sSystem->PrintF("Syntax: " sAPPNAME " <options> file.exe\n\n"
"Options:\n"
"--good Use good packer frontend (default)\n"
"--best Use best packer frontend (notably slower)\n"
"--new Use new packer frontend (experimental)\n"
"--refsize <size> Reference size in kilobytes\n"
"--out <filename> Write output to <filename>\n");
return 1;
}
else
{
in = sSystem->LoadFile(fileName,inSize);
if(!in)
{
sSystem->PrintF("\a - ERROR: couldn't open input file!\n");
return 1;
}
gotMap = loadDebugInfo(fileName,info);
info.FinishedReading();
sSystem->PrintF(" - preprocessing, filtering & reslicing\n");
out = pack.Pack(in,outSize,gotMap ? &info : 0,packerCallback,0);
if(out)
{
warn = pack.GetInfoMessages();
while(*warn)
{
sSystem->PrintF(" - %s\n",warn);
warn += sGetStringLen(warn)+1;
}
warn = pack.GetWarningMessages();
while(*warn)
{
sSystem->PrintF(" - WARNING: %s\n",warn);
warn += sGetStringLen(warn)+1;
}
sSystem->PrintF(" - writing output file %s\n",outFileName);
if(!sSystem->SaveFile(outFileName,out,outSize))
{
sSystem->PrintF("\a - ERROR: couldn't write output file!\n");
return 1;
}
sSystem->PrintF(" - packed executable %d -> %d bytes\n",inSize,outSize);
delete[] out;
if(gotMap)
{
sCopyString(fileBuf,fileName,260);
for(i=sGetStringLen(fileBuf)-1;i>=0 && fileBuf[i]!='.';i--);
if(i>0)
sCopyString(fileBuf+i,".kkm",260-i);
else
sAppendString(fileBuf,".kkm",260);
sSystem->PrintF(" - writing pack ratio analysis %s\n",fileBuf);
mapf = (sU8 *) info.WriteReport();
sSystem->SaveFile(fileBuf,mapf,sGetStringLen((sChar *) mapf));
delete[] mapf;
}
if(refSize)
{
i = pack.GetActualSize() - refSize * 1024;
sSystem->PrintF(" - delta to reference size: %c%d bytes\n",
(i<0)?'-':'+',sAbs(i));
}
}
else
sSystem->PrintF("\a - ERROR: %s\n",pack.GetErrorMessage());
delete[] in;
}
info.Exit();
return 0;
}