示例#1
0
 def __init__(self, sized, minChars, maxChars, delimiter, factor, offset):
     """Constructor of WordSizeRelationType:
     """
     AbstractRelationType.__init__(self, sized, minChars, maxChars,
                                   delimiter, factor, offset)
     self.log = logging.getLogger(
         'netzob.Common.MMSTD.Dictionary.RelationTypes.WordSizeRelationType.py'
     )
示例#2
0
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Loads a ComputedRelationVariable from an XML definition.
        """
        logging.debug("[ ComputedRelationVariable: loadFromXML:")
        if version == "0.1":
            xmlID = xmlRoot.get("id")
            xmlName = xmlRoot.get("name")
            xmlMutable = xmlRoot.get("mutable") == "True"
            xmlLearnable = xmlRoot.get("learnable") == "True"

            # sized
            xmlSized = xmlRoot.find("{" + namespace + "}sized")
            if xmlSized is not None and xmlSized.text != "None":
                sized = xmlSized.text == 'True'
            else:
                sized = True

            # minChars
            xmlMinChars = xmlRoot.find("{" + namespace + "}minChars")
            if xmlMinChars is not None:
                minChars = int(xmlMinChars.text)
            else:
                minChars = 0

            # maxChars
            xmlMaxChars = xmlRoot.find("{" + namespace + "}maxChars")
            if xmlMaxChars is not None:
                maxChars = int(xmlMaxChars.text)
            else:
                maxChars = minChars

            # delimiter
            xmlDelimiter = xmlRoot.find("{" + namespace + "}delimiter")
            if xmlDelimiter is not None and xmlDelimiter.text != "None":
                delimiter = xmlDelimiter.text
            else:
                delimiter = None

            # type
            _type = None
            xmlType = xmlRoot.find("{" + namespace + "}type")
            if xmlType is not None:
                _type = AbstractRelationType.makeType(xmlType.text, sized, minChars, maxChars, delimiter)
                if _type is None:
                    return None
            else:
                logging.error("No type specified for this variable in the xml file.")
                return None

            # ref
            xmlRefID = xmlRoot.find("{" + namespace + "}ref").text

            result = ComputedRelationVariable(xmlID, xmlName, xmlMutable, xmlLearnable, _type, xmlRefID, symbol)
            logging.debug("ComputedRelationVariable: loadFromXML successes: {0} ]".format(result.toString()))
            return result
        logging.debug("ComputedRelationVariable: loadFromXML fails")
        return None
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Loads a ComputedRelationVariable from an XML definition.
        """
        logging.debug("[ ComputedRelationVariable: loadFromXML:")
        if version == "0.1":
            xmlID = xmlRoot.get("id")
            xmlName = xmlRoot.get("name")
            xmlMutable = xmlRoot.get("mutable") == "True"
            xmlLearnable = xmlRoot.get("learnable") == "True"

            # sized
            xmlSized = xmlRoot.find("{" + namespace + "}sized")
            if xmlSized is not None and xmlSized.text != "None":
                sized = xmlSized.text == 'True'
            else:
                sized = True

            # minChars
            xmlMinChars = xmlRoot.find("{" + namespace + "}minChars")
            if xmlMinChars is not None:
                minChars = int(xmlMinChars.text)
            else:
                minChars = 0

            # maxChars
            xmlMaxChars = xmlRoot.find("{" + namespace + "}maxChars")
            if xmlMaxChars is not None:
                maxChars = int(xmlMaxChars.text)
            else:
                maxChars = minChars

            # delimiter
            xmlDelimiter = xmlRoot.find("{" + namespace + "}delimiter")
            if xmlDelimiter is not None and xmlDelimiter.text != "None":
                delimiter = xmlDelimiter.text
            else:
                delimiter = None

            # factor
            xmlFactor = xmlRoot.find("{" + namespace + "}factor")
            if xmlFactor is not None and xmlFactor.text != "None":
                factor = float(xmlFactor.text)
            else:
                factor = 1

            # offset
            xmlOffset = xmlRoot.find("{" + namespace + "}offset")
            if xmlOffset is not None and xmlOffset.text != "None":
                offset = float(xmlOffset.text)
            else:
                offset = 0

            # type
            _type = None
            xmlType = xmlRoot.find("{" + namespace + "}type")
            if xmlType is not None:
                _type = AbstractRelationType.makeType(xmlType.text, sized,
                                                      minChars, maxChars,
                                                      delimiter, factor,
                                                      offset)
                if _type is None:
                    return None
            else:
                logging.error(
                    "No type specified for this variable in the xml file.")
                return None

            # ref
            xmlRefID = xmlRoot.find("{" + namespace + "}ref").text

            result = ComputedRelationVariable(xmlID, xmlName, xmlMutable,
                                              xmlLearnable, _type, xmlRefID,
                                              symbol)
            logging.debug(
                "ComputedRelationVariable: loadFromXML successes: {0} ]".
                format(result.toString()))
            return result
        logging.debug("ComputedRelationVariable: loadFromXML fails")
        return None
