def __init__(self, _id, name, mutable, learnable, pointedID, symbol):
        """Constructor of AbstractRelationVariable:

                @type pointedID: string
                @param pointedID: the pointed variable's ID.
        """
        AbstractVariable.__init__(self, _id, name, mutable, learnable, False)
        self.log = logging.getLogger('netzob.Common.MMSTD.Dictionary.Variable.AbstractRelationVariable.py')
        self.pointedID = pointedID
        self.symbol = symbol
        self.pointedVariable = None
    def __init__(self, _id, name, mutable, learnable, pointedID, symbol):
        """Constructor of AbstractRelationVariable:

                @type pointedID: string
                @param pointedID: the pointed variable's ID.
        """
        AbstractVariable.__init__(self, _id, name, mutable, learnable, False)
        self.log = logging.getLogger(
            'netzob.Common.MMSTD.Dictionary.Variable.AbstractRelationVariable.py'
        )
        self.pointedID = pointedID
        self.symbol = symbol
        self.pointedVariable = None
Exemplo n.º 3
0
    def __init__(self, _id, name, mutable, random, children=None):
        """Constructor of AbstractNodeVariable:

                @type children: netzob.Common.MMSTD.Dictionary.Variable.AbstractVariable.AbstractVariable List
                @param children: the list of this variable's children.
        """
        AbstractVariable.__init__(self, _id, name, mutable, random, True)
        # create logger with the given configuration
        self.log = logging.getLogger('netzob.Common.MMSTD.Dictionary.Variable.AbstractNodeVariable.py')
        self.children = []
        if children is not None:
            self.children.extend(children)
        self.learning = False  # (read access with mutable flag) Tells if the variable reads normally or through an attempt of learning.
Exemplo n.º 4
0
    def __init__(self, _id, name, mutable, learnable, relationType, pointedID, symbol):
        """Constructor of ComputedRelationVariable:
                Mutable and learnable are useless.

                @type relationType: string
                @param relationType: the type of computation we will use.
        """
        AbstractVariable.__init__(self, _id, name, mutable, learnable, False)
        self.log = logging.getLogger('netzob.Common.MMSTD.Dictionary.Variable.ComputedRelationVariable.py')
        self.relationType = relationType
        self.currentValue = None
        self.pointedID = pointedID
        self.symbol = symbol
        self.pointedVariable = None
Exemplo n.º 5
0
    def __init__(self, _id, name, mutable, learnable, children=None):
        """Constructor of AbstractNodeVariable:

                @type children: netzob.Common.MMSTD.Dictionary.Variable.AbstractVariable.AbstractVariable List
                @param children: the list of this variable's children.
        """
        AbstractVariable.__init__(self, _id, name, mutable, learnable, True)
        # create logger with the given configuration
        self.log = logging.getLogger(
            'netzob.Common.MMSTD.Dictionary.Variable.AbstractNodeVariable.py')
        self.children = []
        if children is not None:
            for child in children:
                self.addChild(child)
        self.learning = False  # (read access with mutable flag) Tells if the variable reads normally or through an attempt of learning.
Exemplo n.º 6
0
    def write(self, writingToken):
        """write:
                The relation variable returns a computed or a generated value.
        """
        self.log.debug("[ {0} (relation): write access:".format(AbstractVariable.toString(self)))
        self.directPointer = self.findDirectPointer()

        if self.isDefined(writingToken):
            if not self.directPointer:
                # We will write the real value at notification time. (An awaiting value is written though.)
                self.bindValue(writingToken)
                self.guessValue()
            else:
                # We directly retrieve and write the actual value (which would be deprecated and replaced if the variable is directPointer).
                self.retrieveValue(writingToken)
            self.writeValue(writingToken)
        else:
            self.log.debug("Write abort: the variable is not defined.")
            writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), writingToken.toString()))
