calculated.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:kitty 作者: cisco-sas 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号