mirror of https://github.com/pwndbg/pwndbg.git
* addressify #2506 * changes for addressify #2506 * fix lint * renaming changes * Update pwndbg/commands/hex2ptr.py * tests format changes --------- Co-authored-by: Disconnect3d <dominik.b.czarnota@gmail.com>pull/2529/head
parent
a534af1c28
commit
cd918e435a
@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
import pwndbg.color.message as M
|
||||
import pwndbg.commands
|
||||
from pwndbg.commands import CommandCategory
|
||||
from pwndbg.lib.common import hex2ptr_common
|
||||
|
||||
# Define an argument parser for the command
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Converts a space-separated hex string to a little-endian address.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"hex_string", type=str, help="Hexadecimal string to convert (e.g., '00 70 75 c1 cd ef 59 00')."
|
||||
)
|
||||
|
||||
|
||||
@pwndbg.commands.ArgparsedCommand(parser, category=CommandCategory.MISC)
|
||||
def hex2ptr(hex_string) -> None:
|
||||
combined_args = hex_string.replace(" ", "")
|
||||
try:
|
||||
result = hex2ptr_common(combined_args)
|
||||
print(M.success(f"{hex(result)}"))
|
||||
except Exception as e:
|
||||
print(M.error(str(e)))
|
||||
@ -0,0 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
# common functions
|
||||
def hex2ptr_common(arg: str) -> int:
|
||||
"""Converts a hex string to a little-endian integer address."""
|
||||
arg = "".join(filter(str.isalnum, arg))
|
||||
if len(arg) % 2 != 0:
|
||||
raise ValueError("Hex string must contain an even number of characters.")
|
||||
try:
|
||||
big_endian_num = int(arg, 16)
|
||||
num_bytes = big_endian_num.to_bytes((len(arg) + 1) // 2, byteorder="big")
|
||||
little_endian_num = int.from_bytes(num_bytes, byteorder="little")
|
||||
except ValueError as e:
|
||||
raise ValueError(f"Invalid hex string: {e}")
|
||||
return little_endian_num
|
||||
@ -0,0 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from pwndbg.lib.common import hex2ptr_common
|
||||
|
||||
|
||||
def test_hex2ptr_common_valid_hex():
|
||||
assert hex2ptr_common("00 70 75 c1 cd ef 59 00") == 0x59EFCDC1757000
|
||||
assert hex2ptr_common("12 34 56 78") == 0x78563412
|
||||
|
||||
|
||||
def test_hex2ptr_common_invalid_hex():
|
||||
# Test for odd-length hex string
|
||||
with pytest.raises(ValueError, match="Hex string must contain an even number of characters."):
|
||||
hex2ptr_common("12345")
|
||||
|
||||
# Test for invalid hex characters
|
||||
with pytest.raises(ValueError, match="Invalid hex string"):
|
||||
hex2ptr_common("zz zz zz")
|
||||
Loading…
Reference in new issue