dex_checksum_tools

Struct Dex

Source
pub struct Dex { /* private fields */ }
Expand description

A Dex structure that holds the bytes of a DEX (Dalvik Executable) file.

§Fields

  • bytes - A vector of bytes representing the contents of the DEX file.

§Examples

use dex_checksum_tools::Dex;

if let Ok(mut dex) = Dex::try_from("/path/to/incorrect.dex") {
    println!("Before Correcting: current_checksum {:?}", dex.current_checksum());
    println!("Before Correcting: check_checksum {:?}", dex.check_checksum());
    println!("Before Correcting: expect_checksum {:?}", dex.expect_checksum());
    dex.correct_checksum();
    println!("After Correcting: current_checksum {:?}", dex.current_checksum());
    println!("After Correcting: check_checksum {:?}", dex.check_checksum());
    println!("After Correcting: expect_checksum {:?}", dex.expect_checksum());
    match dex.write_to_file("/path/to/correct.dex") {
        Ok(_) => println!("Successfully wrote to file!"),
        Err(e) => println!("Failed to write to file: {}", e),
    }
}

Implementations§

Source§

impl Dex

Source

pub fn current_checksum(&self) -> [u8; 4]

Calculates the current checksum from the DEX file’s header.

This method extracts the checksum bytes that are stored at offset 8 through 11 in the DEX file header and converts them into a 4-byte array. The checksum is a part of the file’s integrity verification and should match the expected checksum calculated over the rest of the file.

§Returns

A 4-byte array representing the checksum stored in the DEX file header.

§Panics

Panics if the slice of bytes cannot be converted into an array, which indicates an issue with the DEX file format.

Source

pub fn expect_checksum(&self) -> [u8; 4]

Calculates the expected checksum for the DEX file.

This method computes the Adler-32 checksum for the data part of the DEX file starting from byte 12 to the end of the file. The Adler-32 checksum is used for error-checking of the data. It should match the current checksum in the file header for the file to be considered valid.

§Returns

A 4-byte array representing the expected checksum for the DEX file.

§Errors

Returns an error if the Adler-32 checksum cannot be calculated.

Source

pub fn check_checksum(&self) -> bool

Checks if the current checksum matches the expected checksum.

This method compares the current checksum from the file’s header with the expected checksum calculated over the data part of the file.

§Returns
  • true if the checksums match.
  • false otherwise.
Source

pub fn correct_checksum(&mut self) -> bool

Corrects the checksum in the DEX file header if it does not match the expected checksum.

This method calculates the expected checksum using the expect_checksum method and updates the bytes in the DEX file header if the current checksum is incorrect. After calling this method, the checksum in the file header should match the expected checksum for the data part of the DEX file.

§Returns
  • true if the checksum was uncorrected.
  • false otherwise.
Source

pub fn write_to_file(&self, path: &str) -> Result<()>

Writes the DEX file’s bytes to the specified path.

This function creates a new file at the given path and writes the bytes representing the DEX file to it. If the file already exists, it will be truncated before writing.

§Arguments
  • path - A string slice that holds the path where the file will be written.
§Returns

An io::Result<()> which is Ok if the file was written successfully, or an Err with more information if the file could not be written.

Trait Implementations§

Source§

impl Debug for Dex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Dex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl TryFrom<&str> for Dex

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(path: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<File> for Dex

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(f: File) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for Dex

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(path: String) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Dex

§

impl RefUnwindSafe for Dex

§

impl Send for Dex

§

impl Sync for Dex

§

impl Unpin for Dex

§

impl UnwindSafe for Dex

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.