1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 05:01:29 +00:00

Fix encoding issues related to Python 3 upgrade.

Python 3 str() is a unicode already, so can't be decoded.
subprocess.Popen needs to be in text mode (universal_newlines is the
oldest compatible kwarg for this) in order to do line-based buffering.
In general, all the filesystem encoding stuff we were doing is done by
Python itself now.
This commit is contained in:
dmiller
2022-12-07 20:34:07 +00:00
parent 12d41ec2cd
commit 9e4d6f5f5c
14 changed files with 35 additions and 125 deletions

View File

@@ -87,39 +87,6 @@ paths_config = PathsConfig()
log.debug(">>> Platform: %s" % sys.platform)
def wrap_file_in_preferred_encoding(f):
"""Wrap an open file to automatically decode its contents when reading from
the encoding given by locale.getpreferredencoding, or just return the file
if that doesn't work.
The nmap executable will write its output in whatever the system encoding
is. Nmap's output is usually all ASCII, but time zone it prints can be in a
different encoding. If it is not decoded correctly it will be displayed as
garbage characters. This function assists in reading the Nmap output. We
don't know for sure what the encoding used is, but we take a best guess and
decode the output into a proper unicode object so that the screen display
and XML writer interpret it correctly."""
try:
preferredencoding = locale.getpreferredencoding()
except locale.Error:
# This can happen if the LANG environment variable is set to something
# weird.
preferredencoding = None
if preferredencoding is not None:
try:
reader = codecs.getreader(preferredencoding)
return reader(f, "replace")
except LookupError:
# The lookup failed. This can happen if the preferred encoding is
# unknown ("X-MAC-KOREAN" has been observed). Ignore it and return
# the unwrapped file.
log.debug("Unknown encoding \"%s\"." % preferredencoding)
return f
def escape_nmap_filename(filename):
"""Escape '%' characters so they are not interpreted as strftime format
specifiers, which are not supported by Zenmap."""
@@ -235,8 +202,8 @@ class NmapCommand(object):
# We don't need a file name for stdout output, just a handle. A
# TemporaryFile is deleted as soon as it is closed, and in Unix is
# unlinked immediately after creation so it's not even visible.
f = tempfile.TemporaryFile(mode="rb", prefix=APP_NAME + "-stdout-")
self.stdout_file = wrap_file_in_preferred_encoding(f)
f = tempfile.TemporaryFile(mode="r", prefix=APP_NAME + "-stdout-")
self.stdout_file = f
if stderr is None:
stderr = f
@@ -260,6 +227,7 @@ class NmapCommand(object):
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
self.command_process = subprocess.Popen(command_list, bufsize=1,
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=f,
stderr=stderr,