diff options
Diffstat (limited to '')
-rw-r--r-- | zenmap/radialnet/core/ArgvHandle.py | 98 | ||||
-rw-r--r-- | zenmap/radialnet/core/Coordinate.py | 208 | ||||
-rw-r--r-- | zenmap/radialnet/core/Graph.py | 247 | ||||
-rw-r--r-- | zenmap/radialnet/core/Info.py | 63 | ||||
-rw-r--r-- | zenmap/radialnet/core/Interpolation.py | 157 | ||||
-rw-r--r-- | zenmap/radialnet/core/XMLHandler.py | 324 | ||||
-rw-r--r-- | zenmap/radialnet/core/__init__.py | 56 |
7 files changed, 1153 insertions, 0 deletions
diff --git a/zenmap/radialnet/core/ArgvHandle.py b/zenmap/radialnet/core/ArgvHandle.py new file mode 100644 index 0000000..2e72d93 --- /dev/null +++ b/zenmap/radialnet/core/ArgvHandle.py @@ -0,0 +1,98 @@ +# vim: set fileencoding=utf-8 : + +# ***********************IMPORTANT NMAP LICENSE TERMS************************ +# * +# * The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap +# * Project"). Nmap is also a registered trademark of the Nmap Project. +# * +# * This program is distributed under the terms of the Nmap Public Source +# * License (NPSL). The exact license text applying to a particular Nmap +# * release or source code control revision is contained in the LICENSE +# * file distributed with that version of Nmap or source code control +# * revision. More Nmap copyright/legal information is available from +# * https://nmap.org/book/man-legal.html, and further information on the +# * NPSL license itself can be found at https://nmap.org/npsl/ . This +# * header summarizes some key points from the Nmap license, but is no +# * substitute for the actual license text. +# * +# * Nmap is generally free for end users to download and use themselves, +# * including commercial use. It is available from https://nmap.org. +# * +# * The Nmap license generally prohibits companies from using and +# * redistributing Nmap in commercial products, but we sell a special Nmap +# * OEM Edition with a more permissive license and special features for +# * this purpose. See https://nmap.org/oem/ +# * +# * If you have received a written Nmap license agreement or contract +# * stating terms other than these (such as an Nmap OEM license), you may +# * choose to use and redistribute Nmap under those terms instead. +# * +# * The official Nmap Windows builds include the Npcap software +# * (https://npcap.com) for packet capture and transmission. It is under +# * separate license terms which forbid redistribution without special +# * permission. So the official Nmap Windows builds may not be redistributed +# * without special permission (such as an Nmap OEM license). +# * +# * Source is provided to this software because we believe users have a +# * right to know exactly what a program is going to do before they run it. +# * This also allows you to audit the software for security holes. +# * +# * Source code also allows you to port Nmap to new platforms, fix bugs, and add +# * new features. You are highly encouraged to submit your changes as a Github PR +# * or by email to the dev@nmap.org mailing list for possible incorporation into +# * the main distribution. Unless you specify otherwise, it is understood that +# * you are offering us very broad rights to use your submissions as described in +# * the Nmap Public Source License Contributor Agreement. This is important +# * because we fund the project by selling licenses with various terms, and also +# * because the inability to relicense code has caused devastating problems for +# * other Free Software projects (such as KDE and NASM). +# * +# * The free version of Nmap is distributed in the hope that it will be +# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, +# * indemnification and commercial support are all available through the +# * Npcap OEM program--see https://nmap.org/oem/ +# * +# ***************************************************************************/ + +import sys + + +class ArgvHandle: + """ + """ + def __init__(self, argv): + """ + """ + self.__argv = argv + + def get_option(self, option): + """ + """ + if option in self.__argv: + + index = self.__argv.index(option) + + if index + 1 < len(self.__argv): + return self.__argv[index + 1] + + return None + + def has_option(self, option): + """ + """ + return option in self.__argv + + def get_last_value(self): + """ + """ + return self.__argv[-1] + + +if __name__ == '__main__': + + import sys + + h = ArgvHandle(sys.argv) + + print(h.get_last_value()) diff --git a/zenmap/radialnet/core/Coordinate.py b/zenmap/radialnet/core/Coordinate.py new file mode 100644 index 0000000..5c82212 --- /dev/null +++ b/zenmap/radialnet/core/Coordinate.py @@ -0,0 +1,208 @@ +# vim: set fileencoding=utf-8 : + +# ***********************IMPORTANT NMAP LICENSE TERMS************************ +# * +# * The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap +# * Project"). Nmap is also a registered trademark of the Nmap Project. +# * +# * This program is distributed under the terms of the Nmap Public Source +# * License (NPSL). The exact license text applying to a particular Nmap +# * release or source code control revision is contained in the LICENSE +# * file distributed with that version of Nmap or source code control +# * revision. More Nmap copyright/legal information is available from +# * https://nmap.org/book/man-legal.html, and further information on the +# * NPSL license itself can be found at https://nmap.org/npsl/ . This +# * header summarizes some key points from the Nmap license, but is no +# * substitute for the actual license text. +# * +# * Nmap is generally free for end users to download and use themselves, +# * including commercial use. It is available from https://nmap.org. +# * +# * The Nmap license generally prohibits companies from using and +# * redistributing Nmap in commercial products, but we sell a special Nmap +# * OEM Edition with a more permissive license and special features for +# * this purpose. See https://nmap.org/oem/ +# * +# * If you have received a written Nmap license agreement or contract +# * stating terms other than these (such as an Nmap OEM license), you may +# * choose to use and redistribute Nmap under those terms instead. +# * +# * The official Nmap Windows builds include the Npcap software +# * (https://npcap.com) for packet capture and transmission. It is under +# * separate license terms which forbid redistribution without special +# * permission. So the official Nmap Windows builds may not be redistributed +# * without special permission (such as an Nmap OEM license). +# * +# * Source is provided to this software because we believe users have a +# * right to know exactly what a program is going to do before they run it. +# * This also allows you to audit the software for security holes. +# * +# * Source code also allows you to port Nmap to new platforms, fix bugs, and add +# * new features. You are highly encouraged to submit your changes as a Github PR +# * or by email to the dev@nmap.org mailing list for possible incorporation into +# * the main distribution. Unless you specify otherwise, it is understood that +# * you are offering us very broad rights to use your submissions as described in +# * the Nmap Public Source License Contributor Agreement. This is important +# * because we fund the project by selling licenses with various terms, and also +# * because the inability to relicense code has caused devastating problems for +# * other Free Software projects (such as KDE and NASM). +# * +# * The free version of Nmap is distributed in the hope that it will be +# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, +# * indemnification and commercial support are all available through the +# * Npcap OEM program--see https://nmap.org/oem/ +# * +# ***************************************************************************/ + +import math + + +class PolarCoordinate: + """ + Class to implement a polar coordinate object + """ + + def __init__(self, r=0, t=0): + """ + Constructor method of PolarCoordinate class + @type r: number + @param r: The radius of coordinate + @type t: number + @param t: The angle (theta) of coordinate in radians + """ + + self.__r = r + """Radius of polar coordinate""" + self.__t = t + """Angle (theta) of polar coordinate in radians""" + + def get_theta(self): + """ + """ + return math.degrees(self.__t) + + def get_radius(self): + """ + """ + return self.__r + + def set_theta(self, t): + """ + """ + self.__t = math.radians(t) + + def set_radius(self, r): + """ + """ + self.__r = r + + def get_coordinate(self): + """ + Set polar coordinate + @rtype: tuple + @return: Polar coordinates (r, t) + """ + return (self.__r, math.degrees(self.__t)) + + def set_coordinate(self, r, t): + """ + Set polar coordinate + @type r: number + @param r: The radius of coordinate + @type t: number + @param t: The angle (theta) of coordinate + """ + self.__r = r + self.__t = math.radians(t) + + def to_cartesian(self): + """ + Convert polar in cartesian coordinate + @rtype: tuple + @return: cartesian coordinates (x, y) + """ + x = self.__r * math.cos(self.__t) + y = self.__r * math.sin(self.__t) + + return (x, y) + + +class CartesianCoordinate: + """ + Class to implement a cartesian coordinate object + """ + def __init__(self, x=0, y=0): + """ + Constructor method of CartesianCoordinate class + @type x: number + @param x: The x component of coordinate + @type y: number + @param y: The y component of coordinate + """ + self.__x = x + """X component of cartesian coordinate""" + self.__y = y + """Y component of cartesian coordinate""" + + def get_coordinate(self): + """ + Get cartesian coordinate + @rtype: tuple + @return: Cartesian coordinates (x, y) + """ + return (self.__x, self.__y) + + def set_coordinate(self, x, y): + """ + Set cartesian coordinate + @type x: number + @param x: The x component of coordinate + @type y: number + @param y: The y component of coordinate + """ + self.__x = x + self.__y = y + + def to_polar(self): + """ + Convert cartesian in polar coordinate + @rtype: tuple + @return: polar coordinates (r, t) + """ + r = math.sqrt(self.__x ** 2 + self.__y ** 2) + + if self.__x > 0: + + if self.__y >= 0: + t = math.atan(self.__y / self.__x) + + else: + t = math.atan(self.__y / self.__x) + 2 * math.pi + + elif self.__x < 0: + t = math.atan(self.__y / self.__x) + math.pi + + elif self.__x == 0: + + if self.__y == 0: + t = 0 + + if self.__y > 0: + t = math.pi / 2 + + else: + t = -math.pi / 2 + + return (r, t) + + +if __name__ == "__main__": + + # Testing application + + polar = PolarCoordinate(1, math.pi) + cartesian = CartesianCoordinate(-1, 0) + + print(polar.to_cartesian()) + print(cartesian.to_polar()) diff --git a/zenmap/radialnet/core/Graph.py b/zenmap/radialnet/core/Graph.py new file mode 100644 index 0000000..1e0e8b5 --- /dev/null +++ b/zenmap/radialnet/core/Graph.py @@ -0,0 +1,247 @@ +# vim: set fileencoding=utf-8 : + +# ***********************IMPORTANT NMAP LICENSE TERMS************************ +# * +# * The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap +# * Project"). Nmap is also a registered trademark of the Nmap Project. +# * +# * This program is distributed under the terms of the Nmap Public Source +# * License (NPSL). The exact license text applying to a particular Nmap +# * release or source code control revision is contained in the LICENSE +# * file distributed with that version of Nmap or source code control +# * revision. More Nmap copyright/legal information is available from +# * https://nmap.org/book/man-legal.html, and further information on the +# * NPSL license itself can be found at https://nmap.org/npsl/ . This +# * header summarizes some key points from the Nmap license, but is no +# * substitute for the actual license text. +# * +# * Nmap is generally free for end users to download and use themselves, +# * including commercial use. It is available from https://nmap.org. +# * +# * The Nmap license generally prohibits companies from using and +# * redistributing Nmap in commercial products, but we sell a special Nmap +# * OEM Edition with a more permissive license and special features for +# * this purpose. See https://nmap.org/oem/ +# * +# * If you have received a written Nmap license agreement or contract +# * stating terms other than these (such as an Nmap OEM license), you may +# * choose to use and redistribute Nmap under those terms instead. +# * +# * The official Nmap Windows builds include the Npcap software +# * (https://npcap.com) for packet capture and transmission. It is under +# * separate license terms which forbid redistribution without special +# * permission. So the official Nmap Windows builds may not be redistributed +# * without special permission (such as an Nmap OEM license). +# * +# * Source is provided to this software because we believe users have a +# * right to know exactly what a program is going to do before they run it. +# * This also allows you to audit the software for security holes. +# * +# * Source code also allows you to port Nmap to new platforms, fix bugs, and add +# * new features. You are highly encouraged to submit your changes as a Github PR +# * or by email to the dev@nmap.org mailing list for possible incorporation into +# * the main distribution. Unless you specify otherwise, it is understood that +# * you are offering us very broad rights to use your submissions as described in +# * the Nmap Public Source License Contributor Agreement. This is important +# * because we fund the project by selling licenses with various terms, and also +# * because the inability to relicense code has caused devastating problems for +# * other Free Software projects (such as KDE and NASM). +# * +# * The free version of Nmap is distributed in the hope that it will be +# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, +# * indemnification and commercial support are all available through the +# * Npcap OEM program--see https://nmap.org/oem/ +# * +# ***************************************************************************/ + + +class Node(object): + """ + Node class + """ + def __init__(self): + """ + Constructor method of Node class + @type : integer + @param : Node identifier + """ + self.__data = None + """User-controlled data pointer""" + self.__edges = [] + """List of edges to other nodes""" + + def get_data(self): + return self.__data + + def set_data(self, data): + self.__data = data + + def get_edge(self, dest): + """ + Return the edge connecting to dest, or None if none + """ + for edge in self.__edges: + if dest in edge.get_nodes(): + return edge + return None + + def get_edges(self): + """ + Return the list of edges + """ + return self.__edges + + def add_edge(self, edge): + self.__edges.append(edge) + + +class Edge: + """ + """ + def __init__(self, nodes): + """ + """ + self.__weights = [] + self.__nodes = nodes + self.__weights_mean = None + + def get_nodes(self): + """ + """ + return self.__nodes + + def get_weights(self): + """ + """ + return self.__weights + + def set_weights(self, weights): + """ + """ + self.__weights = weights + self.__weights_mean = sum(self.__weights) / len(self.__weights) + + def add_weight(self, weight): + """ + """ + self.__weights.append(weight) + self.__weights_mean = sum(self.__weights) / len(self.__weights) + + def get_weights_mean(self): + """ + """ + return self.__weights_mean + + +class Graph: + """ + Network Graph class + """ + + def __init__(self): + """ + Constructor method of Graph class + @type : list + @param : List of nodes + """ + self.__main_node = None + self.__nodes = [] + self.__max_edge_mean_value = None + self.__min_edge_mean_value = None + + def set_nodes(self, nodes): + """ + """ + self.__nodes = nodes + + def get_nodes(self): + """ + """ + return self.__nodes + + def get_number_of_nodes(self): + """ + Get the number of nodes in graph + @rtype: number + @return: The number of nodes in the graph + """ + return len(self.__nodes) + + def set_main_node(self, node): + """ + Set the main node + @type : number + @param : The node + """ + self.__main_node = node + + def get_main_node(self): + """ + Get the main node + @rtype: Node + @return: The main node + """ + return self.__main_node + + def set_connection(self, a, b, weight=None): + """ + Set node connections + @type : list + @param : List of connections + """ + + # if is a new connection make it + edge = a.get_edge(b) + if edge is None: + edge = Edge((a, b)) + a.add_edge(edge) + b.add_edge(edge) + + # then add new weight value + if weight is not None: + + edge.add_weight(weight) + + mean_weight = edge.get_weights_mean() + if (self.__min_edge_mean_value is None or + mean_weight < self.__min_edge_mean_value): + self.__min_edge_mean_value = mean_weight + if (self.__max_edge_mean_value is None or + mean_weight > self.__max_edge_mean_value): + self.__max_edge_mean_value = mean_weight + + def get_edges(self): + """ + An iterator that yields all edges + """ + for node in self.__nodes: + for edge in node.get_edges(): + if edge.get_nodes()[0] == node: + yield edge + + def get_node_connections(self, node): + """ + """ + connections = [] + + for edge in node.get_edges(): + + (a, b) = edge.get_nodes() + + if a == node: + connections.append(b) + if b == node: + connections.append(a) + + return connections + + def get_max_edge_mean_weight(self): + """ + """ + return self.__max_edge_mean_value + + def get_min_edge_mean_weight(self): + """ + """ + return self.__min_edge_mean_value diff --git a/zenmap/radialnet/core/Info.py b/zenmap/radialnet/core/Info.py new file mode 100644 index 0000000..d04b31a --- /dev/null +++ b/zenmap/radialnet/core/Info.py @@ -0,0 +1,63 @@ +# vim: set fileencoding=utf-8 : + +# ***********************IMPORTANT NMAP LICENSE TERMS************************ +# * +# * The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap +# * Project"). Nmap is also a registered trademark of the Nmap Project. +# * +# * This program is distributed under the terms of the Nmap Public Source +# * License (NPSL). The exact license text applying to a particular Nmap +# * release or source code control revision is contained in the LICENSE +# * file distributed with that version of Nmap or source code control +# * revision. More Nmap copyright/legal information is available from +# * https://nmap.org/book/man-legal.html, and further information on the +# * NPSL license itself can be found at https://nmap.org/npsl/ . This +# * header summarizes some key points from the Nmap license, but is no +# * substitute for the actual license text. +# * +# * Nmap is generally free for end users to download and use themselves, +# * including commercial use. It is available from https://nmap.org. +# * +# * The Nmap license generally prohibits companies from using and +# * redistributing Nmap in commercial products, but we sell a special Nmap +# * OEM Edition with a more permissive license and special features for +# * this purpose. See https://nmap.org/oem/ +# * +# * If you have received a written Nmap license agreement or contract +# * stating terms other than these (such as an Nmap OEM license), you may +# * choose to use and redistribute Nmap under those terms instead. +# * +# * The official Nmap Windows builds include the Npcap software +# * (https://npcap.com) for packet capture and transmission. It is under +# * separate license terms which forbid redistribution without special +# * permission. So the official Nmap Windows builds may not be redistributed +# * without special permission (such as an Nmap OEM license). +# * +# * Source is provided to this software because we believe users have a +# * right to know exactly what a program is going to do before they run it. +# * This also allows you to audit the software for security holes. +# * +# * Source code also allows you to port Nmap to new platforms, fix bugs, and add +# * new features. You are highly encouraged to submit your changes as a Github PR +# * or by email to the dev@nmap.org mailing list for possible incorporation into +# * the main distribution. Unless you specify otherwise, it is understood that +# * you are offering us very broad rights to use your submissions as described in +# * the Nmap Public Source License Contributor Agreement. This is important +# * because we fund the project by selling licenses with various terms, and also +# * because the inability to relicense code has caused devastating problems for +# * other Free Software projects (such as KDE and NASM). +# * +# * The free version of Nmap is distributed in the hope that it will be +# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, +# * indemnification and commercial support are all available through the +# * Npcap OEM program--see https://nmap.org/oem/ +# * +# ***************************************************************************/ + + +INFO = {'name': 'RadialNet', + 'version': '0.44', + 'website': 'http://www.dca.ufrn.br/~joaomedeiros/radialnet/', + 'authors': ['João Paulo de Souza Medeiros'], + 'copyright': 'Copyright (C) 2007, 2008 Insecure.Com LLC'} diff --git a/zenmap/radialnet/core/Interpolation.py b/zenmap/radialnet/core/Interpolation.py new file mode 100644 index 0000000..1d89498 --- /dev/null +++ b/zenmap/radialnet/core/Interpolation.py @@ -0,0 +1,157 @@ +# vim: set fileencoding=utf-8 : + +# ***********************IMPORTANT NMAP LICENSE TERMS************************ +# * +# * The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap +# * Project"). Nmap is also a registered trademark of the Nmap Project. +# * +# * This program is distributed under the terms of the Nmap Public Source +# * License (NPSL). The exact license text applying to a particular Nmap +# * release or source code control revision is contained in the LICENSE +# * file distributed with that version of Nmap or source code control +# * revision. More Nmap copyright/legal information is available from +# * https://nmap.org/book/man-legal.html, and further information on the +# * NPSL license itself can be found at https://nmap.org/npsl/ . This +# * header summarizes some key points from the Nmap license, but is no +# * substitute for the actual license text. +# * +# * Nmap is generally free for end users to download and use themselves, +# * including commercial use. It is available from https://nmap.org. +# * +# * The Nmap license generally prohibits companies from using and +# * redistributing Nmap in commercial products, but we sell a special Nmap +# * OEM Edition with a more permissive license and special features for +# * this purpose. See https://nmap.org/oem/ +# * +# * If you have received a written Nmap license agreement or contract +# * stating terms other than these (such as an Nmap OEM license), you may +# * choose to use and redistribute Nmap under those terms instead. +# * +# * The official Nmap Windows builds include the Npcap software +# * (https://npcap.com) for packet capture and transmission. It is under +# * separate license terms which forbid redistribution without special +# * permission. So the official Nmap Windows builds may not be redistributed +# * without special permission (such as an Nmap OEM license). +# * +# * Source is provided to this software because we believe users have a +# * right to know exactly what a program is going to do before they run it. +# * This also allows you to audit the software for security holes. +# * +# * Source code also allows you to port Nmap to new platforms, fix bugs, and add +# * new features. You are highly encouraged to submit your changes as a Github PR +# * or by email to the dev@nmap.org mailing list for possible incorporation into +# * the main distribution. Unless you specify otherwise, it is understood that +# * you are offering us very broad rights to use your submissions as described in +# * the Nmap Public Source License Contributor Agreement. This is important +# * because we fund the project by selling licenses with various terms, and also +# * because the inability to relicense code has caused devastating problems for +# * other Free Software projects (such as KDE and NASM). +# * +# * The free version of Nmap is distributed in the hope that it will be +# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, +# * indemnification and commercial support are all available through the +# * Npcap OEM program--see https://nmap.org/oem/ +# * +# ***************************************************************************/ + + +class Linear2DInterpolator: + """ + Implements a bidimensional linear interpolator. + """ + + def __init__(self): + """ + Constructor method of Linear2DInterpolator class + """ + self.__start_point = (0, 0) + """Initial point of interpolation""" + self.__final_point = (0, 0) + """Final point of interpolation""" + self.__interpolated_points = [] + """Interpolated points vector""" + + def set_start_point(self, a, b): + """ + Set initial coordinate + Set final coordinate + @type a: number + @param a: The first component of final point + @type b: number + @param b: The second component of final point + """ + self.__start_point = (a, b) + + def set_final_point(self, a, b): + """ + Set final coordinate + @type a: number + @param a: The first component of final point + @type b: number + @param b: The second component of final point + """ + self.__final_point = (a, b) + + def get_weighed_points(self, number_of_pass, pass_vector): + """ + Return the vector of coordinates between the initial and final + coordinates with the specified size + @type number_of_pass: number + @param number_of_pass: The number of pass of interpolation + @rtype: list + @return: A list of tuples with interpolated points + """ + (ai, bi) = self.__start_point + (af, bf) = self.__final_point + + a_conversion_factor = float(af - ai) / sum(pass_vector) + b_conversion_factor = float(bf - bi) / sum(pass_vector) + + a_pass = 0 + b_pass = 0 + + self.__interpolated_points = list(range(number_of_pass)) + + for i in range(0, number_of_pass): + + a_pass += pass_vector[i] * a_conversion_factor + b_pass += pass_vector[i] * b_conversion_factor + self.__interpolated_points[i] = (ai + a_pass, bi + b_pass) + + return self.__interpolated_points + + def get_points(self, number_of_pass): + """ + Return the vector of coordinates between the initial and final + coordinates with the specified size + @type number_of_pass: number + @param number_of_pass: The number of pass of interpolation + @rtype: list + @return: A list of tuples with interpolated points + """ + (ai, bi) = self.__start_point + (af, bf) = self.__final_point + + a_pass = float(af - ai) / number_of_pass + b_pass = float(bf - bi) / number_of_pass + + self.__interpolated_points = list(range(number_of_pass)) + + for i in range(1, number_of_pass + 1): + self.__interpolated_points[i - 1] = (ai + a_pass * i, + bi + b_pass * i) + + return self.__interpolated_points + + +if __name__ == "__main__": + + # Testing application + + i = Linear2DInterpolator() + + i.set_start_point(0, 0) + i.set_final_point(1, 1) + + print(len(i.get_points(10)), i.get_points(10)) diff --git a/zenmap/radialnet/core/XMLHandler.py b/zenmap/radialnet/core/XMLHandler.py new file mode 100644 index 0000000..36eb2e1 --- /dev/null +++ b/zenmap/radialnet/core/XMLHandler.py @@ -0,0 +1,324 @@ +# vim: set fileencoding=utf-8 : + +# ***********************IMPORTANT NMAP LICENSE TERMS************************ +# * +# * The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap +# * Project"). Nmap is also a registered trademark of the Nmap Project. +# * +# * This program is distributed under the terms of the Nmap Public Source +# * License (NPSL). The exact license text applying to a particular Nmap +# * release or source code control revision is contained in the LICENSE +# * file distributed with that version of Nmap or source code control +# * revision. More Nmap copyright/legal information is available from +# * https://nmap.org/book/man-legal.html, and further information on the +# * NPSL license itself can be found at https://nmap.org/npsl/ . This +# * header summarizes some key points from the Nmap license, but is no +# * substitute for the actual license text. +# * +# * Nmap is generally free for end users to download and use themselves, +# * including commercial use. It is available from https://nmap.org. +# * +# * The Nmap license generally prohibits companies from using and +# * redistributing Nmap in commercial products, but we sell a special Nmap +# * OEM Edition with a more permissive license and special features for +# * this purpose. See https://nmap.org/oem/ +# * +# * If you have received a written Nmap license agreement or contract +# * stating terms other than these (such as an Nmap OEM license), you may +# * choose to use and redistribute Nmap under those terms instead. +# * +# * The official Nmap Windows builds include the Npcap software +# * (https://npcap.com) for packet capture and transmission. It is under +# * separate license terms which forbid redistribution without special +# * permission. So the official Nmap Windows builds may not be redistributed +# * without special permission (such as an Nmap OEM license). +# * +# * Source is provided to this software because we believe users have a +# * right to know exactly what a program is going to do before they run it. +# * This also allows you to audit the software for security holes. +# * +# * Source code also allows you to port Nmap to new platforms, fix bugs, and add +# * new features. You are highly encouraged to submit your changes as a Github PR +# * or by email to the dev@nmap.org mailing list for possible incorporation into +# * the main distribution. Unless you specify otherwise, it is understood that +# * you are offering us very broad rights to use your submissions as described in +# * the Nmap Public Source License Contributor Agreement. This is important +# * because we fund the project by selling licenses with various terms, and also +# * because the inability to relicense code has caused devastating problems for +# * other Free Software projects (such as KDE and NASM). +# * +# * The free version of Nmap is distributed in the hope that it will be +# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, +# * indemnification and commercial support are all available through the +# * Npcap OEM program--see https://nmap.org/oem/ +# * +# ***************************************************************************/ + + +# Prevent loading PyXML +import xml +xml.__path__ = [x for x in xml.__path__ if "_xmlplus" not in x] + +import xml.sax +import xml.sax.saxutils +from xml.sax.xmlreader import AttributesImpl as Attributes + + +class XMLNode: + """ + """ + def __init__(self, name): + """ + """ + self.__name = name + self.__text = "" + self.__attrs = dict() + self.__children = [] + + def set_text(self, text): + """ + """ + self.__text = text + + def get_text(self): + """ + """ + return self.__text + + def set_name(self, name): + """ + """ + self.__name = name + + def get_name(self): + """ + """ + return self.__name + + def add_attr(self, key, value): + """ + """ + self.__attrs[key] = value + + def add_child(self, child): + """ + """ + self.__children.append(child) + + def get_keys(self): + """ + """ + return self.__attrs.keys() + + def get_attr(self, attr): + """ + """ + return self.__attrs.get(attr) + + def get_attrs(self): + """ + """ + return self.__attrs + + def get_children(self): + """ + """ + return self.__children + + def query_children(self, name, attr, value, first=False, deep=False): + """ + """ + result = [] + + for child in self.__children: + + if child.get_name() == name: + + if attr in child.get_attrs(): + + c_value = child.get_attr(attr) + + if c_value == value or c_value == str(value): + result.append(child) + + if deep: + + c_result = child.query_children(name, attr, value, first, deep) + + if c_result is not None: + + if first: + return c_result + + else: + result.extend(c_result) + + if first and len(result) > 0: + return result[0] + + if first: + return None + + return result + + def search_children(self, name, first=False, deep=False): + """ + """ + result = [] + + for child in self.__children: + + if child.get_name() == name: + + result.append(child) + + if first: + return result[0] + + if deep: + + c_result = child.search_children(name, first, deep) + + if c_result is not None and c_result != []: + + if first: + return c_result + + else: + result.extend(c_result) + + if first: + return None + + return result + + +class XMLWriter(xml.sax.saxutils.XMLGenerator): + """ + """ + def __init__(self, file, root=None, encoding="utf-8"): + """ + """ + xml.sax.saxutils.XMLGenerator.__init__(self, file, encoding) + + self.__root = root + + def set_root(self, root): + """ + """ + self.__root = root + + def write(self): + """ + """ + self.startDocument() + self.write_xml_node([self.__root]) + self.endDocument() + + def write_xml_node(self, root): + """ + """ + for child in root: + + self.startElement(child.get_name(), Attributes(child.get_attrs())) + + if child.get_text() != "": + self.characters(child.get_text()) + + self.write_xml_node(child.get_children()) + + self.endElement(child.get_name()) + + +class XMLReader(xml.sax.ContentHandler): + """ + """ + def __init__(self, file=None): + """ + """ + xml.sax.ContentHandler.__init__(self) + self.__text = "" + self.__status = [] + + self.__file = file + self.__root = None + + self.__parser = xml.sax.make_parser() + self.__parser.setContentHandler(self) + + def set_file(self, file, root): + """ + """ + self.__file = file + + def get_file(self): + """ + """ + return self.__file + + def get_root(self): + """ + """ + return self.__root + + def parse(self): + """ + """ + if self.__file is not None: + self.__parser.parse(self.__file) + + def startDocument(self): + """ + """ + pass + + def startElement(self, name, attrs): + """ + """ + # create new node + node = XMLNode(name) + + # putting attributes and values in node + for attr in attrs.getNames(): + node.add_attr(attr, attrs.get(attr).strip()) + + # who is my father? + if len(self.__status) > 0: + self.__status[-1].add_child(node) + + if self.__root is None: + self.__root = node + + self.__status.append(node) + + def endElement(self, name): + """ + """ + self.__status[-1].set_text(self.__text.strip()) + + self.__text = "" + self.__status.pop() + + def endDocument(self): + """ + """ + pass + + def characters(self, text): + """ + """ + self.__text += text + + +if __name__ == "__main__": + + import sys + + reader = XMLReader(sys.argv[1]) + reader.parse() + + root = reader.get_root() + + writer = XMLWriter(open("test.xml", 'w'), root) + writer.write() diff --git a/zenmap/radialnet/core/__init__.py b/zenmap/radialnet/core/__init__.py new file mode 100644 index 0000000..c314dd7 --- /dev/null +++ b/zenmap/radialnet/core/__init__.py @@ -0,0 +1,56 @@ +# vim: set fileencoding=utf-8 : + +# ***********************IMPORTANT NMAP LICENSE TERMS************************ +# * +# * The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap +# * Project"). Nmap is also a registered trademark of the Nmap Project. +# * +# * This program is distributed under the terms of the Nmap Public Source +# * License (NPSL). The exact license text applying to a particular Nmap +# * release or source code control revision is contained in the LICENSE +# * file distributed with that version of Nmap or source code control +# * revision. More Nmap copyright/legal information is available from +# * https://nmap.org/book/man-legal.html, and further information on the +# * NPSL license itself can be found at https://nmap.org/npsl/ . This +# * header summarizes some key points from the Nmap license, but is no +# * substitute for the actual license text. +# * +# * Nmap is generally free for end users to download and use themselves, +# * including commercial use. It is available from https://nmap.org. +# * +# * The Nmap license generally prohibits companies from using and +# * redistributing Nmap in commercial products, but we sell a special Nmap +# * OEM Edition with a more permissive license and special features for +# * this purpose. See https://nmap.org/oem/ +# * +# * If you have received a written Nmap license agreement or contract +# * stating terms other than these (such as an Nmap OEM license), you may +# * choose to use and redistribute Nmap under those terms instead. +# * +# * The official Nmap Windows builds include the Npcap software +# * (https://npcap.com) for packet capture and transmission. It is under +# * separate license terms which forbid redistribution without special +# * permission. So the official Nmap Windows builds may not be redistributed +# * without special permission (such as an Nmap OEM license). +# * +# * Source is provided to this software because we believe users have a +# * right to know exactly what a program is going to do before they run it. +# * This also allows you to audit the software for security holes. +# * +# * Source code also allows you to port Nmap to new platforms, fix bugs, and add +# * new features. You are highly encouraged to submit your changes as a Github PR +# * or by email to the dev@nmap.org mailing list for possible incorporation into +# * the main distribution. Unless you specify otherwise, it is understood that +# * you are offering us very broad rights to use your submissions as described in +# * the Nmap Public Source License Contributor Agreement. This is important +# * because we fund the project by selling licenses with various terms, and also +# * because the inability to relicense code has caused devastating problems for +# * other Free Software projects (such as KDE and NASM). +# * +# * The free version of Nmap is distributed in the hope that it will be +# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, +# * indemnification and commercial support are all available through the +# * Npcap OEM program--see https://nmap.org/oem/ +# * +# ***************************************************************************/ |