Exemplo n.º 7
0
    def read(self, readingToken):
        """read:
                The relation variable tries to compare/learn the read value.
        """
        self.log.debug("[ {0} (relation): read access:".format(AbstractVariable.toString(self)))
        self.directPointer = self.findDirectPointer()

        if self.isDefined(readingToken):
            if self.directPointer:
                # We directly retrieve and compare the value.
                self.retrieveValue(readingToken)
                self.compare(readingToken)
            else:
                # We make a small format comparison.
                self.compareFormat(readingToken)
                # We will verify the value at notification time.
                self.bindValue(readingToken)

        else:
            self.log.debug("Read abort: the variable is not defined.")
            readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken, self.currentValue)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), readingToken.toString()))
Exemplo n.º 8
0
    def notifiedWrite(self, writingToken):
        """notify:
                A write access called by a notification of the pointed variable (when it has finished its own treatment).
                It updates the values this variable has written in the writingToken value.

                @type writingToken: netzob.Common.MMSTD.Dictionary.VariableProcessingToken.VariableWritingToken.VariableWritingToken
                @param writingToken: a token which contains all critical information on this access.
        """
        self.log.debug("[ {0} (relation): notifiedWrite access:".format(AbstractVariable.toString(self)))

        if self.isDefined(writingToken):

            # Compute the value
            self.retrieveValue(writingToken)

            # replace the new value in the writing token
            writingToken.setValueForVariable(self, self.currentValue)

        else:
            self.log.debug("Write abort: the variable is neither defined, nor random.")
            writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), writingToken.toString()))
Exemplo n.º 9
0
    def write(self, writingToken):
        """write:
                Each child tries sequentially to write its value.
                If one of them fails, the whole operation is cancelled.
        """
        self.log.debug("[ {0} (Aggregate): write access:".format(AbstractVariable.toString(self)))
        self.resetTokenChoppedIndexes()  # New write access => new final value and new reference to it.
        if self.getChildren() is not None:
            if self.isMutable():
                # mutable.
                self.shuffleChildren()
                self.writeChildren(writingToken)

            else:
                # not mutable.
                self.writeChildren(writingToken)

        else:
            # no child.
            self.log.debug("Write abort: the variable has no child.")
            writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), writingToken.toString()))
Exemplo n.º 10
0
    def write(self, writingToken):
        """write:
                Each child tries sequentially to write its value.
                If one of them fails, the whole operation is cancelled.
        """
        self.log.debug("[ {0} (Alternate): write access:".format(AbstractVariable.toString(self)))
        self.resetTokenChoppedIndexes()  # New write access => new final value and new reference to it.
        if self.getChildren() is not None:
            if self.isMutable():
                # mutable.
                self.shuffleChildren()
                self.writeChildren(writingToken)

            else:
                # not mutable.
                self.writeChildren(writingToken)

        else:
            # no child.
            self.log.debug("Write abort: the variable has no child.")
            writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), writingToken.toString()))
Exemplo n.º 11
0
    def read(self, readingToken):
        """read:
                The relation variable tries to compare/learn the read value.
        """
        self.log.debug("[ {0} (relation): read access:".format(
            AbstractVariable.toString(self)))
        self.directPointer = self.findDirectPointer()

        if self.isDefined(readingToken):
            if self.directPointer:
                # We directly retrieve and compare the value.
                self.retrieveValue(readingToken)
                self.compare(readingToken)
            else:
                # We make a small format comparison.
                self.compareFormat(readingToken)
                # We will verify the value at notification time.
                self.bindValue(readingToken)

        else:
            self.log.debug("Read abort: the variable is not defined.")
            readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken,
                                        self.currentValue)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(),
                                                     readingToken.toString()))
Exemplo n.º 12
0
 def toString(self):
     """toString:
     """
     lgth = 0
     if self.children is not None:
         lgth = len(self.children)
     return _("[Alternate] {0} ({1})").format(AbstractVariable.toString(self), str(lgth))
