From ae0e98ce671086617e840719a87635b8f748595e Mon Sep 17 00:00:00 2001 From: dusoleil Date: Thu, 16 Mar 2023 18:27:29 -0400 Subject: arch: Move private methods to bottom of file Also check type when setting arch. Signed-off-by: dusoleil --- sploit/arch.py | 29 ++++++++++++++++------------- sploit/symtbl.py | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/sploit/arch.py b/sploit/arch.py index 8fc139d..2d95493 100644 --- a/sploit/arch.py +++ b/sploit/arch.py @@ -38,9 +38,11 @@ class Arch: alignment: int nopcode: bytes - def set(self,k): + def set(self,new_arch): """Copy the given Arch into this instance.""" - self.__dict__.update(k.__dict__) + if type(new_arch) is not Arch: + raise TypeError(f'arch: new_arch must be an Arch: {new_arch}') + self.__dict__.update(new_arch.__dict__) x86 = Arch( 4, 'little', 16, b'\x90') x86_64 = Arch( 8, 'little', 16, b'\x90') @@ -49,17 +51,6 @@ THUMB = Arch( 4, 'little', 8, b'\x46\xc0') arch = Arch(**x86_64.__dict__) -def __int(i, signed=False, width=None): - # type conversion from int to int of given sign and width - i = int(i) - width = width or arch.wordsize - bits = 8 * width - if signed: - sign_bit = 1 << (bits - 1) - return (i & (sign_bit - 1)) - (i & sign_bit) - else: - mask = (1 << bits) - 1 - return i & mask def sint(i): """Convert given int to signed int of arch.wordsize width.""" @@ -111,3 +102,15 @@ def itob(i, width=None, byteorder=None): width = width or arch.wordsize byteorder = byteorder or arch.endianness return __int(i,False,width).to_bytes(width, byteorder, signed=False) + +def __int(i, signed=False, width=None): + # type conversion from int to int of given sign and width + i = int(i) + width = width or arch.wordsize + bits = 8 * width + if signed: + sign_bit = 1 << (bits - 1) + return (i & (sign_bit - 1)) - (i & sign_bit) + else: + mask = (1 << bits) - 1 + return i & mask diff --git a/sploit/symtbl.py b/sploit/symtbl.py index 3babd96..4b80a0a 100644 --- a/sploit/symtbl.py +++ b/sploit/symtbl.py @@ -156,7 +156,7 @@ class SymtblImpl: elif symbol in dir(self): raise KeyError(f"Symtbl: key is reserved: {symbol}") elif type(symbol) is not str: - raise KeyError(f"Symtbl: key must be a string: {symbol}") + raise TypeError(f"Symtbl: key must be a string: {symbol}") else: self.__entries__[symbol] = value - (self.base + self.__adjust__) -- cgit v1.2.3