def decodeControllerString(cont_str):
'''
Decode an encoded string received via the control port.
Decodes hexadecimal, and the Tor Control Protocol QuotedString format,
depending on the format of the input string.
Does not support the CString encoding, or raw strings that aren't in
one of the two supported formats.
Throws TypeError when presented with an invalid format.
Only some strings in the tor control protocol need encoding.
The same encodings are used by tor and the controller.
'''
cont_str = cont_str.strip()
if (cont_str.startswith("\"") and cont_str.endswith("\"") and
len(cont_str) >= 2):
# quoted strings escape \ and " with \, then quote with "
# this is safe, because we check the string is "*"
cont_str = cont_str[1:-1]
# the order of these replacements is important: they ensure that
# \\" becomes \" rather than "
cont_str = cont_str.replace("\\\"", "\"")
return cont_str.replace("\\\\", "\\")
else:
# assume hex, throws TypeError on invalid hex
return unhexlify(cont_str)
评论列表
文章目录