This repository was archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathcreate_buffer.py
executable file
·185 lines (142 loc) · 5.3 KB
/
create_buffer.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
# Copyright 2018-2020 Google LLC
#
# This is part of the Google Cloud IoT Device SDK for Embedded C.
# It is licensed under the BSD 3-Clause license; you may not use this file
# except in compliance with the License.
#
# You may obtain a copy of the License at:
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os.path
import struct
from pprint import pprint
h_file_name = ""
c_file_name = ""
ROOTCA_LIST_LEN_MACRO = "IOTC_ROOTCA_LIST_BYTE_LENGTH"
PATTERN = "0x%02x"
NEWLINE = "\n"
INDENT = 4 * " "
PER_LINE = 16
def tabs_2_spaces(s):
return s.replace("\t", " ")
h_pro = tabs_2_spaces(
"""\
/* Copyright 2018-2020 Google LLC
*
* This is part of the Google Cloud IoT Device SDK for Embedded C.
* It is licensed under the BSD 3-Clause license; you may not use this file
* except in compliance with the License.
*
* You may obtain a copy of the License at:
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
""")
h_guard_beg = tabs_2_spaces("#ifndef __%(name)s_H__\n#define __%(name)s_H__")
h_guard_end = tabs_2_spaces("#endif /* __%s_H__ */")
cpp_beg = tabs_2_spaces("#ifdef __cplusplus\nextern \"C\" {\n#endif")
cpp_end = tabs_2_spaces("#ifdef __cplusplus\n}\n#endif")
src_usage_msg = \
"""\
/* generated by create_buffer.py from a PEM file. Example:
* ./create_buffer.py --file_name res/trusted_RootCA_certs/roots.pem
* --array_name iotc_RootCA_list --out_path src/libiotc/tls/certs --no-pretend
*
* The buffer is terminated with '\\n' for consistency with POSIX filesystem reads
*/
"""
def load_file(file_name):
if not os.path.isfile(file_name):
raise RuntimeError("There is no such file: [%s]!" % file_name)
data = []
with open(file_name, "rb") as f:
byte = f.read(1)
while byte:
data.append(struct.unpack('B', byte)[0])
byte = f.read(1)
return data
def convert_to_c_array(data, array_name):
out = "unsigned char %s[ %s ] = {" % (array_name, ROOTCA_LIST_LEN_MACRO)
out += NEWLINE
out += INDENT
while data:
head = data[0: PER_LINE]
data = data[PER_LINE:]
for byte in head[0: -1]:
out += PATTERN % byte
out += ", "
out += PATTERN % head[-1]
if data:
out += ","
out += NEWLINE
out += INDENT
out += " };"
return out
def create_c_file(data, array_name):
out = h_pro + "\n"
out += cpp_beg + "\n\n"
out += '#include "%s"' % (os.path.basename(h_file_name))
out += "\n\n"
out += src_usage_msg
out += convert_to_c_array(data, array_name)
out += "\n\n"
out += cpp_end + "\n"
return out
def create_h_file(data, array_name):
out = h_pro + "\n\n"
out += (h_guard_beg % {"name": array_name.upper()}) + "\n\n"
out += cpp_beg + "\n\n"
out += "#ifndef %s" % (ROOTCA_LIST_LEN_MACRO)
out += "\n"
out += "#define %s %d" % (ROOTCA_LIST_LEN_MACRO, len(data))
out += "\n"
out += "#endif /* %s */" % (ROOTCA_LIST_LEN_MACRO)
out += "\n\n"
out += "extern unsigned char %s[ %s ];\n\n" % (
array_name, ROOTCA_LIST_LEN_MACRO)
out += cpp_end + "\n\n"
out += (h_guard_end % (array_name.upper())) + "\n"
return out
def write_to_file(file_name, s):
print("writing to: %s" % file_name)
with open(file_name, "w") as f:
f.write(s)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="libiotc buffer creator")
parser.add_argument('--file_name', dest='file_name', type=str,
required='True', help='file name that is going to be converted')
parser.add_argument('--out_path', dest='out_path',
type=str, help='output path')
parser.add_argument('--array_name', dest='array_name', type=str,
required='True', help='array name that is going to be used as an output')
parser.add_argument('--no-pretend', dest='no_pret', action='store_const', const=True,
default=False, help='disable printing to the console and enables writing to files')
args = parser.parse_args()
file_name = args.file_name
array_name = args.array_name
out_path = args.out_path
if out_path:
path = out_path
else:
path = os.path.join('.', 'src', 'libiotc')
h_file_name = os.path.join(path, array_name + ".h")
c_file_name = os.path.join(path, array_name + ".c")
data = load_file(file_name)
if args.no_pret:
write_to_file(h_file_name, create_h_file(data, array_name))
write_to_file(c_file_name, create_c_file(data, array_name))
else:
print((create_h_file(data, array_name)))
print((create_c_file(data, array_name)))