Exemplo n.º 13
0
    def read(self, readingToken):
        """read:
                Each child tries sequentially to read a part of the read value.
                If one of them fails, the whole operation is cancelled.
        """
        self.log.debug("[ {0} (Alternate): read access:".format(AbstractVariable.toString(self)))
        if self.getChildren() is not None:
            if self.isMutable():
                # mutable.
                self.shuffleChildren()
                self.readChildren(readingToken)

            else:
                # not mutable.
                self.readChildren(readingToken)

        else:
            # no child.
            self.log.debug("Write abort: the variable has no child.")
            readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken, self.currentValue)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), readingToken.toString()))
Exemplo n.º 14
0
    def writeChildren(self, writingToken):
        """write:
                Each child tries to write its value..
                If it fails, it restore it value and the next child try.
                It stops if one child successes.
        """
        self.log.debug("[ {0} (Alternate): writeChildren:".format(AbstractVariable.toString(self)))

        savedValue = writingToken.getValue()
        savedIndex = writingToken.getIndex()
        for child in self.getChildren():
            # Memorized values for the child and its successor.
            dictOfValues = dict()
            dictOfValue = child.getDictOfValues(writingToken)
            for key, val in dictOfValue.iteritems():
                dictOfValues[key] = val

            child.write(writingToken)
            if writingToken.isOk() and writingToken.getValue() is not None:
                break
            else:
                writingToken.setValue(savedValue)

                # We restore values for the child and its successor.
                child.restore(writingToken)
                vocabulary = writingToken.getVocabulary()
                for key, val in dictOfValues.iteritems():
                    vocabulary.getVariableByID(key).setCurrentValue(val)

        if writingToken.isOk():
            # The value of the variable is simply the value we made.
            self.currentValue = writingToken.getValue()[savedIndex:writingToken.getIndex()]

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), writingToken.toString()))
Exemplo n.º 15
0
 def toString(self):
     """toString:
     """
     lgth = 0
     if self.getChildren() is not None:
         lgth = len(self.getChildren())
     return _("[Aggregate] {0} ({1})").format(AbstractVariable.toString(self), str(lgth))
Exemplo n.º 16
0
    def read(self, readingToken):
        """read:
                Each child tries sequentially to read a part of the read value.
                If one of them fails, the whole operation is cancelled.
        """
        self.log.debug("[ {0} (Aggregate): read access:".format(AbstractVariable.toString(self)))
        if self.getChildren() is not None:
            if self.isMutable():
                # mutable.
                self.sortChildrenToRead(readingToken)
                self.readChildren(readingToken)
            else:
                # not mutable.
                self.readChildren(readingToken)

        else:
            # no child.
            self.log.debug("Write abort: the variable has no child.")
            readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken, self.currentValue)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), readingToken.toString()))
