-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathiProgDecompiler.py
More file actions
126 lines (107 loc) · 5.03 KB
/
iProgDecompiler.py
File metadata and controls
126 lines (107 loc) · 5.03 KB
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
import argparse
import os
import sys
from decode import Decoder
from ipr import IPR
from cal import CAL
def decompile_ipr(ipr_filename, extra):
ipr = IPR(ipr_filename)
ipr.decompile(extra)
with open(os.path.splitext(ipr_filename)[0] + '.lst', 'w', encoding='cp1251') as f:
f.write('\n'.join(ipr.get_lst()))
with open(os.path.splitext(ipr_filename)[0] + '_draft.blr', 'w', encoding='cp1251') as f:
f.write('\n'.join(ipr.get_draft()))
if ipr.cypher:
decrypted_ipr = ipr.get_ipr()
if decrypted_ipr:
with open(os.path.splitext(ipr_filename)[0] + '_decrypted.ipr', 'wb') as f:
f.write(decrypted_ipr)
def decompile_cal(cal_filename, extra):
cal = CAL(cal_filename)
cal.decompile(extra)
cal_lst = cal.get_lst()
if cal_lst:
with open(os.path.splitext(cal_filename)[0] + '.lst', 'w', encoding='cp1251') as f:
f.write('\n'.join(cal_lst))
decrypted_cal = cal.get_data()
if decrypted_cal:
# with open(os.path.splitext(cal_filename)[0] + '_decrypted.bin', 'wb') as f:
# f.write(decrypted_cal)
sn = extra.get('newsn')
if sn is not None:
with open(f'{os.path.splitext(cal_filename)[0]}_{sn:05}.cal', 'w') as f:
f.write(Decoder.encode_cal_bytecode(decrypted_cal, sn))
def decompile(source_filename, args):
if os.path.isfile(source_filename):
file_ext = os.path.splitext(source_filename)[1]
if file_ext == '.ipr':
extra = {
'eph': args.eph,
'epd': args.epd,
}
decompile_ipr(source_filename, extra)
elif file_ext == '.cal':
extra = {
'eph': args.eph,
'newsn': args.newsn,
}
decompile_cal(source_filename, extra)
else:
print('No such file')
def get_args():
def check_serial(sn):
if sn.isdigit():
v = int(sn)
if 0 <= v <= 65535:
return v
else:
raise argparse.ArgumentTypeError(f'недопустимый серийник {v}, должно быть от 0 до 65535')
raise argparse.ArgumentTypeError(f'неверный серийник "{sn}", должно быть число')
def check_serials(s: str):
result = []
for r in s.split(','):
mm = r.split('-', maxsplit=1)
f = check_serial(mm[0])
if len(mm) == 2:
t = check_serial(mm[1])
if f > t:
f, t = t, f
if f != t:
result.append(range(f, t+1))
continue
result.append(f)
return result
parser = argparse.ArgumentParser(description='Дизассемблер скриптов и калькуляторов iProg',
usage='%(prog)s filename [--bruteforce] | [-sn серийники]',
add_help=False)
parser.add_argument('filename', help='Файл скрипта .ipr или файл калькулятора .cal')
popular_sn = ','.join(f'{sn}' for sn in Decoder.most_popular_sn)
parser.add_argument('-sn', type=check_serials, metavar='серийники',
help='Использовать эти серийники для раскодирования. Через "," или диапазоны через "-". '
f'Если не указано, пробуем следующие номера: {popular_sn}')
parser.add_argument('--bruteforce', action='store_true',
help='Поиск sn перебором (возможны ложные срабатывания). Тоже что и -sn 0-65535')
parser.add_argument('--brute-quick', action='store_true',
help='Быстрая проверка (возможны ложные срабатывания)')
parser.add_argument('--newsn', type=check_serial, metavar='серийник',
help='Сохранить с новым серийником (только для .cal)')
parser.add_argument('--brute-all', action='store_true',
help='Поиск всех подходящих sn (только для .cal)')
parser.add_argument('--ignore-check', action='store_true',
help='Игнорировать проверку расшифровки и попытаться сохранить как есть')
parser.add_argument('--eph', type=lambda a: (int(i, 0) for i in a.split(',')),
help='host ep (and for .cal)')
parser.add_argument('--epd', type=lambda a: (int(i, 0) for i in a.split(',')),
help='device ep')
if sys.argv[1:]:
args = parser.parse_args()
return args
else:
parser.print_help()
parser.exit()
def main():
args = get_args()
print(f'Processing: {args.filename}')
Decoder.init_args(args)
decompile(args.filename, args)
main()