embutils.utils.crc module

CRC implementation.

date

2021

author

Christian Wiche

contact

cwichel@gmail.com

license

The MIT License (MIT)

class embutils.utils.crc.CRC(name: str = 'CRC16_CCITT_FALSE', size: int = 16, poly: int = 4129, crc_init: int = 65535, xor_out: int = 0, rev_in: bool = False, rev_out: bool = False)[source]

Bases: object

Generalized table-driven CRC implementation.

Note

  • The CRC model definitions can’t be changed after initialization.

  • The CRC configuration values will be adjusted to the defined bit size.

CRC model initialization.

Parameters
  • name (str) – Model name.

  • size (int) – Size of the CRC in bits.

  • poly (int) – Polynomial generator value.

  • crc_init (int) – Initial value.

  • xor_out (int) – Value to XOR with the CRC result before being returned.

  • rev_in (bool) – If true every input byte will be reversed before being feed to the calculation.

  • rev_out (bool) – If true the final CRC value will be reversed before being returned.

_compute_lookup_table() List[int][source]

Generates the lookup table for the current CRC model.

Returns

Table with 256 pre-computed CRC values.

Return type

List[int]

_compute_normal(data: bytearray, crc_init: int) int[source]

Computes the CRC for model size >= 8bit.

Parameters
  • data (bytearray) – Data to compute the CRC over.

  • crc_init (int) – Overrides the default initial CRC value.

Returns

CRC value.

Return type

int

_compute_small(data: bytearray, crc_init: int) int[source]

Computes the CRC for model size < 8bit.

Parameters
  • data (bytearray) – Data to compute the CRC over.

  • crc_init (int) – Overrides the default initial CRC value.

Returns

CRC value.

Return type

int

_lookup_normal() List[int][source]

Generates the lookup table for model size >= 8bit.

Returns

Table with 256 pre-computed CRC values.

Return type

List[int]

_lookup_small() List[int][source]

Generates the lookup table for model size < 8bit.

Returns

Table with 256 pre-computed CRC values.

Return type

List[int]

compute(data: bytearray, crc_init: Optional[int] = None) int[source]

Computes the CRC for the given bytearray.

Parameters
  • data (bytearray) – Data to compute the CRC over.

  • crc_init (int) – Overrides the default initial CRC value. You can use this parameter to perform chained CRC calculations over data blocks.

Returns

CRC value.

Return type

int

property crc_init: int

Model initial value.

property lookup_table: List[int]

Model pre-computed lookup table.

property name: str

Model name.

property poly: int

Model polynomial.

property reverse_in: bool

Model input bit reverse flag.

property reverse_out: bool

Model output bit reverse flag.

property size: int

Model bit size.

property xor_out: int

Model output XOR value.