diff --git a/Cargo.lock b/Cargo.lock index ee1479e..e2f1c5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 3 name = "androscalpel" version = "0.1.0" dependencies = [ + "androscalpel_serializer", "pyo3", ] diff --git a/androscalpel/Cargo.toml b/androscalpel/Cargo.toml index 0136047..655050f 100644 --- a/androscalpel/Cargo.toml +++ b/androscalpel/Cargo.toml @@ -9,4 +9,5 @@ name = "androscalpel" crate-type = ["cdylib"] [dependencies] +androscalpel_serializer = { version = "0.1.0", path = "../androscalpel_serializer" } pyo3 = "0.19.0" diff --git a/androscalpel/src/apk.rs b/androscalpel/src/apk.rs index a5b4562..188a598 100644 --- a/androscalpel/src/apk.rs +++ b/androscalpel/src/apk.rs @@ -1,9 +1,9 @@ //! Representation of an apk. - -use crate::Class; - use pyo3::prelude::*; +use crate::{Class, Result}; +use androscalpel_serializer::DexFileReader; + /// Represent an apk. #[pyclass] #[derive(Debug, Clone)] @@ -14,8 +14,9 @@ pub struct Apk { impl Apk { /// Add the content of a dex file to the apk. - pub fn add_dex_file(&mut self) { - todo!() + pub fn add_dex_file(&mut self, data: &[u8]) -> Result<()> { + let dex = DexFileReader::new(data)?; + Ok(()) } } diff --git a/androscalpel/src/lib.rs b/androscalpel/src/lib.rs index 7ba3268..644c3f4 100644 --- a/androscalpel/src/lib.rs +++ b/androscalpel/src/lib.rs @@ -1,3 +1,4 @@ +use pyo3::exceptions::PyValueError; use pyo3::prelude::*; pub mod apk; @@ -6,6 +7,62 @@ pub mod class; pub use apk::*; pub use class::*; +#[derive(Debug)] +pub enum Error { + InputTooSmall(String), + SerializationError(String), + DeserializationError(String), + InvalidStringEncoding(String), + InconsistantStruct(String), +} + +impl From for Error { + fn from(error: androscalpel_serializer::Error) -> Self { + match error { + androscalpel_serializer::Error::InputTooSmall(err) => Self::InputTooSmall(err), + androscalpel_serializer::Error::SerializationError(err) => { + Self::SerializationError(err) + } + androscalpel_serializer::Error::DeserializationError(err) => { + Self::DeserializationError(err) + } + androscalpel_serializer::Error::InvalidStringEncoding(err) => { + Self::InvalidStringEncoding(err) + } + androscalpel_serializer::Error::InconsistantStruct(err) => { + Self::InconsistantStruct(err) + } + } + } +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Self::InputTooSmall(msg) => write!(f, "InputTooSmall: {}", msg), + Self::SerializationError(msg) => write!(f, "SerializationError: {}", msg), + Self::DeserializationError(msg) => write!(f, "DeserializationError: {}", msg), + Self::InvalidStringEncoding(msg) => write!(f, "InvalidStringEncoding: {}", msg), + Self::InconsistantStruct(msg) => write!(f, "InconsistantStruct: {}", msg), + } + } +} +impl std::error::Error for Error {} + +impl From for PyErr { + fn from(error: Error) -> Self { + match error { + Error::InputTooSmall(err) => PyValueError::new_err(format!("{err}")), + Error::SerializationError(err) => PyValueError::new_err(format!("{err}")), + Error::DeserializationError(err) => PyValueError::new_err(format!("{err}")), + Error::InvalidStringEncoding(err) => PyValueError::new_err(format!("{err}")), + Error::InconsistantStruct(err) => PyValueError::new_err(format!("{err}")), + } + } +} + +pub type Result = core::result::Result; + /// Androscalpel. #[pymodule] fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> { diff --git a/androscalpel_serializer/src/core/mod.rs b/androscalpel_serializer/src/core/mod.rs index dc4c9db..5ccb3bf 100644 --- a/androscalpel_serializer/src/core/mod.rs +++ b/androscalpel_serializer/src/core/mod.rs @@ -27,11 +27,11 @@ impl ReadSeek for T {} impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - Self::InputTooSmall(msg) => write!(f, "Error: {}", msg), - Self::SerializationError(msg) => write!(f, "Error: {}", msg), - Self::DeserializationError(msg) => write!(f, "Error: {}", msg), - Self::InvalidStringEncoding(msg) => write!(f, "Error: {}", msg), - Self::InconsistantStruct(msg) => write!(f, "Error: {}", msg), + Self::InputTooSmall(msg) => write!(f, "{}", msg), + Self::SerializationError(msg) => write!(f, "{}", msg), + Self::DeserializationError(msg) => write!(f, "{}", msg), + Self::InvalidStringEncoding(msg) => write!(f, "{}", msg), + Self::InconsistantStruct(msg) => write!(f, "{}", msg), } } }