Skip to content

Commit

Permalink
Fix hexdump
Browse files Browse the repository at this point in the history
  • Loading branch information
ptr-yudai committed Oct 17, 2023
1 parent 30526b9 commit 0773605
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions ptrlib/binary/encoding/dump.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from .byteconv import str2bytes
from typing import Union

def hexdump(data, prefix='', postfix=''):
def hexdump(data: Union[str, bytes], prefix: str='', postfix: str=''):
"""Print data in hexdump format
"""
if isinstance(data, str):
data = str2bytes(data)
prev_display = True

for offset in range(0, len(data), 0x10):
output = prefix
output += f"{offset:08x} "

for i, c in enumerate(data[offset:offset+0x10]):
if i == 8: output += " "
output += f"{c:02x} "
Expand All @@ -15,12 +20,14 @@ def hexdump(data, prefix='', postfix=''):
if len(data[offset:]) < 9:
output += " "
output += " |"

for c in data[offset:offset+0x10]:
if 0x20 <= c <= 0x7e:
output += chr(c)
else:
output += "."
output += postfix
output += "|" + postfix

if offset > 0x10 and data[offset-0x10:offset] == data[offset:offset+0x10]:
if prev_display:
print(prefix + "*" + postfix)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='ptrlib',
version='2.0.9',
version='2.0.11',
description='CTF library',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 0773605

Please sign in to comment.