-
Notifications
You must be signed in to change notification settings - Fork 717
Expand file tree
/
Copy pathzlib_compress_fw.py
More file actions
98 lines (85 loc) · 3.06 KB
/
zlib_compress_fw.py
File metadata and controls
98 lines (85 loc) · 3.06 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# zlib_compress_fw.py
#
# Created by qcwap on 2020/9/3.
# Copyright © 2020 钟先耀. All rights reserved.
import zlib
import os
import struct
import sys
copyright = '''
// itlwm
//
// Copyright © 2020 钟先耀. All rights reserved.
#include "FwData.h"
'''
def compress(data):
return zlib.compress(data)
def format_file_name(file_name):
return file_name.replace(".", "_").replace("-", "_")
def write_single_file(target_file, path, file):
src_file = open(path, "rb")
src_data = src_file.read()
src_data = compress(src_data)
src_len = len(src_data)
fw_var_name = format_file_name(file)
target_file.write("\nconst unsigned char ")
target_file.write(fw_var_name)
target_file.write("[] = {")
index = 0;
block = []
while True:
if index + 16 >= src_len:
block = src_data[index:]
else:
block = src_data[index:index + 16]
index += 16;
if len(block) < 16:
if len(block):
for b in block:
if type(b) is str:
b = ord(b)
target_file.write("0x{:02X}, ".format(b))
target_file.write("\n")
break
target_file.write("0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, "
"0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, "
"0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, "
"0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X},\n"
.format(*struct.unpack("BBBBBBBBBBBBBBBB", block)))
target_file.write("};\n")
target_file.write("const long int ")
target_file.write(fw_var_name)
target_file.write("_size = sizeof(")
target_file.write(fw_var_name)
target_file.write(");\n")
src_file.close()
def process_files(target_file, dir):
if not os.path.exists(target_file):
if not os.path.exists(os.path.dirname(target_file)):
os.mkdirs(os.path.dirname(target_file))
target_file_handle = open(target_file, "w")
target_file_handle.write(copyright)
for root, dirs, files in os.walk(dir):
for file in files:
path = os.path.join(root, file)
write_single_file(target_file_handle, path, file)
target_file_handle.write("\n")
target_file_handle.write("const struct FwDesc fwList[] = {")
for file in files:
target_file_handle.write('{IWL_FW("')
target_file_handle.write(file)
target_file_handle.write('", ')
fw_var_name = format_file_name(file)
target_file_handle.write(fw_var_name)
target_file_handle.write(", ")
target_file_handle.write(fw_var_name)
target_file_handle.write("_size)},\n")
target_file_handle.write("};\n")
target_file_handle.write("const int fwNumber = ")
target_file_handle.write(str(len(files)))
target_file_handle.write(";\n")
target_file_handle.close()
if __name__ == '__main__':
process_files(sys.argv[1], sys.argv[2])