Add return_truncated for plugin-specified handling of truncated strings

This commit is contained in:
Andrew Case
2025-03-14 20:59:15 +00:00
parent f38ddaf715
commit bb0a17004f
+18 -2
View File
@@ -150,8 +150,19 @@ def gather_contiguous_bytes_from_address(
)
def bytes_to_decoded_string(data: bytes, encoding: str, errors: str) -> bytes:
def bytes_to_decoded_string(
data: bytes, encoding: str, errors: str, return_truncated: bool = True
) -> bytes:
"""
Args:
data: The `bytes` buffer containing the string of a string at offset 0
encoding: An encoding value for the encoding paramater of `bytes.decode`
errors: An errors value for the errors parameter of `bytes.decode`
return_truncated: Dictates whether truncated strings should be returned or
if a ValueError should be thrown if a truncated (broken) string was decoded
Returns:
bytes: The decoded string starting at offset of data
This function takes a bytes buffer that contains at a string of unknown
length starting at the first byte, and returns the properly decoded string
@@ -173,7 +184,12 @@ def bytes_to_decoded_string(data: bytes, encoding: str, errors: str) -> bytes:
try:
idx = termination_re.search(full_decoded_string).start()
except AttributeError:
idx = len(full_decoded_string)
if return_truncated:
idx = len(full_decoded_string)
else:
raise ValueError(
"return_truncated set to False and truncated string decoded."
)
# cut at terminating byte, if found
data = data[:idx]