forked from Marianpol/pyclip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_optfile.py
124 lines (114 loc) · 3.76 KB
/
mod_optfile.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
#Embedded file name: /build/PyCLIP/android/app/mod_optfile.py
import glob
import json
import os
import pickle
import struct
import sys
from xml.dom.minidom import parseString
import mod_globals
import mod_zip
class optfile:
dict = {}
obf = True
def __init__(self, filename, progress = False, cache = True, zip = False):
self.dict = {}
fn = filename
pn = './cache/' + os.path.basename(fn) + '.p'
if os.path.isfile(pn):
self.dict = pickle.load(open(pn, 'rb'))
elif os.path.isfile(fn):
if not zip:
lf = open(fn, 'rb')
else:
lf = mod_zip.get_file_content(fn)
self.get_dict(lf, progress)
if cache:
pickle.dump(self.dict, open(pn, 'wb'))
def get_string(self, lf, len):
i = lf.tell()
bytes = lf.read(2 * len)
st = ''
j = 0
len = len * 2
while j < len:
x = struct.unpack('<H', bytes[j:j + 2])[0]
if self.obf:
x = x ^ i & 65535 ^ 21845
j += 2
i += 2
st += unichr(x)
return st
def get_2_bytes(self, lf):
i = lf.tell()
bytes = lf.read(2)
x = 0
x = struct.unpack('<H', bytes)[0]
if self.obf == False:
return x
return x ^ i & 65535 ^ 21845
def get_4_bytes(self, lf):
return self.get_2_bytes(lf) + (self.get_2_bytes(lf) << 16)
def get_dict(self, lf, progress):
self.fb = ord(lf.read(1))
if self.fb != 85 and self.fb != 189:
self.obf = False
lf.seek(20)
protlen = self.get_4_bytes(lf)
lf.seek(24 + protlen * 2)
keyoff = self.get_4_bytes(lf)
lf.seek(keyoff - 8)
tb = self.get_4_bytes(lf)
i = keyoff
n = 0
while i < tb:
if progress and i & 255 == 0:
pr = (i + 2 - keyoff) * 100 / (tb - keyoff)
sys.stdout.flush()
lf.seek(i)
addr = self.get_4_bytes(lf)
strl = self.get_4_bytes(lf)
keyl = self.get_4_bytes(lf)
n = n + 1
key = self.get_string(lf, keyl)
lf.seek(addr)
line = self.get_string(lf, strl)
line = line.strip()
line = line.strip('\n')
line = line.replace(u'\xab', '"')
line = line.replace(u'\xbb', '"')
self.dict[key] = line
i = i + 12 + keyl * 2
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))
if len(sys.argv) == 1:
sys.exit(0)
if sys.argv[1] == 'ALLSG':
for file in glob.glob('../EcuRenault/Sessions/*.xml'):
file = os.path.basename(file)
if file.startswith('SG'):
sgFileName = '../EcuRenault/Sessions/S' + file[1:]
ugFileName = '../EcuRenault/Sessions/U' + file[1:-4] + '.json'
if os.path.exists(ugFileName):
continue
try:
of = optfile(sgFileName, False, False)
except:
continue
f = open(ugFileName, 'wt')
f.write(json.dumps(of.dict))
f.close()
exit(0)
if sys.argv[1] == 'ALLBQM':
for file in glob.glob('../Location/*.bqm'):
sgFileName = mod_globals.location_dir + '/' + file
ugFileName = mod_globals.location_dir + '/Convert/' + os.path.basename(file)[:-4] + '.json'
of = optfile(sgFileName, False, False)
rf = json.dumps(of.dict)
f = open(ugFileName, 'wt')
f.write(rf)
f.close()
exit(0)
of = optfile(sys.argv[1])
if len(sys.argv) == 3:
k = sys.argv[2]