'A generic CRC-sum function.
This is suitable to use with:
http://reveng.sourceforge.net/crc-catalogue/all.htm
The "check" value in the document is the CRC-sum of the string "123456789".
Arguments:
data(str):    The data to calculate the CRC-sum of. This should either be a string or a list of bits.
polynom(int): The polynomial to use.
init(int):    If the CRC-sum was calculated in hardware, then this would b
the initial value of the checksum register.
refin(bool):  Should the input bytes be reflected?
refout(bool): Should the checksum be reflected?
xorout(int):  The value to xor the checksum with before outputting'

@staticmethod
def generic_crc(data, polynom, width, init, refin, refout, xorout):

    polynom = (BitPolynom(int(polynom)) | (1 << width))
    if (polynom.degree() != width):
        raise ValueError('Polynomial    is    too    large    for    that    width')
    init &= ((1 << width) - 1)
    xorout &= ((1 << width) - 1)
    if isinstance(data, list):
        inlen = len(data)
        p = BitPolynom(int(''.join((('1' if v else '0') for v in data)), 2))
    elif isinstance(data, str):
        inlen = (len(data) * 8)
        if refin:
            data = fiddling.bitswap(data)
        p = BitPolynom(packing.unpack(data, 'all', endian='big', sign=False))
    p = (p << width)
    p ^= (init << inlen)
    p = (p % polynom)
    res = p.n
    if refout:
        res = fiddling.bitswap_int(res, width)
    res ^= xorout
    return res
