use log instead of print

This commit is contained in:
Jean-Marie 'Histausse' Mineau 2023-09-01 15:03:11 +02:00
parent 625420c5f6
commit 0ae6ce5e88
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
10 changed files with 54 additions and 19 deletions

26
Cargo.lock generated
View file

@ -7,7 +7,9 @@ name = "androscalpel"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"androscalpel_serializer", "androscalpel_serializer",
"log",
"pyo3", "pyo3",
"pyo3-log",
] ]
[[package]] [[package]]
@ -15,6 +17,7 @@ name = "androscalpel_serializer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"androscalpel_serializer_derive", "androscalpel_serializer_derive",
"log",
] ]
[[package]] [[package]]
@ -27,6 +30,12 @@ dependencies = [
"syn 2.0.29", "syn 2.0.29",
] ]
[[package]]
name = "arc-swap"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6"
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -67,6 +76,12 @@ dependencies = [
"scopeguard", "scopeguard",
] ]
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]] [[package]]
name = "memoffset" name = "memoffset"
version = "0.9.0" version = "0.9.0"
@ -151,6 +166,17 @@ dependencies = [
"pyo3-build-config", "pyo3-build-config",
] ]
[[package]]
name = "pyo3-log"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f47b0777feb17f61eea78667d61103758b243a871edc09a7786500a50467b605"
dependencies = [
"arc-swap",
"log",
"pyo3",
]
[[package]] [[package]]
name = "pyo3-macros" name = "pyo3-macros"
version = "0.19.2" version = "0.19.2"

View file

@ -1,5 +1,4 @@
- edditable format - edditable format
- python binding
- sanity checks - sanity checks
- tests - tests
- https://source.android.com/docs/core/runtime/dex-format#system-annotation - https://source.android.com/docs/core/runtime/dex-format#system-annotation

View file

@ -10,4 +10,6 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
androscalpel_serializer = { version = "0.1.0", path = "../androscalpel_serializer" } androscalpel_serializer = { version = "0.1.0", path = "../androscalpel_serializer" }
log = "0.4.20"
pyo3 = "0.19.0" pyo3 = "0.19.0"
pyo3-log = "0.8.3"

View file