Exemplo n.º 17
0
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Loads a repeat variable from an XML definition.
                We do not trust the user and check every field (even mandatory).
        """
        logging.debug("[ RepeatVariable: loadFromXML:")
        if version == "0.1":
            xmlID = xmlRoot.get("id")
            xmlName = xmlRoot.get("name")
            xmlMutable = xmlRoot.get("mutable") == "True"
            xmlLearnable = xmlRoot.get("learnable") == "True"

            xmlChild = xmlRoot.find("{" + namespace + "}variable")
            child = AbstractVariable.loadFromXML(xmlChild, namespace, version, symbol)

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

            # maxIterations
            xmlMaxIterations = xmlRoot.find("{" + namespace + "}maxIterations")
            if xmlMaxIterations is not None:
                maxIterations = int(xmlMaxIterations.text)
            else:
                maxIterations = RepeatVariable.MAX_ITERATIONS

            result = RepeatVariable(xmlID, xmlName, xmlMutable, xmlLearnable, child, minIterations, maxIterations)
            logging.debug("RepeatVariable: loadFromXML successes: {0} ]".format(result.toString()))
            return result
        logging.debug("RepeatVariable: loadFromXML fails")
        return None
Exemplo n.º 18
0
    def notifiedRead(self, readingToken, pointedValue):
        """notifiedRead:
                A read access called by a notification of the pointed variable (when it has finished its own treatment).
                It checks that the new value complies with the reading token value at this very position.

                @type readingToken: netzob.Common.MMSTD.Dictionary.VariableProcessingToken.VariableReadingToken.VariableReadingToken
                @param readingToken: a token which contains all critical information on this access.
        """
        self.log.debug("[ {0} (relation): read access:".format(AbstractVariable.toString(self)))

        if self.isDefined(readingToken):
            for linkedValue in readingToken.getLinkedValue():
                if linkedValue[0] == self.getID():
                    # We compare the pointed value to the value the current variable wrote in memory.
                    if linkedValue[1] != self.computeValue(pointedValue):
                        readingToken.setOk(False)
                        break

        else:
            self.log.debug("Read abort: the variable is neither defined, nor mutable.")
            readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken, pointedValue)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), readingToken.toString()))
Exemplo n.º 19
0
    def write(self, writingToken):
        """write:
                The relation variable returns a computed or a generated value.
        """
        self.log.debug("[ {0} (relation): write access:".format(
            AbstractVariable.toString(self)))
        self.resetTokenChoppedIndexes(
        )  # New write access => new final value and new reference to it.
        self.directPointer = self.findDirectPointer()

        if self.isDefined(writingToken):
            if not self.directPointer:
                # We will write the real value at notification time. (An awaiting value is written though.)
                self.bindValue(writingToken)
                self.guessValue()
            else:
                # We directly retrieve and write the actual value (which would be deprecated and replaced if the variable is directPointer).
                self.retrieveValue(writingToken)
            self.writeValue(writingToken)
        else:
            self.log.debug("Write abort: the variable is not defined.")
            writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(),
                                                     writingToken.toString()))
Exemplo n.º 20
0
    def notifiedRead(self, readingToken, pointedValue):
        """notifiedRead:
                A read access called by a notification of the pointed variable (when it has finished its own treatment).
                It checks that the new value complies with the reading token value at this very position.

                @type readingToken: netzob.Common.MMSTD.Dictionary.VariableProcessingToken.VariableReadingToken.VariableReadingToken
                @param readingToken: a token which contains all critical information on this access.
        """
        self.log.debug("[ {0} (relation): read access:".format(
            AbstractVariable.toString(self)))

        if self.isDefined(readingToken):
            for linkedValue in readingToken.getLinkedValue():
                if linkedValue[0] == self.getID():
                    # We compare the pointed value to the value the current variable wrote in memory.
                    if linkedValue[1] != self.computeValue(pointedValue):
                        readingToken.setOk(False)
                        break

        else:
            self.log.debug(
                "Read abort: the variable is neither defined, nor mutable.")
            readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken, pointedValue)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(),
                                                     readingToken.toString()))
Exemplo n.º 21
0
    def notifiedWrite(self, writingToken):
        """notify:
                A write access called by a notification of the pointed variable (when it has finished its own treatment).
                It updates the values this variable has written in the writingToken value.

                @type writingToken: netzob.Common.MMSTD.Dictionary.VariableProcessingToken.VariableWritingToken.VariableWritingToken
                @param writingToken: a token which contains all critical information on this access.
        """
        self.log.debug("[ {0} (relation): notifiedWrite access:".format(
            AbstractVariable.toString(self)))

        if self.isDefined(writingToken):

            # Compute the value
            self.retrieveValue(writingToken)

            # replace the new value in the writing token
            writingToken.setValueForVariable(self, self.currentValue)

        else:
            self.log.debug(
                "Write abort: the variable is neither defined, nor random.")
            writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(),
                                                     writingToken.toString()))
Exemplo n.º 22
0
 def toString(self):
     """toString:
     """
     lgth = 0
     if self.getChildren() is not None:
         lgth = len(self.getChildren())
     return _("[Aggregate] {0} ({1})").format(
         AbstractVariable.toString(self), str(lgth))
Exemplo n.º 23
0
    def __init__(self, _id, name, mutable, learnable, relationType, pointedID,
                 symbol):
        """Constructor of ComputedRelationVariable:
                Mutable and learnable are useless.

                @type relationType: string
                @param relationType: the type of computation we will use.
        """
        AbstractVariable.__init__(self, _id, name, mutable, learnable, False)
        self.log = logging.getLogger(
            'netzob.Common.MMSTD.Dictionary.Variable.ComputedRelationVariable.py'
        )
        self.relationType = relationType
        self.currentValue = None
        self.pointedID = pointedID
        self.symbol = symbol
        self.pointedVariable = None
Exemplo n.º 24
0
    def read(self, readingToken):
        """read:
                The leaf element tries to compare/learn the read value.
        """
        self.log.debug("[ {0} (leaf): read access:".format(AbstractVariable.toString(self)))
        if self.isMutable():
            if self.isLearnable():
                if self.isDefined(readingToken):
                    # mutable, learnable and defined.
                    self.forget(readingToken)
                    self.compareFormat(readingToken)
                    self.learn(readingToken)
                    self.memorize(readingToken)

                else:
                    # mutable, learnable and not defined.
                    self.compareFormat(readingToken)
                    self.learn(readingToken)
                    self.memorize(readingToken)

            else:
                if self.isDefined(readingToken):
                    # mutable, not learnable and defined.
                    self.compareFormat(readingToken)

                else:
                    # mutable, learnable and not defined.
                    self.compareFormat(readingToken)

        else:
            if self.isLearnable():
                if self.isDefined(readingToken):
                    # not mutable, learnable and defined.
                    self.compare(readingToken)

                else:
                    # not mutable, learnable and not defined.
                    self.compareFormat(readingToken)
                    self.learn(readingToken)
                    self.memorize(readingToken)

            else:
                if self.isDefined(readingToken):
                    # not mutable, not learnable and defined.
                    self.compare(readingToken)

                else:
                    # not mutable, not learnable and not defined.
                    self.log.debug("Read abort: the variable is neither defined, nor mutable.")
                    readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken, self.getValue(readingToken))

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), readingToken.toString()))
Exemplo n.º 25
0
    def write(self, writingToken):
        """write:
                The leaf element returns its value or a generated one.
        """
        self.log.debug("[ {0} (leaf): write access:".format(AbstractVariable.toString(self)))
        self.resetTokenChoppedIndexes()  # New write access => new final value and new reference to it.
        if self.isMutable():
            if self.isLearnable():
                if self.isDefined(writingToken):
                    # mutable, learnable and defined.
                    self.mutate(writingToken)
                    self.writeValue(writingToken)

                else:
                    # mutable, learnable and not defined.
                    self.generate(writingToken)
                    self.memorize(writingToken)
                    self.writeValue(writingToken)
            else:
                if self.isDefined(writingToken):
                    # mutable, not learnable, defined.
                    self.generate(writingToken)
                    self.writeValue(writingToken)

                else:
                    # mutable, not learnable, not defined.
                    self.generate(writingToken)
                    self.writeValue(writingToken)

        else:
            if self.isLearnable():
                if self.isDefined(writingToken):
                    # not mutable, learnable and defined.
                    self.writeValue(writingToken)

                else:
                    # not mutable, learnable and not defined.
                    self.generate(writingToken)
                    self.memorize(writingToken)
                    self.writeValue(writingToken)

            else:
                if self.isDefined(writingToken):
                    # not mutable, not learnable and defined.
                    self.writeValue(writingToken)

                else:
                    # not mutable, not learnable and not defined.
                    self.log.debug("Write abort: the variable is neither defined, nor mutable.")
                    writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), writingToken.toString()))
Exemplo n.º 26
0
    def toString(self):
        """toString:
                For debugging purpose.
        """
        # We simply avoid to print unreadable binary.
        if self.type.getType() == BinaryType.TYPE:
            readableValue = TypeConvertor.bin2strhex(self.originalValue)
        else:
            readableValue = self.bin2str(self.originalValue)

        return "[Data] {0}, type: {1}, original value: {2}".format(AbstractVariable.toString(self), self.type.toString(), readableValue)
Exemplo n.º 27
0
    def toString(self):
        """toString:
                For debugging purpose.
        """
        # We simply avoid to print unreadable binary.
        if self.type.getType() == BinaryType.TYPE:
            readableValue = TypeConvertor.bin2strhex(self.originalValue)
        else:
            readableValue = self.bin2str(self.originalValue)

        return "[Data] {0}, type: {1}, original value: {2}".format(
            AbstractVariable.toString(self), self.type.toString(),
            readableValue)
Exemplo n.º 28
0
    def toXML(self, root, namespace):
        """toXML:
            Creates the xml tree associated to this variable.
            Adds every child's own xml definition as xml child to this tree.
        """
        self.log.debug("[ {0} (Aggregate): toXML:".format(AbstractVariable.toString(self)))
        xmlVariable = etree.SubElement(root, "{" + namespace + "}variable")
        xmlVariable.set("id", str(self.getID()))
        xmlVariable.set("name", str(self.getName()))
        xmlVariable.set("{http://www.w3.org/2001/XMLSchema-instance}type", "netzob:AggregateVariable")
        xmlVariable.set("mutable", str(self.isMutable()))
        xmlVariable.set("learnable", str(self.isLearnable()))

        # Definition of children variables
        for child in self.getChildren():
            child.toXML(xmlVariable, namespace)
        self.log.debug("Variable {0}. ]".format(self.getName()))
Exemplo n.º 29
0
    def toXML(self, root, namespace):
        """toXML:
            Creates the xml tree associated to this variable.
            Adds every child's own xml definition as xml child to this tree.
        """
        self.log.debug("[ {0} (Aggregate): toXML:".format(
            AbstractVariable.toString(self)))
        xmlVariable = etree.SubElement(root, "{" + namespace + "}variable")
        xmlVariable.set("id", str(self.getID()))
        xmlVariable.set("name", str(self.getName()))
        xmlVariable.set("{http://www.w3.org/2001/XMLSchema-instance}type",
                        "netzob:AggregateVariable")
        xmlVariable.set("mutable", str(self.isMutable()))
        xmlVariable.set("learnable", str(self.isLearnable()))

        # Definition of children variables
        for child in self.getChildren():
            child.toXML(xmlVariable, namespace)
        self.log.debug("Variable {0}. ]".format(self.getName()))
Exemplo n.º 30
0
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Loads an aggregate variable from an XML definition.
        """
        logging.debug("[ AggregateVariable: loadFromXML:")
        if version == "0.1":
            xmlID = xmlRoot.get("id")
            xmlName = xmlRoot.get("name")
            xmlMutable = xmlRoot.get("mutable") == "True"
            xmlLearnable = xmlRoot.get("learnable") == "True"

            result = AggregateVariable(xmlID, xmlName, xmlMutable, xmlLearnable, [])
            for xmlChildren in xmlRoot.findall("{" + namespace + "}variable"):
                child = AbstractVariable.loadFromXML(xmlChildren, namespace, version, symbol)
                result.addChild(child)
            logging.debug("AggregateVariable: loadFromXML successes: {0} ]".format(result.toString()))
            return result
        logging.debug("AggregateVariable: loadFromXML fails")
        return None
