Commit 659126cb by devttys0

Added ability to override BlockFile parent class.

parent 8e52d4b1
...@@ -7,6 +7,12 @@ import hashlib ...@@ -7,6 +7,12 @@ import hashlib
import operator as op import operator as op
from binwalk.compat import * from binwalk.compat import *
# This allows other modules/scripts to subclass BlockFile from a custom class. Defaults to io.FileIO.
if has_key(__builtins__, 'BLOCK_FILE_PARENT_CLASS'):
BLOCK_FILE_PARENT_CLASS = __builtins__['BLOCK_FILE_PARENT_CLASS']
else:
BLOCK_FILE_PARENT_CLASS = io.FileIO
def file_md5(file_name): def file_md5(file_name):
''' '''
Generate an MD5 hash of the specified file. Generate an MD5 hash of the specified file.
...@@ -184,7 +190,7 @@ class MathExpression(object): ...@@ -184,7 +190,7 @@ class MathExpression(object):
raise TypeError(node) raise TypeError(node)
class BlockFile(io.FileIO): class BlockFile(BLOCK_FILE_PARENT_CLASS):
''' '''
Abstraction class for accessing binary files. Abstraction class for accessing binary files.
...@@ -275,7 +281,7 @@ class BlockFile(io.FileIO): ...@@ -275,7 +281,7 @@ class BlockFile(io.FileIO):
if trail > 0: if trail > 0:
self.MAX_TRAILING_SIZE = trail self.MAX_TRAILING_SIZE = trail
io.FileIO.__init__(self, fname, mode) super(self.__class__, self).__init__(fname, mode)
# Work around for python 2.6 where FileIO._name is not defined # Work around for python 2.6 where FileIO._name is not defined
try: try:
...@@ -320,7 +326,7 @@ class BlockFile(io.FileIO): ...@@ -320,7 +326,7 @@ class BlockFile(io.FileIO):
data = str2bytes(data) data = str2bytes(data)
while n < l: while n < l:
n += io.FileIO.write(self, data[n:]) n += super(self.__class__, self).write(data[n:])
return n return n
...@@ -337,7 +343,7 @@ class BlockFile(io.FileIO): ...@@ -337,7 +343,7 @@ class BlockFile(io.FileIO):
data = b'' data = b''
while n < 0 or l < n: while n < 0 or l < n:
tmp = io.FileIO.read(self, n-l) tmp = super(self.__class__, self).read(n-l)
if tmp: if tmp:
data += tmp data += tmp
l += len(tmp) l += len(tmp)
...@@ -364,7 +370,7 @@ class BlockFile(io.FileIO): ...@@ -364,7 +370,7 @@ class BlockFile(io.FileIO):
elif whence == os.SEEK_END: elif whence == os.SEEK_END:
self.total_read = self.size + n self.total_read = self.size + n
io.FileIO.seek(self, n, whence) super(self.__class__, self).seek(n, whence)
def read_block(self): def read_block(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