From 167673aa0867c0b6434607ce5a6cf58ef2125e47 Mon Sep 17 00:00:00 2001 From: Salad Dais Date: Sun, 31 Dec 2023 15:52:15 +0000 Subject: [PATCH] Be nicer about zero-length strings in Messages --- client_examples/hello_client.py | 2 +- hippolyzer/lib/base/datatypes.py | 6 ++++++ hippolyzer/lib/base/message/udpdeserializer.py | 16 +++++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/client_examples/hello_client.py b/client_examples/hello_client.py index add2ea8..4512f3c 100644 --- a/client_examples/hello_client.py +++ b/client_examples/hello_client.py @@ -19,7 +19,7 @@ async def amain(): return if message["ChatData"]["SourceType"] != ChatSourceType.AGENT: return - if "hello" not in str(message["ChatData"]["Message"]).lower(): + if "hello" not in message["ChatData"]["Message"].lower(): return await client.send_chat(f'Hello {message["ChatData"]["FromName"]}!', chat_type=ChatType.SHOUT) diff --git a/hippolyzer/lib/base/datatypes.py b/hippolyzer/lib/base/datatypes.py index d29a587..f3f53aa 100644 --- a/hippolyzer/lib/base/datatypes.py +++ b/hippolyzer/lib/base/datatypes.py @@ -317,6 +317,12 @@ class JankStringyBytes(bytes): return item in str(self) return item in bytes(self) + def lower(self): + return str(self).lower() + + def upper(self): + return str(self).upper() + class RawBytes(bytes): __slots__ = () diff --git a/hippolyzer/lib/base/message/udpdeserializer.py b/hippolyzer/lib/base/message/udpdeserializer.py index dc5507d..f33fb4c 100644 --- a/hippolyzer/lib/base/message/udpdeserializer.py +++ b/hippolyzer/lib/base/message/udpdeserializer.py @@ -220,11 +220,17 @@ class UDPMessageDeserializer: if tmpl_variable.probably_binary: return unpacked_data # Truncated strings need to be treated carefully - if tmpl_variable.probably_text and unpacked_data.endswith(b"\x00"): - try: - return unpacked_data.decode("utf8").rstrip("\x00") - except UnicodeDecodeError: - return JankStringyBytes(unpacked_data) + if tmpl_variable.probably_text: + # If it has a null terminator, let's try to decode it first. + # We don't want to do this if there isn't one, because that may change + # the meaning of the data. + if unpacked_data.endswith(b"\x00"): + try: + return unpacked_data.decode("utf8").rstrip("\x00") + except UnicodeDecodeError: + pass + # Failed, return jank stringy bytes + return JankStringyBytes(unpacked_data) elif tmpl_variable.type in {MsgType.MVT_FIXED, MsgType.MVT_VARIABLE}: # No idea if this should be bytes or a string... make an object that's sort of both. return JankStringyBytes(unpacked_data)