示例#4
0
    def validateChanges(self, widget):
        """validateChanges:
                Validate the changes that a user has done on a variable.
                Called by a click on the apply button.

                @type widget: Gtk.widget
                @param widget: the widget which calls this function.
        """
        dialog = self.view.getWidg("dialog")
        if self.editOverCreate:
            anid = self.variable.getID()
        else:
            anid = str(uuid.uuid4())

        name = self.view.getWidg("nameEntry").get_text()
        mutable = self.view.getWidg("mutableCheck").get_active()
        learnable = self.view.getWidg("learnableCheck").get_active()
        strVarType = self.view.getWidg("variableTypeCombo").get_active_text()

        variable = None
        # Aggregate variable
        if strVarType == AggregateVariable.TYPE:
            variable = AggregateVariable(anid, name, mutable, learnable)

        # Alternate Variable
        elif strVarType == AlternateVariable.TYPE:
            variable = AlternateVariable(anid, name, mutable, learnable)

        # Repeat Variable
        elif strVarType == RepeatVariable.TYPE:
            minIterations = int(self.view.getWidg("minSpin").get_text())
            maxIterations = int(self.view.getWidg("maxSpin").get_text())
            variable = RepeatVariable(anid, name, mutable, learnable, None, minIterations, maxIterations)

        # Data Variable
        elif strVarType == DataVariable.TYPE:
            originalValue = str(self.view.getWidg("valueEntry").get_text())
            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()
            vtype = AbstractType.makeType(self.view.getWidg("typeCombo").get_active_text(), sized, minChars, maxChars, delimiter)
            variable = DataVariable(anid, name, mutable, learnable, vtype, originalValue)

