|
1 | | -# convert_binary_to_header.py |
| 1 | +# convert_binary_to_array.py |
2 | 2 | import sys |
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | | -def binary_to_c_array(bin_file, array_name): |
| 5 | +def binary_to_c_array(bin_file, array_name, size_BeforeCompression, compression_activated): |
6 | 6 | with open(bin_file, 'rb') as f: |
7 | 7 | binary_data = f.read() |
8 | 8 |
|
9 | 9 | hex_array = ', '.join(f'0x{b:02x}' for b in binary_data) |
10 | 10 | c_array = f'const unsigned char {array_name}[] = {{\n {hex_array}\n}};\n' |
11 | | - c_array += f'const size_t {array_name}_size = sizeof({array_name});\n' |
| 11 | + c_array += f'const size_t {array_name}_size = sizeof({array_name}); // {len(binary_data)}\n' |
| 12 | + |
| 13 | + if not compression_activated: |
| 14 | + size_BeforeCompression = 0 # set value to 0 if we are not using compression. |
| 15 | + c_array += f'const size_t {array_name}_size_uncompressed = {size_BeforeCompression}; // set to 0 if NOT using the ZSTD compression.\n' |
12 | 16 | return c_array |
13 | 17 |
|
14 | 18 | if __name__ == "__main__": |
15 | | - if len(sys.argv) != 3: |
16 | | - print(f"Usage: {sys.argv[0]} <input_binary_file> <output_header_file>") |
| 19 | + if len(sys.argv) != 5: |
| 20 | + print(f"Usage: {sys.argv[0]} <input_binary_file_before_compression> <input_binary_file_after_compression> <output_header_file> <compression_activated>") |
17 | 21 | sys.exit(1) |
18 | 22 |
|
19 | | - bin_file = sys.argv[1] |
20 | | - header_file_path = sys.argv[2] |
| 23 | + bin_file_beforeCompression = sys.argv[1] |
| 24 | + bin_file_afterCompression = sys.argv[2] # not used if 'compression_activated' is OFF |
| 25 | + header_file_path = sys.argv[3] |
| 26 | + compression_activated = sys.argv[4].lower() == "on" # sys.argv[4] should be "ON" or "OFF" |
| 27 | + |
21 | 28 | header_file = Path(header_file_path).name |
22 | 29 | array_name = header_file.replace('.', '_') |
23 | 30 |
|
24 | | - c_array = binary_to_c_array(bin_file, array_name) |
| 31 | + if not compression_activated: |
| 32 | + bin_file_afterCompression = bin_file_beforeCompression |
| 33 | + |
| 34 | + c_array = binary_to_c_array(bin_file_afterCompression, array_name, Path(bin_file_beforeCompression).stat().st_size, compression_activated ) |
25 | 35 | with open(header_file_path, 'w') as f: |
26 | | - f.write("// generated by convert_binary_to_header.py\n") |
| 36 | + f.write("// generated by convert_binary_to_array.py\n") |
| 37 | + if compression_activated: |
| 38 | + f.write(f"// Data is compressed.\n") |
27 | 39 | f.write(c_array) |
0 commit comments