diff --git a/androscalpel/src/apk.rs b/androscalpel/src/apk.rs index 28f1769..bdfa0d5 100644 --- a/androscalpel/src/apk.rs +++ b/androscalpel/src/apk.rs @@ -2381,4 +2381,8 @@ impl Apk { pub fn from_json(json: &str) -> Result { Ok(serde_json::from_str(json)?) } + + pub fn remove_class(&mut self, class: &IdType) { + self.classes.remove(class); + } } diff --git a/androscalpel/src/lib.rs b/androscalpel/src/lib.rs index 9c5d4b2..c9ad984 100644 --- a/androscalpel/src/lib.rs +++ b/androscalpel/src/lib.rs @@ -14,6 +14,7 @@ mod hashmap_vectorize; pub mod instructions; pub mod method; pub mod method_handle; +pub mod py_utils; pub mod scalar; pub mod value; @@ -76,6 +77,9 @@ fn androscalpel(py: Python, m: &PyModule) -> PyResult<()> { let ins_module = PyModule::new(py, "ins")?; androscalpel_ins(py, ins_module)?; m.add_submodule(ins_module)?; + let utils_module = PyModule::new(py, "utils")?; + py_utils::export_module(py, utils_module)?; + m.add_submodule(utils_module)?; Ok(()) } diff --git a/androscalpel/src/py_utils.rs b/androscalpel/src/py_utils.rs new file mode 100644 index 0000000..5c2fee5 --- /dev/null +++ b/androscalpel/src/py_utils.rs @@ -0,0 +1,53 @@ +//! Function that are can be usefull on the python side. + +use pyo3::prelude::*; +use pyo3::types::PyBytes; + +use crate::Result; +use androscalpel_serializer::{Serializable, Sleb128, Uleb128, Uleb128p1}; + +/// Convert an integer to the uleb128 byte encoding +#[pyfunction] +pub fn int_to_uleb128(py: Python, x: u32) -> Result { + Ok(PyBytes::new(py, &Uleb128(x).serialize_to_vec()?).into()) +} + +/// Convert an integer to the uleb128p1 byte encoding +#[pyfunction] +pub fn int_to_uleb128p1(py: Python, x: u32) -> Result { + Ok(PyBytes::new(py, &Uleb128p1(x).serialize_to_vec()?).into()) +} +/// Convert an integer to the sleb128 byte encoding +#[pyfunction] +pub fn int_to_sleb128(py: Python, x: i32) -> Result { + Ok(PyBytes::new(py, &Sleb128(x).serialize_to_vec()?).into()) +} + +/// Decode an uleb128 encoded integer +#[pyfunction] +pub fn uleb128_to_int(b: &[u8]) -> Result { + Ok(Uleb128::deserialize_from_slice(b)?.0) +} + +/// Decode an uleb128p1 encoded integer +#[pyfunction] +pub fn uleb128p1_to_int(b: &[u8]) -> Result { + Ok(Uleb128p1::deserialize_from_slice(b)?.0) +} + +/// Decode an sleb128 encoded integer +#[pyfunction] +pub fn sleb128_to_int(b: &[u8]) -> Result { + Ok(Sleb128::deserialize_from_slice(b)?.0) +} + +/// export the function in a python module +pub(crate) fn export_module(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(int_to_uleb128, m)?)?; + m.add_function(wrap_pyfunction!(int_to_uleb128p1, m)?)?; + m.add_function(wrap_pyfunction!(int_to_sleb128, m)?)?; + m.add_function(wrap_pyfunction!(uleb128_to_int, m)?)?; + m.add_function(wrap_pyfunction!(uleb128p1_to_int, m)?)?; + m.add_function(wrap_pyfunction!(sleb128_to_int, m)?)?; + Ok(()) +}