diff --git a/include/typings/c.pyi b/include/typings/c.pyi index e23c200d..1676da81 100644 --- a/include/typings/c.pyi +++ b/include/typings/c.pyi @@ -59,7 +59,7 @@ class struct: def hex(self) -> str: ... @staticmethod - def from_hex(s: str) -> 'struct': ... + def fromhex(s: str) -> 'struct': ... def read_char(self, offset=0) -> int: ... def read_uchar(self, offset=0) -> int: ... diff --git a/python/pickle.py b/python/pickle.py index 35976a2b..8eb63b08 100644 --- a/python/pickle.py +++ b/python/pickle.py @@ -143,7 +143,7 @@ class _Unpickler: # generic object cls = _find_class(o[0]) if getattr(cls, '__struct__', False): - inst = cls.from_struct(struct.from_hex(o[1])) + inst = cls.from_struct(struct.fromhex(o[1])) self.tag(index, inst) return inst else: diff --git a/src/cffi.cpp b/src/cffi.cpp index 6cbc6470..44f26637 100644 --- a/src/cffi.cpp +++ b/src/cffi.cpp @@ -47,17 +47,16 @@ namespace pkpy{ vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){ const C99Struct& self = _CAST(C99Struct&, args[0]); SStream ss; - ss << "0x"; for(int i=0; ibind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){ + vm->bind_func<1>(type, "fromhex", [](VM* vm, ArgsView args){ const Str& s = CAST(Str&, args[0]); - if(s.size<4 || s[0]!='0' || s[1]!='x' || s.size%2!=0) vm->ValueError("invalid hex string"); - C99Struct buffer(s.size/2-1, false); - for(int i=2; iValueError("invalid hex string"); + C99Struct buffer(s.size/2, false); + for(int i=0; i='0' && s[i]<='9') c += s[i]-'0'; else if(s[i]>='A' && s[i]<='F') c += s[i]-'A'+10; @@ -66,7 +65,7 @@ namespace pkpy{ if(s[i+1]>='0' && s[i+1]<='9') c += s[i+1]-'0'; else if(s[i+1]>='A' && s[i+1]<='F') c += s[i+1]-'A'+10; else vm->ValueError(fmt("invalid hex char: '", s[i+1], "'")); - buffer.p[i/2-1] = c; + buffer.p[i/2] = c; } return VAR_T(C99Struct, std::move(buffer)); }, {}, BindType::STATICMETHOD); diff --git a/tests/80_c.py b/tests/80_c.py index 2143320d..69c6d7d0 100644 --- a/tests/80_c.py +++ b/tests/80_c.py @@ -68,7 +68,7 @@ for i in range(67): s.write_char(i, i) s_hex = s.hex() -s_r = c.struct.from_hex(s_hex) +s_r = c.struct.fromhex(s_hex) assert (s == s_r and s is not s_r), (s_hex, s_r.hex()) assert s_hex == s_r.hex()