diff --git a/src/adler32.rs b/src/adler32.rs index c37578a..494831e 100644 --- a/src/adler32.rs +++ b/src/adler32.rs @@ -57,21 +57,22 @@ mod tests { #[test] fn test_verify_valid_from_bytes() { // Test data with valid checksum - let bytes: [u8; 16] = [0x44, 0x45, 0x58, 0x0a, + let bytes = Cursor::new(vec![0x44, 0x45, 0x58, 0x0a, 0x30, 0x33, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00]; - let checksum: u32 = 0x14300184; + 0x00, 0x00, 0x00, 0x00]); + let checksum: u32 = 0x00040001; + println!("{}", verify_from_bytes(&bytes, checksum).unwrap()); assert!(verify_from_bytes(&bytes, checksum).unwrap()); } #[test] fn test_verify_invalid_from_bytes() { // Test data with invalid checksum - let bytes: [u8; 16] = [0x44, 0x45, 0x58, 0x0a, + let bytes = Cursor::new(vec![0x44, 0x45, 0x58, 0x0a, 0x30, 0x33, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00]; + 0x00, 0x00, 0x00, 0x00]); let checksum: u32 = 0xcafebabe; assert_eq!(verify_from_bytes(&bytes, checksum).unwrap_err().to_string(), "[adler32] error: computed checksum does not match one in header"); diff --git a/src/error.rs b/src/error.rs index c396c33..292fcaa 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,23 +1,32 @@ +//! Collection of error types for DEX files parsing +//! +//! Note: this mostly unused as of now but might be useful in the future. + use std::error::Error; use std::fmt; +/// A generic error type #[derive(Debug)] pub struct DexError { + /// Error message pub message: String, } impl DexError { + /// Create a new error with the given message pub fn new(msg: &str) -> DexError { DexError { message: msg.to_string() } } } +/// Implement `Display` trait for `DexError` impl fmt::Display for DexError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f,"{}", self.message) } } +/// Implement `Error` trait for `DexError` impl Error for DexError { fn description(&self) -> &str { &self.message diff --git a/src/lib.rs b/src/lib.rs index 1f1d339..fba761c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,31 +4,17 @@ use crate::dex::reader::DexReader; use crate::dex::file::DexFile; use crate::dex::instructions::Instructions; -// pub mod logging; pub mod dex; pub mod error; pub mod adler32; -pub mod mutf8; - -/* Actually unused for now but there should - * be more options as things progress */ -pub struct Config { - pub log_level: u8, -} - -impl Default for Config { - fn default() -> Config { - Config { - log_level: 0 // only show error messages - } - } -} +/// Parse an APK and create a `DexFile` object from the embedded class(es) files pub fn parse(filepath: &str) -> DexFile { let readers = DexReader::build_from_file(filepath); DexFile::merge(readers) } +/// Return the list of qualified method names from a `DexFile` object pub fn get_qualified_method_names(dex: &DexFile) -> Vec { let mut methods = Vec::new(); @@ -45,6 +31,7 @@ pub fn get_qualified_method_names(dex: &DexFile) -> Vec { methods } +/// Get the list of instructions for the given method pub fn get_bytecode_for_method(dex: &DexFile, class_name: &String, method_name: &String) -> Option> {