Crc Rust: Easily calculate CRC checksums in Rust

crc

Rust implementation of CRC.

Usage

Add crc to Cargo.toml

[dependencies]
crc = "3.0"

Examples

Using a well-known algorithm:

const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC);
assert_eq!(X25.checksum(b"123456789"), 0x906e);

Using a custom algorithm:

const CUSTOM_ALG: crc::Algorithm<u16> = crc::Algorithm {
    width: 16,
    poly: 0x8005,
    init: 0xffff,
    refin: false,
    refout: false,
    xorout: 0x0000,
    check: 0xaee7,
    residue: 0x0000
};
let crc = crc::Crc::<u16>::new(&CUSTOM_ALG);
let mut digest = crc.digest();
digest.update(b"123456789");
assert_eq!(digest.finalize(), 0xaee7);

Minimum supported Rust version (MSRV)

This crate's MSRV is 1.56.

At a minimum, the MSRV will be <= the oldest stable release in the last 12 months. MSRV may be bumped in minor version releases.

Lookup table flavors

This crate offers three flavors of lookup tables providing a tradeoff between computation speed and used memory. See the benchmark section for hints, but do benchmarks on your target hardware to decide if the tradeoff is worth it to you.

  1. NoTable provides an implementation that uses no additional memory
  2. Bytewise provides an implementation that uses a lookup table that uses 256 entries of the used width (e.g. for u32 thats 256 * 4 bytes)
  3. Slice16 provides an implementation that uses a lookup table that uses 16 * 256 entries of the used width (e.g. for u32 thats 16 * 256 * 4 bytes)

These can be used by substituting Crc<uxxx> with e.g. Crc<Slice16<uxxx>>. The flavor for Crc<uxxx> is chosen based on three crate features:

  • no-table-mem-limit: Takes precedence over "bytewise-mem-limit" and "slice16-mem-limit"
  • bytewise-mem-limit: Takes precedence over "slice16-mem-limit"
  • slice16-mem-limit: Can be overriden by setting "bytewise-mem-limit" and "slice16-mem-limit"

If no feature is selected, the Bytewise flavor is used.

Note that these tables can bloat your binary size if you precalculate them at compiletime (this happens in Crc::new). Choosing a crate like oncecell or lazystatic to compute them once at runtime may be preferable where binary size is a concern.

Benchmark

cargo bench with AMD Ryzen 7 3800X (comparison).

Throughput (GiB/s)

WidthNoTableBytewiseSlice16
80.1130.5853.11
160.1050.4833.23
320.1110.5163.30
640.1390.5172.92
820.0910.4380.623

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


Download details:

Author: mrhooray
Source: https://github.com/mrhooray/crc-rs

License: Apache-2.0, MIT licenses found
 

#rust 

Crc Rust: Easily calculate CRC checksums in Rust
1.20 GEEK