def apply(self, message): if self.unitsize != UnitSize.NONE: # First we apply the unit size # Default modulo = 2 => 8BITS if self.unitsize == UnitSize.BIT: modulo = 1 else: modulo = UnitSize.getSizeInBits(self.unitsize) / 4 splittedData = [] tmpResult = "" for i in range(0, len(message)): if i > 0 and i % modulo == 0: splittedData.append(tmpResult) tmpResult = "" tmpResult = tmpResult + message[i] splittedData.append(tmpResult) else: splittedData = [message] encodedSplittedData = [] # Now we have the message splitted per unit size # we apply endianess on it # we consider the normal mode is big-endian for i in range(0, len(splittedData)): origNetzobRaw = splittedData[i] # SPECIAL CASE : ASCII we do not compute endianess neither signed/unsigned if not self.formatType == Format.STRING and UnitSize.getSizeInBits(self.unitsize) >= 8 and not self.formatType == Format.BINARY: tmpVal = UnitSize.getSizeInBits(self.unitsize) / 4 - len(origNetzobRaw) if self.endianness == Endianess.BIG: netzobRaw = (tmpVal * "0") + origNetzobRaw else: netzobRaw = origNetzobRaw + (tmpVal * "0") # Convert in Python raw pythonraw = TypeConvertor.netzobRawToPythonRaw(netzobRaw) # Create transformer # - ENDIANESS transformer = ">" if self.endianness == Endianess.LITTLE: transformer = "<" # - SIGNED/UNISGNED if self.sign == Sign.SIGNED: transformer = transformer + (UnitSize.getPackDefiniton(self.unitsize)).lower() else: transformer = transformer + (UnitSize.getPackDefiniton(self.unitsize)).upper() # Apply the transformation (unpackRaw,) = struct.unpack(transformer, pythonraw) localResult = "" fmt = "%" + str(UnitSize.getMaxDigitForTypeAndUnitSize(self.formatType, self.unitsize)) if self.formatType == Format.OCTAL: localResult = (fmt + "o") % unpackRaw elif self.formatType == Format.DECIMAL: localResult = (fmt + "d") % unpackRaw elif self.formatType == Format.HEX: localResult = (fmt + "s") % origNetzobRaw encodedSplittedData.append(localResult) elif self.formatType == Format.STRING: encodedSplittedData.append(TypeConvertor.netzobRawToString(origNetzobRaw)) elif self.formatType == Format.BINARY: encodedSplittedData.append(TypeConvertor.netzobRawToBinary(origNetzobRaw)) elif self.formatType == Format.IPv4: encodedSplittedData.append(TypeConvertor.netzobRawToIPv4(origNetzobRaw)) elif UnitSize.getSizeInBits(self.unitsize) < UnitSize.getSizeInBits(UnitSize.BITS8): encodedSplittedData.append(TypeConvertor.encodeNetzobRawToGivenType(origNetzobRaw, self.formatType)) # Before sending back (;D) we join everything return " ".join(encodedSplittedData)
def apply(self, message): if self.unitsize != UnitSize.NONE: # First we apply the unit size # Default modulo = 2 => 8BITS if self.unitsize == UnitSize.BIT: modulo = 1 else: modulo = UnitSize.getSizeInBits(self.unitsize) / 4 splittedData = [] tmpResult = "" for i in range(0, len(message)): if i > 0 and i % modulo == 0: splittedData.append(tmpResult) tmpResult = "" tmpResult = tmpResult + message[i] splittedData.append(tmpResult) else: splittedData = [message] encodedSplittedData = [] # Now we have the message splitted per unit size # we apply endianess on it # we consider the normal mode is big-endian for i in range(0, len(splittedData)): origNetzobRaw = splittedData[i] # SPECIAL CASE : ASCII we do not compute endianess neither signed/unsigned if not self.formatType == Format.STRING and UnitSize.getSizeInBits( self.unitsize ) >= 8 and not self.formatType == Format.BINARY: tmpVal = UnitSize.getSizeInBits( self.unitsize) / 4 - len(origNetzobRaw) if self.endianness == Endianess.BIG: netzobRaw = (tmpVal * "0") + origNetzobRaw else: netzobRaw = origNetzobRaw + (tmpVal * "0") # Convert in Python raw pythonraw = TypeConvertor.netzobRawToPythonRaw(netzobRaw) # Create transformer # - ENDIANESS transformer = ">" if self.endianness == Endianess.LITTLE: transformer = "<" # - SIGNED/UNISGNED if self.sign == Sign.SIGNED: transformer = transformer + (UnitSize.getPackDefiniton( self.unitsize)).lower() else: transformer = transformer + (UnitSize.getPackDefiniton( self.unitsize)).upper() # Apply the transformation (unpackRaw, ) = struct.unpack(transformer, pythonraw) localResult = "" fmt = "%" + str( UnitSize.getMaxDigitForTypeAndUnitSize( self.formatType, self.unitsize)) if self.formatType == Format.OCTAL: localResult = (fmt + "o") % unpackRaw elif self.formatType == Format.DECIMAL: localResult = (fmt + "d") % unpackRaw elif self.formatType == Format.HEX: localResult = (fmt + "s") % origNetzobRaw encodedSplittedData.append(localResult) elif self.formatType == Format.STRING: encodedSplittedData.append( TypeConvertor.netzobRawToString(origNetzobRaw)) elif self.formatType == Format.BINARY: encodedSplittedData.append( TypeConvertor.netzobRawToBinary(origNetzobRaw)) elif self.formatType == Format.IPv4: encodedSplittedData.append( TypeConvertor.netzobRawToIPv4(origNetzobRaw)) elif UnitSize.getSizeInBits( self.unitsize) < UnitSize.getSizeInBits(UnitSize.BITS8): encodedSplittedData.append( TypeConvertor.encodeNetzobRawToGivenType( origNetzobRaw, self.formatType)) # Before sending back (;D) we join everything return " ".join(encodedSplittedData)