forked from xboxdrv/xboxdrv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin2h.py
93 lines (73 loc) · 3.25 KB
/
bin2h.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
92
93
#!/usr/bin/env python3
# Xbox360 USB Gamepad Userspace Driver
# Copyright (C) 2015 Ingo Ruhnke <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import argparse
import string
import os.path
def build_bin2h(target, sources, prefixpath, namespace):
"""
Takes a list of files and converts them into a C source that can be included
"""
def c_escape(str):
return str.translate(str.maketrans("/.-", "___"))
with open(target, "w") as fout:
fout.write("// autogenerated by scons Bin2H builder, do not edit by hand!\n\n")
if namespace:
fout.write("namespace %s {\n\n" % namespace)
# write down data
for src in sources:
if prefixpath is None:
src_c_name = c_escape(src)
else:
src_c_name = c_escape(os.path.relpath(src, prefixpath))
with open(src, "rb") as fin:
data = fin.read()
fout.write("// \"%s\"\n" % src)
fout.write("const char %s[] = {" % src_c_name)
bytes_arr = ["0x%02x" % c for c in data]
for i in range(len(bytes_arr)):
if i % 13 == 0:
fout.write("\n ")
fout.write(bytes_arr[i])
if i != len(bytes_arr)-1:
fout.write(", ")
fout.write("\n};\n\n")
# write down file table
if False:
fout.write("const char** file_table = {\n")
fout.write(",\n".join([" %-35s %-s" % ("\"%s\"," % src,
c_escape(src))
for src in sources]))
fout.write("\n}\n\n")
if namespace:
fout.write("} // namespace %s\n\n" % namespace)
fout.write("/* EOF */\n")
def main():
parser = argparse.ArgumentParser(description="Convert binary file to .h")
parser.add_argument('SOURCE', action='store', nargs='+', type=str,
help="SOURCE file")
parser.add_argument('-o', '--output', metavar='TARGET', action='store', required=True, type=str,
help="TARGET file")
parser.add_argument('-p', '--prefix', metavar='PREFIX', action='store', required=False, type=str,
help="PREFIX is removed from the path before converting it to a C variable name")
parser.add_argument('--namespace', metavar='NAMESPACE', action='store', type=str,
help="Wrap code into NAMESPACE")
args = parser.parse_args()
build_bin2h(args.output, args.SOURCE, args.prefix, args.namespace)
if __name__ == "__main__":
main()
# EOF #