-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsharjahsat_simple.py
82 lines (67 loc) · 2.27 KB
/
sharjahsat_simple.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
#!/usr/bin/env python3
# Made by sa2kng <[email protected]>
from io import BytesIO
from os import path
from sys import argv
def main():
if len(argv) != 2:
print(f'Useage: {path.basename(argv[0])} <infile>\n'
'Process a single Sharjahsat-1 image from hex/KISS frames.\n'
'Output will have the same name as the input, but with .jpg extension\n')
exit(0)
if not path.exists(argv[1]):
print(f'File not found: {argv[1]}')
exit(1)
frames = parse_file(argv[1])
data = parse_frames(frames)
write_image(path.splitext(argv[1])[0] + '.jpg', data)
def parse_file(infile):
try:
with open(infile, 'r') as f:
return parse_hexfile(f)
except UnicodeDecodeError:
with open(infile, 'rb') as f:
return parse_kissfile(f)
def parse_hexfile(f):
data = []
for row in f:
row = row.replace(' ', '').strip()
if '|' in row:
row = row.split('|')[-1]
if len(row) >= 64: # a bit arbitrary
data.append(row)
return data
def parse_kissfile(infile):
data = []
for row in infile.read().split(b'\xC0'):
if len(row) == 0 or row[0] != 0:
continue
data.append(row[1:].replace(b'\xdb\xdc', b'\xc0').replace(b'\xdb\xdd', b'\xdb').hex(bytes_per_sep=2))
return data
def parse_frames(data):
image = BytesIO()
lastframe = 0
dsize = 246
for row in data:
if 'FFD8FF' in row[52:58]:
lastframe = int(row[46:48] + row[44:46], 16) # it's actually 32bit
dsize = int(row[42:44], 16)
break
for row in data:
did = bytes.fromhex(row[32:40])
dt = row[40:42]
# dlen = int(row[42:44], 16)
addr = int(row[46:48] + row[44:46], 16) # it's actually 32bit
# header = row[0:52]
payload = row[52:]
# print(f'{call} dt:{dt} dl:{dlen} a:{addr} {header} {payload}')
if did == b'ESER' and dt == '41': # and dlen == 246:
image.seek(dsize * (lastframe - addr))
image.write(bytes.fromhex(payload))
return image
def write_image(outfile, data):
print(f'Writing image to: {outfile}')
with open(outfile, 'wb') as f:
f.write(data.getbuffer())
if __name__ == '__main__':
main()