def __init__(self, depends_on, length, algorithm='crc32', encoder=ENC_INT_DEFAULT, fuzzable=False, name=None):
'''
:param depends_on: (name of) field to be checksummed
:param length: length of the checksum field (in bits)
:param algorithm: checksum algorithm name (from Checksum._algos) or a function to calculate the value of the field. func(Bits) -> int
:type encoder: :class:`~kitty.model.low_level.encoder.BitFieldEncoder`
:param encoder: encoder for the field (default: ENC_INT_DEFAULT)
:param fuzzable: is field fuzzable (default: False)
:param name: (unique) name of the field (default: None)
:example:
::
Container(name='checksummed chunk', fields=[
RandomBytes(name='chunk', value='1234', min_length=0, max_length=75),
Checksum(name='CRC', depends_on='chunk', length=32)
])
'''
if algorithm in Checksum._algos:
func = Checksum._algos[algorithm]
else:
try:
res = algorithm(empty_bits)
kassert.is_of_types(res, types.IntType)
func = algorithm
except:
raise KittyException('algorithm should be a func(str)->int or one of the strings %s' % (Checksum._algos.keys(),))
def calc_func(x):
return func(x.bytes) & 0xffffffff
bit_field = BitField(value=0, length=length, encoder=encoder)
super(Checksum, self).__init__(depends_on=depends_on, bit_field=bit_field, calc_func=calc_func, fuzzable=fuzzable, name=name)
评论列表
文章目录