diff --git a/ptrlib/binary/encoding/__init__.py b/ptrlib/binary/encoding/__init__.py index cd9777d..cee4ec0 100644 --- a/ptrlib/binary/encoding/__init__.py +++ b/ptrlib/binary/encoding/__init__.py @@ -1,4 +1,5 @@ from .bitconv import * from .byteconv import * +from .dump import * from .locale import * from .table import * diff --git a/ptrlib/binary/encoding/dump.py b/ptrlib/binary/encoding/dump.py new file mode 100644 index 0000000..565c04c --- /dev/null +++ b/ptrlib/binary/encoding/dump.py @@ -0,0 +1,30 @@ +from .byteconv import str2bytes + +def hexdump(data, prefix='', postfix=''): + 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} " + if len(data[offset:]) < 0x10: + output += " " * (0x10 - len(data[offset:])) + 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 + if offset > 0x10 and data[offset-0x10:offset] == data[offset:offset+0x10]: + if prev_display: + print(prefix + "*" + postfix) + prev_display = False + else: + print(output) + prev_display = True diff --git a/ptrlib/connection/proc.py b/ptrlib/connection/proc.py index 3f19e35..1c83714 100644 --- a/ptrlib/connection/proc.py +++ b/ptrlib/connection/proc.py @@ -165,7 +165,7 @@ def _recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None) -> by self._poll() # poll after received all data return data - def send(self, data: Union[str, bytes]): + def _send(self, data: Union[str, bytes]): """Send raw data Send raw data through the socket diff --git a/ptrlib/connection/sock.py b/ptrlib/connection/sock.py index 8f78957..fbefd5b 100644 --- a/ptrlib/connection/sock.py +++ b/ptrlib/connection/sock.py @@ -87,7 +87,7 @@ def _recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None) -> by return data - def send(self, data: Union[str, bytes]): + def _send(self, data: Union[str, bytes]): """Send raw data Send raw data through the socket diff --git a/ptrlib/connection/tube.py b/ptrlib/connection/tube.py index deb1e6e..4659b5f 100644 --- a/ptrlib/connection/tube.py +++ b/ptrlib/connection/tube.py @@ -20,6 +20,7 @@ class Tube(metaclass=ABCMeta): def __init__(self): self.buf = b'' + self.debug = False @abstractmethod def _settimeout(self, timeout: Optional[Union[int, float]]): @@ -70,6 +71,9 @@ def recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None) -> byt self._settimeout(timeout) data = self._recv(size, timeout=-1) self.buf += data + if self.debug: + logger.info(f"Received {hex(len(data))} ({len(data)}) bytes:") + hexdump(data, prefix=" " + Color.CYAN, postfix=Color.END) # We don't check size > len(self.buf) because Python handles it data, self.buf = self.buf[:size], self.buf[size:] @@ -236,9 +240,15 @@ def recvregex(self, return group, data[:pos] @abstractmethod - def send(self, data: bytes): + def _send(self, data: bytes): pass + def send(self, data: bytes): + self._send(data) + if self.debug: + logger.info(f"Sent {hex(len(data))} ({len(data)}) bytes:") + hexdump(data, prefix=Color.YELLOW, postfix=Color.END) + @abstractmethod def _socket(self) -> Optional[Any]: pass diff --git a/ptrlib/connection/winproc.py b/ptrlib/connection/winproc.py index 7f11503..d510b0f 100644 --- a/ptrlib/connection/winproc.py +++ b/ptrlib/connection/winproc.py @@ -95,7 +95,7 @@ def recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None): return self._recv(min(self.size, size)) - def send(self, data: bytes): + def _send(self, data: bytes): """Send raw data Send raw data through the socket diff --git a/ptrlib/console/color.py b/ptrlib/console/color.py index 714ef8d..673cd17 100644 --- a/ptrlib/console/color.py +++ b/ptrlib/console/color.py @@ -15,7 +15,7 @@ class Color: BOLD = '\033[1m' UNDERLINE = '\033[4m' INVISIBLE = '\033[08m' - REVERCE = '\033[07m' + REVERSE = '\033[07m' class ColoredFormatter(Formatter): def format(self, record: LogRecord):