Skip to content

Commit

Permalink
Merge pull request #802 from Integral-Tech/string-refactor
Browse files Browse the repository at this point in the history
refactor: replace &String with &str to enhance generality
  • Loading branch information
devttys0 authored Dec 22, 2024
2 parents 9d7db82 + 8a9f4b8 commit 481ed5d
Show file tree
Hide file tree
Showing 34 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
//! }
//!
//! /// This function extracts the contents of a FooBar file
//! pub fn extract_foobar_file(file_data: Vec<u8>, offset: usize, output_directory: Option<&String>) -> ExtractionResult {
//! pub fn extract_foobar_file(file_data: Vec<u8>, offset: usize, output_directory: Option<&str>) -> ExtractionResult {
//!
//! // This will be the return value
//! let mut result = ExtractionResult{..Default::default()};
Expand Down
4 changes: 2 additions & 2 deletions src/extractors/androidsparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn android_sparse_extractor() -> Extractor {
pub fn extract_android_sparse(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTFILE_NAME: &str = "unsparsed.img";

Expand Down Expand Up @@ -102,7 +102,7 @@ fn extract_chunk(
sparse_header: &androidsparse::AndroidSparseHeader,
chunk_header: &androidsparse::AndroidSparseChunkHeader,
chunk_data: &[u8],
outfile: &String,
outfile: &str,
chroot: &Chroot,
) -> bool {
if chunk_header.is_raw {
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/arcadyan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn obfuscated_lzma_extractor() -> Extractor {
pub fn extract_obfuscated_lzma(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const LZMA_DATA_OFFSET: usize = 4;
const MIN_DATA_SIZE: usize = 0x100;
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/autel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn autel_extractor() -> Extractor {
pub fn autel_deobfuscate(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTPUT_FILE_NAME: &str = "autel.decoded";

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn bzip2_extractor() -> Extractor {
pub fn bzip2_decompressor(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// Size of decompression buffer
const BLOCK_SIZE: usize = 900 * 1024;
Expand Down
20 changes: 10 additions & 10 deletions src/extractors/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct ExtractionError;

/// Built-in internal extractors must provide a function conforming to this definition.
/// Arguments: file_data, offset, output_directory.
pub type InternalExtractor = fn(&[u8], usize, Option<&String>) -> ExtractionResult;
pub type InternalExtractor = fn(&[u8], usize, Option<&str>) -> ExtractionResult;

/// Enum to define either an Internal or External extractor type
#[derive(Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd)]
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Chroot {
/// assert_eq!(std::path::Path::new(&chroot_dir).exists(), true);
/// # std::fs::remove_dir_all(&chroot_dir);
/// ```
pub fn new(chroot_directory: Option<&String>) -> Chroot {
pub fn new(chroot_directory: Option<&str>) -> Chroot {
let mut chroot_instance = Chroot {
..Default::default()
};
Expand All @@ -121,7 +121,7 @@ impl Chroot {
chroot_instance.chroot_directory = pathbuf.display().to_string();
}
Err(_) => {
chroot_instance.chroot_directory = chroot_dir.clone();
chroot_instance.chroot_directory = chroot_dir.to_string();
}
}
}
Expand Down Expand Up @@ -781,7 +781,7 @@ impl Chroot {
}

/// Returns true if the file path is a symlink.
fn is_symlink(&self, file_path: &String) -> bool {
fn is_symlink(&self, file_path: &str) -> bool {
if let Ok(metadata) = fs::symlink_metadata(file_path) {
return metadata.file_type().is_symlink();
}
Expand Down Expand Up @@ -856,7 +856,7 @@ impl Chroot {

/// Recursively walks a given directory and returns a list of regular non-zero size files in the given directory path.
#[allow(dead_code)]
pub fn get_extracted_files(directory: &String) -> Vec<String> {
pub fn get_extracted_files(directory: &str) -> Vec<String> {
let mut regular_files: Vec<String> = vec![];

for entry in WalkDir::new(directory).into_iter() {
Expand Down Expand Up @@ -884,7 +884,7 @@ pub fn get_extracted_files(directory: &String) -> Vec<String> {
/// Executes an extractor for the provided SignatureResult.
pub fn execute(
file_data: &[u8],
file_path: &String,
file_path: &str,
signature: &SignatureResult,
extractor: &Option<Extractor>,
) -> ExtractionResult {
Expand Down Expand Up @@ -992,8 +992,8 @@ pub fn execute(
/// Spawn an external extractor process.
fn spawn(
file_data: &[u8],
file_path: &String,
output_directory: &String,
file_path: &str,
output_directory: &str,
signature: &SignatureResult,
mut extractor: Extractor,
) -> Result<ProcInfo, std::io::Error> {
Expand Down Expand Up @@ -1143,7 +1143,7 @@ fn proc_wait(mut worker_info: ProcInfo) -> Result<ExtractionResult, ExtractionEr
}

// Create an output directory in which to place extraction results
fn create_output_directory(file_path: &String, offset: usize) -> Result<String, std::io::Error> {
fn create_output_directory(file_path: &str, offset: usize) -> Result<String, std::io::Error> {
let chroot = Chroot::new(None);

// Output directory will be: <file_path.extracted/<hex offset>
Expand All @@ -1167,7 +1167,7 @@ fn create_output_directory(file_path: &String, offset: usize) -> Result<String,

/// Returns true if the size of the provided extractor output directory is greater than zero.
/// Note that any intermediate/carved files must be deleted *before* calling this function.
fn was_something_extracted(output_directory: &String) -> bool {
fn was_something_extracted(output_directory: &str) -> bool {
let output_directory_path = path::Path::new(output_directory);
debug!("Checking output directory {} for results", output_directory);

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/csman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn csman_extractor() -> Extractor {
pub fn extract_csman_dat(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const COMPRESSED_HEADER_SIZE: usize = 2;

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/dahua_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn dahua_zip_extractor() -> Extractor {
pub fn extract_dahua_zip(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTFILE_NAME: &str = "dahua.zip";
const ZIP_HEADER: &[u8] = b"PK";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/dlink_tlv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn dlink_tlv_extractor() -> Extractor {
pub fn extract_dlink_tlv_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTPUT_FILE_NAME: &str = "image.bin";

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/dlke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn dlke_extractor() -> Extractor {
pub fn extract_dlke_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const ENCRYPTED_FILE_NAME: &str = "encrypted.bin";
const SIGNATURE_FILE_NAME: &str = "signature.bin";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/dtb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn dtb_extractor() -> Extractor {
pub fn extract_dtb(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
let mut heirerarchy: Vec<String> = Vec::new();

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/dxbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn dxbc_extractor() -> Extractor {
pub fn extract_dxbc_file(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTFILE_NAME: &str = "shader.dxbc";

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/gif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn gif_extractor() -> Extractor {
pub fn extract_gif_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTFILE_NAME: &str = "image.gif";

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/gpg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn gpg_extractor() -> Extractor {
pub fn gpg_decompress(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// Size of the GPG header
const HEADER_SIZE: usize = 2;
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn gzip_extractor() -> Extractor {
pub fn gzip_decompress(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
let mut exresult = ExtractionResult {
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/inflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct DeflateResult {
pub fn inflate_decompressor(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> DeflateResult {
// Size of decompression buffer
const BLOCK_SIZE: usize = 8192;
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/jboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn sch2_extractor() -> Extractor {
pub fn extract_jboot_sch2_kernel(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// Output file name
const OUTFILE_NAME: &str = "kernel.bin";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/jpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn jpeg_extractor() -> Extractor {
pub fn extract_jpeg_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTFILE_NAME: &str = "image.jpg";

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/lzma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn lzma_extractor() -> Extractor {
pub fn lzma_decompress(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// Output file name
const OUTPUT_FILE_NAME: &str = "decompressed.bin";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/mbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn mbr_extractor() -> Extractor {
pub fn extract_mbr_partitions(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// Return value
let mut result = ExtractionResult {
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/mh01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn mh01_extractor() -> Extractor {
pub fn extract_mh01_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// File names for the three portions of the MH01 firmware image
const IV_FILE_NAME: &str = "iv.bin";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/pcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn pcapng_extractor() -> Extractor {
pub fn pcapng_carver(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// Output file name
const OUTPUT_FILE_NAME: &str = "capture.pcapng";
Expand Down
6 changes: 3 additions & 3 deletions src/extractors/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn pem_certificate_extractor() -> Extractor {
pub fn pem_certificate_carver(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const CERTIFICATE_FILE_NAME: &str = "pem.crt";
pem_carver(
Expand All @@ -78,7 +78,7 @@ pub fn pem_certificate_carver(
pub fn pem_key_carver(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const KEY_FILE_NAME: &str = "pem.key";
pem_carver(file_data, offset, output_directory, Some(KEY_FILE_NAME))
Expand All @@ -87,7 +87,7 @@ pub fn pem_key_carver(
pub fn pem_carver(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
fname: Option<&str>,
) -> ExtractionResult {
let mut result = ExtractionResult {
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn png_extractor() -> Extractor {
pub fn extract_png_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const PNG_HEADER_LEN: usize = 8;
const OUTFILE_NAME: &str = "image.png";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/riff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn riff_extractor() -> Extractor {
pub fn extract_riff_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTFILE_NAME: &str = "image.riff";
const WAV_OUTFILE_NAME: &str = "video.wav";
Expand Down
6 changes: 3 additions & 3 deletions src/extractors/romfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn romfs_extractor() -> Extractor {
pub fn extract_romfs(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
let mut result = ExtractionResult {
..Default::default()
Expand Down Expand Up @@ -228,8 +228,8 @@ fn process_romfs_entries(
fn extract_romfs_entries(
romfs_data: &[u8],
romfs_files: &Vec<RomFSEntry>,
parent_directory: &String,
chroot_directory: &String,
parent_directory: &str,
chroot_directory: &str,
) -> usize {
let mut file_count: usize = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/shrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn shrs_extractor() -> Extractor {
pub fn extract_shrs_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const IV_FILE_NAME: &str = "iv.bin";
const ENCRYPTED_FILE_NAME: &str = "encrypted.bin";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn svg_extractor() -> Extractor {
pub fn extract_svg_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTFILE_NAME: &str = "image.svg";

Expand Down
4 changes: 2 additions & 2 deletions src/extractors/swapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn swapped_extractor_u16() -> Extractor {
pub fn extract_swapped_u16(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const SWAP_BYTE_COUNT: usize = 2;
extract_swapped(file_data, offset, output_directory, SWAP_BYTE_COUNT)
Expand All @@ -43,7 +43,7 @@ pub fn extract_swapped_u16(
fn extract_swapped(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
n: usize,
) -> ExtractionResult {
const OUTPUT_FILE_NAME: &str = "swapped.bin";
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/trx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn trx_extractor() -> Extractor {
pub fn extract_trx_partitions(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
const CRC_DATA_START_OFFSET: usize = 12;

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/uimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn uimage_extractor() -> Extractor {
pub fn extract_uimage(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
output_directory: Option<&str>,
) -> ExtractionResult {
// If no name is povided in the uImage header, use this as the output file name
const DEFAULT_OUTPUT_FILE_NAME: &str = "uimage_data";
Expand Down
Loading

0 comments on commit 481ed5d

Please sign in to comment.