@ -1,4 +1,5 @@
//! Representation of an apk. //! Representation of an apk.
use log::info;
use pyo3::prelude::*; use pyo3::prelude::*;
use crate::{Class, DexString, Error, Result}; use crate::{Class, DexString, Error, Result};
@ -92,11 +93,11 @@ impl Apk {
| ACC_ENUM)) | ACC_ENUM))
!= 0 != 0
{ {
println!( info!(
"Unexpected flags found in class_def_item.access_flags for {}: 0x{:x}", "Unexpected flags found in class_def_item.access_flags for {}: 0x{:x}",
<&DexString as Into<String>>::into(&name), <&DexString as Into<String>>::into(&name),
class_item.access_flags class_item.access_flags
); // TODO: better logging );
} }
self.classes.push(Class { self.classes.push(Class {
name, name,

View file

@ -14,7 +14,7 @@ pub struct Class {
pub name: DexString, pub name: DexString,
/// If the class is visible everywhere /// If the class is visible everywhere
#[pyo3(get, set)] #[pyo3(get, set)]
pub is_public: bool, // TODO: is it possible to not be public non a non inner class? pub is_public: bool,
/// If the class is subclassable /// If the class is subclassable
#[pyo3(get, set)] #[pyo3(get, set)]
pub is_final: bool, pub is_final: bool,

View file

@ -145,6 +145,7 @@ impl DexString {
/// Androscalpel. /// Androscalpel.
#[pymodule] #[pymodule]
fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> { fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> {
pyo3_log::init();
m.add_class::<Class>()?; m.add_class::<Class>()?;
m.add_class::<Apk>()?; m.add_class::<Apk>()?;
m.add_class::<DexString>()?; m.add_class::<DexString>()?;

View file

@ -5,3 +5,4 @@ edition = "2021"
[dependencies] [dependencies]
androscalpel_serializer_derive = { path = "../androscalpel_serializer_derive" } androscalpel_serializer_derive = { path = "../androscalpel_serializer_derive" }
log = "0.4.20"

View file

@ -1,4 +1,5 @@
use crate::{EncodedValue, Error, ReadSeek, Result, Serializable, Uleb128}; use crate::{EncodedValue, Error, ReadSeek, Result, Serializable, Uleb128};
use log::warn;
use std::io::Write; use std::io::Write;
// To derive Serializable // To derive Serializable
@ -60,9 +61,8 @@ impl Serializable for EncodedAnnotation {
let mut idx_0 = elements[0].name_idx; let mut idx_0 = elements[0].name_idx;
for idx_1 in elements.iter().map(|elt| elt.name_idx).skip(1) { for idx_1 in elements.iter().map(|elt| elt.name_idx).skip(1) {
if idx_0 > idx_1 { if idx_0 > idx_1 {
// TODO: use proper logging warn!(
println!( "while deserializing EncodedAnnotation: element must be sorted in increasing order by string_id index, found {idx_0:?} before {idx_1:?}"
"Waring while deserializing EncodedAnnotation: element must be sorted in increasing order by string_id index, found {idx_0:?} before {idx_1:?}"
); );
} }
idx_0 = idx_1; idx_0 = idx_1;

View file

@ -5,6 +5,7 @@ use crate::{
MapList, MethodHandleItem, MethodIdItem, ProtoIdItem, Result, Serializable, StringDataItem, MapList, MethodHandleItem, MethodIdItem, ProtoIdItem, Result, Serializable, StringDataItem,
StringIdItem, TypeIdItem, StringIdItem, TypeIdItem,
}; };
use log::{error, info, warn};
use std::io::{Cursor, Seek, SeekFrom}; use std::io::{Cursor, Seek, SeekFrom};
pub struct DexFileReader<'a> { pub struct DexFileReader<'a> {
@ -144,32 +145,32 @@ impl<'a> DexFileReader<'a> {
fn sanity_check(&self) -> Result<()> { fn sanity_check(&self) -> Result<()> {
if self.header.magic.version != [0x30, 0x33, 0x39] { if self.header.magic.version != [0x30, 0x33, 0x39] {
println!( warn!(
"DEX 039 is the only verion currently supported, found {}", "DEX 039 is the only verion currently supported, found {}",
std::str::from_utf8(self.header.magic.version.as_slice()) std::str::from_utf8(self.header.magic.version.as_slice())
.unwrap_or(&format!("{:x?}", self.header.magic.version)) .unwrap_or(&format!("{:x?}", self.header.magic.version))
); // TODO: use proper logging );
} }
// TODO: check checksum // TODO: check checksum
// TODO: check signature // TODO: check signature
if self.header.file_size as usize != self.data.len() { if self.header.file_size as usize != self.data.len() {
println!( info!(
"Unexpected file size found: {}, expected {}", "Unexpected file size found: {}, expected {}",
self.header.file_size, self.header.file_size,
self.data.len() self.data.len()
); // TODO: use proper logging );
} }
if self.header.header_size != 0x70 { if self.header.header_size != 0x70 {
println!( info!(
"Unexpected header size found: 0x{:x}", "Unexpected header size found: 0x{:x}",
self.header.header_size self.header.header_size
); // TODO: use proper logging );
} }
if self.header.endian_tag != EndianConstant::EndianConstant { if self.header.endian_tag != EndianConstant::EndianConstant {
println!("Wrong endian_tag found: {:x?}", self.header.endian_tag); // TODO: use proper logging warn!("Wrong endian_tag found: {:x?}", self.header.endian_tag);
} }
if self.header.link_off != 0 || self.header.link_size != 0 { if self.header.link_off != 0 || self.header.link_size != 0 {
println!("Found non empty link section"); // TODO: use proper logging info!("Found non empty link section, the section will be ignored");
} }
for item in &self.map_list.list { for item in &self.map_list.list {
match item.type_ { match item.type_ {
@ -261,8 +262,7 @@ impl<'a> DexFileReader<'a> {
MapItemType::HiddenapiClassDataItem => todo!(), MapItemType::HiddenapiClassDataItem => todo!(),
*/ */
MapItemType::UnkownType(ty) => { MapItemType::UnkownType(ty) => {
println!("Unknown Type found in map_list: 0x{ty:04x}"); // TODO: use proper info!("Unknown Type found in map_list: 0x{ty:04x}, it will be ignored");
// loggin
} }
_ => (), _ => (),
} }
@ -273,8 +273,7 @@ impl<'a> DexFileReader<'a> {
let mut duplicate = false; let mut duplicate = false;
for (ty, val) in occurences { for (ty, val) in occurences {
if val > 1 { if val > 1 {
println!("Found multiple {} occurence of {:?} in map_list", val, ty); error!("Found multiple {} occurence of {:?} in map_list", val, ty);
// TODO: use proper loggin
duplicate = true; duplicate = true;
} }
} }

View file

@ -1,3 +1,9 @@
import logging
FORMAT = "[%(levelname)s] %(name)s %(filename)s:%(lineno)d: %(message)s"
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.INFO)
import androscalpel as asc import androscalpel as asc
import zipfile as z import zipfile as z