-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenarr.py
More file actions
124 lines (79 loc) · 2.19 KB
/
Copy pathgenarr.py
File metadata and controls
124 lines (79 loc) · 2.19 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
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
#=================================#
# [ OWNER ]
# CREATOR : Vladislav Khudash
# AGE : 17
# LOCATION : Ukraine
#
# [ PINFO ]
# DATE : 01.08.2026
# PROJECT : EFILDR-GENARR
# PLATFORM : ANY
#=================================#
'''
EFILDR-GENARR Utility
Converts any binary file
into a raw C-style byte array initializer {x,y,z}.
Optimized for performance
via block buffering and disabled GC.
Usage:
python genarr.py <file>
'''
from sys import argv, stdout
from gc import disable
from os import stat
from os.path import basename
# Stream binary file contents
# as a raw C-style array initializer
def genarr(
fp: str,
*,
# Cache ASCII byte values (0-255)
# to eliminate runtime string allocation
_tab=tuple(str(i).encode('ascii')
for i in range(256)).__getitem__
) -> None:
with open(fp, 'rb') as f:
# Cache methods to local scope for speed
rd = f.read
wt = stdout.buffer.write
sp = b''
cm = b','
jn = cm.join
mp = map
wt(b'{')
# Read and convert file data
# in stable 4KB blocks
while ck := rd(4096):
wt(sp) # Write block separator
wt(jn(mp(_tab, ck))) # Convert and write chunk
sp=cm # Set comma for next blocks
wt(b'};')
def main() -> int:
if len(argv) != 2:
# Enforce proper usage
print(
f'Usage: python {basename(argv[0])} <file>'
' <-> '
'Make bin file into C byte array'
)
return 1
# Get target file path
_fp=argv[1]
try:
# Check if file is empty
if not stat(_fp).st_size:
print(f'[-] file({_fp}) is empty')
return 1
except OSError as e:
print(f'[-] cannot open file({_fp}) | errno({e.errno}): {e}')
return 1
print('\n')
disable() # Disable GC to prevent stutter
genarr(_fp) # Generate C-style byte array
print('\n\n')
# Flush remaining data to stdout
stdout.buffer.flush()
return 0
if __name__ == '__main__':
c = main() # Return code for exit
raise SystemExit(c)