def add(self, byte): """Add a new byte to the CRC engine. 'byte' should be a python character. E.g: c.add('x') """ # CRC-16 polynomial poly_s = byte ^ (self.state >> 8) poly_t = poly_s ^ (poly_s >> 4) result = s16l(self.state, 8) ^ poly_t ^ s16l(poly_t, 5) ^ s16l(poly_t, 12) self.state = result
def add(self, byte): """Add a new byte to the CRC engine. 'byte' should be a python character. E.g: c.add('x') """ # CRC-16 polynomial s = byte ^ (self.state >> 8) t = s ^ (s >> 4) r = s16l(self.state, 8) ^ t ^ s16l(t, 5) ^ s16l(t, 12) self.state = r
def check_s16l(value, n, expected): assert s16l(value, n) == expected
def test_s16l_ffff(self): for num, expected in [(1, 0xfffe), (8, 0xff00), (15, 0x8000), (16, 0)]: self.assertEqual(s16l(0xffff, num), expected)
def test_s16l_zero(self): for num in range(16): self.assertEqual(s16l(0, num), 0)