From 1288135d02c0fb8f656eceaba84f02ab4df18ed0 Mon Sep 17 00:00:00 2001
From: devttys0 <heffnercj@gmail.com>
Date: Mon, 18 May 2015 15:57:38 -0400
Subject: [PATCH] Change output directory to CWD; fixed entropy BLOCK_SIZE bug.

---
 src/binwalk/modules/entropy.py   |  9 ++++++++-
 src/binwalk/modules/extractor.py | 19 +++++++++----------
 2 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/src/binwalk/modules/entropy.py b/src/binwalk/modules/entropy.py
index b2d1b12..dc109ff 100644
--- a/src/binwalk/modules/entropy.py
+++ b/src/binwalk/modules/entropy.py
@@ -171,6 +171,10 @@ class Entropy(Module):
         else:
             block_size = self.block_size
 
+        # Make sure block size is greater than 0
+        if block_size <= 0:
+            block_size = self.DEFAULT_BLOCK_SIZE
+
         binwalk.core.common.debug("Entropy block size (%d data points): %d" % (self.DEFAULT_DATA_POINTS, block_size))
 
         while True:
@@ -301,13 +305,16 @@ class Entropy(Module):
 
         # TODO: legend is not displayed properly when saving plots to disk
         if self.save_plot:
+            # Save graph to CWD
+            out_file = os.path.join(os.getcwd(), os.path.basename(fname))
+
             # exporters.ImageExporter is different in different versions of pyqtgraph
             try:
                 exporter = exporters.ImageExporter(plt.plotItem)
             except TypeError:
                 exporter = exporters.ImageExporter.ImageExporter(plt.plotItem)
             exporter.parameters()['width'] = self.FILE_WIDTH
-            exporter.export(binwalk.core.common.unique_file_name(fname, self.FILE_FORMAT))
+            exporter.export(binwalk.core.common.unique_file_name(out_file, self.FILE_FORMAT))
         else:
             plt.setLabel('left', self.YLABEL, units=self.YUNITS)
             plt.setLabel('bottom', self.XLABEL, units=self.XUNITS)
diff --git a/src/binwalk/modules/extractor.py b/src/binwalk/modules/extractor.py
index 05789d3..d54f6ea 100644
--- a/src/binwalk/modules/extractor.py
+++ b/src/binwalk/modules/extractor.py
@@ -122,8 +122,6 @@ class Extractor(Module):
         # Holds a dictionary of the last directory listing for a given directory; used for identifying
         # newly created/extracted files that need to be appended to self.pending.
         self.last_directory_listing = {}
-        # Set to the directory path of the first extracted directory; this allows us to track recursion depth.
-        self.base_recursion_dir = ""
 
     def callback(self, r):
         # Make sure the file attribute is set to a compatible instance of binwalk.core.common.BlockFile
@@ -171,7 +169,7 @@ class Extractor(Module):
                     # If recursion was specified, and the file is not the same one we just dd'd
                     if self.matryoshka and file_path != dd_file_path and scan_extracted_files:
                         # If the recursion level of this file is less than or equal to our desired recursion level
-                        if len(real_file_path.split(self.base_recursion_dir)[1].split(os.path.sep)) <= self.matryoshka:
+                        if len(real_file_path.split(self.directory)[1].split(os.path.sep)) <= self.matryoshka:
                             # If this is a directory and we are supposed to process directories for this extractor,
                             # then add all files under that directory to the list of pending files.
                             if os.path.isdir(file_path):
@@ -335,6 +333,11 @@ class Extractor(Module):
         if not has_key(self.extraction_directories, path):
             basedir = os.path.dirname(path)
             basename = os.path.basename(path)
+
+            # Make sure we put the initial extracted file in the CWD
+            if self.directory is None:
+                basedir = os.getcwd()
+
             outdir = os.path.join(basedir, '_' + basename)
             output_directory = unique_file_name(outdir, extension='extracted')
 
@@ -347,9 +350,9 @@ class Extractor(Module):
             output_directory = self.extraction_directories[path]
 
         # Set the initial base extraction directory for later determining the level of recusion
-        # TODO: This is no longer needed since self.directory has the same information. Update code accordingly.
-        if not self.base_recursion_dir:
-            self.base_recursion_dir = os.path.realpath(output_directory) + os.path.sep
+        if self.directory is None:
+            self.directory = os.path.realpath(output_directory) + os.path.sep
+
 
         return output_directory
 
@@ -398,10 +401,6 @@ class Extractor(Module):
 
         output_directory = self.build_output_directory(file_name)
 
-        # Update self.directory with the first output_directory path
-        if self.directory is None:
-            self.directory = output_directory
-
         # Extract to end of file if no size was specified
         if not size:
             size = file_size(file_path) - offset
--
libgit2 0.26.0