@@ -601,9 +601,10 @@ impl Binwalk {
601
601
pub fn extract (
602
602
& self ,
603
603
file_data : & [ u8 ] ,
604
- file_path : & String ,
604
+ file_name : impl Into < String > ,
605
605
file_map : & Vec < signatures:: common:: SignatureResult > ,
606
606
) -> HashMap < String , extractors:: common:: ExtractionResult > {
607
+ let file_path = file_name. into ( ) ;
607
608
let mut extraction_results: HashMap < String , extractors:: common:: ExtractionResult > =
608
609
HashMap :: new ( ) ;
609
610
@@ -622,7 +623,7 @@ impl Binwalk {
622
623
Some ( _) => {
623
624
// Run an extraction for this signature
624
625
let mut extraction_result =
625
- extractors:: common:: execute ( file_data, file_path, signature, & extractor) ;
626
+ extractors:: common:: execute ( file_data, & file_path, signature, & extractor) ;
626
627
627
628
if !extraction_result. success {
628
629
debug ! (
@@ -653,7 +654,7 @@ impl Binwalk {
653
654
// Re-run the extraction
654
655
extraction_result = extractors:: common:: execute (
655
656
file_data,
656
- file_path,
657
+ & file_path,
657
658
& new_signature,
658
659
& extractor,
659
660
) ;
@@ -669,13 +670,13 @@ impl Binwalk {
669
670
extraction_results
670
671
}
671
672
672
- /// Analyze a file and optionally extract the file contents.
673
+ /// Analyze a data buffer and optionally extract the file contents.
673
674
///
674
675
/// ## Example
675
676
///
676
677
/// ```
677
- /// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_624_0 () -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
678
- /// use binwalk::Binwalk;
678
+ /// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_672_0 () -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
679
+ /// use binwalk::{ Binwalk, common} ;
679
680
///
680
681
/// let target_path = std::path::Path::new("tests")
681
682
/// .join("inputs")
@@ -688,6 +689,8 @@ impl Binwalk {
688
689
/// .display()
689
690
/// .to_string();
690
691
///
692
+ /// let file_data = common::read_file(&target_path).expect("Failed to read file data");
693
+ ///
691
694
/// # std::fs::remove_dir_all(&extraction_directory);
692
695
/// let binwalker = Binwalk::configure(Some(target_path),
693
696
/// Some(extraction_directory.clone()),
@@ -696,7 +699,7 @@ impl Binwalk {
696
699
/// None,
697
700
/// false)?;
698
701
///
699
- /// let analysis_results = binwalker.analyze( &binwalker.base_target_file, true);
702
+ /// let analysis_results = binwalker.analyze_buf(&file_data, &binwalker.base_target_file, true);
700
703
///
701
704
/// assert_eq!(analysis_results.file_map.len(), 1);
702
705
/// assert_eq!(analysis_results.extractions.len(), 1);
@@ -707,44 +710,101 @@ impl Binwalk {
707
710
/// .exists(), true);
708
711
/// # std::fs::remove_dir_all(&extraction_directory);
709
712
/// # Ok(binwalker)
710
- /// # } _doctest_main_src_binwalk_rs_624_0 (); }
713
+ /// # } _doctest_main_src_binwalk_rs_672_0 (); }
711
714
/// ```
712
- pub fn analyze ( & self , target_file : & String , do_extraction : bool ) -> AnalysisResults {
715
+ pub fn analyze_buf (
716
+ & self ,
717
+ file_data : & [ u8 ] ,
718
+ target_file : impl Into < String > ,
719
+ do_extraction : bool ,
720
+ ) -> AnalysisResults {
721
+ let file_path = target_file. into ( ) ;
722
+
713
723
// Return value
714
724
let mut results: AnalysisResults = AnalysisResults {
715
- file_path : target_file . clone ( ) ,
725
+ file_path : file_path . clone ( ) ,
716
726
..Default :: default ( )
717
727
} ;
718
728
719
- debug ! ( "Analysis start: {}" , target_file) ;
720
-
721
- // Read file into memory
722
- if let Ok ( file_data) = read_file ( target_file) {
723
- // Scan file data for signatures
724
- info ! ( "Scanning {}" , target_file) ;
725
- results. file_map = self . scan ( & file_data) ;
729
+ // Scan file data for signatures
730
+ debug ! ( "Analysis start: {}" , file_path) ;
731
+ results. file_map = self . scan ( file_data) ;
726
732
727
- // Only extract if told to, and if there were some signatures found in this file
728
- if do_extraction && !results. file_map . is_empty ( ) {
729
- // Extract everything we can
730
- debug ! (
731
- "Submitting {} signature results to extractor" ,
732
- results. file_map. len( )
733
- ) ;
734
- results. extractions = self . extract ( & file_data, target_file, & results. file_map ) ;
735
- }
733
+ // Only extract if told to, and if there were some signatures found in this file
734
+ if do_extraction && !results. file_map . is_empty ( ) {
735
+ // Extract everything we can
736
+ debug ! (
737
+ "Submitting {} signature results to extractor" ,
738
+ results. file_map. len( )
739
+ ) ;
740
+ results. extractions = self . extract ( file_data, & file_path, & results. file_map ) ;
736
741
}
737
742
738
- debug ! ( "Analysis end: {}" , target_file ) ;
743
+ debug ! ( "Analysis end: {}" , file_path ) ;
739
744
740
745
results
741
746
}
747
+
748
+ /// Analyze a file on disk and optionally extract its contents.
749
+ ///
750
+ /// ## Example
751
+ ///
752
+ /// ```
753
+ /// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_745_0() -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
754
+ /// use binwalk::Binwalk;
755
+ ///
756
+ /// let target_path = std::path::Path::new("tests")
757
+ /// .join("inputs")
758
+ /// .join("gzip.bin")
759
+ /// .display()
760
+ /// .to_string();
761
+ ///
762
+ /// let extraction_directory = std::path::Path::new("tests")
763
+ /// .join("extractions")
764
+ /// .display()
765
+ /// .to_string();
766
+ ///
767
+ /// # std::fs::remove_dir_all(&extraction_directory);
768
+ /// let binwalker = Binwalk::configure(Some(target_path),
769
+ /// Some(extraction_directory.clone()),
770
+ /// None,
771
+ /// None,
772
+ /// None,
773
+ /// false)?;
774
+ ///
775
+ /// let analysis_results = binwalker.analyze(&binwalker.base_target_file, true);
776
+ ///
777
+ /// assert_eq!(analysis_results.file_map.len(), 1);
778
+ /// assert_eq!(analysis_results.extractions.len(), 1);
779
+ /// assert_eq!(std::path::Path::new(&extraction_directory)
780
+ /// .join("gzip.bin.extracted")
781
+ /// .join("0")
782
+ /// .join("decompressed.bin")
783
+ /// .exists(), true);
784
+ /// # std::fs::remove_dir_all(&extraction_directory);
785
+ /// # Ok(binwalker)
786
+ /// # } _doctest_main_src_binwalk_rs_745_0(); }
787
+ /// ```
788
+ #[ allow( dead_code) ]
789
+ pub fn analyze ( & self , target_file : impl Into < String > , do_extraction : bool ) -> AnalysisResults {
790
+ let file_path = target_file. into ( ) ;
791
+
792
+ let file_data = match read_file ( & file_path) {
793
+ Err ( _) => {
794
+ error ! ( "Failed to read data from {}" , file_path) ;
795
+ b"" . to_vec ( )
796
+ }
797
+ Ok ( data) => data,
798
+ } ;
799
+
800
+ self . analyze_buf ( & file_data, & file_path, do_extraction)
801
+ }
742
802
}
743
803
744
804
/// Initializes the extraction output directory
745
805
fn init_extraction_directory (
746
- target_file : & String ,
747
- extraction_directory : & String ,
806
+ target_file : & str ,
807
+ extraction_directory : & str ,
748
808
) -> Result < String , std:: io:: Error > {
749
809
// Create the output directory, equivalent of mkdir -p
750
810
match fs:: create_dir_all ( extraction_directory) {
0 commit comments