fix == for ids

This commit is contained in:
Jean-Marie Mineau 2023-11-29 18:15:11 +01:00
parent 67efc6365d
commit 026b9ddd41
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
6 changed files with 41 additions and 15 deletions

View file

@ -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()

View file

@ -93,7 +93,7 @@ impl DexString {
.extract()
.or(<String as FromPyObject>::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()),
}
}

View file

@ -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

View file

@ -59,6 +59,7 @@ fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<DexAnnotation>()?;
m.add_class::<DexArray>()?;
m.add_class::<FieldVisibility>()?;
m.add_class::<MethodVisibility>()?;
m.add_class::<Method>()?;
m.add_class::<Field>()?;
m.add_class::<Class>()?;

View file

@ -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})")
}
}

View file

@ -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"