blueloveTH 2 سال پیش
والد
کامیت
e32a907336
2فایلهای تغییر یافته به همراه23 افزوده شده و 1 حذف شده
  1. 4 0
      src/c.pyi
  2. 19 1
      src/cffi.h

+ 4 - 0
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: ...

+ 19 - 1
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<intptr_t>(ptr);
+        return "0x" + ss.str();
+    }
+
     static void _register(VM* vm, PyObject* mod, PyObject* type){
         vm->bind_default_constructor<VoidP>(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 << "<void* at " << self.ptr;
+            ss << "<void* at " << self.hex();
             if(self.base_offset != 1) ss << ", base_offset=" << self.base_offset;
             ss << ">";
             return VAR(ss.str());