Exemplo n.º 31
0
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Loads an alternate variable from an XML definition.
        """
        logging.debug("[ AlternateVariable: loadFromXML:")
        if version == "0.1":
            xmlID = xmlRoot.get("id")
            xmlName = xmlRoot.get("name")
            xmlMutable = xmlRoot.get("mutable") == "True"
            xmlLearnable = xmlRoot.get("learnable") == "True"

            result = AlternateVariable(xmlID, xmlName, xmlMutable, xmlLearnable, [])
            for xmlChildren in xmlRoot.findall("{" + namespace + "}variable"):
                child = AbstractVariable.loadFromXML(xmlChildren, namespace, version, symbol)
                result.addChild(child)

            logging.debug("AlternateVariable: loadFromXML successes: {0} ]".format(result.toString()))
            return result
        logging.debug("AlternateVariable: loadFromXML fails")
        return None
Exemplo n.º 32
0
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Loads a repeat variable from an XML definition.
                We do not trust the user and check every field (even mandatory).
        """
        logging.debug("[ RepeatVariable: loadFromXML:")
        if version == "0.1":
            xmlID = xmlRoot.get("id")
            xmlName = xmlRoot.get("name")
            xmlMutable = xmlRoot.get("mutable") == "True"
            xmlLearnable = xmlRoot.get("learnable") == "True"

            xmlChild = xmlRoot.find("{" + namespace + "}variable")
            child = AbstractVariable.loadFromXML(xmlChild, namespace, version,
                                                 symbol)

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

            # maxIterations
            xmlMaxIterations = xmlRoot.find("{" + namespace + "}maxIterations")
            if xmlMaxIterations is not None:
                maxIterations = int(xmlMaxIterations.text)
            else:
                maxIterations = RepeatVariable.MAX_ITERATIONS

            result = RepeatVariable(xmlID, xmlName, xmlMutable, xmlLearnable,
                                    child, minIterations, maxIterations)
            logging.debug(
                "RepeatVariable: loadFromXML successes: {0} ]".format(
                    result.toString()))
            return result
        logging.debug("RepeatVariable: loadFromXML fails")
        return None
