Module CrcMoose
[frames] | no frames]

Module CrcMoose

source code

This module can model common CRC algorithms given the set of defining parameters. This is intended to be easy to use for experimentation rather than optimized for speed. It is slow even for a native Python CRC implementation.

Several common CRC algorithms are predefined in this module.

Examples

>>> '%X' % CRC32.calcString('123456789')
'CBF43926'

This test function runs all of the defined algorithms on the test input string '123456789':

>>> _printResults()
CRC-5-USB: 19
CRC-8-SMBUS: F4
CRC-15: 059E
CRC-16: BB3D
CRC-16-USB: B4C8
CRC-CCITT: 29B1
CRC-HDLC: 906E
CRC-24: 21CF02
CRC-32: CBF43926
CRC-32C: E3069283
CRC-64: 46A5A9388A5BEFFE
CRC-256: 79B96BDC0C519B239BE759EC0688C86FD25A3F4DF1E7F054AD1F923D0739DAC8

Calculating in parts:

>>> value = CRC32.calcString('1234')
>>> '%X' % CRC32.calcString('56789', value)
'CBF43926'

Or, done a different way:

>>> crc = CrcRegister(CRC32)
>>> crc.takeString('1234')
>>> crc.takeString('56789')
>>> '%X' % crc.getFinalValue()
'CBF43926'

Inversion of a CRC function:

>>> CRC_CCITT.reverse().reflect().calcWord(54321, 16, 0)
1648
>>> CRC_CCITT.calcWord(_, 16, 0)
54321

A 15-bit CRC is used in CAN protocols. The following sample CAN frame (in binary here) is converted to hexadecimal for the calcWord call. The bits after the 15-bit CRC are not included in the CRC:

0 11101000001 0 0 0 0001 00010010 011000010111011 1 1 1 1111111

This sample CAN frame was found in this paper: <http://www.anthony-marino.com/documents/HDL_implementation_CAN.pdf>

>>> '%X' % CRC15.calcWord(0x3A08112, 27)
'30BB'

If the CRC is included, the remainder should always be zero:

>>> print CRC15.calcWord(0x1D0408930BB, 42)
0

A 5-bit CRC is used some kinds of USB packets. Here is a sample start-of-frame packet:

10100101 01100111000 01111

(found at <http://www.nital.com/corporate/usb2snooper.html>)

The first field is the PID (not included in the CRC), the next 11-bit field is the frame number (0xE6, LSb-first order), and the final five bits are the CRC (0x1E, LSb-first order).

>>> '%X' % CRC5_USB.calcWord(0xE6, 11)
'1E'



Author: Ray Burr

License: MIT License

Contact: http://www.nightmare.com/~ryb/

Version: 20070611

Classes
  CrcAlgorithm
Represents the parameters of a CRC algorithm.
  CrcRegister
Holds the intermediate state of the CRC algorithm.
Functions
 
reflect(value, width) source code
 
formatBinaryString(value, width) source code
Variables
  CRC32 = <CrcMoose.CrcAlgorithm "CRC-32" @ 0x404d79ec>
Same CRC algorithm as Python's zlib.crc32
  CRC16 = <CrcMoose.CrcAlgorithm "CRC-16" @ 0x404d7a4c>
  CRC16_USB = <CrcMoose.CrcAlgorithm "CRC-16-USB" @ 0x404d7b8c>
Used in USB data packets.
  CRC_CCITT = <CrcMoose.CrcAlgorithm "CRC-CCITT" @ 0x404d7cec>
  CRC_HDLC = <CrcMoose.CrcAlgorithm "CRC-HDLC" @ 0x4052a08c>
This is the algorithm used in X.25 and for the HDLC 2-byte FCS.
  CRC8_SMBUS = <CrcMoose.CrcAlgorithm "CRC-8-SMBUS" @ 0x4052a0cc>
Used in ATM HEC and SMBus.
  CRC24 = <CrcMoose.CrcAlgorithm "CRC-24" @ 0x4052a10c>
Used in RFC-2440 and MIL STD 188-184.
  CRC15 = <CrcMoose.CrcAlgorithm "CRC-15" @ 0x4052a14c>
Used in Controller Area Network frames.
  CRC32C = <CrcMoose.CrcAlgorithm "CRC-32C" @ 0x4052a18c>
Used in iSCSI (RFC-3385); usually credited to Guy Castagnoli.
  CRC5_USB = <CrcMoose.CrcAlgorithm "CRC-5-USB" @ 0x4052a1cc>
CRC used in USB Token and Start-Of-Frame packets
  CRC64 = <CrcMoose.CrcAlgorithm "CRC-64" @ 0x4052a20c>
ISO 3309
  CRC256 = <CrcMoose.CrcAlgorithm "CRC-256" @ 0x4052a24c>
This is just to show off the ability to handle a very wide CRC.