CRC (crc v0.11.0)

View Source

This module is used to calculate CRC (Cyclic Redundancy Check) values for binary data. It uses NIF functions written in C to iterate over the given binary calculating the CRC checksum value.

CRC implementations have been tested against these online calculators to validate their correctness to the best of our ability.

https://www.lammertbies.nl/comm/info/crc-calculation.html http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

Summary

Functions

Calculate a CRC checksum for the input based on the crc params given.

Calculates a 16-bit CCITT CRC with the given seed, seed defaults to 0xFFFF if one is not given.

Calculates a 16-bit CCITT 0x1D0F CRC

Calculates a 16-bit CCITT Kermit CRC

Calculates a 16-bit CCITT XMODEM CRC

Calculates an XOR checksum for the given binary

Calculate a CRC checksum for the input based on the crc params given.

Calculates a 8-bit CRC with polynomial x^8+x^6+x^3+x^2+1, 0x14D. Chosen based on Koopman, et al. (0xA6 in his notation = 0x14D >> 1): http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf

Calculates a 16-bit ANSI CRC checksum for the provided binary

Calculates a 16-bit DNP CRC

Calculates a 16-bit modbus CRC

Calculates a 16-bit Sick CRC

Calculates a 32-bit CRC

Takes a resource result from crc_update/2 and finalizes the multi-part CRC calculation.

Initialize a resource to be used for doing CRC calculations. The returned resource can be used with crc/2 or crc_update/2 to calculate CRC checksums.

Begins or continues a multi-part CRC calculation.

Returns a list of all the compiled CRC models.

Returns a list of all compiled CRC Models that match the filter given.

Functions

calculate(input, params)

Calculate a CRC checksum for the input based on the crc params given.

See CRC.crc/2 for details on valid params.

This function has the parameter order reversed to allow easier use with pipelines. allowing code to be written like:

Examples

read_data() |> CRC.calculate(:crc_16) |> do_something()

ccitt_16(input)

@spec ccitt_16(binary()) :: number()

Calculates a 16-bit CCITT CRC with the given seed, seed defaults to 0xFFFF if one is not given.

This CCIT method uses a 0x1021 polynomial.

ccitt_16(input, seed)

@spec ccitt_16(binary(), number()) :: number()

ccitt_16_1D0F(input)

@spec ccitt_16_1D0F(binary()) :: number()

Calculates a 16-bit CCITT 0x1D0F CRC

This CCIT method uses a 0x1021 polynomial.

ccitt_16_kermit(input)

@spec ccitt_16_kermit(binary()) :: number()

Calculates a 16-bit CCITT Kermit CRC

This CCIT method uses a 0x8408 polynomial.

ccitt_16_kermit(input, seed)

@spec ccitt_16_kermit(binary(), number()) :: number()

ccitt_16_xmodem(input)

@spec ccitt_16_xmodem(binary()) :: number()

Calculates a 16-bit CCITT XMODEM CRC

This CCIT method uses a 0x1021 polynomial.

checksum_xor(input)

@spec checksum_xor(binary()) :: number()

Calculates an XOR checksum for the given binary

crc(params, input)

Calculate a CRC checksum for the input based on the crc params given.

params can be an atom for one of the compiled models. See CRC.list/0 for a full list or a Map with parameters to create a model at runtime. The map given should have all of the following keys:

width - (unsigned integer) representation for the width of the CRC in bits poly - (unsigned integer) the polynomial used for the CRC calculation init - (unsigned integer) The initial value used when starting the calculation refin - (boolean) if the input value should be reflected. This is used for changing between endian's refout - (boolean) if the outvalue should be reflected when calculation is completed xorout - (unsigned integer) Final xor value used when completing the CRC calculation

Examples

%{
  width: 16,
  poly: 0x1021,
  init: 0x00,
  refin: false,
  refout: false,
  xorout: 0x00
}

You can also extend one of the compiled models at runtime by creating a map with extend key set to the model you wish to extend and the keys you wish to override for that model.

For example to override the initial value for the :crc_16_ccitt_false model to 0x1D0F you would pass the following Map as params:

`%{extend: :crc_16_ccitt_false, init: 0x1D0F}`

You can learn more about CRC calculation here: https://www.sunshine2k.de/articles/coding/crc/understanding_crc.html

crc_8(input, seed \\ 255)

@spec crc_8(binary(), number()) :: number()

Calculates a 8-bit CRC with polynomial x^8+x^6+x^3+x^2+1, 0x14D. Chosen based on Koopman, et al. (0xA6 in his notation = 0x14D >> 1): http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf

seed defaults to 0xFF if one is not given

crc_16(input)

@spec crc_16(binary()) :: number()

Calculates a 16-bit ANSI CRC checksum for the provided binary

crc_16_dnp(input)

@spec crc_16_dnp(binary()) :: number()

Calculates a 16-bit DNP CRC

crc_16_modbus(input)

@spec crc_16_modbus(binary()) :: number()

Calculates a 16-bit modbus CRC

crc_16_sick(input)

@spec crc_16_sick(binary()) :: number()

Calculates a 16-bit Sick CRC

crc_32(input)

@spec crc_32(binary()) :: number()

Calculates a 32-bit CRC

crc_final(resource)

Takes a resource result from crc_update/2 and finalizes the multi-part CRC calculation.

crc_init(params)

Initialize a resource to be used for doing CRC calculations. The returned resource can be used with crc/2 or crc_update/2 to calculate CRC checksums.

Resource is created using the same params types that are used with crc/2:

  • atom's for compiled models
  • Map with model values
  • Map to extend a compiled model.

If used with crc/2 the returned resource can be re-used multiple times, but using a map or atom for a compiled model will likely be slightly more performant.

When using with crc_update/2 a new resource will be returned with every call that should be used to continue the calculation.

crc_update(resource, input)

Begins or continues a multi-part CRC calculation.

Takes a resource from result of crc_init/1 or previous crc_update/2 call, and binary input, returns a new resource to be used to continue or finalize the CRC calculation.

list()

@spec list() :: [{atom(), String.t()}]

Returns a list of all the compiled CRC models.

list(filter)

@spec list(binary()) :: [{atom(), String.t()}]

Returns a list of all compiled CRC Models that match the filter given.

Filter is compiled into a regular expression and matched against the model name and description.