-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_joiner.py
71 lines (54 loc) · 1.68 KB
/
file_joiner.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
def join(inpath, outpath):
result = None
paths = sorted(os.listdir(inpath))
files = []
for f in paths:
words = []
with open(os.path.join(inpath, f), "rb") as in_file:
while True:
bytes_ = in_file.read(4)
if len(bytes_) == 0:
break
word = bytes_[0] << 24
word += bytes_[1] << 16
word += bytes_[2] << 8
word += bytes_[3]
words.append(word)
files.append(words)
"""if result is None:
result = words
else:
result = xor(result, words)"""
while len(files) > 1:
tmp = []
for i in range(0, len(files), 2):
a, b = files[i:i+2]
for j in range(1, len(a), 2):
a[j], b[j-1] = b[j-1], a[j]
tmp.append(xor(a, b))
files = tmp
result = files[0]
to_unpad = 0
if result[-1] == 0x04040404:
result.pop(-1)
else:
to_unpad = (result[-1] & 0xff)
with open(outpath, "wb") as out_file:
if to_unpad:
last = result[-1]
result = result[:-1]
for word in result:
out_file.write(word.to_bytes(4, "big"))
if to_unpad:
bytes_ = last.to_bytes(4, "big")[:-to_unpad]
out_file.write(bytes_)
def xor(arr1, arr2):
arr3 = []
for b1, b2 in zip(arr1, arr2):
arr3.append(b1^b2)
return arr3
if __name__ == "__main__":
join("./test_out", "./joined.txt")