Exemplo n.º 33
0
    def readChildren(self, readingToken):
        """read:
                Each child tries to read the value.
                If it fails, it restore it value and the next child try.
                It stops if one child successes.
        """
        self.log.debug("[ {0} (Alternate): readChildren:".format(AbstractVariable.toString(self)))
        savedIndex = readingToken.getIndex()
        for child in self.getChildren():
            # Memorized values for the child and its successors.
            dictOfValues = dict()
            dictOfValue = child.getDictOfValues(readingToken)
            for key, val in dictOfValue.iteritems():
                dictOfValues[key] = val

            child.read(readingToken)
            if readingToken.isOk():
                break
            else:
                readingToken.setIndex(savedIndex)

                # We restore values for the child and its successors.
                child.restore(readingToken)
                vocabulary = readingToken.getVocabulary()
                for key, val in dictOfValues.iteritems():
                    vocabulary.getVariableByID(key).setCurrentValue(val)

        if readingToken.isOk():
            # The value of the variable is simply the value we 'ate'.
            self.currentValue = readingToken.getValue()[savedIndex:readingToken.getIndex()]

        if self.isLearnable() and not readingToken.isOk() and not self.isLearning():
            # If we dont not found a proper child but the node can learn, we learn the value.
            self.learn(child, readingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(), readingToken.toString()))
