def getMangledName()

in codegen_sources/preprocessing/obfuscation/bobskater_obfuscator.py [0:0]


    def getMangledName(self, nodeStack, strId, node):
        """
        Determine whether a strId used somewhere should be
        mangled
        """
        frameEntry = self._rootFrame.findEntryAtStack(nodeStack, strId)
        if frameEntry is None:
            return False
        isBuiltin = frameEntry.parent == self._rootFrame

        alreadyMangledName = frameEntry.value
        if alreadyMangledName:
            if isinstance(node, ast.Attribute):
                try:
                    parent = node.value.id
                    if (
                        parent == "self"
                        or parent.startswith("VAR_")
                        or parent.startswith("CLASS_")
                    ):
                        return alreadyMangledName
                except AttributeError:
                    return False
            else:
                self._debugMsg = 'Already mangled; "' + alreadyMangledName + '"'
                return alreadyMangledName  # It has a mangled name, use it
        if alreadyMangledName is False:
            self._debugMsg = "Already mangled; Don't mangle"
            return False  # It was instructed to not mangle

        if isBuiltin:
            # Dont rename builtins
            self._debugMsg = "Don't mangle; Builtin"
            frameEntry.value = False
            return False

        if strId.startswith("__") and strId.endswith("__"):
            # Generally functions that cannot be renamed such as __init__
            frameEntry.value = False
            return False

        stackNode = (
            frameEntry.parent.source
        )  # ClassDef, FunctionDef, etc that defined the stack
        sourceNode = node  # The node that the id came from
        #         if isinstance(stackNode, (ast.ClassDef,ast.Module)):
        #             #Anything in the class namespace (static variables, methods)
        #             #and anything in the module namespace (will probs be exported)
        #             #should not be mangled
        #             self._debugMsg = "Don't mangle; Class or Module namespace"
        #             frameEntry.value = False
        #             return False
        if isinstance(sourceNode, ast.alias):
            # An imported name, don't mangle those
            self._debugMsg = "Don't mangle; import name"
            frameEntry.value = False
            return False
        elif (
            isinstance(sourceNode, ast.arg)
            and hasattr(nodeStack[-1], "defaults")
            and nodeStack[-1].defaults
        ):
            self._debugMsg = "Don't mangle; kwargs"
            # An argument node with keyword args, don't mangle the keyword args
            # Slice the keyword arguments
            # TODO: I have no idea if this functions in not Python 3.5
            argumentsNode = nodeStack[-1]
            # Slice the number of default nodes from the end
            kwStrs = list(
                map(lambda n: n.arg, argumentsNode.args[-len(argumentsNode.defaults) :])
            )
            if self.debug:
                self._logger.debug(
                    "kwarg debug %s %s %s", kwStrs, strId, strId in kwStrs
                )
            if strId in kwStrs:
                frameEntry.value = False
                return False

        # Otherwise, return the name we're mangling to for this
        # string ID and store it
        # Make sure the mangleName isn't in the current scope already (as an unmangled name)
        ids = frameEntry.parent.getAllIds()
        if isinstance(sourceNode, ast.ClassDef):
            mangledName = self.names_generator.get_new_name(
                sourceNode.name, ObfuscatedNameType.CLASS
            )
        elif isinstance(sourceNode, ast.FunctionDef):
            mangledName = self.names_generator.get_new_name(
                sourceNode.name, ObfuscatedNameType.FUNCTION
            )
        elif isinstance(sourceNode, ast.arg):
            mangledName = self.names_generator.get_new_name(
                sourceNode.arg, ObfuscatedNameType.VARIABLE
            )
        elif isinstance(sourceNode, ast.Name):
            mangledName = self.names_generator.get_new_name(
                sourceNode.id, ObfuscatedNameType.VARIABLE
            )
        elif isinstance(sourceNode, ast.Attribute):
            oldname = sourceNode.attr
            try:
                parent = sourceNode.value.id
                if (
                    parent == "self"
                    or parent.startswith("VAR_")
                    or parent.startswith("CLASS_")
                ):
                    if self.names_generator.function_is_obfuscated(oldname):
                        # we consider that if it was already defined as function, it is a function. Otherwise, it is a variable
                        mangledName = self.names_generator.get_new_name(
                            oldname, ObfuscatedNameType.FUNCTION
                        )
                    else:
                        mangledName = self.names_generator.get_new_name(
                            oldname, ObfuscatedNameType.VARIABLE, isAttribute=True
                        )
                else:
                    # probably an import. Don't mangle
                    return False
            except AttributeError:
                return False

        frameEntry.value = mangledName
        return mangledName