forked from decompiler-explorer/decompiler-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompile_retdec.py
65 lines (53 loc) · 2.14 KB
/
decompile_retdec.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
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
RETDEC_INSTALL = Path(os.getenv("RETDEC_INSTALL_PATH", "/home/decompiler_user/retdec/bin"))
RETDEC_DECOMPILER = RETDEC_INSTALL / 'retdec-decompiler'
def main():
with tempfile.TemporaryDirectory() as tempdir:
conts = sys.stdin.buffer.read()
infile = tempfile.NamedTemporaryFile(dir=tempdir, delete=False)
infile.write(conts)
infile.flush()
outfile = tempfile.NamedTemporaryFile(dir=tempdir, delete=False)
outfile.close()
decomp = subprocess.run([RETDEC_DECOMPILER, '--output', outfile.name, '--cleanup', '--silent', infile.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if decomp.returncode != 0:
print(f'{decomp.stdout.decode()}\n{decomp.stderr.decode()}')
sys.exit(1)
infile.close()
with open(outfile.name, 'rb') as f:
sys.stdout.buffer.write(f.read())
def version():
proc = subprocess.run([RETDEC_DECOMPILER, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# RetDec version : v4.0-415-g05c9b113
# Commit hash : 05c9b11351d3e82012d823fa3709f940033768cf
# Build date : 2022-04-13T20:37:02Z
output = proc.stdout.decode()
lines = output.split('\n')
version_lines = [l for l in lines if l.startswith('RetDec version : ')]
commit_lines = [l for l in lines if l.startswith('Commit hash : ')]
assert len(version_lines) == 1
assert len(commit_lines) == 1
version = version_lines[0][18:]
assert version[0] == 'v'
version = version[1:]
# strip second hyphen and beyond cause we don't care
version = '-'.join(version.split('-')[:2])
revision = commit_lines[0][18:26] # 8 chars is enough
print(version)
print(revision)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--name':
print('RetDec')
sys.exit(0)
if len(sys.argv) > 1 and sys.argv[1] == '--url':
print('https://github.com/avast/retdec')
sys.exit(0)
if len(sys.argv) > 1 and sys.argv[1] == '--version':
version()
sys.exit(0)
main()