mirror of
https://github.com/nmap/nmap.git
synced 2025-12-07 21:21:31 +00:00
Ensure UTF-8 encoding used throughout zenmap
This commit is contained in:
@@ -93,7 +93,7 @@ import sys
|
||||
from string import Template
|
||||
from zenmapCore.Version import *
|
||||
from zenmapCore.Name import *
|
||||
with open(sys.argv[1],"r") as f:
|
||||
with open(sys.argv[1],"r",encoding="utf-8") as f:
|
||||
sys.stdout.write(Template(f.read()).substitute(
|
||||
VERSION=VERSION,
|
||||
APP_WEB_SITE=APP_WEB_SITE,
|
||||
|
||||
@@ -73,14 +73,14 @@ NAME_PY = os.path.join("zenmapCore", "Name.py")
|
||||
def update_date(base_dir):
|
||||
name_file = os.path.join(base_dir, NAME_PY)
|
||||
print(">>> Updating %s" % name_file)
|
||||
nf = open(name_file, "r")
|
||||
nf = open(name_file, "r", encoding="utf-8")
|
||||
ncontent = nf.read()
|
||||
nf.close()
|
||||
ncontent = re.sub(r'APP_COPYRIGHT *= *"Copyright 2005-....',
|
||||
'APP_COPYRIGHT = "Copyright 2005-%d' % (datetime.today().year),
|
||||
ncontent)
|
||||
# Write the modified file.
|
||||
nf = open(name_file, "w")
|
||||
nf = open(name_file, "w", encoding="utf-8")
|
||||
nf.write(ncontent)
|
||||
nf.close()
|
||||
|
||||
@@ -88,7 +88,7 @@ def update_date(base_dir):
|
||||
def update_version(base_dir, version):
|
||||
version = re.sub(r'(?=[^0-9.])', '+', version, 1)
|
||||
print(">>> Updating %s" % os.path.join(base_dir, VERSION_PY))
|
||||
vf = open(os.path.join(base_dir, VERSION_PY), "w")
|
||||
vf = open(os.path.join(base_dir, VERSION_PY), "w", encoding="utf-8")
|
||||
print("VERSION = \"%s\"" % version, file=vf)
|
||||
vf.close()
|
||||
|
||||
|
||||
@@ -321,5 +321,5 @@ if __name__ == "__main__":
|
||||
|
||||
root = reader.get_root()
|
||||
|
||||
writer = XMLWriter(open("test.xml", 'w'), root)
|
||||
writer = XMLWriter(open("test.xml", 'wb'), root)
|
||||
writer.write()
|
||||
|
||||
@@ -286,7 +286,7 @@ class NetworkInventory(object):
|
||||
"""Saves the scan with the given list index into a file with a given
|
||||
path. With format = "xml", saves Nmap XML; otherwise saves plain text
|
||||
output."""
|
||||
f = open(path, 'w')
|
||||
f = open(path, 'wb')
|
||||
if format == "xml":
|
||||
self.get_scans()[index].write_xml(f)
|
||||
self.filenames[self.get_scans()[index]] = f
|
||||
@@ -352,7 +352,7 @@ class NetworkInventory(object):
|
||||
self._generate_filenames(path)
|
||||
|
||||
for scan, filename in self.filenames.items():
|
||||
f = open(os.path.join(path, filename), "w")
|
||||
f = open(os.path.join(path, filename), "wb")
|
||||
scan.write_xml(f)
|
||||
f.close()
|
||||
|
||||
|
||||
@@ -739,7 +739,7 @@ class NmapParserSAX(ParserBasics, ContentHandler):
|
||||
|
||||
def parse_file(self, filename):
|
||||
"""Parse an Nmap XML file from the named file."""
|
||||
with open(filename, "r") as f:
|
||||
with open(filename, "rb") as f:
|
||||
self.parse(f)
|
||||
self.filename = filename
|
||||
|
||||
@@ -1002,12 +1002,12 @@ class NmapParserSAX(ParserBasics, ContentHandler):
|
||||
f."""
|
||||
if self.nmap_output == "":
|
||||
return
|
||||
f.write(self.nmap_output)
|
||||
f.write(self.nmap_output.encode('utf-8','xmlcharrefreplace'))
|
||||
|
||||
def write_xml(self, f):
|
||||
"""Write the XML representation of this object to the file-like object
|
||||
f."""
|
||||
writer = XMLGenerator(f)
|
||||
writer = XMLGenerator(f, encoding='utf-8')
|
||||
writer.startDocument()
|
||||
if self.xml_stylesheet_data is not None:
|
||||
writer.processingInstruction(
|
||||
@@ -1033,7 +1033,7 @@ class NmapParserSAX(ParserBasics, ContentHandler):
|
||||
def write_xml_to_file(self, filename):
|
||||
"""Write the XML representation of this scan to the file whose name is
|
||||
given."""
|
||||
fd = open(filename, "w")
|
||||
fd = open(filename, "wb")
|
||||
self.write_xml(fd)
|
||||
fd.close()
|
||||
|
||||
|
||||
@@ -199,7 +199,7 @@ def return_if_exists(path, create=False):
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
elif create:
|
||||
f = open(path, "w")
|
||||
f = open(path, "wb")
|
||||
f.close()
|
||||
return path
|
||||
raise Exception("File '%s' does not exist or could not be found!" % path)
|
||||
|
||||
@@ -76,17 +76,21 @@ class RecentScans(object):
|
||||
self.using_file = True
|
||||
|
||||
# Recovering saved targets
|
||||
recent_file = open(self.recent_scans_file, "r")
|
||||
for enc in ('utf-8', None):
|
||||
try:
|
||||
with open(self.recent_scans_file, "r", encoding=enc) as recent_file:
|
||||
self.temp_list = [
|
||||
t for t in recent_file.read().split(";")
|
||||
if t != "" and t != "\n"]
|
||||
recent_file.close()
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
break
|
||||
else:
|
||||
self.using_file = False
|
||||
|
||||
def save(self):
|
||||
if self.using_file:
|
||||
recent_file = open(self.recent_scans_file, "w")
|
||||
recent_file = open(self.recent_scans_file, "w", encoding="utf-8")
|
||||
recent_file.write(";".join(self.temp_list))
|
||||
recent_file.close()
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ class ScriptDB (object):
|
||||
|
||||
self.lineno = 1
|
||||
self.line = ""
|
||||
with open(script_db_path, "r") as self.f:
|
||||
with open(script_db_path, "r", encoding="utf-8") as self.f:
|
||||
self.entries_list = self.parse()
|
||||
|
||||
def syntax_error(self, message):
|
||||
@@ -296,20 +296,20 @@ class ScriptMetadata (object):
|
||||
self.get_string_variable(filename, "author")]
|
||||
|
||||
filepath = os.path.join(self.scripts_dir, filename)
|
||||
with open(filepath, "r") as f:
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
for tag_name, tag_text in nsedoc_tags_iter(f):
|
||||
if tag_name == "output" and not entry.output:
|
||||
entry.output = tag_text
|
||||
elif tag_name == "usage" and not entry.usage:
|
||||
entry.usage = tag_text
|
||||
except IOError as e:
|
||||
except (IOError, UnicodeError) as e:
|
||||
entry.description = "Error getting metadata: {}".format(e)
|
||||
|
||||
return entry
|
||||
|
||||
@staticmethod
|
||||
def get_file_contents(filename):
|
||||
with open(filename, "r") as f:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
contents = f.read()
|
||||
return contents
|
||||
|
||||
@@ -343,7 +343,7 @@ class ScriptMetadata (object):
|
||||
|
||||
@staticmethod
|
||||
def get_requires(filename):
|
||||
with open(filename, "r") as f:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
requires = ScriptMetadata.get_requires_from_file(f)
|
||||
return requires
|
||||
|
||||
@@ -359,7 +359,7 @@ class ScriptMetadata (object):
|
||||
|
||||
@staticmethod
|
||||
def get_script_args(filename):
|
||||
with open(filename, "r") as f:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
args = ScriptMetadata.get_script_args_from_file(f)
|
||||
return args
|
||||
|
||||
@@ -414,8 +414,11 @@ class ScriptMetadata (object):
|
||||
else:
|
||||
libname = filename
|
||||
|
||||
try:
|
||||
self.library_arguments[libname] = self.get_script_args(filepath)
|
||||
self.library_requires[libname] = self.get_requires(filepath)
|
||||
except (IOError, UnicodeError) as e:
|
||||
log.debug("Unable to process {}: {}".format(libname, e))
|
||||
|
||||
|
||||
def get_script_entries(scripts_dir, nselib_dir):
|
||||
@@ -424,7 +427,8 @@ def get_script_entries(scripts_dir, nselib_dir):
|
||||
metadata = ScriptMetadata(scripts_dir, nselib_dir)
|
||||
try:
|
||||
scriptdb = ScriptDB(os.path.join(scripts_dir, "script.db"))
|
||||
except IOError:
|
||||
except (IOError, UnicodeError) as e:
|
||||
log.debug("Unable to process script.db: {}".format(e))
|
||||
return []
|
||||
entries = []
|
||||
for dbentry in scriptdb.get_entries_list():
|
||||
|
||||
@@ -77,17 +77,21 @@ class TargetList(object):
|
||||
self.using_file = True
|
||||
|
||||
# Recovering saved targets
|
||||
target_file = open(self.target_list_file, "r")
|
||||
for enc in ('utf-8', None):
|
||||
try:
|
||||
with open(self.target_list_file, "r", encoding=enc) as target_file:
|
||||
self.temp_list = [
|
||||
t for t in target_file.read().split(";")
|
||||
if t != "" and t != "\n"]
|
||||
target_file.close()
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
break
|
||||
else:
|
||||
self.using_file = False
|
||||
|
||||
def save(self):
|
||||
if self.using_file:
|
||||
target_file = open(self.target_list_file, "w")
|
||||
target_file = open(self.target_list_file, "w", encoding="utf-8")
|
||||
target_file.write(";".join(self.temp_list))
|
||||
target_file.close()
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ class UmitConfigParser(ConfigParser):
|
||||
if self.filenames:
|
||||
log.debug("saving to %s" % self.filenames)
|
||||
try:
|
||||
with open(self.filenames, 'w') as fp:
|
||||
with open(self.filenames, 'w', encoding="utf-8") as fp:
|
||||
self.write(fp)
|
||||
except Exception as e:
|
||||
self.failed = e
|
||||
|
||||
@@ -1 +1 @@
|
||||
VERSION = "7.95"
|
||||
VERSION = "7.95+SVN"
|
||||
|
||||
@@ -48,7 +48,7 @@ if directory is not None:
|
||||
os.chdir(directory)
|
||||
|
||||
for fn in filenames:
|
||||
with open(fn, "r") as f:
|
||||
with open(fn, "rb") as f:
|
||||
parser = xml.sax.make_parser()
|
||||
parser.setContentHandler(Handler())
|
||||
parser.parse(f)
|
||||
|
||||
Reference in New Issue
Block a user