2020-12-07 21:39:07 +01:00
|
|
|
#!env python
|
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
"""
|
|
|
|
Convert a file to a c array
|
|
|
|
bin2carray <output file> <input file>
|
|
|
|
"""
|
2020-12-07 22:35:52 +01:00
|
|
|
|
2020-12-07 21:39:07 +01:00
|
|
|
import os
|
2021-03-23 22:15:11 +01:00
|
|
|
import os.path
|
2020-12-07 21:39:07 +01:00
|
|
|
import sys
|
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Main script function
|
|
|
|
"""
|
|
|
|
if len(sys.argv) < 3:
|
|
|
|
return -1
|
2020-12-07 21:39:07 +01:00
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
source_file = sys.argv[2]
|
|
|
|
dest_file = sys.argv[1]
|
2020-12-07 21:39:07 +01:00
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
print(f'{source_file} --> {dest_file}')
|
2020-12-07 21:39:07 +01:00
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
with open(source_file, 'rb') as src:
|
|
|
|
data = src.read()
|
2020-12-07 22:35:52 +01:00
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
with open(dest_file, 'w', encoding='utf-8') as dest:
|
|
|
|
header_guard = '_' + os.path.basename(dest_file).replace('.', '_').replace('-', '_') + '_H_'
|
|
|
|
header_guard = header_guard.upper()
|
|
|
|
dest.write(f'#ifndef {header_guard}\n')
|
|
|
|
dest.write(f'#define {header_guard}\n')
|
|
|
|
dest.write(f'static const char binary_blob[{len(data)}] = {{\n')
|
|
|
|
for idx, current in enumerate(data, start=1):
|
|
|
|
if idx % 4 == 0:
|
|
|
|
dest.write(hex(current)+',\n')
|
|
|
|
else:
|
|
|
|
dest.write(hex(current)+',')
|
2020-12-07 22:35:52 +01:00
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
dest.write('};\n')
|
|
|
|
dest.write(f'#endif /* {header_guard} */\n')
|
2020-12-07 21:39:07 +01:00
|
|
|
|
2023-06-23 22:43:45 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|