From e32a907336c66eeab653c6fcadd7129951c1f4f0 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 27 May 2023 15:01:32 +0800 Subject: [PATCH] ... --- src/c.pyi | 4 ++++ src/cffi.h | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/c.pyi b/src/c.pyi index e0f2640e..4b81f103 100644 --- a/src/c.pyi +++ b/src/c.pyi @@ -20,6 +20,10 @@ class void_p: def __ne__(self, other: 'void_p') -> bool: ... def offset(self, i: int) -> 'void_p': ... + def hex(self) -> str: ... + @staticmethod + def from_hex(s: str) -> 'void_p': ... + def read_char(self) -> int: ... def read_uchar(self) -> int: ... def read_short(self) -> int: ... diff --git a/src/cffi.h b/src/cffi.h index 221e517e..1d64b6bd 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -46,13 +46,31 @@ struct VoidP{ return ptr != other.ptr || base_offset != other.base_offset; } + Str hex() const{ + std::stringstream ss; + ss << std::hex << reinterpret_cast(ptr); + return "0x" + ss.str(); + } + static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_default_constructor(type); + vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){ + std::string s = CAST(Str&, args[0]).str(); + size_t size; + intptr_t ptr = std::stoll(s, &size, 16); + if(size != s.size()) vm->ValueError("invalid literal for void_p(): " + s); + return VAR_T(VoidP, (void*)ptr); + }); + vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){ + VoidP& self = _CAST(VoidP&, args[0]); + return VAR(self.hex()); + }); + vm->bind__repr__(OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ VoidP& self = _CAST(VoidP&, obj); std::stringstream ss; - ss << ""; return VAR(ss.str());