remove manual imprementation of some functions

This commit is contained in:
Jean-Marie Mineau 2025-01-23 12:36:51 +01:00
parent eb89441dad
commit 83b3330aa2
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
11 changed files with 32 additions and 131 deletions

View file

@ -11,7 +11,7 @@ use crate::{
}; };
/// Annotation with a visibility /// Annotation with a visibility
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DexAnnotationItem { pub struct DexAnnotationItem {
// TODO: the get/set will probably be wonky on the python side when edditing // TODO: the get/set will probably be wonky on the python side when edditing
@ -32,10 +32,6 @@ pub struct DexAnnotationItem {
#[pymethods] #[pymethods]
impl DexAnnotationItem { impl DexAnnotationItem {
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
#[new] #[new]
pub fn new(annotation: DexAnnotation) -> Self { pub fn new(annotation: DexAnnotation) -> Self {
Self { Self {
@ -126,7 +122,7 @@ impl<V: VisitorMut> VisitableMut<V> for DexAnnotationItem {
} }
/// An annotation. /// An annotation.
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DexAnnotation { pub struct DexAnnotation {
// TODO: check the relation between type and encoded_value. // TODO: check the relation between type and encoded_value.
@ -142,10 +138,6 @@ pub struct DexAnnotation {
#[pymethods] #[pymethods]
impl DexAnnotation { impl DexAnnotation {
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
#[new] #[new]
pub fn new(type_: IdType, elements: HashMap<DexString, DexValue>) -> Self { pub fn new(type_: IdType, elements: HashMap<DexString, DexValue>) -> Self {
Self { type_, elements } Self { type_, elements }

View file

@ -28,7 +28,7 @@ pub struct DexFile {
} }
/// Represent an apk. /// Represent an apk.
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
pub struct Apk { pub struct Apk {
pub dex_files: HashMap<String, DexFile>, // TODO: use accessort for chache invalidation pub dex_files: HashMap<String, DexFile>, // TODO: use accessort for chache invalidation
@ -3087,10 +3087,6 @@ impl Apk {
Ok(()) Ok(())
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
#[pyo3(name = "gen_raw_dex")] //Sad GIL noise #[pyo3(name = "gen_raw_dex")] //Sad GIL noise
pub fn py_gen_raw_dex(&self, py: Python<'_>) -> Result<HashMap<String, PyObject>> { pub fn py_gen_raw_dex(&self, py: Python<'_>) -> Result<HashMap<String, PyObject>> {
Ok(self Ok(self

View file

@ -12,7 +12,7 @@ use crate::{
use androscalpel_serializer::consts::*; use androscalpel_serializer::consts::*;
/// Represent an apk /// Represent an apk
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Class { pub struct Class {
/// Type, format described at /// Type, format described at
@ -382,10 +382,6 @@ impl Class {
} }
flags flags
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }
impl<V: Visitor> Visitable<V> for Class { impl<V: Visitor> Visitable<V> for Class {

View file

@ -16,7 +16,7 @@ use crate::{
// type TmpHandlerType = (Vec<(IdType, u32)>, Option<u32>); // type TmpHandlerType = (Vec<(IdType, u32)>, Option<u32>);
/// The code run by a method. /// The code run by a method.
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Code { pub struct Code {
// TODO: remove and compute this value from code? // TODO: remove and compute this value from code?
@ -39,6 +39,7 @@ pub struct Code {
pub insns: Vec<Instruction>, pub insns: Vec<Instruction>,
} }
// TODO reimplement PartialEq: label should become address independant
impl PartialEq for Code { impl PartialEq for Code {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
let comparable_self = self.semantic_comparable().unwrap(); let comparable_self = self.semantic_comparable().unwrap();
@ -50,8 +51,6 @@ impl PartialEq for Code {
} }
} }
// TODO reimplement PartialEq: label should become address independant
#[pymethods] #[pymethods]
impl Code { impl Code {
pub fn to_json(&self) -> Result<String> { pub fn to_json(&self) -> Result<String> {
@ -150,10 +149,6 @@ impl Code {
handles handles
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
/// Return all the labels used by instrutions in the code. /// Return all the labels used by instrutions in the code.
pub fn get_referenced_label(&self) -> HashSet<String> { pub fn get_referenced_label(&self) -> HashSet<String> {
let mut used_labels = HashSet::new(); let mut used_labels = HashSet::new();

View file

@ -2,12 +2,10 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::cmp::{Ord, Ordering, PartialOrd}; use std::cmp::{Ord, Ordering, PartialOrd};
use std::collections::hash_map::DefaultHasher;
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::{Hash, Hasher}; use std::hash::Hash;
use anyhow::{anyhow, bail, Context}; use anyhow::{anyhow, bail, Context};
use pyo3::class::basic::CompareOp;
use pyo3::prelude::*; use pyo3::prelude::*;
use crate::{scalar::*, DexString, DexValue, Result, Visitable, VisitableMut, Visitor, VisitorMut}; use crate::{scalar::*, DexString, DexValue, Result, Visitable, VisitableMut, Visitor, VisitorMut};
@ -15,7 +13,7 @@ use androscalpel_serializer::{StringDataItem, Uleb128};
/// The type of a method. The shorty is formated as described in /// The type of a method. The shorty is formated as described in
/// <https://source.android.com/docs/core/runtime/dex-format#shortydescriptor> /// <https://source.android.com/docs/core/runtime/dex-format#shortydescriptor>
#[pyclass] #[pyclass(eq, ord, hash, frozen)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IdMethodType { pub struct IdMethodType {
/// Type formated as described by <https://source.android.com/docs/core/runtime/dex-format#shortydescriptor> /// Type formated as described by <https://source.android.com/docs/core/runtime/dex-format#shortydescriptor>
@ -167,12 +165,6 @@ impl IdMethodType {
self.parameters.clone() self.parameters.clone()
} }
pub fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
hasher.finish()
}
/// Return all strings referenced in the Id. /// Return all strings referenced in the Id.
pub fn get_all_strings(&self) -> HashSet<DexString> { pub fn get_all_strings(&self) -> HashSet<DexString> {
let mut strings = HashSet::new(); let mut strings = HashSet::new();
@ -198,10 +190,6 @@ impl IdMethodType {
protos.insert(self.clone()); protos.insert(self.clone());
protos protos
} }
fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool {
op.matches(self.cmp(other))
}
} }
impl SmaliName for IdMethodType { impl SmaliName for IdMethodType {
@ -241,7 +229,7 @@ impl IdMethodType {
/// as described here <https://source.android.com/docs/core/runtime/dex-format#typedescriptor> /// as described here <https://source.android.com/docs/core/runtime/dex-format#typedescriptor>
// Not a clean rust enum because we want to be compatible with python, and maybe support strange // Not a clean rust enum because we want to be compatible with python, and maybe support strange
// malware edge case? // malware edge case?
#[pyclass] #[pyclass(eq, ord, hash, frozen)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct IdType(pub(crate) DexString); pub struct IdType(pub(crate) DexString);
@ -620,12 +608,6 @@ impl IdType {
} }
} }
pub fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
hasher.finish()
}
/// Return all strings referenced in the Id. /// Return all strings referenced in the Id.
pub fn get_all_strings(&self) -> HashSet<DexString> { pub fn get_all_strings(&self) -> HashSet<DexString> {
let mut strings = HashSet::new(); let mut strings = HashSet::new();
@ -639,10 +621,6 @@ impl IdType {
types.insert(self.clone()); types.insert(self.clone());
types types
} }
fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool {
op.matches(self.cmp(other))
}
// TODO: TESTS // TODO: TESTS
} }
@ -658,7 +636,7 @@ impl SmaliName for IdType {
} }
} }
#[pyclass] #[pyclass(eq, ord, hash, frozen)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IdField { pub struct IdField {
/// The name of the field, format described at /// The name of the field, format described at
@ -770,12 +748,6 @@ impl IdField {
format!("IdField(\"{name}\", {ty}, {class})") format!("IdField(\"{name}\", {ty}, {class})")
} }
pub fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
hasher.finish()
}
/// Return all strings referenced in the Id. /// Return all strings referenced in the Id.
pub fn get_all_strings(&self) -> HashSet<DexString> { pub fn get_all_strings(&self) -> HashSet<DexString> {
let mut strings = HashSet::new(); let mut strings = HashSet::new();
@ -799,10 +771,6 @@ impl IdField {
fields.insert(self.clone()); fields.insert(self.clone());
fields fields
} }
fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool {
op.matches(self.cmp(other))
}
} }
impl Ord for IdField { impl Ord for IdField {
@ -835,7 +803,7 @@ impl SmaliName for IdField {
} }
/// The Id of a method. /// The Id of a method.
#[pyclass] #[pyclass(eq, ord, hash, frozen)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IdMethod { pub struct IdMethod {
/// The class containing the method. /// The class containing the method.
@ -966,12 +934,6 @@ impl IdMethod {
) )
} }
pub fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
hasher.finish()
}
/// Return all strings referenced in the Id. /// Return all strings referenced in the Id.
pub fn get_all_strings(&self) -> HashSet<DexString> { pub fn get_all_strings(&self) -> HashSet<DexString> {
let mut strings = HashSet::new(); let mut strings = HashSet::new();
@ -1002,10 +964,6 @@ impl IdMethod {
method_ids.insert(self.clone()); method_ids.insert(self.clone());
method_ids method_ids
} }
fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool {
op.matches(self.cmp(other))
}
} }
impl Ord for IdMethod { impl Ord for IdMethod {
@ -1040,8 +998,8 @@ impl SmaliName for IdMethod {
} }
} }
#[pyclass] #[pyclass(eq, ord, hash, frozen)]
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)] #[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct IdEnum(pub IdField); pub struct IdEnum(pub IdField);
impl<V: Visitor> Visitable<V> for IdEnum { impl<V: Visitor> Visitable<V> for IdEnum {
@ -1098,10 +1056,6 @@ impl IdEnum {
pub fn get_all_field_ids(&self) -> HashSet<IdField> { pub fn get_all_field_ids(&self) -> HashSet<IdField> {
self.0.get_all_field_ids() self.0.get_all_field_ids()
} }
fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool {
op.matches(self.cmp(other))
}
} }
// Not to sure about this one // Not to sure about this one

View file

@ -1,15 +1,13 @@
use crate::{Result, Visitable, VisitableMut, Visitor, VisitorMut}; use crate::{Result, Visitable, VisitableMut, Visitor, VisitorMut};
use pyo3::exceptions::PyTypeError;
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::{Ord, PartialOrd}; use std::cmp::{Ord, PartialOrd};
use std::collections::hash_map::DefaultHasher;
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::{Hash, Hasher}; use std::hash::Hash;
use pyo3::class::basic::CompareOp;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*; use pyo3::prelude::*;
#[pyclass] #[pyclass(eq, ord, frozen, hash)]
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd)] #[derive(Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct DexString(pub androscalpel_serializer::StringDataItem); pub struct DexString(pub androscalpel_serializer::StringDataItem);
@ -198,23 +196,10 @@ impl DexString {
self.into() self.into()
} }
fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
hasher.finish()
}
/// Return all strings references in the value. /// Return all strings references in the value.
pub fn get_all_strings(&self) -> HashSet<DexString> { pub fn get_all_strings(&self) -> HashSet<DexString> {
let mut strings = HashSet::new(); let mut strings = HashSet::new();
strings.insert(self.clone()); strings.insert(self.clone());
strings strings
} }
fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> PyResult<bool> {
let other: Self = other
.extract()
.or(<String as FromPyObject>::extract_bound(other).map(|string| string.into()))?;
Ok(op.matches(self.0.cmp(&other.0)))
}
} }

View file

@ -12,7 +12,7 @@ use crate::{
use androscalpel_serializer::consts::*; use androscalpel_serializer::consts::*;
/// Represent a field. /// Represent a field.
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Field { pub struct Field {
/// The structure used to reference this field. /// The structure used to reference this field.
@ -252,10 +252,6 @@ impl Field {
pub fn has_annotations(&self) -> bool { pub fn has_annotations(&self) -> bool {
!self.annotations.is_empty() !self.annotations.is_empty()
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }
impl<V: Visitor> Visitable<V> for Field { impl<V: Visitor> Visitable<V> for Field {

View file

@ -31,9 +31,8 @@ const I32_MIN_AS_I64: i64 = i32::MIN as i64;
const I32_MAX_AS_I64: i64 = i32::MAX as i64; const I32_MAX_AS_I64: i64 = i32::MAX as i64;
const U16_MAX_AS_USIZE: usize = u16::MAX as usize; const U16_MAX_AS_USIZE: usize = u16::MAX as usize;
// TODO: impl Eq for call site to derive here? #[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[pyclass]
pub enum Instruction { pub enum Instruction {
/// Waste a cycle. /// Waste a cycle.
Nop {}, Nop {},
@ -2097,10 +2096,6 @@ macro_rules! raw_ins_invoke {
#[pymethods] #[pymethods]
impl Instruction { impl Instruction {
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
pub fn to_json(&self) -> Result<String> { pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(self)?) Ok(serde_json::to_string(self)?)
} }
@ -6601,7 +6596,7 @@ impl Instruction {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct CallSite { pub struct CallSite {
#[pyo3(get)] #[pyo3(get)]
@ -6616,10 +6611,6 @@ pub struct CallSite {
#[pymethods] #[pymethods]
impl CallSite { impl CallSite {
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
pub fn to_json(&self) -> Result<String> { pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(self)?) Ok(serde_json::to_string(self)?)
} }

View file

@ -12,7 +12,7 @@ use crate::{
use androscalpel_serializer::consts::*; use androscalpel_serializer::consts::*;
/// Represent a method. /// Represent a method.
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Method { pub struct Method {
/// The structure used to reference this method. /// The structure used to reference this method.
@ -293,10 +293,6 @@ impl Method {
.iter() .iter()
.any(|list| !list.is_empty()) .any(|list| !list.is_empty())
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }
impl<V: Visitor> Visitable<V> for Method { impl<V: Visitor> Visitable<V> for Method {

View file

@ -13,8 +13,8 @@ use crate::{
}; };
/// The structure use to reference a method invocation. /// The structure use to reference a method invocation.
#[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[pyclass]
pub enum MethodHandle { pub enum MethodHandle {
StaticPut { field: IdField }, StaticPut { field: IdField },
StaticGet { field: IdField }, StaticGet { field: IdField },

View file

@ -10,7 +10,7 @@ use crate::{
}; };
use pyo3::prelude::*; use pyo3::prelude::*;
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct DexByte(pub i8); pub struct DexByte(pub i8);
#[pymethods] #[pymethods]
@ -42,7 +42,7 @@ impl DexByte {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct DexShort(pub i16); pub struct DexShort(pub i16);
#[pymethods] #[pymethods]
@ -74,7 +74,7 @@ impl DexShort {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct DexChar(pub u16); pub struct DexChar(pub u16);
#[pymethods] #[pymethods]
@ -106,7 +106,7 @@ impl DexChar {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct DexInt(pub i32); pub struct DexInt(pub i32);
#[pymethods] #[pymethods]
@ -138,7 +138,7 @@ impl DexInt {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct DexLong(pub i64); pub struct DexLong(pub i64);
#[pymethods] #[pymethods]
@ -170,7 +170,7 @@ impl DexLong {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub struct DexFloat(pub f32); pub struct DexFloat(pub f32);
#[pymethods] #[pymethods]
@ -202,7 +202,7 @@ impl DexFloat {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub struct DexDouble(pub f64); pub struct DexDouble(pub f64);
#[pymethods] #[pymethods]
@ -235,7 +235,7 @@ impl DexDouble {
} }
/* DexString is already define in lib.rs, TODO: move the version in lib.rs here /* DexString is already define in lib.rs, TODO: move the version in lib.rs here
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DexString(pub u32); pub struct DexString(pub u32);
#[pymethods] #[pymethods]
@ -268,7 +268,7 @@ impl DexString {
} }
*/ */
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct DexNull; pub struct DexNull;
#[pymethods] #[pymethods]
@ -296,7 +296,7 @@ impl DexNull {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct DexBoolean(pub bool); pub struct DexBoolean(pub bool);
#[pymethods] #[pymethods]
@ -328,7 +328,7 @@ impl DexBoolean {
} }
} }
#[pyclass] #[pyclass(eq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DexArray(pub Vec<DexValue>); pub struct DexArray(pub Vec<DexValue>);
#[pymethods] #[pymethods]