Commit c00c53ae by Craig Heffner

Fixed the way --length works

parent 6286cc4c
......@@ -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
......
......@@ -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
......
......@@ -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):
......
......@@ -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
......
......@@ -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
......
......@@ -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)
......
......@@ -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:
......
......@@ -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
......
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