aboutsummaryrefslogtreecommitdiff
path: root/py/makeqstrdata.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-04-14 14:20:25 +0100
committerDamien George <damien.p.george@gmail.com>2016-04-14 14:20:25 +0100
commit49bb04ee64d719c83ae3f2ded4c9f9578707bd84 (patch)
tree41a9b64dc88ad6f5ee1da47d52783fa128be9000 /py/makeqstrdata.py
parent0c1de1cdeea98e55710d27f7926da717086ac0fc (diff)
py/makeqstrdata: Fix rendering of qstrs that have non-printable ASCII.
The qstr data needs to be turned into a proper C string so non-ASCII chars must be properly escaped according to C rules.
Diffstat (limited to 'py/makeqstrdata.py')
-rw-r--r--py/makeqstrdata.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py
index 4215ff301..7cad46e6e 100644
--- a/py/makeqstrdata.py
+++ b/py/makeqstrdata.py
@@ -106,9 +106,15 @@ def parse_input_headers(infiles):
def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
qhash = compute_hash(qstr, cfg_bytes_hash)
- # Calculate len of str, taking escapes into account
- qlen = len(qstr.replace("\\\\", "-").replace("\\", ""))
- qdata = qstr.replace('"', '\\"')
+ if all(32 <= ord(c) <= 126 and c != '\\' for c in qstr):
+ # qstr is all printable ASCII so render it as-is (for easier debugging)
+ qlen = len(qstr)
+ qdata = qstr
+ else:
+ # qstr contains non-printable codes so render entire thing as hex pairs
+ qbytes = qstr.encode('utf8')
+ qlen = len(qbytes)
+ qdata = ''.join(('\\x%02x' % b) for b in qbytes)
if qlen >= (1 << (8 * cfg_bytes_len)):
print('qstr is too long:', qstr)
assert False