diff --git a/androscalpel/src/dex_id.rs b/androscalpel/src/dex_id.rs index 165a2de..60b1bf1 100644 --- a/androscalpel/src/dex_id.rs +++ b/androscalpel/src/dex_id.rs @@ -60,7 +60,11 @@ impl IdMethodType { self.parameters.clone() } - fn __hash__(&self) -> u64 { + pub fn __eq__(&self, other: &Self) -> bool { + self == other + } + + pub fn __hash__(&self) -> u64 { let mut hasher = DefaultHasher::new(); self.hash(&mut hasher); hasher.finish() @@ -323,12 +327,16 @@ impl IdType { } } - fn __hash__(&self) -> u64 { + pub fn __hash__(&self) -> u64 { let mut hasher = DefaultHasher::new(); self.hash(&mut hasher); hasher.finish() } + pub fn __eq__(&self, other: &Self) -> bool { + self == other + } + // TODO: TESTS } @@ -366,29 +374,39 @@ impl IdField { pub fn __str__(&self) -> String { let class: String = self.class_.get_name().into(); let name: String = (&self.name).into(); - format!("{class}.{name}") + let ty: String = self.type_.get_name().into(); + format!("({ty}) {class}.{name}") } pub fn __repr__(&self) -> String { - let class: String = self.class_.get_name().into(); + let class: String = self.class_.__repr__(); let name: String = (&self.name).into(); - format!("IdField({class}.{name})") + let ty: String = self.type_.__repr__(); + format!("IdField(({ty}){class}.{name})") } - fn __hash__(&self) -> u64 { + pub fn __eq__(&self, other: &Self) -> bool { + self == other + } + + pub fn __hash__(&self) -> u64 { let mut hasher = DefaultHasher::new(); self.hash(&mut hasher); hasher.finish() } } +/// The Id of a method. #[pyclass] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct IdMethod { + /// The class containing the method. #[pyo3(get, set)] pub class_: IdType, + /// The prototype (aka type or signature) of the method. #[pyo3(get, set)] pub proto: IdMethodType, + /// The name of the method. #[pyo3(get, set)] pub name: DexString, } @@ -396,7 +414,11 @@ pub struct IdMethod { #[pymethods] impl IdMethod { #[new] - pub fn new(class_: IdType, proto: IdMethodType, name: DexString) -> Self { + pub fn new( + #[pyo3(from_py_with = "crate::dex_string::as_dex_string")] name: DexString, + proto: IdMethodType, + class_: IdType, + ) -> Self { Self { class_, proto, @@ -423,7 +445,11 @@ impl IdMethod { format!("DexMethod({})", self.__str__()) } - fn __hash__(&self) -> u64 { + pub fn __eq__(&self, other: &Self) -> bool { + self == other + } + + pub fn __hash__(&self) -> u64 { let mut hasher = DefaultHasher::new(); self.hash(&mut hasher); hasher.finish() diff --git a/androscalpel/src/dex_string.rs b/androscalpel/src/dex_string.rs index f44d53d..28e8bb2 100644 --- a/androscalpel/src/dex_string.rs +++ b/androscalpel/src/dex_string.rs @@ -93,7 +93,7 @@ impl DexString { .extract() .or(::extract(other).map(|string| string.into()))?; match op { - CompareOp::Eq => Ok((self == &other).into_py(py)), + CompareOp::Eq => Ok((self == &other).into_py(py)), // TODO: alpha order? _ => Ok(py.NotImplemented()), } } diff --git a/androscalpel/src/field.rs b/androscalpel/src/field.rs index 05138ef..5781138 100644 --- a/androscalpel/src/field.rs +++ b/androscalpel/src/field.rs @@ -99,9 +99,8 @@ impl Field { } pub fn __repr__(&self) -> String { - let name: String = (&self.descriptor.name).into(); - let ty: String = (&self.descriptor.type_.0).into(); - format!("Field({name}, {ty})") + let descr = self.descriptor.__str__(); + format!("Field({descr})") } /// Return the value as a python object diff --git a/androscalpel/src/lib.rs b/androscalpel/src/lib.rs index d88508e..03b6128 100644 --- a/androscalpel/src/lib.rs +++ b/androscalpel/src/lib.rs @@ -59,6 +59,7 @@ fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; + m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/androscalpel/src/method.rs b/androscalpel/src/method.rs index b1c4531..fa465d5 100644 --- a/androscalpel/src/method.rs +++ b/androscalpel/src/method.rs @@ -99,8 +99,7 @@ impl Method { } pub fn __repr__(&self) -> String { - let name: String = (&self.descriptor.name).into(); - let ty: String = self.descriptor.proto.__str__(); - format!("Method({name}, {ty})") + let dscr = self.descriptor.__str__(); + format!("Method({dscr})") } } diff --git a/test.py b/test.py index 2d523a6..0bf5ba1 100644 --- a/test.py +++ b/test.py @@ -6,6 +6,7 @@ logging.getLogger().setLevel(logging.INFO) import androscalpel as asc import zipfile as z +from androscalpel import * APK_NAME = "test.apk" DEX_NAME = "classes.dex"