-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (62 loc) · 2.11 KB
/
main.py
File metadata and controls
77 lines (62 loc) · 2.11 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
import os
import time
import makebin
import string
makebin.main()
def find_files(directory, extension):
results = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(extension):
results.append(os.path.join(root, file)) # Full path
return results
file_list = find_files('./', '.bin')
file_list += find_files('./', '.dat')
print(file_list)
print()
count = 0
delay = 0.005
# Clear previous contents
with open("binaries.txt", "w") as f:
pass
# Open for appending
with open("binaries.txt", "a") as f:
for i in file_list:
with open(i, "rb") as file:
# Write file header
f.write(f"--- File: {i} ---\n")
print(f"Reading file: {i}")
data = file.read(1)
counter = 1
while data:
print(f"Reading byte {counter} of file '{i}'")
time.sleep(delay)
byte_repr = repr(data)
byte_value = int.from_bytes(data, byteorder='big')
# Decode value
if 32 <= byte_value <= 126:
char_repr = chr(byte_value)
if char_repr in string.digits:
decoded = f"{char_repr}"
else:
decoded = f"'{char_repr}'"
elif byte_value == 10:
decoded = "'\\n'"
elif byte_value == 13:
decoded = "'\\r'"
elif byte_value == 9:
decoded = "'\\t'"
else:
decoded = "non-printable"
line = f"{byte_repr} -> {byte_value} -> {decoded}\n"
print("Appending byte to file binaries.txt")
print("Data:", line.strip())
f.write(line)
time.sleep(delay)
counter += 1
data = file.read(1)
time.sleep(delay)
# Add separator between files
f.write("\n")
count += 1
print(f"Finished reading {count} files out of {len(file_list)}\n")