Skip to content

Commit

Permalink
Merge pull request #1191 from gcmoreira/fix_text_renderer_hexdump
Browse files Browse the repository at this point in the history
Fix hexdump text render. Set default to 16 bytes width
  • Loading branch information
ikelos authored Jul 4, 2024
2 parents 0da48fa + 52e6812 commit 1e58547
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions volatility3/cli/text_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
vollog.debug("Disassembly library capstone not found")


def hex_bytes_as_text(value: bytes) -> str:
def hex_bytes_as_text(value: bytes, width: int = 16) -> str:
"""Renders HexBytes as text.
Args:
Expand All @@ -36,19 +36,24 @@ def hex_bytes_as_text(value: bytes) -> str:
"""
if not isinstance(value, bytes):
raise TypeError(f"hex_bytes_as_text takes bytes not: {type(value)}")
ascii = []
hex = []
count = 0
output = ""
for byte in value:
hex.append(f"{byte:02x}")
ascii.append(chr(byte) if 0x20 < byte <= 0x7E else ".")
if (count % 8) == 7:
output += "\n"
output += " ".join(hex[count - 7 : count + 1])
output += "\t"
output += "".join(ascii[count - 7 : count + 1])
count += 1

printables = ""
output = "\n"
for count, byte in enumerate(value):
output += f"{byte:02x} "
char = chr(byte)
printables += char if 0x20 <= byte <= 0x7E else "."
if count % width == width - 1:
output += printables
if count < len(value) - 1:
output += "\n"
printables = ""

# Handle leftovers when the lenght is not mutiple of width
if printables:
output += " " * (width - len(printables))
output += printables

return output


Expand Down

0 comments on commit 1e58547

Please sign in to comment.