diff --git a/src/binwalk/core/common.py b/src/binwalk/core/common.py index a1c7d20..08c8829 100644 --- a/src/binwalk/core/common.py +++ b/src/binwalk/core/common.py @@ -50,6 +50,13 @@ def error(msg): sys.stderr.write("\nERROR: " + msg + "\n") +def critical(msg): + ''' + Prints critical messages to stderr + ''' + sys.stderr.write("\nCRITICAL: " + msg + "\n") + + def get_module_path(): root = __file__ if os.path.islink(root): @@ -176,7 +183,7 @@ def strings(filename, minimum=4): with BlockFile(filename) as f: while True: (data, dlen) = f.read_block() - if not data: + if dlen < 1: break for c in data: @@ -451,10 +458,10 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs): return n - def read(self, n=-1): + def read(self, n=-1, override=False): '''' Reads up to n bytes of data (or to EOF if n is not specified). - Will not read more than self.length bytes. + Will not read more than self.length bytes unless override == True. io.FileIO.read does not guaruntee that all requested data will be read; this method overrides io.FileIO.read and does guaruntee that all data will be read. @@ -464,9 +471,10 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs): l = 0 data = b'' - if self.total_read < self.length: + if override == True or (self.total_read < self.length): # Don't read more than self.length bytes from the file - if (self.total_read + n) > self.length: + # unless an override has been requested. + if override == False and (self.total_read + n) > self.length: n = self.length - self.total_read while n < 0 or l < n: @@ -486,7 +494,7 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs): Peeks at data in file. ''' pos = self.tell() - data = self.read(n) + data = self.read(n, override=True) self.seek(pos) return data diff --git a/src/binwalk/core/magic.py b/src/binwalk/core/magic.py index f2a5783..4a65dac 100644 --- a/src/binwalk/core/magic.py +++ b/src/binwalk/core/magic.py @@ -814,9 +814,8 @@ class Magic(object): # Signatures are ordered based on the length of their magic bytes (largest first). # If this offset has already been matched to a previous signature, ignore it unless - # self.show_invalid has been specified. Also ignore obviously invalid offsets (<1) - # as well as those outside the specified self.data range - # (dlen). + # self.show_invalid has been specified. Also ignore obviously invalid offsets (<0) + # as well as those outside the specified self.data range (dlen). if (offset not in matched_offsets or self.show_invalid) and offset >= 0 and offset < dlen: # if offset >= 0 and offset < dlen: # Analyze the data at this offset using the current diff --git a/src/binwalk/modules/compression.py b/src/binwalk/modules/compression.py index f531588..477e551 100644 --- a/src/binwalk/modules/compression.py +++ b/src/binwalk/modules/compression.py @@ -270,7 +270,7 @@ class RawCompression(Module): while not file_done: (data, dlen) = fp.read_block() - if not data: + if dlen < 1: break for i in range(0, dlen): diff --git a/src/binwalk/modules/disasm.py b/src/binwalk/modules/disasm.py index 0a71cb8..d175af9 100644 --- a/src/binwalk/modules/disasm.py +++ b/src/binwalk/modules/disasm.py @@ -109,7 +109,7 @@ class Disasm(Module): result = None (data, dlen) = fp.read_block() - if not data: + if dlen < 1: break # If this data block doesn't contain at least two different bytes, skip it diff --git a/src/binwalk/modules/entropy.py b/src/binwalk/modules/entropy.py index d30125f..ef72d49 100644 --- a/src/binwalk/modules/entropy.py +++ b/src/binwalk/modules/entropy.py @@ -173,7 +173,7 @@ class Entropy(Module): file_offset = fp.tell() (data, dlen) = fp.read_block() - if not data: + if dlen < 1: break i = 0 diff --git a/src/binwalk/modules/extractor.py b/src/binwalk/modules/extractor.py index 1502f95..5de964c 100644 --- a/src/binwalk/modules/extractor.py +++ b/src/binwalk/modules/extractor.py @@ -782,7 +782,7 @@ class Extractor(Module): while total_size < size: (data, dlen) = fdin.read_block() - if not data: + if dlen < 1: break else: total_size += (dlen - adjust) diff --git a/src/binwalk/modules/heuristics.py b/src/binwalk/modules/heuristics.py index eb28707..a030ac8 100644 --- a/src/binwalk/modules/heuristics.py +++ b/src/binwalk/modules/heuristics.py @@ -177,7 +177,7 @@ class HeuristicCompressionAnalyzer(Module): while i < block.length: j = 0 (d, dlen) = fp.read_block() - if not d: + if dlen < 1: break while j < dlen: diff --git a/src/binwalk/modules/signature.py b/src/binwalk/modules/signature.py index 4258f0b..4ad59df 100644 --- a/src/binwalk/modules/signature.py +++ b/src/binwalk/modules/signature.py @@ -138,7 +138,7 @@ class Signature(Module): while True: (data, dlen) = fp.read_block() - if not data: + if dlen < 1: break current_block_offset = 0