From 28f170fe81e25b865cd981f658a3a5f3c5845504 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Sun, 14 Jan 2024 13:28:12 +0100 Subject: [PATCH] Implement hexdumping Not used yet, though --- sopass.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/sopass.py b/sopass.py index 63caa7e..d13dfc6 100755 --- a/sopass.py +++ b/sopass.py @@ -8,6 +8,7 @@ from stat import * # suggested usageā€¦ import selectors from dataclasses import dataclass import logging as log +import math # Note: get/setrlimit are in resource module def setup_logging(): @@ -21,9 +22,19 @@ class Direction: sigil: str color: str -def hexdump(data: bytes) -> str: - # TODO: dump somehow - ... +def hexdump(data: bytes) -> list[str]: # We will be prefixing the lines with sigils, so no need to join. + result = [] + offset = 0 + offset_len = math.ceil(math.log(len(data), 16)) + while data: + left_chunk, right_chunk, data = data[:8], data[8:16], data[16:] + left_ascii = ''.join(chr(x) if x in range(32,127) else ',' for x in left_chunk) + right_ascii = ''.join(chr(x) if x in range(32,127) else ',' for x in right_chunk) + offset_str = f'{offset:0{offset_len}x}' + line = f"{offset_str} {left_chunk.hex(' ').ljust(23)} {right_chunk.hex(' ').ljust(23)} |{left_ascii.ljust(8)}{right_ascii.ljust(8)}|" + offset += 16 + result.append(line) + return result def fdinfo(fd: int) -> str: stat = os.fstat(fd)