Exemplo n.º 34
0
 def toString(self):
     """toString:
     """
     return "[Repeat] {0}, iterations: ({1}, {2})".format(
         AbstractVariable.toString(self), str(self.minIterations),
         str(self.maxIterations))
Exemplo n.º 35
0
 def __init__(self, _id, name, mutable, learnable):
     """Constructor of AbstractLeafVariable:
     """
     AbstractVariable.__init__(self, _id, name, mutable, learnable, False)
     self.log = logging.getLogger('netzob.Common.MMSTD.Dictionary.Variable.AbstractLeafVariable.py')
Exemplo n.º 36
0
    def write(self, writingToken):
        """write:
                The leaf element returns its value or a generated one.
        """
        self.log.debug("[ {0} (leaf): write access:".format(
            AbstractVariable.toString(self)))
        self.resetTokenChoppedIndexes(
        )  # New write access => new final value and new reference to it.
        if self.isMutable():
            if self.isLearnable():
                if self.isDefined(writingToken):
                    # mutable, learnable and defined.
                    self.mutate(writingToken)
                    self.writeValue(writingToken)

                else:
                    # mutable, learnable and not defined.
                    self.generate(writingToken)
                    self.memorize(writingToken)
                    self.writeValue(writingToken)
            else:
                if self.isDefined(writingToken):
                    # mutable, not learnable, defined.
                    self.generate(writingToken)
                    self.writeValue(writingToken)

                else:
                    # mutable, not learnable, not defined.
                    self.generate(writingToken)
                    self.writeValue(writingToken)

        else:
            if self.isLearnable():
                if self.isDefined(writingToken):
                    # not mutable, learnable and defined.
                    self.writeValue(writingToken)

                else:
                    # not mutable, learnable and not defined.
                    self.generate(writingToken)
                    self.memorize(writingToken)
                    self.writeValue(writingToken)

            else:
                if self.isDefined(writingToken):
                    # not mutable, not learnable and defined.
                    self.writeValue(writingToken)

                else:
                    # not mutable, not learnable and not defined.
                    self.log.debug(
                        "Write abort: the variable is neither defined, nor mutable."
                    )
                    writingToken.setOk(False)

        # Variable notification
        if writingToken.isOk():
            self.notifyBoundedVariables("write", writingToken)

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(),
                                                     writingToken.toString()))
