add help functions

This commit is contained in:
Jean-Marie Mineau 2024-04-24 11:27:21 +02:00
parent f8a7d8907b
commit c27a047f89
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
4 changed files with 53 additions and 8 deletions

View file

@ -101,10 +101,8 @@ impl Code {
strings.extend(ins.get_all_strings()); strings.extend(ins.get_all_strings());
} }
if let Some(names) = &self.parameter_names { if let Some(names) = &self.parameter_names {
for name in names { for name in names.iter().flatten() {
if let Some(name) = name { strings.insert(name.clone());
strings.insert(name.clone());
}
} }
} }
strings strings

View file

@ -14907,7 +14907,7 @@ impl InvokeVirtual {
vd, vd,
vf, vf,
vg, vg,
b: meth_idx as u16, b: meth_idx,
} }
} else if consec && len <= 255 { } else if consec && len <= 255 {
let a = self.args.len() as u8; let a = self.args.len() as u8;

View file

@ -1,3 +1,6 @@
#![allow(clippy::unnecessary_fallible_conversions)]
// DexString has Into<String> but it's only for
// python, TryInto should be prefered
use anyhow::Result; use anyhow::Result;
use pyo3::prelude::*; use pyo3::prelude::*;

View file

@ -3,11 +3,15 @@
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyBytes; use pyo3::types::PyBytes;
use std::io::Cursor; use std::collections::HashSet;
use std::io::{Cursor, Seek, SeekFrom};
use std::path::PathBuf; use std::path::PathBuf;
use crate::Result; use crate::{Apk, IdType, Result};
use androscalpel_serializer::{Serializable, Sleb128, Uleb128, Uleb128p1}; use androscalpel_serializer::{
DexFileReader, HeaderItem, Serializable, Sleb128, Uleb128, Uleb128p1,
};
use apk_frauder::{end_of_central_directory::EndCentralDirectory, ZipFileReader};
/// Convert an integer to the uleb128 byte encoding /// Convert an integer to the uleb128 byte encoding
#[pyfunction] #[pyfunction]
@ -44,6 +48,46 @@ pub fn sleb128_to_int(b: &[u8]) -> Result<i32> {
Ok(Sleb128::deserialize_from_slice(b)?.0) Ok(Sleb128::deserialize_from_slice(b)?.0)
} }
// TODO: list_defined_classes, is_dex, is_zip take &[u8], but should allow to also read from file
/// List all classes defined in a dex file.
#[pyfunction]
pub fn list_defined_classes(dex: &[u8]) -> Result<HashSet<IdType>> {
let dex = DexFileReader::new(dex)?;
dex.get_class_defs()
.iter()
.map(|cdef| Apk::get_id_type_from_idx(cdef.class_idx as usize, &dex))
.collect()
}
/// Test if a file is as .dex file an return the dex version if it is, else return None.
#[pyfunction]
pub fn is_dex(file: &[u8]) -> Option<usize> {
HeaderItem::deserialize_from_slice(file)
.ok()
.and_then(|header| String::from_utf8(header.magic.version.to_vec()).ok())
.and_then(|version| version.parse::<usize>().ok())
}
/// Test if a file is a zip file.
#[pyfunction]
pub fn is_zip(file: &[u8]) -> bool {
let mut file = Cursor::new(file);
let ecd_off = if let Some(off) = ZipFileReader::get_end_of_central_directory_offset(&mut file) {
off
} else {
return false;
};
if file.seek(SeekFrom::Start(ecd_off)).is_err() {
return false;
}
if let Ok(sig) = apk_frauder::Signature::deserialize(&mut file) {
EndCentralDirectory::SIGNATURE == sig
} else {
false
}
}
/// Replace the dex files a an apk an resigned the apk. /// Replace the dex files a an apk an resigned the apk.
/// ///
/// # Warning /// # Warning