Skip to content

Commit

Permalink
adds commnets and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adregan committed Dec 8, 2015
1 parent cdb87ba commit fef9963
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crc.py
Expand Up @@ -6,6 +6,14 @@ def __init__(self):
self.table = {c: self._compute_entry(c) for c in range(256)}

def _compute_entry(self, c):
''' _compute_entry : Int -> Int
Builds an entry in the CRC table from an Int (0 <= c <= 255) using
the CRC algorithm described here:
http://stigge.org/martin/pub/SAR-PR-2006-05.pdf
>>> assert crc._compute_entry(0) == 0
>>> assert crc._compute_entry(113) == 654459306
>>> assert crc._compute_entry(248) == 3009837614
'''
for _ in range(8):
if c & 1:
c = CRC_POLY ^ c >> 1
Expand All @@ -16,10 +24,16 @@ def _compute_entry(self, c):

# create : bytes -> bytes
def create(self, buf):
''' Where buf is the contents of the chunk type and the chunk data
''' create : Bytes -> Bytes
Where buf is the contents of the chunk type and the chunk data
as a string of bytes.
'''
crc = INITIAL_CRC
for i, byt in enumerate(buf):
crc = self.table[(crc ^ byt) & 0xFF] ^ (crc >> 8)

return (crc ^ INITIAL_CRC).to_bytes(4, 'big')

if __name__ == '__main__':
import doctest
doctest.testmod(extraglobs={'crc': CRC()})

0 comments on commit fef9963

Please sign in to comment.