mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-25 01:02:24 +00:00
testcrypt.py: marshal string literals more efficiently.
The testcrypt protocol expects a string literal to be a concatenation of literal bytes other than '%' and '\n', and %-escaped hex digit pairs. But testcrypt.py was only ever using the latter format, so even a legible ASCII string like "123" was being sent to testcrypt as the unreadable and needlessly long "%31%32%33". When debugging, I often arrange to save the testcrypt input stream to a file, and sometimes I use that file as the starting point for editing. So it is actually useful to have the protocol exchange be legible to humans. Hence, here's a change to testcrypt.py which makes it only use the %-escape encoding for byte values that aren't printable ASCII.
This commit is contained in:
parent
844e766b03
commit
bed4e12f15
@ -140,6 +140,14 @@ class Value(object):
|
|||||||
def __int__(self):
|
def __int__(self):
|
||||||
return int(self.__long__())
|
return int(self.__long__())
|
||||||
|
|
||||||
|
def marshal_string(val):
|
||||||
|
val = coerce_to_bytes(val)
|
||||||
|
assert isinstance(val, bytes), "Bad type for val_string input"
|
||||||
|
return "".join(
|
||||||
|
chr(b) if (0x20 <= b < 0x7F and b != 0x25)
|
||||||
|
else "%{:02x}".format(b)
|
||||||
|
for b in val)
|
||||||
|
|
||||||
def make_argword(arg, argtype, fnname, argindex, to_preserve):
|
def make_argword(arg, argtype, fnname, argindex, to_preserve):
|
||||||
typename, consumed = argtype
|
typename, consumed = argtype
|
||||||
if typename.startswith("opt_"):
|
if typename.startswith("opt_"):
|
||||||
@ -147,12 +155,9 @@ def make_argword(arg, argtype, fnname, argindex, to_preserve):
|
|||||||
return "NULL"
|
return "NULL"
|
||||||
typename = typename[4:]
|
typename = typename[4:]
|
||||||
if typename == "val_string":
|
if typename == "val_string":
|
||||||
arg = coerce_to_bytes(arg)
|
retwords = childprocess.funcall("newstring", [marshal_string(arg)])
|
||||||
if isinstance(arg, bytes):
|
arg = make_retvals([typename], retwords, unpack_strings=False)[0]
|
||||||
retwords = childprocess.funcall(
|
to_preserve.append(arg)
|
||||||
"newstring", ["".join("%{:02x}".format(b) for b in arg)])
|
|
||||||
arg = make_retvals([typename], retwords, unpack_strings=False)[0]
|
|
||||||
to_preserve.append(arg)
|
|
||||||
if typename == "val_mpint" and isinstance(arg, numbers.Integral):
|
if typename == "val_mpint" and isinstance(arg, numbers.Integral):
|
||||||
retwords = childprocess.funcall("mp_literal", ["0x{:x}".format(arg)])
|
retwords = childprocess.funcall("mp_literal", ["0x{:x}".format(arg)])
|
||||||
arg = make_retvals([typename], retwords)[0]
|
arg = make_retvals([typename], retwords)[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user