first python binding

This commit is contained in:
Jean-Marie 'Histausse' Mineau 2023-08-31 18:03:55 +02:00
parent a9da701e51
commit cfc8e4743e
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
4 changed files with 53 additions and 9 deletions

View file

@ -15,10 +15,7 @@ pub struct Apk {
impl Apk { impl Apk {
/// Add the content of a dex file to the apk. /// Add the content of a dex file to the apk.
pub fn add_dex_file(&mut self, data: &[u8]) -> Result<()> { pub fn add_dex_file(&mut self, data: &[u8]) -> Result<()> {
println!("Start parsing file");
let dex = DexFileReader::new(data)?; let dex = DexFileReader::new(data)?;
println!("Header and lists parsed");
println!("Start parsing classes");
for class in dex.get_class_defs() { for class in dex.get_class_defs() {
self.add_class_from_dex_file(class, &dex)?; self.add_class_from_dex_file(class, &dex)?;
} }
@ -92,10 +89,10 @@ impl Apk {
impl Apk { impl Apk {
#[new] #[new]
fn new() -> Self { fn new() -> Self {
println!("Create APK");
Self { classes: vec![] } Self { classes: vec![] }
} }
#[pyo3(name = "add_dex_file")]
fn py_add_dex_file(&mut self, data: &[u8]) -> Result<()> { fn py_add_dex_file(&mut self, data: &[u8]) -> Result<()> {
self.add_dex_file(data) self.add_dex_file(data)
} }

View file

@ -35,11 +35,35 @@ impl Class {
} }
} }
pub fn __str__(&self) -> DexString { pub fn __str__(&self) -> String {
self.name.clone() let name: String = (&self.name).into();
let file = if let Some(file) = &self.source_file {
let file: String = file.into();
format!(" defined in {file}\n")
} else {
"".into()
};
let superclass = if let Some(spcl) = &self.superclass {
let spcl: String = spcl.into();
format!(" extends: {spcl}\n")
} else {
"".into()
};
let interfaces = if self.interfaces.is_empty() {
"".into()
} else {
let mut interfaces: String = " implements:\n".into();
for it in &self.interfaces {
let it: String = it.into();
interfaces += &format!(" {it}\n");
}
interfaces
};
format!("{name}\n{file}{superclass}{interfaces}")
} }
pub fn __repr__(&self) -> DexString { pub fn __repr__(&self) -> String {
self.name.clone() (&self.name).into()
} }
} }

View file

@ -79,6 +79,30 @@ impl From<androscalpel_serializer::StringDataItem> for DexString {
} }
} }
impl From<&DexString> for String {
fn from(DexString(string): &DexString) -> Self {
string
.try_into()
.unwrap_or(format!("InvalidEncoding:{:x?}", string.data))
}
}
impl From<DexString> for String {
fn from(string: DexString) -> Self {
(&string).into()
}
}
#[pymethods]
impl DexString {
pub fn __str__(&self) -> String {
self.into()
}
pub fn __repr__(&self) -> String {
self.into()
}
}
/// Androscalpel. /// Androscalpel.
#[pymodule] #[pymodule]
fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> { fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> {

View file

@ -25,7 +25,6 @@ impl<'a> DexFileReader<'a> {
pub fn new(data: &'a [u8]) -> Result<Self> { pub fn new(data: &'a [u8]) -> Result<Self> {
let mut buffer = Cursor::new(data); let mut buffer = Cursor::new(data);
let header = HeaderItem::deserialize(&mut buffer)?; let header = HeaderItem::deserialize(&mut buffer)?;
println!("{header:x?}");
let mut tmp_file = Self { let mut tmp_file = Self {
data, data,
header, header,