本文整理汇总了Python中netzob.Common.Type.TypeConvertor.TypeConvertor.netzobRawToBinary方法的典型用法代码示例。如果您正苦于以下问题:Python TypeConvertor.netzobRawToBinary方法的具体用法?Python TypeConvertor.netzobRawToBinary怎么用?Python TypeConvertor.netzobRawToBinary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netzob.Common.Type.TypeConvertor.TypeConvertor
的用法示例。
在下文中一共展示了TypeConvertor.netzobRawToBinary方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: apply
# 需要导入模块: from netzob.Common.Type.TypeConvertor import TypeConvertor [as 别名]
# 或者: from netzob.Common.Type.TypeConvertor.TypeConvertor import netzobRawToBinary [as 别名]
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)