-
Notifications
You must be signed in to change notification settings - Fork 49
/
website.py
executable file
·390 lines (374 loc) · 12.5 KB
/
website.py
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python3
import tempfile
import cgi
import cgitb
import os
import string
import subprocess
import sys
# cgi tracebacks
cgitb.enable()
def print_headers(content_type: str) -> None:
print(f"Content-Type: {content_type}; charset=utf-8")
print()
sys.stdout.flush()
form = cgi.FieldStorage()
if "source" in form:
source = form["source"].value if "source" in form else ""
context = form["context"].value if "context" in form else None
if "glabel" not in source and ".global" not in source:
source = "glabel foo\n" + source
source = bytes(source, "utf-8")
script_path = os.path.join(os.path.dirname(__file__), "m2c.py")
cmd = ["python3", script_path, "/dev/stdin"]
if "debug" in form:
cmd.append("--debug")
if "void" in form:
cmd.append("--void")
if "noifs" in form:
cmd.append("--gotos-only")
if "noswitches" in form:
cmd.append("--no-switches")
if "noandor" in form:
cmd.append("--no-andor")
if "nocasts" in form:
cmd.append("--no-casts")
if "allman" in form:
cmd.append("--allman")
elif "knr" in form:
cmd.append("--knr")
if "extraswitchindent" in form:
cmd.append("--indent-switch-contents")
if "leftptr" in form:
cmd.extend(["--pointer-style", "left"])
if "zfillconstants" in form:
cmd.append("--zfill-constants")
if "globals" in form:
value = form.getfirst("globals")
if value in ("all", "used", "none"):
cmd.extend(["--globals", value])
if "visualize" in form:
cmd.append("--visualize")
if "target" in form:
value = form.getfirst("target")
if value in (
"ppc-mwcc-c",
"ppc-mwcc-c++",
"mips-ido-c",
"mips-gcc-c",
"mipsel-gcc-c",
):
cmd.extend(["--target", value])
if "nounkinference" in form:
cmd.append("--no-unk-inference")
if "stackstructs" in form:
cmd.append("--stack-structs")
comment_style = form.getfirst("comment_style", "multiline")
if "none" in comment_style:
cmd.append("--comment-style=none")
elif "oneline" in comment_style:
cmd.append("--comment-style=oneline")
else:
cmd.append("--comment-style=multiline")
if "unaligned" in comment_style:
cmd.append("--comment-column=0")
function = form["functionselect"].value if "functionselect" in form else "all"
FUNCTION_ALPHABET = string.ascii_letters + string.digits + "_"
function = "".join(c for c in function if c in FUNCTION_ALPHABET)
if function and function != "all":
cmd.extend(["--function", function])
regvars = ""
if "regvarsselect" in form:
sel = form["regvarsselect"].value
if sel in ("saved", "most", "all"):
regvars = sel
elif sel == "custom":
regvars = form.getvalue("regvars", "")
REG_ALPHABET = string.ascii_lowercase + string.digits + ","
regvars = "".join(c for c in regvars if c in REG_ALPHABET)
if regvars:
cmd.append("--reg-vars")
cmd.append(regvars)
try:
if context:
with tempfile.NamedTemporaryFile() as f:
f.write(bytes(context, "utf-8"))
f.file.close()
# There's no need to do caching on the temporary context file
cmd.extend(["--no-cache", "--context", f.name])
res = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
input=source,
timeout=15,
)
else:
res = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
input=source,
timeout=15,
)
except:
# Set the headers for the cgitb traceback
print_headers(content_type="text/html")
raise
if "visualize" in form and res.returncode == 0:
print_headers(content_type="image/svg+xml")
print(res.stdout.decode("utf-8", "replace"))
else:
print_headers(content_type="text/html")
print("<!DOCTYPE html><html>")
if "dark" in form:
print(
"""
<head>
<style>
body {
background-color: #454545;
color: #c0c0c0;
}
</style>
</head>
"""
)
print("<body><pre><plaintext>", end="")
print(res.stdout.decode("utf-8", "replace"))
elif "?go" in os.environ.get("REQUEST_URI", ""):
print_headers(content_type="text/html")
else:
print_headers(content_type="text/html")
print(
"""
<!DOCTYPE html><html>
<head>
<style>
* {
box-sizing: border-box;
}
html, body, form, .main, .sidebar {
margin: 0;
height: 100%;
}
textarea, .sidebar iframe {
width: 100%;
height: 100%;
border: 1px solid #bbb;
margin: 1px 0;
}
label {
white-space: nowrap;
}
[data-regvars]:not([data-regvars=custom]) [name=regvars] {
display: none;
}
[name=regvars] {
width: 150px;
}
.main, .sidebar {
display: flex;
flex-direction: column;
padding: 10px;
}
.sidebar {
display: none;
}
.has-sidebar form {
display: flex;
flex-direction: row;
}
.has-sidebar .main, .has-sidebar .sidebar {
flex: 1;
}
.has-sidebar .sidebar {
display: flex;
}
.dark-theme {
background-color: #2d2d2d;
color: #c0c0c0;
}
.dark-theme div,
.dark-theme body,
.dark-theme form,
.dark-theme textarea {
background-color: rgba(255, 255, 255, 0.0225);
color: #c0c0c0;
box-shadow: 1px 1px 10px 0px black;
}
</style>
</head>
<body>
<form action="?go" method="post">
<div class="main">
<div>
Assembly:
</div>
<div style="flex: 20;">
<textarea name="source" spellcheck="false"></textarea>
</div>
<div style="margin-top: 10px;">
Existing C source, preprocessed (optional):
</div>
<div style="flex: 11;">
<textarea name="context" spellcheck="false"></textarea>
</div>
<div style="margin-top: 10px;" id="options">
<input type="submit" value="Decompile">
<input type="submit" name="visualize" value="Visualize">
<label>Function: <select name="functionselect"></select></label>
</label>
<label>Global declarations:
<select name="globals">
<option value="used">used</option>
<option value="all">all</option>
<option value="none">none</option>
</select>
</label>
<label>Target arch, compiler, & language:
<select name="target">
<option value="mips-ido-c">MIPS, IDO, C</option>
<option value="mips-gcc-c">MIPS, GCC, C</option>
<option value="mipsel-gcc-c">MIPSEL, GCC, C</option>
<option value="ppc-mwcc-c++">PPC, MWCC, C++</option>
<option value="ppc-mwcc-c">PPC, MWCC, C</option>
</select>
</label>
<label>Comment style:
<select name="comment_style">
<option value="multiline">/* ... */, aligned</option>
<option value="multiline_unaligned">/* ... */, unaligned</option>
<option value="oneline">// ..., aligned</option>
<option value="oneline_unaligned">// ..., unaligned</option>
<option value="none">no comments</option>
</select>
</label>
<label>Use single var for:
<select name="regvarsselect">
<option value="none">none</option>
<option value="saved">saved regs</option>
<option value="all">all regs</option>
<option value="custom">custom</option>
</select>
</label>
<input name="regvars" value="">
<label><input type="checkbox" name="void">Force void return type</label>
<label><input type="checkbox" name="debug">Debug info</label>
<label><input type="checkbox" name="noandor">Disable &&/||</label>
<label><input type="checkbox" name="nocasts">Hide type casts</label>
<label><input type="checkbox" name="allman">Allman braces</label>
<label><input type="checkbox" name="knr">K&R braces</label>
<label><input type="checkbox" name="extraswitchindent">Indent switch contents an extra level</label>
<label><input type="checkbox" name="leftptr">* to the left</label>
<label><input type="checkbox" name="zfillconstants">0-fill constants</label>
<label><input type="checkbox" name="noifs">Use gotos for everything</label> (to use a goto for a single branch, add "# GOTO" to the asm)
<label><input type="checkbox" name="noswitches">Disable irregular switch detection</label>
<label><input type="checkbox" name="nounkinference">Disable unknown struct/type inference</label>
<label><input type="checkbox" name="stackstructs">Stack struct templates</label>
<label><input type="checkbox" name="usesidebar">Output sidebar</label>
<label><input type="checkbox" name="dark">Dark mode</label>
</div>
</div>
<div class="sidebar">
<div>
Output:
</div>
<div style="flex: 1;">
<iframe src="about:blank" name="outputframe"></iframe>
</div>
</div>
<script>
var formEl = document.getElementsByTagName("form")[0]
var sourceEl = document.getElementsByName("source")[0];
var contextEl = document.getElementsByName("context")[0];
var sidebarCheckboxEl = document.getElementsByName("usesidebar")[0];
var savedSource = localStorage.mips_to_c_saved_source;
var savedContext = localStorage.mips_to_c_saved_context;
var savedOptions = localStorage.mips_to_c_saved_options;
if (savedSource) sourceEl.value = savedSource;
if (savedContext) contextEl.value = savedContext;
if (savedOptions) {
savedOptions = JSON.parse(savedOptions);
for (var key in savedOptions) {
var el = document.getElementsByName(key)[0], val = savedOptions[key];
if (el) {
if (el.type === "checkbox")
el.checked = val === "yes";
else
el.value = val;
}
}
}
var darkModeCheckbox = document.getElementsByName("dark")[0];
if (!savedOptions || !("dark" in savedOptions)) {
darkModeCheckbox.checked = window.matchMedia("prefers-color-scheme: dark").matches;
}
function updateDarkMode() {
document.documentElement.className = darkModeCheckbox.checked ? "dark-theme" : "";
}
updateDarkMode();
darkModeCheckbox.addEventListener("change", updateDarkMode);
var functionSelect = document.getElementsByName("functionselect")[0];
function updateFunctions() {
var prevValue = functionSelect.value;
functionSelect.innerHTML = "<option value='all'>all functions</option>";
for (let match of sourceEl.value.matchAll(/^\\s*glabel\\s+([A-Za-z0-9_]+)/mg)) {
var name = match[1];
var option = document.createElement("option");
option.value = name;
option.innerText = name;
option.selected = (name == prevValue);
functionSelect.appendChild(option);
}
}
updateFunctions();
functionSelect.style.width = getComputedStyle(functionSelect).width;
sourceEl.addEventListener("blur", updateFunctions);
var regVarsSelect = document.getElementsByName("regvarsselect")[0];
function updateRegVars(e) {
document.body.setAttribute("data-regvars", regVarsSelect.value);
if (regVarsSelect.value === "custom" && e) {
document.getElementsByName("regvars")[0].value = "s0,s1,s2";
}
}
updateRegVars();
regVarsSelect.addEventListener("change", updateRegVars);
sourceEl.addEventListener("change", function() {
localStorage.mips_to_c_saved_source = sourceEl.value;
});
contextEl.addEventListener("change", function() {
localStorage.mips_to_c_saved_context = contextEl.value;
});
document.getElementById("options").addEventListener("change", function(event) {
var shouldSave = ["usesidebar", "allman", "knr", "extraswitchindent", "leftptr", "zfillconstants", "globals", "nocasts", "noandor", "noifs", "noswitches", "dark", "regvarsselect", "regvars", "comment_style", "target", "nounkinference", "stackstructs"];
var options = {};
for (var key of shouldSave) {
var el = document.getElementsByName(key)[0];
options[key] = (el.type === "checkbox" ?
(el.checked ? "yes" : "no") :
el.value);
}
localStorage.mips_to_c_saved_options = JSON.stringify(options);
});
function updateSidebar() {
document.body.classList.toggle("has-sidebar", sidebarCheckboxEl.checked);
formEl.setAttribute("target", sidebarCheckboxEl.checked ? "outputframe" : "");
try {
document.getElementsByName("outputframe")[0].contentDocument.body.innerHTML = "";
} catch (e) {}
}
updateSidebar();
sidebarCheckboxEl.addEventListener("change", updateSidebar);
formEl.addEventListener("submit", function() {
if (!sidebarCheckboxEl.checked) return;
try {
document.getElementsByName("outputframe")[0].contentDocument.body.innerHTML = "Decompiling...";
} catch (e) {}
});
</script>
</form>
</body>
</html>
"""
)