add utils functions for python

This commit is contained in:
Jean-Marie 'Histausse' Mineau 2024-01-27 01:06:52 +01:00
parent 53457cbb97
commit 0f87b75e8a
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
3 changed files with 61 additions and 0 deletions

View file

@ -2381,4 +2381,8 @@ impl Apk {
pub fn from_json(json: &str) -> Result<Self> { pub fn from_json(json: &str) -> Result<Self> {
Ok(serde_json::from_str(json)?) Ok(serde_json::from_str(json)?)
} }
pub fn remove_class(&mut self, class: &IdType) {
self.classes.remove(class);
}
} }

View file

@ -14,6 +14,7 @@ mod hashmap_vectorize;
pub mod instructions; pub mod instructions;
pub mod method; pub mod method;
pub mod method_handle; pub mod method_handle;
pub mod py_utils;
pub mod scalar; pub mod scalar;
pub mod value; pub mod value;
@ -76,6 +77,9 @@ fn androscalpel(py: Python, m: &PyModule) -> PyResult<()> {
let ins_module = PyModule::new(py, "ins")?; let ins_module = PyModule::new(py, "ins")?;
androscalpel_ins(py, ins_module)?; androscalpel_ins(py, ins_module)?;
m.add_submodule(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(()) Ok(())
} }

View file

@ -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<PyObject> {
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<PyObject> {
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<PyObject> {
Ok(PyBytes::new(py, &Sleb128(x).serialize_to_vec()?).into())
}
/// Decode an uleb128 encoded integer
#[pyfunction]
pub fn uleb128_to_int(b: &[u8]) -> Result<u32> {
Ok(Uleb128::deserialize_from_slice(b)?.0)
}
/// Decode an uleb128p1 encoded integer
#[pyfunction]
pub fn uleb128p1_to_int(b: &[u8]) -> Result<u32> {
Ok(Uleb128p1::deserialize_from_slice(b)?.0)
}
/// Decode an sleb128 encoded integer
#[pyfunction]
pub fn sleb128_to_int(b: &[u8]) -> Result<i32> {
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(())
}