Skip to content

Commit

Permalink
Update crcmod to Py3
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricp committed Oct 27, 2020
1 parent f9303d1 commit d43bad7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 602 deletions.
2 changes: 0 additions & 2 deletions crcmod/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
try:
from crcmod.crcmod import *
import crcmod.predefined
except ImportError:
# Make this backward compatible
from crcmod import *
import predefined
__doc__ = crcmod.__doc__
30 changes: 15 additions & 15 deletions crcmod/_crcfunpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# 2) Provide a version that can be used on systems where a C compiler is not
# available for building extension modules.
#
# Copyright (c) 2004 Raymond L. Buvel
# Copyright (c) 2009 Raymond L. Buvel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,60 +28,60 @@
def _crc8(data, crc, table):
crc = crc & 0xFF
for x in data:
crc = table[ord(x) ^ crc]
crc = table[x ^ crc]
return crc

def _crc8r(data, crc, table):
crc = crc & 0xFF
for x in data:
crc = table[ord(x) ^ crc]
crc = table[x ^ crc]
return crc

def _crc16(data, crc, table):
crc = crc & 0xFFFF
for x in data:
crc = table[ord(x) ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
crc = table[x ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
return crc

def _crc16r(data, crc, table):
crc = crc & 0xFFFF
for x in data:
crc = table[ord(x) ^ (crc & 0xFF)] ^ (crc >> 8)
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc

def _crc24(data, crc, table):
crc = crc & 0xFFFFFF
for x in data:
crc = table[ord(x) ^ (int(crc>>16) & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
crc = table[x ^ (crc>>16 & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
return crc

def _crc24r(data, crc, table):
crc = crc & 0xFFFFFF
for x in data:
crc = table[ord(x) ^ int(crc & 0xFF)] ^ (crc >> 8)
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc

def _crc32(data, crc, table):
crc = crc & 0xFFFFFFFFL
crc = crc & 0xFFFFFFFF
for x in data:
crc = table[ord(x) ^ (int(crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00L)
crc = table[x ^ ((crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00)
return crc

def _crc32r(data, crc, table):
crc = crc & 0xFFFFFFFFL
crc = crc & 0xFFFFFFFF
for x in data:
crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc

def _crc64(data, crc, table):
crc = crc & 0xFFFFFFFFFFFFFFFFL
crc = crc & 0xFFFFFFFFFFFFFFFF
for x in data:
crc = table[ord(x) ^ (int(crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00L)
crc = table[x ^ ((crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00)
return crc

def _crc64r(data, crc, table):
crc = crc & 0xFFFFFFFFFFFFFFFFL
crc = crc & 0xFFFFFFFFFFFFFFFF
for x in data:
crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc

90 changes: 40 additions & 50 deletions crcmod/crcmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
The following are the public components of this module.
Crc -- a class that creates instances providing the same interface as the
md5 and sha modules in the Python standard library. These instances also
provide a method for generating a C/C++ function to compute the CRC.
algorithms in the hashlib module in the Python standard library. These
instances also provide a method for generating a C/C++ function to compute
the CRC.
mkCrcFun -- create a Python function to compute the CRC using the specified
polynomial and initial value. This provides a much simpler interface if
Expand All @@ -41,10 +42,10 @@
# If the extension module was not built, drop back to the Python implementation
# even though it is significantly slower.
try:
import _crcfunext as _crcfun
import crcmod._crcfunext as _crcfun
_usingExtension = True
except ImportError:
import _crcfunpy as _crcfun
import crcmod._crcfunpy as _crcfun
_usingExtension = False

import sys, struct
Expand All @@ -53,9 +54,9 @@
class Crc:
'''Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.
Instances of this class have the same interface as the md5 and sha modules
in the Python standard library. See the documentation for these modules
for examples of how to use a Crc instance.
Instances of this class have the same interface as the algorithms in the
hashlib module in the Python standard library. See the documentation of
this module for examples of how to use a Crc instance.
The string representation of a Crc instance identifies the polynomial,
initial value, XOR out value, and the current CRC value. The print
Expand All @@ -68,9 +69,9 @@ class Crc:
The following are the parameters supplied to the constructor.
poly -- The generator polynomial to use in calculating the CRC. The value
is specified as a Python integer or long integer. The bits in this integer
are the coefficients of the polynomial. The only polynomials allowed are
those that generate 8, 16, 24, 32, or 64 bit CRCs.
is specified as a Python integer. The bits in this integer are the
coefficients of the polynomial. The only polynomials allowed are those
that generate 8, 16, 24, 32, or 64 bit CRCs.
initCrc -- Initial value used to start the CRC calculation. This initial
value should be the initial shift register value XORed with the final XOR
Expand All @@ -85,7 +86,7 @@ class Crc:
xorOut -- Final value to XOR with the calculated CRC value. Used by some
CRC algorithms. Defaults to zero.
'''
def __init__(self, poly, initCrc=~0L, rev=True, xorOut=0, initialize=True):
def __init__(self, poly, initCrc=~0, rev=True, xorOut=0, initialize=True):
if not initialize:
# Don't want to perform the initialization when using new or copy
# to create a new instance.
Expand Down Expand Up @@ -158,11 +159,11 @@ def digest(self):
crc = self.crcValue
lst = []
while n > 0:
lst.append(chr(crc & 0xFF))
lst.append(crc & 0xFF)
crc = crc >> 8
n -= 1
lst.reverse()
return ''.join(lst)
return bytes(lst)

def hexdigest(self):
'''Return the current CRC value as a string of hex digits. The length
Expand Down Expand Up @@ -265,7 +266,7 @@ def generateCode(self, functionName, out, dataType=None, crcType=None):
out.write(_codeTemplate % parms)

#-----------------------------------------------------------------------------
def mkCrcFun(poly, initCrc=~0L, rev=True, xorOut=0):
def mkCrcFun(poly, initCrc=~0, rev=True, xorOut=0):
'''Return a function that computes the CRC using the specified polynomial.
poly -- integer representation of the generator polynomial
Expand Down Expand Up @@ -293,9 +294,8 @@ def crcfun(data, crc=initCrc):

def _verifyPoly(poly):
msg = 'The degree of the polynomial must be 8, 16, 24, 32 or 64'
poly = long(poly) # Use a common representation for all operations
for n in (8,16,24,32,64):
low = 1L<<n
low = 1<<n
high = low*2
if low <= poly < high:
return n
Expand All @@ -305,13 +305,10 @@ def _verifyPoly(poly):
# Bit reverse the input value.

def _bitrev(x, n):
x = long(x)
y = 0L
for i in xrange(n):
y = (y << 1) | (x & 1L)
y = 0
for i in range(n):
y = (y << 1) | (x & 1)
x = x >> 1
if ((1L<<n)-1) <= sys.maxint:
return int(y)
return y

#-----------------------------------------------------------------------------
Expand All @@ -320,32 +317,24 @@ def _bitrev(x, n):
# bit of the polynomial has been stripped off.

def _bytecrc(crc, poly, n):
crc = long(crc)
poly = long(poly)
mask = 1L<<(n-1)
for i in xrange(8):
mask = 1<<(n-1)
for i in range(8):
if crc & mask:
crc = (crc << 1) ^ poly
else:
crc = crc << 1
mask = (1L<<n) - 1
mask = (1<<n) - 1
crc = crc & mask
if mask <= sys.maxint:
return int(crc)
return crc

def _bytecrc_r(crc, poly, n):
crc = long(crc)
poly = long(poly)
for i in xrange(8):
if crc & 1L:
for i in range(8):
if crc & 1:
crc = (crc >> 1) ^ poly
else:
crc = crc >> 1
mask = (1L<<n) - 1
mask = (1<<n) - 1
crc = crc & mask
if mask <= sys.maxint:
return int(crc)
return crc

#-----------------------------------------------------------------------------
Expand All @@ -357,15 +346,15 @@ def _bytecrc_r(crc, poly, n):
# have been checked for validity by the caller.

def _mkTable(poly, n):
mask = (1L<<n) - 1
poly = long(poly) & mask
table = [_bytecrc(long(i)<<(n-8),poly,n) for i in xrange(256)]
mask = (1<<n) - 1
poly = poly & mask
table = [_bytecrc(i<<(n-8),poly,n) for i in range(256)]
return table

def _mkTable_r(poly, n):
mask = (1L<<n) - 1
poly = _bitrev(long(poly) & mask, n)
table = [_bytecrc_r(long(i),poly,n) for i in xrange(256)]
mask = (1<<n) - 1
poly = _bitrev(poly & mask, n)
table = [_bytecrc_r(i,poly,n) for i in range(256)]
return table

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -404,19 +393,20 @@ def _mkTable_r(poly, n):
def _verifyParams(poly, initCrc, xorOut):
sizeBits = _verifyPoly(poly)

mask = (1L<<sizeBits) - 1
# First return value is the poly size (in bits)
out = [sizeBits]

mask = (1<<sizeBits) - 1

# Adjust the initial CRC to the correct data type (unsigned value).
initCrc = long(initCrc) & mask
if mask <= sys.maxint:
initCrc = int(initCrc)
initCrc = initCrc & mask
out.append(initCrc)

# Similar for XOR-out value.
xorOut = long(xorOut) & mask
if mask <= sys.maxint:
xorOut = int(xorOut)
xorOut = xorOut & mask
out.append(xorOut)

return (sizeBits, initCrc, xorOut)
return out

#-----------------------------------------------------------------------------
# The following function returns a Python function to compute the CRC.
Expand Down
45 changes: 10 additions & 35 deletions crcmod/predefined.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,13 @@
NON_REVERSE = False

# The following table defines the parameters of well-known CRC algorithms.
# The "Check" value is the CRC for the ASCII byte sequence "123456789". It
# The "Check" value is the CRC for the ASCII byte sequence b"123456789". It
# can be used for unit tests.
_crc_definitions_table = [
# Name Identifier-name, Poly Reverse Init-value XOR-out Check
[ 'crc-8', 'Crc8', 0x107, NON_REVERSE, 0x00, 0x00, 0xF4, ],
[ 'crc-8-darc', 'Crc8Darc', 0x139, REVERSE, 0x00, 0x00, 0x15, ],
[ 'crc-8-i-code', 'Crc8ICode', 0x11D, NON_REVERSE, 0xFD, 0x00, 0x7E, ],
[ 'crc-8-itu', 'Crc8Itu', 0x107, NON_REVERSE, 0x55, 0x55, 0xA1, ],
[ 'crc-8-maxim', 'Crc8Maxim', 0x131, REVERSE, 0x00, 0x00, 0xA1, ],
[ 'crc-8-rohc', 'Crc8Rohc', 0x107, REVERSE, 0xFF, 0x00, 0xD0, ],
[ 'crc-8-wcdma', 'Crc8Wcdma', 0x19B, REVERSE, 0x00, 0x00, 0x25, ],

[ 'crc-16', 'Crc16', 0x18005, REVERSE, 0x0000, 0x0000, 0xBB3D, ],
[ 'crc-16-buypass', 'Crc16Buypass', 0x18005, NON_REVERSE, 0x0000, 0x0000, 0xFEE8, ],
[ 'crc-16-dds-110', 'Crc16Dds110', 0x18005, NON_REVERSE, 0x800D, 0x0000, 0x9ECF, ],
[ 'crc-16-dect', 'Crc16Dect', 0x10589, NON_REVERSE, 0x0001, 0x0001, 0x007E, ],
[ 'crc-16-dnp', 'Crc16Dnp', 0x13D65, REVERSE, 0xFFFF, 0xFFFF, 0xEA82, ],
[ 'crc-16-en-13757', 'Crc16En13757', 0x13D65, NON_REVERSE, 0xFFFF, 0xFFFF, 0xC2B7, ],
[ 'crc-16-genibus', 'Crc16Genibus', 0x11021, NON_REVERSE, 0x0000, 0xFFFF, 0xD64E, ],
[ 'crc-16-maxim', 'Crc16Maxim', 0x18005, REVERSE, 0xFFFF, 0xFFFF, 0x44C2, ],
[ 'crc-16-mcrf4xx', 'Crc16Mcrf4xx', 0x11021, REVERSE, 0xFFFF, 0x0000, 0x6F91, ],
[ 'crc-16-riello', 'Crc16Riello', 0x11021, REVERSE, 0x554D, 0x0000, 0x63D0, ],
[ 'crc-16-t10-dif', 'Crc16T10Dif', 0x18BB7, NON_REVERSE, 0x0000, 0x0000, 0xD0DB, ],
[ 'crc-16-teledisk', 'Crc16Teledisk', 0x1A097, NON_REVERSE, 0x0000, 0x0000, 0x0FB3, ],
[ 'crc-16-usb', 'Crc16Usb', 0x18005, REVERSE, 0x0000, 0xFFFF, 0xB4C8, ],
[ 'x-25', 'CrcX25', 0x11021, REVERSE, 0x0000, 0xFFFF, 0x906E, ],
[ 'xmodem', 'CrcXmodem', 0x11021, NON_REVERSE, 0x0000, 0x0000, 0x31C3, ],
Expand All @@ -81,24 +64,16 @@
[ 'crc-aug-ccitt', 'CrcAugCcitt', 0x11021, NON_REVERSE, 0x1D0F, 0x0000, 0xE5CC, ],

[ 'crc-24', 'Crc24', 0x1864CFB, NON_REVERSE, 0xB704CE, 0x000000, 0x21CF02, ],
[ 'crc-24-flexray-a', 'Crc24FlexrayA', 0x15D6DCB, NON_REVERSE, 0xFEDCBA, 0x000000, 0x7979BD, ],
[ 'crc-24-flexray-b', 'Crc24FlexrayB', 0x15D6DCB, NON_REVERSE, 0xABCDEF, 0x000000, 0x1F23B8, ],

[ 'crc-32', 'Crc32', 0x104C11DB7, REVERSE, 0x00000000, 0xFFFFFFFF, 0xCBF43926, ],
[ 'crc-32-bzip2', 'Crc32Bzip2', 0x104C11DB7, NON_REVERSE, 0x00000000, 0xFFFFFFFF, 0xFC891918, ],
[ 'crc-32c', 'Crc32C', 0x11EDC6F41, REVERSE, 0x00000000, 0xFFFFFFFF, 0xE3069283, ],
[ 'crc-32d', 'Crc32D', 0x1A833982B, REVERSE, 0x00000000, 0xFFFFFFFF, 0x87315576, ],
[ 'crc-32-mpeg', 'Crc32Mpeg', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0x00000000, 0x0376E6E7, ],
[ 'posix', 'CrcPosix', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0xFFFFFFFF, 0x765E7680, ],
[ 'crc-32q', 'Crc32Q', 0x1814141AB, NON_REVERSE, 0x00000000, 0x00000000, 0x3010BF7F, ],
[ 'jamcrc', 'CrcJamCrc', 0x104C11DB7, REVERSE, 0xFFFFFFFF, 0x00000000, 0x340BC6D9, ],
[ 'xfer', 'CrcXfer', 0x1000000AF, NON_REVERSE, 0x00000000, 0x00000000, 0xBD0BE338, ],

[ 'crc-32', 'Crc32', 0x104c11db7, REVERSE, 0x00000000, 0xFFFFFFFF, 0xCBF43926, ],
[ 'crc-32c', 'Crc32C', 0x11edc6f41, REVERSE, 0x00000000, 0xFFFFFFFF, 0xE3069283, ],
[ 'crc-32-mpeg', 'Crc32Mpeg', 0x104c11db7, NON_REVERSE, 0xFFFFFFFF, 0x00000000, 0x0376E6E7, ],
[ 'posix', 'CrcPosix', 0x104c11db7, NON_REVERSE, 0xFFFFFFFF, 0xFFFFFFFF, 0x765E7680, ],

# 64-bit
# Name Identifier-name, Poly Reverse Init-value XOR-out Check
[ 'crc-64', 'Crc64', 0x1000000000000001B, REVERSE, 0x0000000000000000, 0x0000000000000000, 0x46A5A9388A5BEFFE, ],
[ 'crc-64-we', 'Crc64We', 0x142F0E1EBA9EA3693, NON_REVERSE, 0x0000000000000000, 0xFFFFFFFFFFFFFFFF, 0x62EC59E3F1A4F00A, ],
[ 'crc-64-jones', 'Crc64Jones', 0x1AD93D23594C935A9, REVERSE, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0xCAA717168609F281, ],
[ 'crc-64-jones', 'Crc64Jones', 0x1ad93d23594c935a9, REVERSE, 0x0000000000000000, 0x0000000000000000, 0xE9C6D914C4B8D9CA, ],
]


Expand Down Expand Up @@ -129,7 +104,7 @@ def _simplify_name(name):
_crc_definitions.append(crc_definition)
name = _simplify_name(table_entry[0])
if name in _crc_definitions_by_name:
raise Exception("Duplicate entry for '%s' in CRC table" % name)
raise Exception("Duplicate entry for '{0}' in CRC table".format(name))
_crc_definitions_by_name[name] = crc_definition
_crc_definitions_by_identifier[table_entry[1]] = crc_definition

Expand All @@ -139,14 +114,14 @@ def _get_definition_by_name(crc_name):
if not definition:
definition = _crc_definitions_by_identifier.get(crc_name, None)
if not definition:
raise KeyError("Unkown CRC name '%s'" % crc_name)
raise KeyError("Unkown CRC name '{0}'".format(crc_name))
return definition


class PredefinedCrc(crcmod.Crc):
def __init__(self, crc_name):
definition = _get_definition_by_name(crc_name)
crcmod.Crc.__init__(self, poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
super().__init__(poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])


# crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
Expand Down
Loading

0 comments on commit d43bad7

Please sign in to comment.