Commit c5ffe186 by Slavek Kabrda

Make sure binwalk always opens files with utf-8 encoding

In Python 3.4, if the interpreter is launched with e.g. LANG=C environment,
binwalk fails with UnicodeDecodeError, since it tries to decode included
magic files (such as src/binwalk/magic/linux) with ascii codec. This patch
makes sure that utf-8 codec is always used.
parent b195008f
# Code to handle displaying and logging of results. # Code to handle displaying and logging of results.
# Anything in binwalk that prints results to screen should use this class. # Anything in binwalk that prints results to screen should use this class.
import codecs
import sys import sys
import csv as pycsv import csv as pycsv
import datetime import datetime
...@@ -29,7 +30,7 @@ class Display(object): ...@@ -29,7 +30,7 @@ class Display(object):
self._configure_formatting() self._configure_formatting()
if log: if log:
self.fp = open(log, "a") self.fp = codecs.open(log, "a", encoding='utf-8')
if csv: if csv:
self.csv = pycsv.writer(self.fp) self.csv = pycsv.writer(self.fp)
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
__all__ = ['Magic'] __all__ = ['Magic']
import codecs
import re import re
import struct import struct
import datetime import datetime
...@@ -789,7 +790,7 @@ class Magic(object): ...@@ -789,7 +790,7 @@ class Magic(object):
Returns None. Returns None.
''' '''
fp = open(fname, "r") fp = codecs.open(fname, "r", encoding='utf-8')
lines = fp.readlines() lines = fp.readlines()
self.parse(lines) self.parse(lines)
fp.close() fp.close()
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# This is automatically invoked by core.module code if extraction has been # This is automatically invoked by core.module code if extraction has been
# enabled by the user; other modules need not reference this module directly. # enabled by the user; other modules need not reference this module directly.
import codecs
import os import os
import re import re
import sys import sys
...@@ -317,7 +318,7 @@ class Extractor(Module): ...@@ -317,7 +318,7 @@ class Extractor(Module):
''' '''
try: try:
# Process each line from the extract file, ignoring comments # Process each line from the extract file, ignoring comments
with open(fname, 'r') as f: with codecs.open(fname, 'r', encoding='utf-8') as f:
for rule in f.readlines(): for rule in f.readlines():
self.add_rule(rule.split(self.COMMENT_DELIM, 1)[0]) self.add_rule(rule.split(self.COMMENT_DELIM, 1)[0])
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
......
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