tools/make-frozen.py: Properly escape hex chars when making C strings.
This commit is contained in:
parent
1e2f829293
commit
5985e41afc
|
@ -53,11 +53,31 @@ print("};")
|
||||||
print("const char mp_frozen_str_content[] = {")
|
print("const char mp_frozen_str_content[] = {")
|
||||||
for f, st in modules:
|
for f, st in modules:
|
||||||
data = open(sys.argv[1] + "/" + f, "rb").read()
|
data = open(sys.argv[1] + "/" + f, "rb").read()
|
||||||
# Python2 vs Python3 tricks
|
|
||||||
data = repr(data)
|
# We need to properly escape the script data to create a C string.
|
||||||
if data[0] == "b":
|
# When C parses hex characters of the form \x00 it keeps parsing the hex
|
||||||
data = data[1:]
|
# data until it encounters a non-hex character. Thus one must create
|
||||||
data = data[1:-1]
|
# strings of the form "data\x01" "abc" to properly encode this kind of
|
||||||
data = data.replace('"', '\\"')
|
# data. We could just encode all characters as hex digits but it's nice
|
||||||
print('"%s\\0"' % data)
|
# to be able to read the resulting C code as ASCII when possible.
|
||||||
|
|
||||||
|
data = bytearray(data) # so Python2 extracts each byte as an integer
|
||||||
|
esc_dict = {ord('\n'): '\\n', ord('\r'): '\\r', ord('"'): '\\"', ord('\\'): '\\\\'}
|
||||||
|
chrs = ['"']
|
||||||
|
break_str = False
|
||||||
|
for c in data:
|
||||||
|
try:
|
||||||
|
chrs.append(esc_dict[c])
|
||||||
|
except KeyError:
|
||||||
|
if 32 <= c <= 126:
|
||||||
|
if break_str:
|
||||||
|
chrs.append('" "')
|
||||||
|
break_str = False
|
||||||
|
chrs.append(chr(c))
|
||||||
|
else:
|
||||||
|
chrs.append('\\x%02x' % c)
|
||||||
|
break_str = True
|
||||||
|
chrs.append('\\0"')
|
||||||
|
print(''.join(chrs))
|
||||||
|
|
||||||
print("};")
|
print("};")
|
||||||
|
|
Loading…
Reference in New Issue