Skip to content

Commit c840e23

Browse files
committed
Ensure UTF-8 encoding used throughout zenmap
1 parent 480803e commit c840e23

File tree

12 files changed

+48
-36
lines changed

12 files changed

+48
-36
lines changed

zenmap/install_scripts/macosx/make-bundle.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import sys
9393
from string import Template
9494
from zenmapCore.Version import *
9595
from zenmapCore.Name import *
96-
with open(sys.argv[1],"r") as f:
96+
with open(sys.argv[1],"r",encoding="utf-8") as f:
9797
sys.stdout.write(Template(f.read()).substitute(
9898
VERSION=VERSION,
9999
APP_WEB_SITE=APP_WEB_SITE,

zenmap/install_scripts/utils/version_update.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,22 @@
7373
def update_date(base_dir):
7474
name_file = os.path.join(base_dir, NAME_PY)
7575
print(">>> Updating %s" % name_file)
76-
nf = open(name_file, "r")
76+
nf = open(name_file, "r", encoding="utf-8")
7777
ncontent = nf.read()
7878
nf.close()
7979
ncontent = re.sub(r'APP_COPYRIGHT *= *"Copyright 2005-....',
8080
'APP_COPYRIGHT = "Copyright 2005-%d' % (datetime.today().year),
8181
ncontent)
8282
# Write the modified file.
83-
nf = open(name_file, "w")
83+
nf = open(name_file, "w", encoding="utf-8")
8484
nf.write(ncontent)
8585
nf.close()
8686

8787

8888
def update_version(base_dir, version):
8989
version = re.sub(r'(?=[^0-9.])', '+', version, 1)
9090
print(">>> Updating %s" % os.path.join(base_dir, VERSION_PY))
91-
vf = open(os.path.join(base_dir, VERSION_PY), "w")
91+
vf = open(os.path.join(base_dir, VERSION_PY), "w", encoding="utf-8")
9292
print("VERSION = \"%s\"" % version, file=vf)
9393
vf.close()
9494

zenmap/radialnet/core/XMLHandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,5 @@ def characters(self, text):
321321

322322
root = reader.get_root()
323323

324-
writer = XMLWriter(open("test.xml", 'w'), root)
324+
writer = XMLWriter(open("test.xml", 'wb'), root)
325325
writer.write()

zenmap/zenmapCore/NetworkInventory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def save_to_file(self, path, index, format="xml"):
286286
"""Saves the scan with the given list index into a file with a given
287287
path. With format = "xml", saves Nmap XML; otherwise saves plain text
288288
output."""
289-
f = open(path, 'w')
289+
f = open(path, 'wb')
290290
if format == "xml":
291291
self.get_scans()[index].write_xml(f)
292292
self.filenames[self.get_scans()[index]] = f
@@ -352,7 +352,7 @@ def save_to_dir(self, path):
352352
self._generate_filenames(path)
353353

354354
for scan, filename in self.filenames.items():
355-
f = open(os.path.join(path, filename), "w")
355+
f = open(os.path.join(path, filename), "wb")
356356
scan.write_xml(f)
357357
f.close()
358358

zenmap/zenmapCore/NmapParser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ def parse(self, f):
739739

740740
def parse_file(self, filename):
741741
"""Parse an Nmap XML file from the named file."""
742-
with open(filename, "r") as f:
742+
with open(filename, "rb") as f:
743743
self.parse(f)
744744
self.filename = filename
745745

@@ -1002,12 +1002,12 @@ def write_text(self, f):
10021002
f."""
10031003
if self.nmap_output == "":
10041004
return
1005-
f.write(self.nmap_output)
1005+
f.write(self.nmap_output.encode('utf-8','xmlcharrefreplace'))
10061006

10071007
def write_xml(self, f):
10081008
"""Write the XML representation of this object to the file-like object
10091009
f."""
1010-
writer = XMLGenerator(f)
1010+
writer = XMLGenerator(f, encoding='utf-8')
10111011
writer.startDocument()
10121012
if self.xml_stylesheet_data is not None:
10131013
writer.processingInstruction(
@@ -1033,7 +1033,7 @@ def get_xml(self):
10331033
def write_xml_to_file(self, filename):
10341034
"""Write the XML representation of this scan to the file whose name is
10351035
given."""
1036-
fd = open(filename, "w")
1036+
fd = open(filename, "wb")
10371037
self.write_xml(fd)
10381038
fd.close()
10391039

zenmap/zenmapCore/Paths.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def return_if_exists(path, create=False):
199199
if os.path.exists(path):
200200
return path
201201
elif create:
202-
f = open(path, "w")
202+
f = open(path, "wb")
203203
f.close()
204204
return path
205205
raise Exception("File '%s' does not exist or could not be found!" % path)

zenmap/zenmapCore/RecentScans.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,21 @@ def __init__(self):
7676
self.using_file = True
7777

7878
# Recovering saved targets
79-
recent_file = open(self.recent_scans_file, "r")
80-
self.temp_list = [
81-
t for t in recent_file.read().split(";")
82-
if t != "" and t != "\n"]
83-
recent_file.close()
79+
for enc in ('utf-8', None):
80+
try:
81+
with open(self.recent_scans_file, "r", encoding=enc) as recent_file:
82+
self.temp_list = [
83+
t for t in recent_file.read().split(";")
84+
if t != "" and t != "\n"]
85+
except UnicodeDecodeError:
86+
continue
87+
break
8488
else:
8589
self.using_file = False
8690

8791
def save(self):
8892
if self.using_file:
89-
recent_file = open(self.recent_scans_file, "w")
93+
recent_file = open(self.recent_scans_file, "w", encoding="utf-8")
9094
recent_file.write(";".join(self.temp_list))
9195
recent_file.close()
9296

zenmap/zenmapCore/ScriptMetadata.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(self, script_db_path=None):
8484

8585
self.lineno = 1
8686
self.line = ""
87-
with open(script_db_path, "r") as self.f:
87+
with open(script_db_path, "r", encoding="utf-8") as self.f:
8888
self.entries_list = self.parse()
8989

9090
def syntax_error(self, message):
@@ -296,20 +296,20 @@ def get_metadata(self, filename):
296296
self.get_string_variable(filename, "author")]
297297

298298
filepath = os.path.join(self.scripts_dir, filename)
299-
with open(filepath, "r") as f:
299+
with open(filepath, "r", encoding="utf-8") as f:
300300
for tag_name, tag_text in nsedoc_tags_iter(f):
301301
if tag_name == "output" and not entry.output:
302302
entry.output = tag_text
303303
elif tag_name == "usage" and not entry.usage:
304304
entry.usage = tag_text
305-
except IOError as e:
305+
except (IOError, UnicodeError) as e:
306306
entry.description = "Error getting metadata: {}".format(e)
307307

308308
return entry
309309

310310
@staticmethod
311311
def get_file_contents(filename):
312-
with open(filename, "r") as f:
312+
with open(filename, "r", encoding="utf-8") as f:
313313
contents = f.read()
314314
return contents
315315

@@ -343,7 +343,7 @@ def get_list_variable(self, filename, varname):
343343

344344
@staticmethod
345345
def get_requires(filename):
346-
with open(filename, "r") as f:
346+
with open(filename, "r", encoding="utf-8") as f:
347347
requires = ScriptMetadata.get_requires_from_file(f)
348348
return requires
349349

@@ -359,7 +359,7 @@ def get_requires_from_file(f):
359359

360360
@staticmethod
361361
def get_script_args(filename):
362-
with open(filename, "r") as f:
362+
with open(filename, "r", encoding="utf-8") as f:
363363
args = ScriptMetadata.get_script_args_from_file(f)
364364
return args
365365

@@ -414,8 +414,11 @@ def construct_library_arguments(self):
414414
else:
415415
libname = filename
416416

417-
self.library_arguments[libname] = self.get_script_args(filepath)
418-
self.library_requires[libname] = self.get_requires(filepath)
417+
try:
418+
self.library_arguments[libname] = self.get_script_args(filepath)
419+
self.library_requires[libname] = self.get_requires(filepath)
420+
except (IOError, UnicodeError) as e:
421+
log.debug("Unable to process {}: {}".format(libname, e))
419422

420423

421424
def get_script_entries(scripts_dir, nselib_dir):
@@ -424,7 +427,8 @@ def get_script_entries(scripts_dir, nselib_dir):
424427
metadata = ScriptMetadata(scripts_dir, nselib_dir)
425428
try:
426429
scriptdb = ScriptDB(os.path.join(scripts_dir, "script.db"))
427-
except IOError:
430+
except (IOError, UnicodeError) as e:
431+
log.debug("Unable to process script.db: {}".format(e))
428432
return []
429433
entries = []
430434
for dbentry in scriptdb.get_entries_list():

zenmap/zenmapCore/TargetList.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,21 @@ def __init__(self):
7777
self.using_file = True
7878

7979
# Recovering saved targets
80-
target_file = open(self.target_list_file, "r")
81-
self.temp_list = [
82-
t for t in target_file.read().split(";")
83-
if t != "" and t != "\n"]
84-
target_file.close()
80+
for enc in ('utf-8', None):
81+
try:
82+
with open(self.target_list_file, "r", encoding=enc) as target_file:
83+
self.temp_list = [
84+
t for t in target_file.read().split(";")
85+
if t != "" and t != "\n"]
86+
except UnicodeDecodeError:
87+
continue
88+
break
8589
else:
8690
self.using_file = False
8791

8892
def save(self):
8993
if self.using_file:
90-
target_file = open(self.target_list_file, "w")
94+
target_file = open(self.target_list_file, "w", encoding="utf-8")
9195
target_file.write(";".join(self.temp_list))
9296
target_file.close()
9397

zenmap/zenmapCore/UmitConfigParser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def save_changes(self):
8787
if self.filenames:
8888
log.debug("saving to %s" % self.filenames)
8989
try:
90-
with open(self.filenames, 'w') as fp:
90+
with open(self.filenames, 'w', encoding="utf-8") as fp:
9191
self.write(fp)
9292
except Exception as e:
9393
self.failed = e

zenmap/zenmapCore/Version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "7.95"
1+
VERSION = "7.95+SVN"

zenmap/zenmapCore/data/locale/xgettext-profile_editor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def startElement(self, name, attrs):
4848
os.chdir(directory)
4949

5050
for fn in filenames:
51-
with open(fn, "r") as f:
51+
with open(fn, "rb") as f:
5252
parser = xml.sax.make_parser()
5353
parser.setContentHandler(Handler())
5454
parser.parse(f)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy