-
Notifications
You must be signed in to change notification settings - Fork 632
/
Copy pathmain.cpp
164 lines (145 loc) · 4.87 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
#include <library/cpp/resource/registry.h>
#include <util/stream/file.h>
#include <util/folder/path.h>
using namespace NResource;
class TAsmWriter {
private:
IOutputStream& AsmOut;
TString AsmPrefix;
public:
TAsmWriter(IOutputStream& out, const TString& prefix) : AsmOut(out), AsmPrefix(prefix)
{
}
void Write(TStringBuf filename, const TString& data, bool raw) {
TString constname(Basename(filename));
if (constname.rfind('.') != TStringBuf::npos) {
constname = constname.substr(0, constname.rfind('.'));
}
WriteHeader(constname);
if (raw) {
WriteRaw(constname, data);
} else {
WriteIncBin(constname, filename, data);
}
WriteFooter(constname);
WriteSymbolSize(constname);
}
private:
void WriteHeader(const TStringBuf& constname) {
AsmOut << "global " << AsmPrefix << constname << "\n";
AsmOut << "global " << AsmPrefix << constname << "Size\n";
AsmOut << "SECTION .rodata\n";
}
void WriteFooter(TStringBuf constname) {
AsmOut << AsmPrefix << constname << "Size:\n";
AsmOut << "dd " << AsmPrefix << constname << ".end - " << AsmPrefix << constname << "\n";
}
void WriteSymbolSize(TStringBuf constname) {
AsmOut << "%ifidn __OUTPUT_FORMAT__,elf64\n";
AsmOut << "size " << AsmPrefix << constname << " " << AsmPrefix << constname << ".end - " << AsmPrefix << constname << "\n";
AsmOut << "size " << AsmPrefix << constname << "Size 4\n";
AsmOut << "%endif\n";
}
void WriteIncBin(TStringBuf constname, TStringBuf filename, const TString& data) {
AsmOut << AsmPrefix << constname << ":\nincbin \"" << Basename(filename) << "\"\n";
AsmOut << ".end:\n";
TFixedBufferFileOutput out(filename.data());
out << data;
}
void WriteRaw(TStringBuf constname, const TString& data) {
AsmOut << AsmPrefix << constname << ":\ndb ";
for (size_t i = 0; i < data.size() - 1; i++) {
unsigned char c = static_cast<unsigned char>(data[i]);
AsmOut << IntToString<10, unsigned char>(c) << ",";
}
AsmOut << IntToString<10, unsigned char>(static_cast<unsigned char>(data[data.size() - 1])) << "\n";
AsmOut << ".end:\n";
}
TString Basename(TStringBuf origin) {
TString result(origin);
if (result.rfind('/') != TString::npos) {
result = result.substr(result.rfind('/') + 1);
} else if (result.rfind('\\') != TString::npos) {
result = result.substr(result.rfind('\\') + 1);
}
return result;
}
};
static TString CompressPath(const TVector<TStringBuf>& replacements, TStringBuf in) {
for (auto r : replacements) {
TStringBuf from, to;
r.Split('=', from, to);
if (in.StartsWith(from)) {
return Compress(TString(to) + in.SubStr(from.size()));
}
}
return Compress(in);
}
int main(int argc, char** argv) {
int ind = 0;
if (argc < 4) {
Cerr << "usage: " << argv[ind] << "asm_output --prefix? [-? origin_resource ro_resource]+" << Endl;
return 1;
}
TVector<TStringBuf> replacements;
ind++;
if (TStringBuf(argv[ind]) == "--compress-only") {
ind++;
while (ind + 1 < argc) {
TUnbufferedFileInput inp(argv[ind]);
TString data = inp.ReadAll();
TString compressed = Compress(TStringBuf(data.data(), data.size()));
TFixedBufferFileOutput out(argv[ind+1]);
out << compressed;
ind += 2;
}
return 0;
}
TFixedBufferFileOutput asmout(argv[ind]);
ind++;
TString prefix;
if (TStringBuf(argv[ind]) == "--prefix") {
prefix = "_";
ind++;
}
else {
prefix = "";
}
while (TStringBuf(argv[ind]).StartsWith("--replace=")) {
replacements.push_back(TStringBuf(argv[ind]).SubStr(TStringBuf("--replace=").size()));
ind++;
}
TAsmWriter aw(asmout, prefix);
bool raw;
bool error = false;
while (ind < argc) {
TString compressed;
if ("-"sv == argv[ind]) {
ind++;
if (ind >= argc) {
error = true;
break;
}
compressed = CompressPath(replacements, TStringBuf(argv[ind]));
raw = true;
}
else {
TUnbufferedFileInput inp(argv[ind]);
TString data = inp.ReadAll();
compressed = Compress(TStringBuf(data.data(), data.size()));
raw = false;
}
ind++;
if (ind >= argc) {
error = true;
break;
}
aw.Write(argv[ind], compressed, raw);
ind++;
}
if (error) {
Cerr << "Incorrect number of parameters at argument " << ind - 1 << argv[ind-1] << Endl;
return 1;
}
return 0;
}