def normalize(data): """Given the specified data, this static methods normalize its representation using Netzob types. :parameter data: the data to normalize :type data: :class:`object` :return: an abstractType which value is data :rtype: :class:`netzob.Common.Models.Types.AbstractType.AbstractType` >>> from netzob.all import * >>> normalizedData = AbstractType.normalize("netzob") >>> print normalizedData.__class__ <class 'netzob.Common.Models.Types.ASCII.ASCII'> >>> print normalizedData.value bitarray('011011100110010101110100011110100110111101100010') """ if data is None: raise TypeError("Cannot normalize None data") if isinstance(data, AbstractType): return data if isinstance(data, int): from netzob.Common.Models.Types.Integer import Integer return Integer(value=data) if isinstance(data, str): try: from netzob.Common.Models.Types.ASCII import ASCII normalizedData = ASCII(value=data) except: from netzob.Common.Models.Types.Raw import Raw normalizedData = Raw(value=data) return normalizedData raise TypeError("Not a valid data ({0}), impossible to normalize it.", type(data))
def _generateDataValues(self, cellsData): result = [] for data in cellsData: if len(data) > 0: data = data[:8] # We take at most 8 bytes unitSize = int(AbstractType.UNITSIZE_8) * len(data) unitSize = int(pow(2, math.ceil(math.log(unitSize, 2)))) # Round to the nearest upper power of 2 result.append(Integer.encode(data, endianness=AbstractType.ENDIAN_BIG, unitSize=str(unitSize))) else: result.append(0) return result
def decode(data, unitSize=AbstractType.UNITSIZE_32, endianness=AbstractType.defaultEndianness(), sign=AbstractType.SIGN_UNSIGNED): """Decodes the specified Timestamp data into its raw representation >>> from netzob.all import * >>> value = 1444494130 >>> print len(Timestamp.decode(value)) 4 """ if data is None: raise TypeError("Data cannot be None") return Integer.decode(data, unitSize=unitSize, endianness=endianness, sign=sign)
def _generateDataValues(self, cellsData): result = [] for data in cellsData: if len(data) > 0: data = data[:8] # We take at most 8 bytes unitSize = int(AbstractType.UNITSIZE_8) * len(data) unitSize = int(pow(2, math.ceil(math.log( unitSize, 2)))) # Round to the nearest upper power of 2 result.append( Integer.encode(data, endianness=AbstractType.ENDIAN_BIG, unitSize=str(unitSize))) else: result.append(0) return result