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))
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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))
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()))
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()))
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()))
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()))
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))
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()))
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()))
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)
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)
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()))
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()))
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()))
def toString(self): """toString: """ return "[Repeat] {0}, iterations: ({1}, {2})".format( AbstractVariable.toString(self), str(self.minIterations), str(self.maxIterations))
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()))
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()))
def toString(self): """toString: """ return "[Repeat] {0}, iterations: ({1}, {2})".format(AbstractVariable.toString(self), str(self.minIterations), str(self.maxIterations))
def toString(self): """toString: """ return "[Direct Relation] {0}, pointed ID: {1}".format(AbstractVariable.toString(self), str(self.getPointedID()))
def toString(self): """toString: """ return "[Computed Relation] {0}, pointed ID: {1}, type: {2}.".format(AbstractVariable.toString(self), str(self.getPointedID()), self.relationType.toString())
def toString(self): """toString: """ return "[Computed Relation] {0}, pointed ID: {1}, type: {2}.".format( AbstractVariable.toString(self), str(self.getPointedID()), self.relationType.toString())