Beispiel #1
0
def crc(message,
        polynom,
        init=0,
        in_reflection=False,
        out_reflection=False,
        out_xor=False):
    root, reversed_, width = generate_root(polynom)

    # we have to augment the string with 'width' bits
    augmented_message = message + "\x00" * (width / 8)

    #if no IN reflection is needed, we'll work in bit 7 first mode.
    bit7_first = not in_reflection

    bits = _bits.bits(augmented_message, bit7_first)
    if width in MASK:
        mask = MASK[width]
    else:
        mask = (1 << width) - 1
    reg = init

    #TODO: why ? check http://www.tty1.net/pycrc/#download
    for i in xrange(width):
        lowest_bit_set = (reg & 1) == 1
        if lowest_bit_set:
            reg ^= root
        reg = (reg >> 1) | (lowest_bit_set << (width - 1))

    for new_read_bit in bits:
        highest_reg_bit_set = (((reg >> (width - 1)) & 1) == 1)
        reg = ((reg << 1) & mask) | int(new_read_bit)
        if highest_reg_bit_set:
            reg ^= root

    if out_reflection:
        reg = _bits.reverse(reg, width)

    if out_xor:
        reg = ~reg
    reg &= mask
    return reg, width
Beispiel #2
0
def crc(message, polynom, init=0, in_reflection=False, out_reflection=False, out_xor=False):
    root, reversed_, width = generate_root(polynom)

    # we have to augment the string with 'width' bits
    augmented_message = message + "\x00" * (width / 8)

    #if no IN reflection is needed, we'll work in bit 7 first mode.
    bit7_first = not in_reflection

    bits = _bits.bits(augmented_message, bit7_first)
    if width in MASK:
        mask = MASK[width]
    else:
        mask = (1 << width) - 1
    reg = init

    #TODO: why ? check http://www.tty1.net/pycrc/#download
    for i in xrange(width):
        lowest_bit_set = (reg & 1) == 1
        if lowest_bit_set:
            reg ^= root
        reg = (reg >> 1) | (lowest_bit_set << (width - 1))


    for new_read_bit in bits:
        highest_reg_bit_set = (((reg >> (width - 1)) & 1) == 1)
        reg = ((reg << 1) & mask) | int(new_read_bit)
        if highest_reg_bit_set:
            reg ^= root

    if out_reflection:
        reg = _bits.reverse(reg, width)

    if out_xor:
        reg = ~reg
    reg &= mask
    return reg, width
Beispiel #3
0
#
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp._bits import bits, reverse, compress, decompress

assert "".join(str(int(i)) for i in bits("AE")) == "01000001" + "01000101"
assert "".join(
    str(int(i))
    for i in bits("AE", bit7_first=False)) == "10000010" + "10100010"

assert reverse(10341313, 32) == 2211690752
assert reverse(reverse(10341313, 32), 32) == 10341313

c = compress(1)
c.write_bit(1)
assert c.getdata() == "\x80"

c = compress(2)
c.write_bit(1)
assert c.getdata() == "\x00\x80"

c = compress(2)
c.write_variablenumber(109)
assert c.getdata() == "`\xdf"

c = compress(2)
c.write_fixednumber(109, 9)
assert c.getdata() == '\x806'

#c = compress(2);c.write_bit(1);c.write_fixednumber(15, 5);c.write_variablenumber(2049);
#test = c.getdata()
Beispiel #4
0
#
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp._bits import bits, reverse, compress, decompress

assert "".join(str(int(i)) for i in bits("AE")) == "01000001" + "01000101"
assert "".join(str(int(i)) for i in bits("AE", bit7_first=False)) == "10000010" + "10100010"

assert reverse(10341313, 32) == 2211690752
assert reverse(reverse(10341313, 32), 32) == 10341313

c = compress(1)
c.write_bit(1)
assert c.getdata() == "\x80"

c = compress(2)
c.write_bit(1)
assert c.getdata() == "\x00\x80"

c = compress(2)
c.write_variablenumber(109)
assert c.getdata() == "`\xdf"

c = compress(2)
c.write_fixednumber(109, 9)
assert c.getdata() == '\x806'
 
#c = compress(2);c.write_bit(1);c.write_fixednumber(15, 5);c.write_variablenumber(2049);
#test = c.getdata()
test = 'U\xbd`U'
d = decompress(test, 2)