#===============================================================================
#        # Direct Relation Variable
#        elif strVarType == DirectRelationVariable.TYPE:
#
#            # We find the variable by its ID.
#            pointedID = str(self.view.getWidg("IDEntry").get_text())
#
#            variable = DirectRelationVariable(anid, name, mutable, learnable, pointedID, self.treeController.symbol)
#===============================================================================

        # Computed Relation Variable
        elif strVarType == ComputedRelationVariable.TYPE:

            # We find the variable by its ID.
            pointedID = str(self.view.getWidg("IDEntry").get_text())

            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()
            vtype = AbstractRelationType.makeType(self.view.getWidg("relationTypeCombo").get_active_text(), sized, minChars, maxChars, delimiter)
            variable = ComputedRelationVariable(anid, name, mutable, learnable, vtype, pointedID, self.treeController.symbol)

        if variable is not None:
            # This part is for saving and transfering children when transforming a node variable into an other kind of node variable.
            if self.editOverCreate:
                father = self.variable.getFathers()[0]
                # We transform a node variable into a node variable.
                if (self.variable.getVariableType() == AggregateVariable.TYPE or self.variable.getVariableType() == AlternateVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    children = self.variable.getChildren()
                    self.variable = variable
                    for child in children:
                        self.variable.addChild(child)

                # We transform a repeat variable into a node variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.addChild(child)

                # We transform a repeat variable into a repeat variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == RepeatVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.addChild(child)

                # We do not manage/save children.
                else:
                    self.variable = variable
                self.variable.addFather(father)
                self.treeController.editVariable(self.variable)

            else:
                self.variable.addChild(variable)
                self.treeController.registerVariable(self.rootEntry, variable)
        dialog.destroy()
 def __init__(self, sized, minChars, maxChars, delimiter, factor, offset):
     """Constructor of WordSizeRelationType:
     """
     AbstractRelationType.__init__(self, sized, minChars, maxChars, delimiter, factor, offset)
     self.log = logging.getLogger('netzob.Common.MMSTD.Dictionary.RelationTypes.WordSizeRelationType.py')
示例#6
0
    def validateChanges(self, widget):
        """validateChanges:
                Validate the changes that a user has done on a variable.
                Called by a click on the apply button.

                @type widget: Gtk.widget
                @param widget: the widget which calls this function.
        """

        dialog = self.view.getWidg("dialog")
        if self.editOverCreate:
            anid = self.variable.getID()
        else:
            anid = str(uuid.uuid4())

        name = self.view.getWidg("nameEntry").get_text()
        mutable = self.view.getWidg("mutableCheck").get_active()
        learnable = self.view.getWidg("learnableCheck").get_active()
        varTypeIndex = self.view.getWidg("variableTypeCombo").get_active()

        variable = None
        # Aggregate variable
        if varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AggregateVariable.TYPE):
            variable = AggregateVariable(anid, name, mutable, learnable)

        # Alternate Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AlternateVariable.TYPE):
            variable = AlternateVariable(anid, name, mutable, learnable)

        # Repeat Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(RepeatVariable.TYPE):
            minIterations = int(self.view.getWidg("minSpin").get_text())
            maxIterations = int(self.view.getWidg("maxSpin").get_text())
            variable = RepeatVariable(anid, name, mutable, learnable, None, minIterations, maxIterations)

        # Data Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(DataVariable.TYPE):
            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()
            vtype = AbstractType.makeType(VariableTreeController.TYPE_INDEX_LIST[self.view.getWidg("typeCombo").get_active()], sized, minChars, maxChars, delimiter)
            originalValue = vtype.str2bin(self.view.getWidg("valueEntry").get_text())
            variable = DataVariable(anid, name, mutable, learnable, vtype, originalValue)

#===============================================================================
#        # Direct Relation Variable
#        elif strVarType == DirectRelationVariable.TYPE:
#
#            # We find the variable by its ID.
#            pointedID = str(self.view.getWidg("IDEntry").get_text())
#
#            variable = DirectRelationVariable(anid, name, mutable, learnable, pointedID, self.treeController.symbol)
#===============================================================================

        # Computed Relation Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(ComputedRelationVariable.TYPE):

            # We find the variable by its ID.
            pointedID = str(self.view.getWidg("IDEntry").get_text())

            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()

            factor = float(self.view.getWidg("factorEntry").get_text())
            offset = float(self.view.getWidg("offsetEntry").get_text())
            vtype = AbstractRelationType.makeType(VariableTreeController.RELATION_TYPE_INDEX_LIST[self.view.getWidg("relationTypeCombo").get_active()], sized, minChars, maxChars, delimiter, factor, offset)
            variable = ComputedRelationVariable(anid, name, mutable, learnable, vtype, pointedID, self.treeController.symbol)

        if variable is not None:
            # This part is for saving and transfering children when transforming a node variable into an other kind of node variable.
            if self.editOverCreate:
                father = None
                if len(self.variable.getFathers()) > 0:
                    father = self.variable.getFathers()[0]
                # We transform a node variable into a node variable.
                if (self.variable.getVariableType() == AggregateVariable.TYPE or self.variable.getVariableType() == AlternateVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    children = self.variable.getChildren()
                    self.variable = variable
                    for child in children:
                        self.variable.addChild(child)

                # We transform a repeat variable into a node variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.addChild(child)

                # We transform a repeat variable into a repeat variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == RepeatVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.setChild(child)

                # We do not manage/save children.
                else:
                    self.variable = variable
                if father is not None:
                    self.variable.addFather(father)
                self.treeController.editVariable(self.variable)

            else:
                if self.variable.getVariableType() == RepeatVariable.TYPE:
                    self.variable.setChild(variable)
                else:
                    self.variable.addChild(variable)
                self.treeController.registerVariable(self.rootEntry, variable)
        dialog.destroy()
 def setRelationType(self, relationType):
     self.dataType = AbstractRelationType.makeType(relationType)