bump pyo3 and use anyhow for main functions

This commit is contained in:
Jean-Marie Mineau 2023-11-28 16:22:40 +01:00
parent 4e57289bab
commit b8b4e28f2d
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
10 changed files with 401 additions and 115 deletions

View file

@ -10,6 +10,7 @@ crate-type = ["cdylib"]
[dependencies]
androscalpel_serializer = { version = "0.1.0", path = "../androscalpel_serializer" }
anyhow = { version = "1.0.75", features = ["backtrace"] }
log = "0.4.20"
pyo3 = "0.19.0"
pyo3 = { version = "0.20.0", features = ["anyhow"] }
pyo3-log = "0.8.3"

View file

@ -1,5 +1,6 @@
//! Representation of an apk.
use anyhow::{anyhow, Result};
use std::collections::HashMap;
use log::info;
@ -8,10 +9,6 @@ use pyo3::prelude::*;
use crate::*;
use androscalpel_serializer::*;
// Prioritize Results from this crate
use crate::Error;
use crate::Result;
/// Represent an apk.
#[pyclass]
#[derive(Debug, Clone)]
@ -124,14 +121,14 @@ impl Apk {
.get_struct_at_offset::<EncodedArray>(class_item.static_values_off)?
.values;
if values.len() > static_fields.len() {
return Err(Error::InconsistantStruct(format!(
return Err(anyhow!(
"Inconsistant static_values array found in {}: \
|static_values| = {}, |static_fields| = {}, \
|static_values| should be <= |static_fields|",
<&DexString as Into<String>>::into(&name),
values.len(),
static_fields.len()
)));
));
}
for (i, value) in values.iter().enumerate() {
static_fields[i].value = Some(Self::encoded_value_to_dex_value(value, dex)?);
@ -210,11 +207,14 @@ impl Apk {
let parameters: Vec<IdType> = if proto.parameters_off == 0 {
vec![]
} else {
dex.get_struct_at_offset::<TypeList>(proto.parameters_off)?
let mut parameters = vec![];
for ty in dex
.get_struct_at_offset::<TypeList>(proto.parameters_off)?
.list
.iter()
.map(|ty| Self::get_id_type_from_idx(ty.type_idx as usize, dex).clone())
.collect::<Result<_>>()?
{
parameters.push(Self::get_id_type_from_idx(ty.type_idx as usize, dex)?);
}
parameters
};
Ok(IdMethodType {
shorty,
@ -372,10 +372,10 @@ impl Apk {
(pbl, prv, prt) => {
let class: String = descriptor.class_.0.into();
let name: String = descriptor.name.into();
return Err(Error::InconsistantStruct(format!(
return Err(anyhow!(
"Inconsistant visiblity found in {class}.{name}: \
(public: {pbl}, private: {prv}, protected: {prt})"
)));
));
}
};
if access_flags
@ -474,11 +474,11 @@ impl Apk {
(false, false, true) => MethodVisibility::Protected,
(false, false, false) => MethodVisibility::None_,
(pbl, prv, prt) => {
return Err(Error::InconsistantStruct(format!(
return Err(anyhow!(
"Inconsistant visiblity found in {}: \
(public: {pbl}, private: {prv}, protected: {prt})",
descriptor.__repr__()
)));
));
// TODO: replace by public?
}
};

View file

@ -1,8 +1,9 @@
//! The class identifying dex structure.
use anyhow::anyhow;
use pyo3::prelude::*;
use crate::{DexString, Error, Result};
use crate::{DexString, Result};
use androscalpel_serializer::{StringDataItem, Uleb128};
#[pyclass]
@ -289,9 +290,7 @@ impl IdType {
Ok(())
} else {
let format: String = (&self.0).into();
Err(Error::InconsistantStruct(format!(
"{format} is not a valid type"
)))
Err(anyhow!("{format} is not a valid type"))
}
}

View file

@ -1,5 +1,6 @@
use anyhow::Result;
use pyo3::class::basic::CompareOp;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
pub mod annotation;
@ -22,62 +23,6 @@ pub use method_handle::*;
pub use scalar::*;
pub use value::*;
#[derive(Debug, Clone)]
pub enum Error {
InputTooSmall(String),
SerializationError(String),
DeserializationError(String),
InvalidStringEncoding(String),
InconsistantStruct(String),
}
impl From<androscalpel_serializer::Error> 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<Error> for PyErr {
fn from(error: Error) -> Self {
match error {
Error::InputTooSmall(err) => PyValueError::new_err(err.to_string()),
Error::SerializationError(err) => PyValueError::new_err(err.to_string()),
Error::DeserializationError(err) => PyValueError::new_err(err.to_string()),
Error::InvalidStringEncoding(err) => PyValueError::new_err(err.to_string()),
Error::InconsistantStruct(err) => PyValueError::new_err(err.to_string()),
}
}
}
pub type Result<T> = core::result::Result<T, Error>;
#[pyclass]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct DexString(androscalpel_serializer::StringDataItem);