Exemplo n.º 37
0
 def __init__(self, _id, name, mutable, learnable):
     """Constructor of AbstractLeafVariable:
     """
     AbstractVariable.__init__(self, _id, name, mutable, learnable, False)
     self.log = logging.getLogger(
         'netzob.Common.MMSTD.Dictionary.Variable.AbstractLeafVariable.py')
Exemplo n.º 38
0
 def toString(self):
     """toString:
     """
     return "[Computed Relation] {0}, pointed ID: {1}, type: {2}.".format(AbstractVariable.toString(self), str(self.getPointedID()), self.relationType.toString())
Exemplo n.º 39
0
 def toString(self):
     """toString:
     """
     return "[Repeat] {0}, iterations: ({1}, {2})".format(AbstractVariable.toString(self), str(self.minIterations), str(self.maxIterations))
Exemplo n.º 40
0
 def toString(self):
     """toString:
     """
     return "[Direct Relation] {0}, pointed ID: {1}".format(AbstractVariable.toString(self), str(self.getPointedID()))
Exemplo n.º 41
0
    def read(self, readingToken):
        """read:
                The leaf element tries to compare/learn the read value.
        """
        self.log.debug("[ {0} (leaf): read access:".format(
            AbstractVariable.toString(self)))
        if self.isMutable():
            if self.isLearnable():
                if self.isDefined(readingToken):
                    # mutable, learnable and defined.
                    self.forget(readingToken)
                    self.compareFormat(readingToken)
                    self.learn(readingToken)
                    self.memorize(readingToken)

                else:
                    # mutable, learnable and not defined.
                    self.compareFormat(readingToken)
                    self.learn(readingToken)
                    self.memorize(readingToken)

            else:
                if self.isDefined(readingToken):
                    # mutable, not learnable and defined.
                    self.compareFormat(readingToken)

                else:
                    # mutable, learnable and not defined.
                    self.compareFormat(readingToken)

        else:
            if self.isLearnable():
                if self.isDefined(readingToken):
                    # not mutable, learnable and defined.
                    self.compare(readingToken)

                else:
                    # not mutable, learnable and not defined.
                    self.compareFormat(readingToken)
                    self.learn(readingToken)
                    self.memorize(readingToken)

            else:
                if self.isDefined(readingToken):
                    # not mutable, not learnable and defined.
                    self.compare(readingToken)

                else:
                    # not mutable, not learnable and not defined.
                    self.log.debug(
                        "Read abort: the variable is neither defined, nor mutable."
                    )
                    readingToken.setOk(False)

        # Variable notification
        if readingToken.isOk():
            self.notifyBoundedVariables("read", readingToken,
                                        self.getValue(readingToken))

        self.log.debug("Variable {0}: {1}. ]".format(self.getName(),
                                                     readingToken.toString()))
Exemplo n.º 42
0
 def toString(self):
     """toString:
     """
     return "[Computed Relation] {0}, pointed ID: {1}, type: {2}.".format(
         AbstractVariable.toString(self), str(self.getPointedID()),
         self.relationType.toString())