| Total Complexity | 8 |
| Total Lines | 47 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
| 1 | """Implements the _Crc64Calculator class. |
||
| 9 | class _Crc64Calculator(object): |
||
| 10 | """Implements a class that calculates a 64-bit Cyclic Redundancy Check checksum. |
||
| 11 | |||
| 12 | Class initialiser requires a polynomial to seed the construction of a lookup table and an |
||
| 13 | optional initial XOR value. |
||
| 14 | """ |
||
| 15 | |||
| 16 | def __init__(self, polynomial, initial_xor=0xffffffffffffffff): |
||
| 17 | self._construct_lookup_table(polynomial) |
||
| 18 | self._crc64 = initial_xor |
||
| 19 | |||
| 20 | |||
| 21 | @property |
||
| 22 | def crc64(self): |
||
| 23 | """Returns the current CRC-64. |
||
| 24 | """ |
||
| 25 | |||
| 26 | return Crc64Result(self._crc64) |
||
| 27 | |||
| 28 | |||
| 29 | def update(self, content): |
||
| 30 | """Enumerates the bytes of the supplied bytearray and updates the CRC-64. |
||
| 31 | No return value. |
||
| 32 | """ |
||
| 33 | |||
| 34 | for byte in content: |
||
| 35 | self._crc64 = (self._crc64 >> 8) ^ self._lookup_table[(self._crc64 & 0xff) ^ byte] |
||
| 36 | |||
| 37 | |||
| 38 | def _construct_lookup_table(self, polynomial): |
||
| 39 | """Precomputes a CRC-64 lookup table seeded from the supplied polynomial. |
||
| 40 | No return value. |
||
| 41 | """ |
||
| 42 | |||
| 43 | self._lookup_table = [] |
||
| 44 | |||
| 45 | for i in range(0, 256): |
||
| 46 | lookup_value = i |
||
| 47 | |||
| 48 | for _ in range(0, 8): |
||
| 49 | if lookup_value & 0x1 == 0x1: |
||
| 50 | lookup_value = (lookup_value >> 1) ^ polynomial |
||
| 51 | |||
| 52 | else: |
||
| 53 | lookup_value = lookup_value >> 1 |
||
| 54 | |||
| 55 | self._lookup_table.append(lookup_value) |
||
| 56 |