Commit ec1917f2 by Craig Heffner

Added --count option

parent f004d4dd
...@@ -483,3 +483,4 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs): ...@@ -483,3 +483,4 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs):
return (data, dlen) return (data, dlen)
return InternalBlockFile(fname, mode=mode, **kwargs) return InternalBlockFile(fname, mode=mode, **kwargs)
...@@ -449,6 +449,12 @@ class Module(object): ...@@ -449,6 +449,12 @@ class Module(object):
self.validate(r) self.validate(r)
self._plugins_result(r) self._plugins_result(r)
# Update the progress status automatically if it is not being done manually by the module
if r.offset and r.file and self.AUTO_UPDATE_STATUS:
self.status.total = r.file.length
self.status.completed = r.offset
self.status.fp = r.file
for dependency in self.dependencies: for dependency in self.dependencies:
try: try:
getattr(self, dependency.attribute).callback(r) getattr(self, dependency.attribute).callback(r)
...@@ -458,12 +464,6 @@ class Module(object): ...@@ -458,12 +464,6 @@ class Module(object):
if r.valid: if r.valid:
self.results.append(r) self.results.append(r)
# Update the progress status automatically if it is not being done manually by the module
if r.offset and r.file and self.AUTO_UPDATE_STATUS:
self.status.total = r.file.length
self.status.completed = r.offset
self.status.fp = r.file
if r.display: if r.display:
display_args = self._build_display_args(r) display_args = self._build_display_args(r)
if display_args: if display_args:
......
...@@ -72,6 +72,11 @@ class Extractor(Module): ...@@ -72,6 +72,11 @@ class Extractor(Module):
type=int, type=int,
kwargs={'max_size' : 0}, kwargs={'max_size' : 0},
description='Limit the size of each extracted file'), description='Limit the size of each extracted file'),
Option(short='n',
long='count',
type=int,
kwargs={'max_count' : 0},
description='Limit the number of extracted files'),
Option(short='r', Option(short='r',
long='rm', long='rm',
kwargs={'remove_after_execute' : True}, kwargs={'remove_after_execute' : True},
...@@ -84,6 +89,7 @@ class Extractor(Module): ...@@ -84,6 +89,7 @@ class Extractor(Module):
KWARGS = [ KWARGS = [
Kwarg(name='max_size', default=None), Kwarg(name='max_size', default=None),
Kwarg(name='max_count', default=None),
Kwarg(name='base_directory', default=None), Kwarg(name='base_directory', default=None),
Kwarg(name='remove_after_execute', default=False), Kwarg(name='remove_after_execute', default=False),
Kwarg(name='load_default_rules', default=False), Kwarg(name='load_default_rules', default=False),
...@@ -100,6 +106,8 @@ class Extractor(Module): ...@@ -100,6 +106,8 @@ class Extractor(Module):
self.directory = None self.directory = None
# Key value pairs of input file path and output extraction path # Key value pairs of input file path and output extraction path
self.output = {} self.output = {}
# Number of extracted files
self.extraction_count = 0
if self.load_default_rules: if self.load_default_rules:
self.load_defaults() self.load_defaults()
...@@ -154,7 +162,7 @@ class Extractor(Module): ...@@ -154,7 +162,7 @@ class Extractor(Module):
# Only extract valid results that have been marked for extraction and displayed to the user. # Only extract valid results that have been marked for extraction and displayed to the user.
# Note that r.display is still True even if --quiet has been specified; it is False if the result has been # Note that r.display is still True even if --quiet has been specified; it is False if the result has been
# explicitly excluded via the -y/-x options. # explicitly excluded via the -y/-x options.
if r.valid and r.extract and r.display: if r.valid and r.extract and r.display and (not self.max_count or self.extraction_count < self.max_count):
# Create some extract output for this file, it it doesn't already exist # Create some extract output for this file, it it doesn't already exist
if not binwalk.core.common.has_key(self.output, r.file.path): if not binwalk.core.common.has_key(self.output, r.file.path):
self.output[r.file.path] = ExtractInfo() self.output[r.file.path] = ExtractInfo()
...@@ -165,6 +173,9 @@ class Extractor(Module): ...@@ -165,6 +173,9 @@ class Extractor(Module):
# If the extraction was successful, self.extract will have returned the output directory and name of the dd'd file # If the extraction was successful, self.extract will have returned the output directory and name of the dd'd file
if extraction_directory and dd_file: if extraction_directory and dd_file:
# Track the number of extracted files
self.extraction_count += 1
# Get the full path to the dd'd file and save it in the output info for this file # Get the full path to the dd'd file and save it in the output info for this file
dd_file_path = os.path.join(extraction_directory, dd_file) dd_file_path = os.path.join(extraction_directory, dd_file)
self.output[r.file.path].carved[r.offset] = dd_file_path self.output[r.file.path].carved[r.offset] = dd_file_path
......
...@@ -191,7 +191,13 @@ class General(Module): ...@@ -191,7 +191,13 @@ class General(Module):
if swap is None: if swap is None:
swap = self.swap_size swap = self.swap_size
return binwalk.core.common.BlockFile(fname, subclass=self.subclass, length=length, offset=offset, swap=swap, block=block, peek=peek) return binwalk.core.common.BlockFile(fname,
subclass=self.subclass,
length=length,
offset=offset,
swap=swap,
block=block,
peek=peek)
def _open_target_files(self): def _open_target_files(self):
''' '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment