forked from lly-q/MIO-KITCHEN-SOURCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splituapp.py
91 lines (68 loc) · 2.58 KB
/
splituapp.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
#!/usr/bin/env python
# split_app for Python2/3 by SuperR. @XDA
#
# For extracting img files from UPDATE.APP
# Based on the app_structure file in split_up_data.pl by McSpoon
from __future__ import print_function
from string import printable
from struct import unpack
import sys
from os import makedirs, name, sep, path
def extract(source, flist):
byte_num = 4
out_dir = 'output'
img_files = []
try:
makedirs(out_dir)
finally:
...
with open(source, 'rb') as f:
while True:
i = f.read(byte_num)
if not i:
break
elif i != b'\x55\xAA\x5A\xA5':
continue
header_size = list(unpack('<L', f.read(byte_num)))[0]
f.seek(16, 1)
file_size = list(unpack('<L', f.read(byte_num)))[0]
f.seek(32, 1)
try:
filename = str(f.read(16).decode())
filename = ''.join(f for f in filename if f in printable).lower()
except Exception or BaseException:
filename = ''
f.seek(22, 1)
crc_data = f.read(header_size - 98)
if not flist or filename in flist:
if filename in img_files:
filename = filename + '_2'
print(f'Extracting {filename}.img ...')
chunk = 10240
try:
with open(out_dir + sep + filename + '.img', 'wb') as o:
while file_size > 0:
if chunk > file_size:
chunk = file_size
o.write(f.read(chunk))
file_size -= chunk
except Exception as e:
print('ERROR: Failed to create ' + filename + '.img:%s\n' % e)
return
img_files.append(filename)
if name != 'nt':
if path.isfile('crc'):
print('Calculating crc value for ' + filename + '.img ...\n')
crc_val = []
if sys.version_info.major < 3:
for i in crc_data:
crc_val.append('%02X' % int(i))
else:
for i in crc_data:
crc_val.append('%02X' % i)
else:
f.seek(file_size, 1)
x_bytes = byte_num - f.tell() % byte_num
if x_bytes < byte_num:
f.seek(x_bytes, 1)
print('\nExtraction complete')