Skip to content

Commit

Permalink
More doc and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukhio committed Jun 27, 2024
1 parent a56dd65 commit f7d76bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
11 changes: 6 additions & 5 deletions src/adler32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 3 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let mut methods = Vec::new();

Expand All @@ -45,6 +31,7 @@ pub fn get_qualified_method_names(dex: &DexFile) -> Vec<String> {
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<Vec<Instructions>> {
Expand Down

0 comments on commit f7d76bc

Please sign in to comment.