File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -53,11 +53,31 @@ def module_name(f):
5353print ("const char mp_frozen_str_content[] = {" )
5454for f , st in modules :
5555 data = open (sys .argv [1 ] + "/" + f , "rb" ).read ()
56- # Python2 vs Python3 tricks
57- data = repr (data )
58- if data [0 ] == "b" :
59- data = data [1 :]
60- data = data [1 :- 1 ]
61- data = data .replace ('"' , '\\ "' )
62- print ('"%s\\ 0"' % data )
56+
57+ # We need to properly escape the script data to create a C string.
58+ # When C parses hex characters of the form \x00 it keeps parsing the hex
59+ # data until it encounters a non-hex character. Thus one must create
60+ # strings of the form "data\x01" "abc" to properly encode this kind of
61+ # data. We could just encode all characters as hex digits but it's nice
62+ # to be able to read the resulting C code as ASCII when possible.
63+
64+ data = bytearray (data ) # so Python2 extracts each byte as an integer
65+ esc_dict = {ord ('\n ' ): '\\ n' , ord ('\r ' ): '\\ r' , ord ('"' ): '\\ "' , ord ('\\ ' ): '\\ \\ ' }
66+ chrs = ['"' ]
67+ break_str = False
68+ for c in data :
69+ try :
70+ chrs .append (esc_dict [c ])
71+ except KeyError :
72+ if 32 <= c <= 126 :
73+ if break_str :
74+ chrs .append ('" "' )
75+ break_str = False
76+ chrs .append (chr (c ))
77+ else :
78+ chrs .append ('\\ x%02x' % c )
79+ break_str = True
80+ chrs .append ('\\ 0"' )
81+ print ('' .join (chrs ))
82+
6383print ("};" )
You can’t perform that action at this time.
0 commit comments