feat: Add new gcloud commands, API clients, and third-party libraries across various services.

This commit is contained in:
2026-01-01 20:26:35 +01:00
parent 5e23cbece0
commit a19e592eb7
25221 changed files with 8324611 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
try:
from gcloud_crcmod.python3.crcmod import *
import gcloud_crcmod.python3.predefined
except ImportError:
# Make this backward compatible
from crcmod import *
import predefined
__doc__ = crcmod.__doc__

View File

@@ -0,0 +1,107 @@
#-----------------------------------------------------------------------------
# Low level CRC functions for use by crcmod. This version is implemented in
# Python for a couple of reasons. 1) Provide a reference implememtation.
# 2) Provide a version that can be used on systems where a C compiler is not
# available for building extension modules.
#
# Copyright (c) 2009 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
def _get_buffer_view(in_obj):
if isinstance(in_obj, str):
raise TypeError('Unicode-objects must be encoded before calculating a CRC')
mv = memoryview(in_obj)
if mv.ndim > 1:
raise BufferError('Buffer must be single dimension')
return mv
def _crc8(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFF
for x in mv.tobytes():
crc = table[x ^ crc]
return crc
def _crc8r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFF
for x in mv.tobytes():
crc = table[x ^ crc]
return crc
def _crc16(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFF
for x in mv.tobytes():
crc = table[x ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
return crc
def _crc16r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc24(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc>>16 & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
return crc
def _crc24r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc32(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ ((crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00)
return crc
def _crc32r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc64(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFFFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ ((crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00)
return crc
def _crc64r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFFFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc

View File

@@ -0,0 +1,457 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''crcmod is a Python module for gererating objects that compute the Cyclic
Redundancy Check. Any 8, 16, 24, 32, or 64 bit polynomial can be used.
The following are the public components of this module.
Crc -- a class that creates instances providing the same interface as the
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
all you need is a function for CRC calculation.
'''
__all__ = '''mkCrcFun Crc
'''.split()
# Select the appropriate set of low-level CRC functions for this installation.
# If the extension module was not built, drop back to the Python implementation
# even though it is significantly slower.
try:
import gcloud_crcmod.python3._crcfunext as _crcfun
_usingExtension = True
except ImportError:
from gcloud_crcmod.python3 import _crcfunpy as _crcfun
_usingExtension = False
import sys, struct
#-----------------------------------------------------------------------------
class Crc:
'''Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.
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
statement can be used to output this information.
If you need to generate a C/C++ function for use in another application,
use the generateCode method. If you need to generate code for another
language, subclass Crc and override the generateCode method.
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. 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
value. That is equivalent to the CRC result the algorithm should return for
a zero-length string. Defaults to all bits set because that starting value
will take leading zero bytes into account. Starting with zero will ignore
all leading zero bytes.
rev -- A flag that selects a bit reversed algorithm when True. Defaults to
True because the bit reversed algorithms are more efficient.
xorOut -- Final value to XOR with the calculated CRC value. Used by some
CRC algorithms. Defaults to zero.
'''
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.
return
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
self.digest_size = sizeBits//8
self.initCrc = initCrc
self.xorOut = xorOut
self.poly = poly
self.reverse = rev
(crcfun, table) = _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)
self._crc = crcfun
self.table = table
self.crcValue = self.initCrc
def __str__(self):
lst = []
lst.append('poly = 0x%X' % self.poly)
lst.append('reverse = %s' % self.reverse)
fmt = '0x%%0%dX' % (self.digest_size*2)
lst.append('initCrc = %s' % (fmt % self.initCrc))
lst.append('xorOut = %s' % (fmt % self.xorOut))
lst.append('crcValue = %s' % (fmt % self.crcValue))
return '\n'.join(lst)
def new(self, arg=None):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the initial
value. If a string is provided in the optional arg parameter, it is
passed to the update method.
'''
n = Crc(poly=None, initialize=False)
n._crc = self._crc
n.digest_size = self.digest_size
n.initCrc = self.initCrc
n.xorOut = self.xorOut
n.table = self.table
n.crcValue = self.initCrc
n.reverse = self.reverse
n.poly = self.poly
if arg is not None:
n.update(arg)
return n
def copy(self):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the current
value. This allows multiple CRC calculations using a common initial
string.
'''
c = self.new()
c.crcValue = self.crcValue
return c
def update(self, data):
'''Update the current CRC value using the string specified as the data
parameter.
'''
self.crcValue = self._crc(data, self.crcValue)
def digest(self):
'''Return the current CRC value as a string of bytes. The length of
this string is specified in the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append(crc & 0xFF)
crc = crc >> 8
n -= 1
lst.reverse()
return bytes(lst)
def hexdigest(self):
'''Return the current CRC value as a string of hex digits. The length
of this string is twice the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append('%02X' % (crc & 0xFF))
crc = crc >> 8
n -= 1
lst.reverse()
return ''.join(lst)
def generateCode(self, functionName, out, dataType=None, crcType=None):
'''Generate a C/C++ function.
functionName -- String specifying the name of the function.
out -- An open file-like object with a write method. This specifies
where the generated code is written.
dataType -- An optional parameter specifying the data type of the input
data to the function. Defaults to UINT8.
crcType -- An optional parameter specifying the data type of the CRC
value. Defaults to one of UINT8, UINT16, UINT32, or UINT64 depending
on the size of the CRC value.
'''
if dataType is None:
dataType = 'UINT8'
if crcType is None:
size = 8*self.digest_size
if size == 24:
size = 32
crcType = 'UINT%d' % size
if self.digest_size == 1:
# Both 8-bit CRC algorithms are the same
crcAlgor = 'table[*data ^ (%s)crc]'
elif self.reverse:
# The bit reverse algorithms are all the same except for the data
# type of the crc variable which is specified elsewhere.
crcAlgor = 'table[*data ^ (%s)crc] ^ (crc >> 8)'
else:
# The forward CRC algorithms larger than 8 bits have an extra shift
# operation to get the high byte.
shift = 8*(self.digest_size - 1)
crcAlgor = 'table[*data ^ (%%s)(crc >> %d)] ^ (crc << 8)' % shift
fmt = '0x%%0%dX' % (2*self.digest_size)
if self.digest_size <= 4:
fmt = fmt + 'U,'
else:
# Need the long long type identifier to keep gcc from complaining.
fmt = fmt + 'ULL,'
# Select the number of entries per row in the output code.
n = {1:8, 2:8, 3:4, 4:4, 8:2}[self.digest_size]
lst = []
for i, val in enumerate(self.table):
if (i % n) == 0:
lst.append('\n ')
lst.append(fmt % val)
poly = 'polynomial: 0x%X' % self.poly
if self.reverse:
poly = poly + ', bit reverse algorithm'
if self.xorOut:
# Need to remove the comma from the format.
preCondition = '\n crc = crc ^ %s;' % (fmt[:-1] % self.xorOut)
postCondition = preCondition
else:
preCondition = ''
postCondition = ''
if self.digest_size == 3:
# The 24-bit CRC needs to be conditioned so that only 24-bits are
# used from the 32-bit variable.
if self.reverse:
preCondition += '\n crc = crc & 0xFFFFFFU;'
else:
postCondition += '\n crc = crc & 0xFFFFFFU;'
parms = {
'dataType' : dataType,
'crcType' : crcType,
'name' : functionName,
'crcAlgor' : crcAlgor % dataType,
'crcTable' : ''.join(lst),
'poly' : poly,
'preCondition' : preCondition,
'postCondition' : postCondition,
}
out.write(_codeTemplate % parms)
#-----------------------------------------------------------------------------
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
initCrc -- default initial CRC value
rev -- when true, indicates that the data is processed bit reversed.
xorOut -- the final XOR value
The returned function has the following user interface
def crcfun(data, crc=initCrc):
'''
# First we must verify the params
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
# Make the function (and table), return the function
return _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)[0]
#-----------------------------------------------------------------------------
# Naming convention:
# All function names ending with r are bit reverse variants of the ones
# without the r.
#-----------------------------------------------------------------------------
# Check the polynomial to make sure that it is acceptable and return the number
# of bits in the CRC.
def _verifyPoly(poly):
msg = 'The degree of the polynomial must be 8, 16, 24, 32 or 64'
for n in (8,16,24,32,64):
low = 1<<n
high = low*2
if low <= poly < high:
return n
raise ValueError(msg)
#-----------------------------------------------------------------------------
# Bit reverse the input value.
def _bitrev(x, n):
y = 0
for i in range(n):
y = (y << 1) | (x & 1)
x = x >> 1
return y
#-----------------------------------------------------------------------------
# The following functions compute the CRC for a single byte. These are used
# to build up the tables needed in the CRC algorithm. Assumes the high order
# bit of the polynomial has been stripped off.
def _bytecrc(crc, poly, n):
mask = 1<<(n-1)
for i in range(8):
if crc & mask:
crc = (crc << 1) ^ poly
else:
crc = crc << 1
mask = (1<<n) - 1
crc = crc & mask
return crc
def _bytecrc_r(crc, poly, n):
for i in range(8):
if crc & 1:
crc = (crc >> 1) ^ poly
else:
crc = crc >> 1
mask = (1<<n) - 1
crc = crc & mask
return crc
#-----------------------------------------------------------------------------
# The following functions compute the table needed to compute the CRC. The
# table is returned as a list. Note that the array module does not support
# 64-bit integers on a 32-bit architecture as of Python 2.3.
#
# These routines assume that the polynomial and the number of bits in the CRC
# have been checked for validity by the caller.
def _mkTable(poly, n):
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 = (1<<n) - 1
poly = _bitrev(poly & mask, n)
table = [_bytecrc_r(i,poly,n) for i in range(256)]
return table
#-----------------------------------------------------------------------------
# Map the CRC size onto the functions that handle these sizes.
_sizeMap = {
8 : [_crcfun._crc8, _crcfun._crc8r],
16 : [_crcfun._crc16, _crcfun._crc16r],
24 : [_crcfun._crc24, _crcfun._crc24r],
32 : [_crcfun._crc32, _crcfun._crc32r],
64 : [_crcfun._crc64, _crcfun._crc64r],
}
#-----------------------------------------------------------------------------
# Build a mapping of size to struct module type code. This table is
# constructed dynamically so that it has the best chance of picking the best
# code to use for the platform we are running on. This should properly adapt
# to 32 and 64 bit machines.
_sizeToTypeCode = {}
for typeCode in 'B H I L Q'.split():
size = {1:8, 2:16, 4:32, 8:64}.get(struct.calcsize(typeCode),None)
if size is not None and size not in _sizeToTypeCode:
_sizeToTypeCode[size] = '256%s' % typeCode
_sizeToTypeCode[24] = _sizeToTypeCode[32]
del typeCode, size
#-----------------------------------------------------------------------------
# The following function validates the parameters of the CRC, namely,
# poly, and initial/final XOR values.
# It returns the size of the CRC (in bits), and "sanitized" initial/final XOR values.
def _verifyParams(poly, initCrc, xorOut):
sizeBits = _verifyPoly(poly)
mask = (1<<sizeBits) - 1
# Adjust the initial CRC to the correct data type (unsigned value).
initCrc = initCrc & mask
# Similar for XOR-out value.
xorOut = xorOut & mask
return (sizeBits, initCrc, xorOut)
#-----------------------------------------------------------------------------
# The following function returns a Python function to compute the CRC.
#
# It must be passed parameters that are already verified & sanitized by
# _verifyParams().
#
# The returned function calls a low level function that is written in C if the
# extension module could be loaded. Otherwise, a Python implementation is
# used.
#
# In addition to this function, a list containing the CRC table is returned.
def _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut):
if rev:
tableList = _mkTable_r(poly, sizeBits)
_fun = _sizeMap[sizeBits][1]
else:
tableList = _mkTable(poly, sizeBits)
_fun = _sizeMap[sizeBits][0]
_table = tableList
if _usingExtension:
_table = struct.pack(_sizeToTypeCode[sizeBits], *tableList)
if xorOut == 0:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return fun(data, crc, table)
else:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return xorOut ^ fun(data, xorOut ^ crc, table)
return crcfun, tableList
#-----------------------------------------------------------------------------
_codeTemplate = '''// Automatically generated CRC function
// %(poly)s
%(crcType)s
%(name)s(%(dataType)s *data, int len, %(crcType)s crc)
{
static const %(crcType)s table[256] = {%(crcTable)s
};
%(preCondition)s
while (len > 0)
{
crc = %(crcAlgor)s;
data++;
len--;
}%(postCondition)s
return crc;
}
'''

View File

@@ -0,0 +1,163 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''
crcmod.predefined defines some well-known CRC algorithms.
To use it, e.g.:
import crcmod.predefined
crc32func = crcmod.predefined.mkPredefinedCrcFun("crc-32")
crc32class = crcmod.predefined.PredefinedCrc("crc-32")
crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
But if doing 'from crc.predefined import *', only PredefinedCrc is imported.
'''
# local imports
# import crcmod
from gcloud_crcmod.python3 import crcmod
__all__ = [
'PredefinedCrc',
'mkPredefinedCrcFun',
]
REVERSE = True
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 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, ],
[ 'modbus', 'CrcModbus', 0x18005, REVERSE, 0xFFFF, 0x0000, 0x4B37, ],
# Note definitions of CCITT are disputable. See:
# http://homepages.tesco.net/~rainstorm/crc-catalogue.htm
# http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm
[ 'kermit', 'CrcKermit', 0x11021, REVERSE, 0x0000, 0x0000, 0x2189, ],
[ 'crc-ccitt-false', 'CrcCcittFalse', 0x11021, NON_REVERSE, 0xFFFF, 0x0000, 0x29B1, ],
[ '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, ],
# 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, ],
]
def _simplify_name(name):
"""
Reduce CRC definition name to a simplified form:
* lowercase
* dashes removed
* spaces removed
* any initial "CRC" string removed
"""
name = name.lower()
name = name.replace('-', '')
name = name.replace(' ', '')
if name.startswith('crc'):
name = name[len('crc'):]
return name
_crc_definitions_by_name = {}
_crc_definitions_by_identifier = {}
_crc_definitions = []
_crc_table_headings = [ 'name', 'identifier', 'poly', 'reverse', 'init', 'xor_out', 'check' ]
for table_entry in _crc_definitions_table:
crc_definition = dict(zip(_crc_table_headings, table_entry))
_crc_definitions.append(crc_definition)
name = _simplify_name(table_entry[0])
if name in _crc_definitions_by_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
def _get_definition_by_name(crc_name):
definition = _crc_definitions_by_name.get(_simplify_name(crc_name), None)
if not definition:
definition = _crc_definitions_by_identifier.get(crc_name, None)
if not definition:
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)
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
Crc = PredefinedCrc
def mkPredefinedCrcFun(crc_name):
definition = _get_definition_by_name(crc_name)
return crcmod.mkCrcFun(poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
# crcmod.predefined.mkCrcFun is an alias for crcmod.predefined.mkPredefinedCrcFun
mkCrcFun = mkPredefinedCrcFun