add array support to dex values

This commit is contained in:
Jean-Marie Mineau 2023-10-03 16:44:45 +02:00
parent feff847310
commit d69036d3b5
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
5 changed files with 69 additions and 9 deletions

View file

@ -1,4 +1,8 @@
- enum
- method
- code
- edditable format - edditable format
- sanity checks - sanity checks
- tests - tests
- DexValues will become a problem ? (eg need to clone the vector for the array)
- https://source.android.com/docs/core/runtime/dex-format#system-annotation - https://source.android.com/docs/core/runtime/dex-format#system-annotation

View file

@ -269,7 +269,12 @@ impl Apk {
dex, dex,
)?)), )?)),
EncodedValue::Enum(val) => Ok(DexValue::Enum(IdEnum(*val))), EncodedValue::Enum(val) => Ok(DexValue::Enum(IdEnum(*val))),
EncodedValue::Array(_val) => todo!(), //Ok(DexValue::Array(DexArray(*val))), EncodedValue::Array(arr) => Ok(DexValue::Array(DexArray(
arr.values
.iter()
.map(|val| Self::encoded_value_to_dex_value(val, dex))
.collect::<Result<_>>()?,
))),
EncodedValue::Annotation(_val) => todo!(), //Ok(DexValue::Annotation(DexAnnotation(*val))), EncodedValue::Annotation(_val) => todo!(), //Ok(DexValue::Annotation(DexAnnotation(*val))),
EncodedValue::Null => Ok(DexValue::Null(DexNull)), EncodedValue::Null => Ok(DexValue::Null(DexNull)),
EncodedValue::Boolean(val) => Ok(DexValue::Boolean(DexBoolean(*val))), EncodedValue::Boolean(val) => Ok(DexValue::Boolean(DexBoolean(*val))),

View file

@ -276,3 +276,20 @@ impl IntoPy<PyObject> for MethodHandle {
} }
} }
} }
impl MethodHandle {
// Not exposed to python, but meh, let's keep it coherent
pub fn __str__(&self) -> String {
match self {
Self::StaticPut(val) => val.__str__(),
Self::StaticGet(val) => val.__str__(),
Self::InstancePut(val) => val.__str__(),
Self::InstanceGet(val) => val.__str__(),
Self::InvokeStatic(val) => val.__str__(),
Self::InvokeInstance(val) => val.__str__(),
Self::InvokeConstructor(val) => val.__str__(),
Self::InvokeDirect(val) => val.__str__(),
Self::InvokeInterface(val) => val.__str__(),
}
}
}

View file

@ -1,5 +1,6 @@
//! The class identifying dex structure. //! The class identifying dex structure.
use crate::DexValue;
use pyo3::prelude::*; use pyo3::prelude::*;
// TODO: move DexString here // TODO: move DexString here
@ -233,24 +234,31 @@ impl DexBoolean {
} }
#[pyclass] #[pyclass]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone)]
pub struct DexArray; pub struct DexArray(pub Vec<DexValue>);
#[pymethods] #[pymethods]
impl DexArray { impl DexArray {
#[new] #[new]
pub fn _new() -> Self { pub fn _new(arr: Vec<DexValue>) -> Self {
Self Self(arr)
} }
pub fn get_value(&self) { pub fn get_value(&self) -> Vec<DexValue> {
todo!() self.0.clone() // TODO: this will becom a problem...
} }
pub fn __str__(&self) -> String { pub fn __str__(&self) -> String {
self.__repr__() format!(
"[{}]",
self.0
.iter()
.map(|val| val.__str__())
.collect::<Vec<String>>()
.join(", ")
)
} }
pub fn __repr__(&self) -> String { pub fn __repr__(&self) -> String {
"DexArray".into() "DexArray()".into()
} }
} }

View file

@ -74,6 +74,32 @@ impl<'source> FromPyObject<'source> for DexValue {
} }
} }
impl DexValue {
// Not exposed to python, but meh, let's keep it coherent
pub fn __str__(&self) -> String {
match self {
DexValue::Byte(val) => val.__str__(),
DexValue::Short(val) => val.__str__(),
DexValue::Char(val) => val.__str__(),
DexValue::Int(val) => val.__str__(),
DexValue::Long(val) => val.__str__(),
DexValue::Float(val) => val.__str__(),
DexValue::Double(val) => val.__str__(),
DexValue::MethodType(val) => val.__str__(),
DexValue::MethodHandle(val) => val.__str__(),
DexValue::String(val) => val.__str__(),
DexValue::Type(val) => val.__str__(),
DexValue::Field(val) => val.__str__(),
DexValue::Method(val) => val.__str__(),
DexValue::Enum(val) => val.__str__(),
DexValue::Array(val) => val.__str__(),
DexValue::Annotation(val) => val.__str__(),
DexValue::Null(val) => val.__str__(),
DexValue::Boolean(val) => val.__str__(),
}
}
}
impl IntoPy<PyObject> for DexValue { impl IntoPy<PyObject> for DexValue {
fn into_py(self, py: Python<'_>) -> PyObject { fn into_py(self, py: Python<'_>) -> PyObject {
match self { match self {