This commit is contained in:
Jean-Marie 'Histausse' Mineau 2023-08-31 16:01:31 +02:00
parent 559ae665cf
commit 68b11dc036
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
5 changed files with 91 additions and 12 deletions

View file

@ -1,8 +1,8 @@
//! Representation of an apk.
use pyo3::prelude::*;
use crate::{Class, Result};
use androscalpel_serializer::{ClassDefItem, DexFileReader};
use crate::{Class, Error, Result};
use androscalpel_serializer::{ClassDefItem, DexFileReader, TypeList, NO_INDEX};
/// Represent an apk.
#[pyclass]
@ -15,7 +15,10 @@ pub struct Apk {
impl Apk {
/// Add the content of a dex file to the apk.
pub fn add_dex_file(&mut self, data: &[u8]) -> Result<()> {
println!("Start parsing file");
let dex = DexFileReader::new(data)?;
println!("Header and lists parsed");
println!("Start parsing classes");
for class in dex.get_class_defs() {
self.add_class_from_dex_file(class, &dex)?;
}
@ -23,8 +26,65 @@ impl Apk {
}
/// Add a class from a dex file reader.
fn add_class_from_dex_file(&mut self, class: &ClassDefItem, dex: &DexFileReader) -> Result<()> {
todo!()
fn add_class_from_dex_file(
&mut self,
class_item: &ClassDefItem,
dex: &DexFileReader,
) -> Result<()> {
let name_idx = dex
.get_type_ids()
.get(class_item.class_idx as usize)
.ok_or(Error::InconsistantStruct(format!(
"type idx {} out of bound of type_ids (|type_ids| = {})",
class_item.class_idx,
dex.get_type_ids().len()
)))?
.descriptor_idx;
let name = dex.get_string(name_idx)?.into();
let superclass = if class_item.class_idx == NO_INDEX.0 {
None
} else {
let superclass_idx = dex
.get_type_ids()
.get(class_item.class_idx as usize)
.ok_or(Error::InconsistantStruct(format!(
"type idx {} out of bound of type_ids (|type_ids| = {})",
class_item.class_idx,
dex.get_type_ids().len()
)))?
.descriptor_idx;
Some(dex.get_string(superclass_idx)?.into())
};
let interfaces = if class_item.interfaces_off == 0 {
vec![]
} else {
let type_list = dex.get_struct_at_offset::<TypeList>(class_item.interfaces_off)?;
let mut list = vec![];
for ty in type_list.list {
let ty = dex.get_type_ids().get(ty.type_idx as usize).ok_or(
Error::InconsistantStruct(format!(
"type idx {} out of bound of type_ids (|type_ids| = {})",
ty.type_idx,
dex.get_type_ids().len()
)),
)?;
let ty = dex.get_string(ty.descriptor_idx)?.into();
list.push(ty);
}
list
};
let source_file = if class_item.source_file_idx == NO_INDEX.0 {
None
} else {
Some(dex.get_string(class_item.source_file_idx)?.into())
};
self.classes.push(Class {
name,
superclass,
interfaces,
source_file,
});
Ok(())
}
}
@ -32,6 +92,11 @@ impl Apk {
impl Apk {
#[new]
fn new() -> Self {
println!("Create APK");
Self { classes: vec![] }
}
fn py_add_dex_file(&mut self, data: &[u8]) -> Result<()> {
self.add_dex_file(data)
}
}

View file

@ -10,8 +10,6 @@ use crate::DexString;
pub struct Class {
#[pyo3(get, set)]
pub name: DexString,
#[pyo3(get, set)]
pub ty: DexString,
// pub access_flags: // TODO
#[pyo3(get, set)]
pub superclass: Option<DexString>,
@ -28,10 +26,9 @@ pub struct Class {
#[pymethods]
impl Class {
#[new]
pub fn new(name: DexString, ty: DexString) -> Self {
pub fn new(name: DexString) -> Self {
Self {
name,
ty,
superclass: None,
interfaces: vec![],
source_file: None,