Commit c00c53ae by Craig Heffner

Fixed the way --length works

parent 6286cc4c
...@@ -50,6 +50,13 @@ def error(msg): ...@@ -50,6 +50,13 @@ def error(msg):
sys.stderr.write("\nERROR: " + msg + "\n") sys.stderr.write("\nERROR: " + msg + "\n")
def critical(msg):
'''
Prints critical messages to stderr
'''
sys.stderr.write("\nCRITICAL: " + msg + "\n")
def get_module_path(): def get_module_path():
root = __file__ root = __file__
if os.path.islink(root): if os.path.islink(root):
...@@ -176,7 +183,7 @@ def strings(filename, minimum=4): ...@@ -176,7 +183,7 @@ def strings(filename, minimum=4):
with BlockFile(filename) as f: with BlockFile(filename) as f:
while True: while True:
(data, dlen) = f.read_block() (data, dlen) = f.read_block()
if not data: if dlen < 1:
break break
for c in data: for c in data:
...@@ -451,10 +458,10 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs): ...@@ -451,10 +458,10 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs):
return n 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). 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; 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. 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): ...@@ -464,9 +471,10 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs):
l = 0 l = 0
data = b'' 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 # 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 n = self.length - self.total_read
while n < 0 or l < n: while n < 0 or l < n:
...@@ -486,7 +494,7 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs): ...@@ -486,7 +494,7 @@ def BlockFile(fname, mode='r', subclass=io.FileIO, **kwargs):
Peeks at data in file. Peeks at data in file.
''' '''
pos = self.tell() pos = self.tell()
data = self.read(n) data = self.read(n, override=True)
self.seek(pos) self.seek(pos)
return data return data
......
...@@ -814,9 +814,8 @@ class Magic(object): ...@@ -814,9 +814,8 @@ class Magic(object):
# Signatures are ordered based on the length of their magic bytes (largest first). # 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 # 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) # self.show_invalid has been specified. Also ignore obviously invalid offsets (<0)
# as well as those outside the specified self.data range # as well as those outside the specified self.data range (dlen).
# (dlen).
if (offset not in matched_offsets or self.show_invalid) and offset >= 0 and offset < dlen: if (offset not in matched_offsets or self.show_invalid) and offset >= 0 and offset < dlen:
# if offset >= 0 and offset < dlen: # if offset >= 0 and offset < dlen:
# Analyze the data at this offset using the current # Analyze the data at this offset using the current
......
...@@ -270,7 +270,7 @@ class RawCompression(Module): ...@@ -270,7 +270,7 @@ class RawCompression(Module):
while not file_done: while not file_done:
(data, dlen) = fp.read_block() (data, dlen) = fp.read_block()
if not data: if dlen < 1:
break break
for i in range(0, dlen): for i in range(0, dlen):
......
...@@ -109,7 +109,7 @@ class Disasm(Module): ...@@ -109,7 +109,7 @@ class Disasm(Module):
result = None result = None
(data, dlen) = fp.read_block() (data, dlen) = fp.read_block()
if not data: if dlen < 1:
break break
# If this data block doesn't contain at least two different bytes, skip it # If this data block doesn't contain at least two different bytes, skip it
......
...@@ -173,7 +173,7 @@ class Entropy(Module): ...@@ -173,7 +173,7 @@ class Entropy(Module):
file_offset = fp.tell() file_offset = fp.tell()
(data, dlen) = fp.read_block() (data, dlen) = fp.read_block()
if not data: if dlen < 1:
break break
i = 0 i = 0
......
...@@ -782,7 +782,7 @@ class Extractor(Module): ...@@ -782,7 +782,7 @@ class Extractor(Module):
while total_size < size: while total_size < size:
(data, dlen) = fdin.read_block() (data, dlen) = fdin.read_block()
if not data: if dlen < 1:
break break
else: else:
total_size += (dlen - adjust) total_size += (dlen - adjust)
......
...@@ -177,7 +177,7 @@ class HeuristicCompressionAnalyzer(Module): ...@@ -177,7 +177,7 @@ class HeuristicCompressionAnalyzer(Module):
while i < block.length: while i < block.length:
j = 0 j = 0
(d, dlen) = fp.read_block() (d, dlen) = fp.read_block()
if not d: if dlen < 1:
break break
while j < dlen: while j < dlen:
......
...@@ -138,7 +138,7 @@ class Signature(Module): ...@@ -138,7 +138,7 @@ class Signature(Module):
while True: while True:
(data, dlen) = fp.read_block() (data, dlen) = fp.read_block()
if not data: if dlen < 1:
break break
current_block_offset = 0 current_block_offset = 0
......
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