Vendor dependencies

This commit is contained in:
2026-08-01 16:11:49 +03:00
parent 7f139a0241
commit 6b5e7f0f8b
29706 changed files with 9575646 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"6963ce42209481ae64b1aaa32943ac800e849ea0b4f7afc5c241a1edbfeaf6aa",".github/workflows/ci.yaml":"fff0746853063fc7d16af686ccc1087bcbd8e6b691edc03d11e4f587e5cbfc38","Cargo.lock":"e0cfa67f112a45ce77b50b1ac7779a3be6252fcbc47acff5263fd0a2496d83ee","Cargo.toml":"bc9ea8bd807209d10bd7ab9ecd78f7275723850c3eab962093e1fe6325ce5c4b","Cargo.toml.orig":"7c7e6696c796a594de96766380b8333f5c78e2a34471ffdc067bd6839e30c20d","LICENSE-APACHE":"470355a7eed93fcc4281ec2e0f82ca3b94e7af1e4d83629f91de8cfac34d750e","LICENSE-MIT":"3488679340a49ecc34d342c4009d2dabf76f4a21f12aec2ca99b15805d656544","README.md":"7acfe4229e5c624ccaf211bb5d8c887da10bb78c551b689bda85bce93ae1ffc8","benches/bench.rs":"54d55294d8856c680b1bc6578a98633a389c1dfa050440629be8238cf54d3f71","src/crc128.rs":"d6b514a880db934022b9a0bdf020180b6d90f4413b1d026647268cbead5261c6","src/crc16.rs":"b15ef0ecaa7769f26f4eb1ab4761cba92965158ded11342a506e08554431ca52","src/crc32.rs":"b551bd9523814a6f3dbbfd33d403c4a68cf29303e1b6d31deb11cc0738abeacb","src/crc64.rs":"468b93940ed01f1ee8fd322be794feea69c1d534792bfc1ae817995d1f9ccfe8","src/crc8.rs":"cdc655e92e5eb50dab29f25ac62da188b64b4b599b956268eea41d3ec3628e96","src/lib.rs":"97d79080ae2f26fdf52af11bf6870360dcdba125306bfc09100b1555bfbeef38","src/table.rs":"e1d2b1a30e269acdbe49bce6a65ea4e45cc8ccb94bff57ffe3fb8ae096f68345","src/util.rs":"0892a1cb1ddf522e2bc6456250f698a5d129eaf0e60076c8cdb38a9e20596fed","tests/crc.rs":"f066d4272b8043e16742fbdbd4d661adba5780924278dc14052bc79489dfa447"},"package":"5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d"}
+6
View File
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "2c8fd9615d620b5a5f8c9556b79a4ca173d6d401"
},
"path_in_vcs": ""
}
+49
View File
@@ -0,0 +1,49 @@
name: ci
on:
pull_request: {}
push:
branches:
- master
schedule:
- cron: '0 0 * * 0'
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt, clippy
- run: cargo clippy -- -D warnings
- run: cargo fmt --all -- --check
build:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.83.0
- stable
- beta
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- run: cargo check
- run: cargo build
test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- beta
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- run: cargo test
+16
View File
@@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "crc"
version = "3.4.0"
dependencies = [
"crc-catalog",
]
[[package]]
name = "crc-catalog"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
+60
View File
@@ -0,0 +1,60 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
rust-version = "1.83"
name = "crc"
version = "3.4.0"
authors = [
"Rui Hu <code@mrhooray.com>",
"Akhil Velagapudi <4@4khil.com>",
]
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Rust implementation of CRC with support of various standards"
documentation = "https://docs.rs/crc"
readme = "README.md"
keywords = [
"crc",
"crc16",
"crc32",
"crc64",
"hash",
]
categories = [
"algorithms",
"no-std",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/mrhooray/crc-rs.git"
[lib]
name = "crc"
path = "src/lib.rs"
[[test]]
name = "crc"
path = "tests/crc.rs"
[[bench]]
name = "bench"
path = "benches/bench.rs"
harness = true
[dependencies.crc-catalog]
version = "2.4.0"
[dev-dependencies]
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "crc"
version = "3.4.0"
authors = ["Rui Hu <code@mrhooray.com>", "Akhil Velagapudi <4@4khil.com>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/mrhooray/crc-rs.git"
documentation = "https://docs.rs/crc"
description = "Rust implementation of CRC with support of various standards"
keywords = ["crc", "crc16", "crc32", "crc64", "hash"]
categories = ["algorithms", "no-std"]
edition = "2021"
rust-version = "1.83"
[dependencies]
crc-catalog = "2.4.0"
[dev-dependencies]
[[bench]]
name = "bench"
harness = true
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0 January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 crc-rs Developers
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.
+89
View File
@@ -0,0 +1,89 @@
# crc
Rust implementation of CRC.
[![ci](https://github.com/mrhooray/crc-rs/actions/workflows/ci.yaml/badge.svg)](https://github.com/mrhooray/crc-rs/actions/workflows/ci.yaml)
[![Crate](https://img.shields.io/crates/v/crc.svg)](https://crates.io/crates/crc)
[![Docs](https://docs.rs/crc/badge.svg)](https://docs.rs/crc)
[![License](https://img.shields.io/crates/l/crc.svg?maxAge=2592000)](https://github.com/mrhooray/crc-rs#license)
### Usage
Add `crc` to `Cargo.toml`
```toml
[dependencies]
crc = "3.4.0"
```
### Examples
Using a well-known algorithm:
```rust
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:
```rust
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.83.
At a minimum, the MSRV will be <= the oldest stable release in the last 12 months. MSRV may be bumped in minor version releases.
### Implementations
This crate has several pluggable implementations:
1. `NoTable` doesn't use a lookup table, and thus minimizes binary size and memory usage.
2. `Table<1>` uses a lookup table with 256 entries (e.g. for u32 thats 256 * 4 bytes).
3. `Table<16>` uses a lookup table with 16 * 256 entries (e.g. for u32 thats 16 * 256 * 4 bytes).
`Table<1>` is the default implementation, but this can be overridden by specifying `I` in `Crc<W, I>`. E.g.: `Crc<u32, NoTable>`, `Crc<u64, Table<16>>`, ...
NOTE: Lookup tables will increase binary size if they're generated at compile-time. Wrapping `Crc` initialization in a `std::cell::OnceCell` may be preferable if binary size is a concern.
### Benchmark
`cargo bench` with AMD Ryzen 7 3800X ([comparison](http://create.stephan-brumme.com/crc32/)).
#### Throughput (GiB/s)
| Width | NoTable | Bytewise | Slice16 |
|-------|---------|----------|---------|
| 8 | 0.113 | 0.585 | 3.11 |
| 16 | 0.105 | 0.483 | 3.23 |
| 32 | 0.111 | 0.516 | 3.30 |
| 64 | 0.139 | 0.517 | 2.92 |
| 82 | 0.091 | 0.438 | 0.623 |
### License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT License ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
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.
+177
View File
@@ -0,0 +1,177 @@
#![feature(test)]
extern crate test;
use crc::*;
use test::{black_box, Bencher};
pub const BLUETOOTH: Crc<u8> = Crc::<u8>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_SLICE16: Crc<u8, Table<16>> = Crc::<u8, Table<16>>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_BYTEWISE: Crc<u8, Table<1>> = Crc::<u8, Table<1>>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_NOLOOKUP: Crc<u8, NoTable> = Crc::<u8, NoTable>::new(&CRC_8_BLUETOOTH);
pub const X25: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_SDLC);
pub const X25_SLICE16: Crc<u16, Table<16>> = Crc::<u16, Table<16>>::new(&CRC_16_IBM_SDLC);
pub const X25_BYTEWISE: Crc<u16, Table<1>> = Crc::<u16, Table<1>>::new(&CRC_16_IBM_SDLC);
pub const X25_NOLOOKUP: Crc<u16, NoTable> = Crc::<u16, NoTable>::new(&CRC_16_IBM_SDLC);
pub const ISCSI: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
pub const ISCSI_SLICE16: Crc<u32, Table<16>> = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
pub const ISCSI_BYTEWISE: Crc<u32, Table<1>> = Crc::<u32, Table<1>>::new(&CRC_32_ISCSI);
pub const ISCSI_NOLOOKUP: Crc<u32, NoTable> = Crc::<u32, NoTable>::new(&CRC_32_ISCSI);
pub const GSM_40: Crc<u64> = Crc::<u64>::new(&CRC_40_GSM);
pub const ECMA: Crc<u64> = Crc::<u64>::new(&CRC_64_ECMA_182);
pub const ECMA_SLICE16: Crc<u64, Table<16>> = Crc::<u64, Table<16>>::new(&CRC_64_ECMA_182);
pub const ECMA_BYTEWISE: Crc<u64, Table<1>> = Crc::<u64, Table<1>>::new(&CRC_64_ECMA_182);
pub const ECMA_NOLOOKUP: Crc<u64, NoTable> = Crc::<u64, NoTable>::new(&CRC_64_ECMA_182);
pub const DARC: Crc<u128> = Crc::<u128>::new(&CRC_82_DARC);
pub const DARC_SLICE16: Crc<u128, Table<16>> = Crc::<u128, Table<16>>::new(&CRC_82_DARC);
pub const DARC_BYTEWISE: Crc<u128, Table<1>> = Crc::<u128, Table<1>>::new(&CRC_82_DARC);
pub const DARC_NOLOOKUP: Crc<u128, NoTable> = Crc::<u128, NoTable>::new(&CRC_82_DARC);
static KB: usize = 1024;
// Baseline benchmark
fn baseline(data: &[u8]) -> usize {
data.iter()
.fold(0usize, |acc, v| acc.wrapping_add(*v as usize))
}
#[bench]
fn bench_baseline(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| baseline(black_box(&bytes)));
}
// CRC-8 benchmarks
#[bench]
fn bench_crc8_nolookup(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| BLUETOOTH_NOLOOKUP.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc8_bytewise(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| BLUETOOTH_BYTEWISE.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc8_slice16(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| BLUETOOTH_SLICE16.checksum(black_box(&bytes)));
}
// CRC-16 benchmarks
#[bench]
fn bench_crc16_nolookup(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| X25_NOLOOKUP.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc16_bytewise(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| X25_BYTEWISE.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc16_slice16(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| X25_SLICE16.checksum(black_box(&bytes)));
}
// CRC-32 benchmarks
#[bench]
fn bench_crc32_nolookup(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| ISCSI_NOLOOKUP.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc32_bytewise(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| ISCSI_BYTEWISE.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc32_slice16(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| ISCSI_SLICE16.checksum(black_box(&bytes)));
}
// CRC-64 benchmarks
#[bench]
fn bench_crc64_nolookup(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| ECMA_NOLOOKUP.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc64_bytewise(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| ECMA_BYTEWISE.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc64_slice16(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| ECMA_SLICE16.checksum(black_box(&bytes)));
}
// CRC-82 benchmarks
#[bench]
fn bench_crc82_nolookup(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| DARC_NOLOOKUP.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc82_bytewise(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| DARC_BYTEWISE.checksum(black_box(&bytes)));
}
#[bench]
fn bench_crc82_slice16(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| DARC_SLICE16.checksum(black_box(&bytes)));
}
// Miscellaneous benchmarks
#[bench]
fn bench_crc40(b: &mut Bencher) {
let size = 16 * KB;
let bytes = vec![0u8; size];
b.bytes = size as u64;
b.iter(|| GSM_40.checksum(black_box(&bytes)));
}
+277
View File
@@ -0,0 +1,277 @@
use crate::table::crc128_table;
use crate::util::crc128;
use crate::*;
use crc_catalog::Algorithm;
impl<const L: usize> Crc<u128, Table<L>>
where
Table<L>: private::Sealed,
{
pub const fn new(algorithm: &'static Algorithm<u128>) -> Self {
Self {
algorithm,
data: crc128_table(algorithm.width, algorithm.poly, algorithm.refin),
}
}
pub const fn checksum(&self, bytes: &[u8]) -> u128 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}
const fn update(&self, crc: u128, bytes: &[u8]) -> u128 {
update_table(crc, self.algorithm, &self.data, bytes)
}
pub const fn digest(&self) -> Digest<'_, u128, Table<L>> {
self.digest_with_initial(self.algorithm.init)
}
/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u128) -> Digest<'_, u128, Table<L>> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
pub const fn table(&self) -> &<Table<L> as Implementation>::Data<u128> {
&self.data
}
}
impl<'a, const L: usize> Digest<'a, u128, Table<L>>
where
Table<L>: private::Sealed,
{
const fn new(crc: &'a Crc<u128, Table<L>>, value: u128) -> Self {
Digest { crc, value }
}
pub const fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}
pub const fn finalize(self) -> u128 {
finalize(self.crc.algorithm, self.value)
}
}
const fn init(algorithm: &Algorithm<u128>, initial: u128) -> u128 {
if algorithm.refin {
initial.reverse_bits() >> (128u8 - algorithm.width)
} else {
initial << (128u8 - algorithm.width)
}
}
const fn finalize(algorithm: &Algorithm<u128>, mut crc: u128) -> u128 {
if algorithm.refin ^ algorithm.refout {
crc = crc.reverse_bits();
}
if !algorithm.refout {
crc >>= 128u8 - algorithm.width;
}
crc ^ algorithm.xorout
}
const fn update_table<const L: usize>(
mut crc: u128,
algorithm: &Algorithm<u128>,
table: &[[u128; 256]; L],
bytes: &[u8],
) -> u128 {
let len = bytes.len();
let mut i = 0;
let reflect = algorithm.refin;
// Process 16 bytes at a time when L=16
if L == 16 {
while i + 16 <= len {
if reflect {
// XOR the first 16 bytes with the current CRC value
let current0 = bytes[i] ^ (crc as u8);
let current1 = bytes[i + 1] ^ ((crc >> 8) as u8);
let current2 = bytes[i + 2] ^ ((crc >> 16) as u8);
let current3 = bytes[i + 3] ^ ((crc >> 24) as u8);
let current4 = bytes[i + 4] ^ ((crc >> 32) as u8);
let current5 = bytes[i + 5] ^ ((crc >> 40) as u8);
let current6 = bytes[i + 6] ^ ((crc >> 48) as u8);
let current7 = bytes[i + 7] ^ ((crc >> 56) as u8);
let current8 = bytes[i + 8] ^ ((crc >> 64) as u8);
let current9 = bytes[i + 9] ^ ((crc >> 72) as u8);
let current10 = bytes[i + 10] ^ ((crc >> 80) as u8);
let current11 = bytes[i + 11] ^ ((crc >> 88) as u8);
let current12 = bytes[i + 12] ^ ((crc >> 96) as u8);
let current13 = bytes[i + 13] ^ ((crc >> 104) as u8);
let current14 = bytes[i + 14] ^ ((crc >> 112) as u8);
let current15 = bytes[i + 15] ^ ((crc >> 120) as u8);
crc = table[0][current15 as usize]
^ table[1][current14 as usize]
^ table[2][current13 as usize]
^ table[3][current12 as usize]
^ table[4][current11 as usize]
^ table[5][current10 as usize]
^ table[6][current9 as usize]
^ table[7][current8 as usize]
^ table[8][current7 as usize]
^ table[9][current6 as usize]
^ table[10][current5 as usize]
^ table[11][current4 as usize]
^ table[12][current3 as usize]
^ table[13][current2 as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
} else {
// For non-reflected CRC128
let current0 = bytes[i] ^ ((crc >> 120) as u8);
let current1 = bytes[i + 1] ^ ((crc >> 112) as u8);
let current2 = bytes[i + 2] ^ ((crc >> 104) as u8);
let current3 = bytes[i + 3] ^ ((crc >> 96) as u8);
let current4 = bytes[i + 4] ^ ((crc >> 88) as u8);
let current5 = bytes[i + 5] ^ ((crc >> 80) as u8);
let current6 = bytes[i + 6] ^ ((crc >> 72) as u8);
let current7 = bytes[i + 7] ^ ((crc >> 64) as u8);
let current8 = bytes[i + 8] ^ ((crc >> 56) as u8);
let current9 = bytes[i + 9] ^ ((crc >> 48) as u8);
let current10 = bytes[i + 10] ^ ((crc >> 40) as u8);
let current11 = bytes[i + 11] ^ ((crc >> 32) as u8);
let current12 = bytes[i + 12] ^ ((crc >> 24) as u8);
let current13 = bytes[i + 13] ^ ((crc >> 16) as u8);
let current14 = bytes[i + 14] ^ ((crc >> 8) as u8);
let current15 = bytes[i + 15] ^ (crc as u8);
crc = table[0][current15 as usize]
^ table[1][current14 as usize]
^ table[2][current13 as usize]
^ table[3][current12 as usize]
^ table[4][current11 as usize]
^ table[5][current10 as usize]
^ table[6][current9 as usize]
^ table[7][current8 as usize]
^ table[8][current7 as usize]
^ table[9][current6 as usize]
^ table[10][current5 as usize]
^ table[11][current4 as usize]
^ table[12][current3 as usize]
^ table[13][current2 as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
}
i += 16;
}
}
// Process remaining bytes one at a time using the table (for L=1 and L=16)
if L > 0 {
if reflect {
while i < len {
let table_index = ((crc ^ bytes[i] as u128) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let table_index = (((crc >> 120) ^ bytes[i] as u128) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc << 8);
i += 1;
}
}
} else {
// This section is for NoTable case (L=0)
let poly = if reflect {
let poly = algorithm.poly.reverse_bits();
poly >> (128u8 - algorithm.width)
} else {
algorithm.poly << (128u8 - algorithm.width)
};
if reflect {
while i < len {
let to_crc = (crc ^ bytes[i] as u128) & 0xFF;
crc = crc128(poly, reflect, to_crc) ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let to_crc = ((crc >> 120) ^ bytes[i] as u128) & 0xFF;
crc = crc128(poly, reflect, to_crc) ^ (crc << 8);
i += 1;
}
}
}
crc
}
#[cfg(test)]
mod test {
use crate::*;
use crc_catalog::{Algorithm, CRC_82_DARC};
/// Test this optimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
let data: &[&str] = &[
"",
"1",
"1234",
"123456789",
"0123456789ABCDE",
"01234567890ABCDEFGHIJK",
"01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
];
pub const CRC_82_DARC_NONREFLEX: Algorithm<u128> = Algorithm {
width: 82,
poly: 0x0308c0111011401440411,
init: 0x000000000000000000000,
refin: false,
refout: true,
xorout: 0x000000000000000000000,
check: 0x09ea83f625023801fd612,
residue: 0x000000000000000000000,
};
let algs_to_test = [&CRC_82_DARC, &CRC_82_DARC_NONREFLEX];
for alg in algs_to_test {
for data in data {
let crc_slice16 = Crc::<u128, Table<16>>::new(alg);
let crc_nolookup = Crc::<u128, NoTable>::new(alg);
let expected = Crc::<u128, Table<1>>::new(alg).checksum(data.as_bytes());
// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
let mut digest = crc_slice16.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
// Check that we didn't break updating from multiple sources
if data.len() > 2 {
let data = data.as_bytes();
let data1 = &data[..data.len() / 2];
let data2 = &data[data.len() / 2..];
let mut digest = crc_slice16.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
}
}
}
}
}
+247
View File
@@ -0,0 +1,247 @@
use crate::table::crc16_table;
use crate::util::crc16;
use crate::*;
use crc_catalog::Algorithm;
impl<const L: usize> Crc<u16, Table<L>>
where
Table<L>: private::Sealed,
{
pub const fn new(algorithm: &'static Algorithm<u16>) -> Self {
Self {
algorithm,
data: crc16_table(algorithm.width, algorithm.poly, algorithm.refin),
}
}
pub const fn checksum(&self, bytes: &[u8]) -> u16 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}
const fn update(&self, crc: u16, bytes: &[u8]) -> u16 {
update_table(crc, self.algorithm, &self.data, bytes)
}
pub const fn digest(&self) -> Digest<'_, u16, Table<L>> {
self.digest_with_initial(self.algorithm.init)
}
/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u16) -> Digest<'_, u16, Table<L>> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
pub const fn table(&self) -> &<Table<L> as Implementation>::Data<u16> {
&self.data
}
}
impl<'a, const L: usize> Digest<'a, u16, Table<L>>
where
Table<L>: private::Sealed,
{
const fn new(crc: &'a Crc<u16, Table<L>>, value: u16) -> Self {
Digest { crc, value }
}
pub const fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}
pub const fn finalize(self) -> u16 {
finalize(self.crc.algorithm, self.value)
}
}
const fn init(algorithm: &Algorithm<u16>, initial: u16) -> u16 {
if algorithm.refin {
initial.reverse_bits() >> (16u8 - algorithm.width)
} else {
initial << (16u8 - algorithm.width)
}
}
const fn finalize(algorithm: &Algorithm<u16>, mut crc: u16) -> u16 {
if algorithm.refin ^ algorithm.refout {
crc = crc.reverse_bits();
}
if !algorithm.refout {
crc >>= 16u8 - algorithm.width;
}
crc ^ algorithm.xorout
}
const fn update_table<const L: usize>(
mut crc: u16,
algorithm: &Algorithm<u16>,
table: &[[u16; 256]; L],
bytes: &[u8],
) -> u16 {
let len = bytes.len();
let mut i = 0;
let reflect = algorithm.refin;
// Process 16 bytes at a time when L=16
if L == 16 {
while i + 16 <= len {
if reflect {
let current0 = bytes[i] ^ (crc as u8);
let current1 = bytes[i + 1] ^ ((crc >> 8) as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][bytes[i + 3] as usize]
^ table[13][bytes[i + 2] as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
} else {
let current0 = bytes[i] ^ ((crc >> 8) as u8);
let current1 = bytes[i + 1] ^ (crc as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][bytes[i + 3] as usize]
^ table[13][bytes[i + 2] as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
}
i += 16;
}
}
// Process remaining bytes one at a time using the table (for L=1 and L=16)
if L > 0 {
if reflect {
while i < len {
let table_index = ((crc ^ bytes[i] as u16) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let table_index = (((crc >> 8) ^ bytes[i] as u16) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc << 8);
i += 1;
}
}
} else {
// This section is for NoTable case (L=0)
let poly = if reflect {
let poly = algorithm.poly.reverse_bits();
poly >> (16u8 - algorithm.width)
} else {
algorithm.poly << (16u8 - algorithm.width)
};
if reflect {
while i < len {
let to_crc = (crc ^ bytes[i] as u16) & 0xFF;
crc = crc16(poly, reflect, to_crc) ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let to_crc = ((crc >> 8) ^ bytes[i] as u16) & 0xFF;
crc = crc16(poly, reflect, to_crc) ^ (crc << 8);
i += 1;
}
}
}
crc
}
#[cfg(test)]
mod test {
use crate::*;
use crc_catalog::{Algorithm, CRC_16_IBM_SDLC};
/// Test this optimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
let data: &[&str] = &[
"",
"1",
"1234",
"123456789",
"0123456789ABCDE",
"01234567890ABCDEFGHIJK",
"01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
];
pub const CRC_16_IBM_SDLC_NONREFLEX: Algorithm<u16> = Algorithm {
width: 16,
poly: 0x1021,
init: 0xffff,
refin: false,
refout: true,
xorout: 0xffff,
check: 0x906e,
residue: 0xf0b8,
};
let algs_to_test = [&CRC_16_IBM_SDLC, &CRC_16_IBM_SDLC_NONREFLEX];
for alg in algs_to_test {
for data in data {
let crc_slice16 = Crc::<u16, Table<16>>::new(alg);
let crc_nolookup = Crc::<u16, NoTable>::new(alg);
let expected = Crc::<u16, Table<1>>::new(alg).checksum(data.as_bytes());
// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
let mut digest = crc_slice16.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
// Check that we didn't break updating from multiple sources
if data.len() > 2 {
let data = data.as_bytes();
let data1 = &data[..data.len() / 2];
let data2 = &data[data.len() / 2..];
let mut digest = crc_slice16.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
}
}
}
}
}
+256
View File
@@ -0,0 +1,256 @@
use crate::table::crc32_table;
use crate::util::crc32;
use crate::*;
use crc_catalog::Algorithm;
impl<const L: usize> Crc<u32, Table<L>>
where
Table<L>: private::Sealed,
{
pub const fn new(algorithm: &'static Algorithm<u32>) -> Self {
Self {
algorithm,
data: crc32_table(algorithm.width, algorithm.poly, algorithm.refin),
}
}
pub const fn checksum(&self, bytes: &[u8]) -> u32 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}
const fn update(&self, crc: u32, bytes: &[u8]) -> u32 {
update_table(crc, self.algorithm, &self.data, bytes)
}
pub const fn digest(&self) -> Digest<'_, u32, Table<L>> {
self.digest_with_initial(self.algorithm.init)
}
/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u32) -> Digest<'_, u32, Table<L>> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
pub const fn table(&self) -> &<Table<L> as Implementation>::Data<u32> {
&self.data
}
}
impl<'a, const L: usize> Digest<'a, u32, Table<L>>
where
Table<L>: private::Sealed,
{
const fn new(crc: &'a Crc<u32, Table<L>>, value: u32) -> Self {
Digest { crc, value }
}
pub const fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}
pub const fn finalize(self) -> u32 {
finalize(self.crc.algorithm, self.value)
}
}
const fn init(algorithm: &Algorithm<u32>, initial: u32) -> u32 {
if algorithm.refin {
initial.reverse_bits() >> (32u8 - algorithm.width)
} else {
initial << (32u8 - algorithm.width)
}
}
const fn finalize(algorithm: &Algorithm<u32>, mut crc: u32) -> u32 {
if algorithm.refin ^ algorithm.refout {
crc = crc.reverse_bits();
}
if !algorithm.refout {
crc >>= 32u8 - algorithm.width;
}
crc ^ algorithm.xorout
}
const fn update_table<const L: usize>(
mut crc: u32,
algorithm: &Algorithm<u32>,
table: &[[u32; 256]; L],
bytes: &[u8],
) -> u32 {
let len = bytes.len();
let mut i = 0;
let reflect = algorithm.refin;
// Process 16 bytes at a time when L=16
if L == 16 {
while i + 16 <= len {
if reflect {
// XOR the first 4 bytes with the current CRC value
let mut current_slice = [0u8; 4];
current_slice[0] = bytes[i] ^ (crc as u8);
current_slice[1] = bytes[i + 1] ^ ((crc >> 8) as u8);
current_slice[2] = bytes[i + 2] ^ ((crc >> 16) as u8);
current_slice[3] = bytes[i + 3] ^ ((crc >> 24) as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][current_slice[3] as usize]
^ table[13][current_slice[2] as usize]
^ table[14][current_slice[1] as usize]
^ table[15][current_slice[0] as usize];
} else {
// For non-reflected CRC32
let mut current_slice = [0u8; 4];
current_slice[0] = bytes[i] ^ ((crc >> 24) as u8);
current_slice[1] = bytes[i + 1] ^ ((crc >> 16) as u8);
current_slice[2] = bytes[i + 2] ^ ((crc >> 8) as u8);
current_slice[3] = bytes[i + 3] ^ (crc as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][current_slice[3] as usize]
^ table[13][current_slice[2] as usize]
^ table[14][current_slice[1] as usize]
^ table[15][current_slice[0] as usize];
}
i += 16;
}
}
// Process remaining bytes one at a time using the table (for L=1 and L=16)
if L > 0 {
if reflect {
while i < len {
let table_index = ((crc ^ bytes[i] as u32) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let table_index = (((crc >> 24) ^ bytes[i] as u32) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc << 8);
i += 1;
}
}
} else {
// This section is for NoTable case (L=0)
let poly = if reflect {
let poly = algorithm.poly.reverse_bits();
poly >> (32u8 - algorithm.width)
} else {
algorithm.poly << (32u8 - algorithm.width)
};
if reflect {
while i < len {
let to_crc = (crc ^ bytes[i] as u32) & 0xFF;
crc = crc32(poly, reflect, to_crc) ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let to_crc = ((crc >> 24) ^ bytes[i] as u32) & 0xFF;
crc = crc32(poly, reflect, to_crc) ^ (crc << 8);
i += 1;
}
}
}
crc
}
#[cfg(test)]
mod test {
use crate::*;
use crc_catalog::{Algorithm, CRC_32_ISCSI};
/// Test this optimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
let data: &[&str] = &[
"",
"1",
"1234",
"123456789",
"0123456789ABCDE",
"01234567890ABCDEFGHIJK",
"01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
];
pub const CRC_32_ISCSI_NONREFLEX: Algorithm<u32> = Algorithm {
width: 32,
poly: 0x1edc6f41,
init: 0xffffffff,
// This is the only flag that affects the optimized code path
refin: false,
refout: true,
xorout: 0xffffffff,
check: 0xe3069283,
residue: 0xb798b438,
};
let algs_to_test = [&CRC_32_ISCSI, &CRC_32_ISCSI_NONREFLEX];
for alg in algs_to_test {
for data in data {
let crc_slice16 = Crc::<u32, Table<16>>::new(alg);
let crc_nolookup = Crc::<u32, NoTable>::new(alg);
let expected = Crc::<u32, Table<1>>::new(alg).checksum(data.as_bytes());
// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
let mut digest = crc_slice16.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
// Check that we didn't break updating from multiple sources
if data.len() > 2 {
let data = data.as_bytes();
let data1 = &data[..data.len() / 2];
let data2 = &data[data.len() / 2..];
let mut digest = crc_slice16.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
}
}
}
}
}
+261
View File
@@ -0,0 +1,261 @@
use crate::table::crc64_table;
use crate::util::crc64;
use crate::*;
use crc_catalog::Algorithm;
impl<const L: usize> Crc<u64, Table<L>>
where
Table<L>: private::Sealed,
{
pub const fn new(algorithm: &'static Algorithm<u64>) -> Self {
Self {
algorithm,
data: crc64_table(algorithm.width, algorithm.poly, algorithm.refin),
}
}
pub const fn checksum(&self, bytes: &[u8]) -> u64 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}
const fn update(&self, crc: u64, bytes: &[u8]) -> u64 {
update_table(crc, self.algorithm, &self.data, bytes)
}
pub const fn digest(&self) -> Digest<'_, u64, Table<L>> {
self.digest_with_initial(self.algorithm.init)
}
/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u64) -> Digest<'_, u64, Table<L>> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
pub const fn table(&self) -> &<Table<L> as Implementation>::Data<u64> {
&self.data
}
}
impl<'a, const L: usize> Digest<'a, u64, Table<L>>
where
Table<L>: private::Sealed,
{
const fn new(crc: &'a Crc<u64, Table<L>>, value: u64) -> Self {
Digest { crc, value }
}
pub const fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}
pub const fn finalize(self) -> u64 {
finalize(self.crc.algorithm, self.value)
}
}
const fn init(algorithm: &Algorithm<u64>, initial: u64) -> u64 {
if algorithm.refin {
initial.reverse_bits() >> (64u8 - algorithm.width)
} else {
initial << (64u8 - algorithm.width)
}
}
const fn finalize(algorithm: &Algorithm<u64>, mut crc: u64) -> u64 {
if algorithm.refin ^ algorithm.refout {
crc = crc.reverse_bits();
}
if !algorithm.refout {
crc >>= 64u8 - algorithm.width;
}
crc ^ algorithm.xorout
}
const fn update_table<const L: usize>(
mut crc: u64,
algorithm: &Algorithm<u64>,
table: &[[u64; 256]; L],
bytes: &[u8],
) -> u64 {
let len = bytes.len();
let mut i = 0;
let reflect = algorithm.refin;
// Process 16 bytes at a time when L=16
if L == 16 {
while i + 16 <= len {
if reflect {
// XOR the first 8 bytes with the current CRC value
let current0 = bytes[i] ^ (crc as u8);
let current1 = bytes[i + 1] ^ ((crc >> 8) as u8);
let current2 = bytes[i + 2] ^ ((crc >> 16) as u8);
let current3 = bytes[i + 3] ^ ((crc >> 24) as u8);
let current4 = bytes[i + 4] ^ ((crc >> 32) as u8);
let current5 = bytes[i + 5] ^ ((crc >> 40) as u8);
let current6 = bytes[i + 6] ^ ((crc >> 48) as u8);
let current7 = bytes[i + 7] ^ ((crc >> 56) as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][current7 as usize]
^ table[9][current6 as usize]
^ table[10][current5 as usize]
^ table[11][current4 as usize]
^ table[12][current3 as usize]
^ table[13][current2 as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
} else {
// For non-reflected CRC64
let current0 = bytes[i] ^ ((crc >> 56) as u8);
let current1 = bytes[i + 1] ^ ((crc >> 48) as u8);
let current2 = bytes[i + 2] ^ ((crc >> 40) as u8);
let current3 = bytes[i + 3] ^ ((crc >> 32) as u8);
let current4 = bytes[i + 4] ^ ((crc >> 24) as u8);
let current5 = bytes[i + 5] ^ ((crc >> 16) as u8);
let current6 = bytes[i + 6] ^ ((crc >> 8) as u8);
let current7 = bytes[i + 7] ^ (crc as u8);
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][current7 as usize]
^ table[9][current6 as usize]
^ table[10][current5 as usize]
^ table[11][current4 as usize]
^ table[12][current3 as usize]
^ table[13][current2 as usize]
^ table[14][current1 as usize]
^ table[15][current0 as usize];
}
i += 16;
}
}
// Process remaining bytes one at a time using the table (for L=1 and L=16)
if L > 0 {
if reflect {
while i < len {
let table_index = ((crc ^ bytes[i] as u64) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let table_index = (((crc >> 56) ^ bytes[i] as u64) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc << 8);
i += 1;
}
}
} else {
// This section is for NoTable case (L=0)
let poly = if reflect {
let poly = algorithm.poly.reverse_bits();
poly >> (64u8 - algorithm.width)
} else {
algorithm.poly << (64u8 - algorithm.width)
};
if reflect {
while i < len {
let to_crc = (crc ^ bytes[i] as u64) & 0xFF;
crc = crc64(poly, reflect, to_crc) ^ (crc >> 8);
i += 1;
}
} else {
while i < len {
let to_crc = ((crc >> 56) ^ bytes[i] as u64) & 0xFF;
crc = crc64(poly, reflect, to_crc) ^ (crc << 8);
i += 1;
}
}
}
crc
}
#[cfg(test)]
mod test {
use crate::*;
use crc_catalog::{Algorithm, CRC_64_ECMA_182};
/// Test this optimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
let data: &[&str] = &[
"",
"1",
"1234",
"123456789",
"0123456789ABCDE",
"01234567890ABCDEFGHIJK",
"01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
];
pub const CRC_64_ECMA_182_REFLEX: Algorithm<u64> = Algorithm {
width: 64,
poly: 0x42f0e1eba9ea3693,
init: 0x0000000000000000,
refin: true,
refout: false,
xorout: 0x0000000000000000,
check: 0x6c40df5f0b497347,
residue: 0x0000000000000000,
};
let algs_to_test = [&CRC_64_ECMA_182, &CRC_64_ECMA_182_REFLEX];
for alg in algs_to_test {
for data in data {
let crc_slice16 = Crc::<u64, Table<16>>::new(alg);
let crc_nolookup = Crc::<u64, NoTable>::new(alg);
let expected = Crc::<u64, Table<1>>::new(alg).checksum(data.as_bytes());
// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
let mut digest = crc_slice16.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
// Check that we didn't break updating from multiple sources
if data.len() > 2 {
let data = data.as_bytes();
let data1 = &data[..data.len() / 2];
let data2 = &data[data.len() / 2..];
let mut digest = crc_slice16.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
}
}
}
}
}
+204
View File
@@ -0,0 +1,204 @@
use crate::table::crc8_table;
use crate::util::crc8;
use crate::*;
use crc_catalog::Algorithm;
impl<const L: usize> Crc<u8, Table<L>>
where
Table<L>: private::Sealed,
{
pub const fn new(algorithm: &'static Algorithm<u8>) -> Self {
Self {
algorithm,
data: crc8_table(algorithm.width, algorithm.poly, algorithm.refin),
}
}
pub const fn checksum(&self, bytes: &[u8]) -> u8 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}
const fn update(&self, crc: u8, bytes: &[u8]) -> u8 {
update_table(crc, self.algorithm, &self.data, bytes)
}
pub const fn digest(&self) -> Digest<'_, u8, Table<L>> {
self.digest_with_initial(self.algorithm.init)
}
/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u8) -> Digest<'_, u8, Table<L>> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
pub const fn table(&self) -> &<Table<L> as Implementation>::Data<u8> {
&self.data
}
}
impl<'a, const L: usize> Digest<'a, u8, Table<L>>
where
Table<L>: private::Sealed,
{
const fn new(crc: &'a Crc<u8, Table<L>>, value: u8) -> Self {
Digest { crc, value }
}
pub const fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}
pub const fn finalize(self) -> u8 {
finalize(self.crc.algorithm, self.value)
}
}
const fn init(algorithm: &Algorithm<u8>, initial: u8) -> u8 {
if algorithm.refin {
initial.reverse_bits() >> (8u8 - algorithm.width)
} else {
initial << (8u8 - algorithm.width)
}
}
const fn finalize(algorithm: &Algorithm<u8>, mut crc: u8) -> u8 {
if algorithm.refin ^ algorithm.refout {
crc = crc.reverse_bits();
}
if !algorithm.refout {
crc >>= 8u8 - algorithm.width;
}
crc ^ algorithm.xorout
}
const fn update_table<const L: usize>(
mut crc: u8,
algorithm: &Algorithm<u8>,
table: &[[u8; 256]; L],
bytes: &[u8],
) -> u8 {
let len = bytes.len();
let mut i = 0;
// Process 16 bytes at a time when L=16
if L == 16 {
while i + 16 <= len {
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][bytes[i + 3] as usize]
^ table[13][bytes[i + 2] as usize]
^ table[14][bytes[i + 1] as usize]
^ table[15][(bytes[i] ^ crc) as usize];
i += 16;
}
}
// Process remaining bytes one at a time using the table (for L=1 and L=16)
if L > 0 {
while i < len {
crc = table[0][(crc ^ bytes[i]) as usize];
i += 1;
}
} else {
// This section is for NoTable case (L=0)
let poly = if algorithm.refin {
let poly = algorithm.poly.reverse_bits();
poly >> (8u8 - algorithm.width)
} else {
algorithm.poly << (8u8 - algorithm.width)
};
while i < len {
crc = crc8(poly, algorithm.refin, crc ^ bytes[i]);
i += 1;
}
}
crc
}
#[cfg(test)]
mod test {
use crate::*;
use crc_catalog::{Algorithm, CRC_8_BLUETOOTH};
/// Test this optimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
let data: &[&str] = &[
"",
"1",
"1234",
"123456789",
"0123456789ABCDE",
"01234567890ABCDEFGHIJK",
"01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
];
pub const CRC_8_BLUETOOTH_NONREFLEX: Algorithm<u8> = Algorithm {
width: 8,
poly: 0xa7,
init: 0x00,
refin: false,
refout: true,
xorout: 0x00,
check: 0x26,
residue: 0x00,
};
let algs_to_test = [&CRC_8_BLUETOOTH, &CRC_8_BLUETOOTH_NONREFLEX];
for alg in algs_to_test {
for data in data {
let crc_slice16 = Crc::<u8, Table<16>>::new(alg);
// let crc_slice8 = Crc::<u8, Table<8>>::new(alg);
let crc_nolookup = Crc::<u8, NoTable>::new(alg);
let expected = Crc::<u8, Table<1>>::new(alg).checksum(data.as_bytes());
// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
let mut digest = crc_slice16.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);
// Check that we didn't break updating from multiple sources
if data.len() > 2 {
let data = data.as_bytes();
let data1 = &data[..data.len() / 2];
let data2 = &data[data.len() / 2..];
let mut digest = crc_slice16.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
}
}
}
}
}
+93
View File
@@ -0,0 +1,93 @@
//! # crc
//! Rust implementation of CRC.
//!
//! ### Examples
//! Using a well-known algorithm:
//! ```rust
//! 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:
//! ```rust
//! 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);
//! ```
#![no_std]
#![forbid(unsafe_code)]
pub use crc_catalog::algorithm::*;
pub use crc_catalog::{Algorithm, Width};
mod crc128;
mod crc16;
mod crc32;
mod crc64;
mod crc8;
mod table;
mod util;
/// A trait for CRC implementations.
pub trait Implementation {
/// Associated data necessary for the implementation (e.g. lookup tables).
type Data<W>;
}
/// A table-based implementation of the CRC algorithm, with `L` lanes.
/// The number of entries in the lookup table is `L * 256`.
#[derive(Copy, Clone)]
pub struct Table<const L: usize> {}
/// An implementation of the CRC algorithm with no lookup table.
pub type NoTable = Table<0>;
type DefaultImpl = Table<1>;
impl<const L: usize> Implementation for Table<L> {
type Data<W> = [[W; 256]; L];
}
mod private {
pub trait Sealed {}
impl Sealed for super::Table<0> {}
impl Sealed for super::Table<1> {}
impl Sealed for super::Table<16> {}
}
/// Crc instance with a specific width, algorithm, and implementation.
#[derive(Clone)]
pub struct Crc<W: Width, I: Implementation = DefaultImpl> {
pub algorithm: &'static Algorithm<W>,
data: I::Data<W>,
}
#[derive(Clone)]
pub struct Digest<'a, W: Width, I: Implementation = DefaultImpl> {
crc: &'a Crc<W, I>,
value: W,
}
#[cfg(test)]
mod test {
use super::{Crc, CRC_32_ISCSI};
#[test]
fn test_clone() {
const CRC: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
let crc = CRC.clone();
let digest = crc.digest();
let _digest = digest.clone();
}
}
+207
View File
@@ -0,0 +1,207 @@
use crate::util::*;
pub(crate) const fn crc8_table<const L: usize>(
width: u8,
poly: u8,
reflect: bool,
) -> [[u8; 256]; L] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (8u8 - width)
} else {
poly << (8u8 - width)
};
let mut table = [[0u8; 256]; L];
// Generate first table (lane 0) directly
let mut i = 0;
while i < 256 && L > 0 {
table[0][i] = crc8(poly, reflect, i as u8);
i += 1;
}
// Generate subsequent lanes based on lane 0
if L > 1 {
let mut i = 0;
while i < 256 {
let mut e = 1;
while e < L {
let one_lower = table[e - 1][i];
table[e][i] = table[0][one_lower as usize];
e += 1;
}
i += 1;
}
}
table
}
pub(crate) const fn crc16_table<const L: usize>(
width: u8,
poly: u16,
reflect: bool,
) -> [[u16; 256]; L] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (16u8 - width)
} else {
poly << (16u8 - width)
};
let mut table = [[0u16; 256]; L];
// Generate first table (lane 0) directly
let mut i = 0;
while i < 256 && L > 0 {
table[0][i] = crc16(poly, reflect, i as u16);
i += 1;
}
// Generate subsequent lanes based on lane 0
if L > 1 {
let mut i = 0;
while i < 256 {
let mut e = 1;
while e < L {
let one_lower = table[e - 1][i];
if reflect {
table[e][i] = (one_lower >> 8) ^ table[0][(one_lower & 0xFF) as usize];
} else {
table[e][i] = (one_lower << 8) ^ table[0][((one_lower >> 8) & 0xFF) as usize];
}
e += 1;
}
i += 1;
}
}
table
}
pub(crate) const fn crc32_table<const L: usize>(
width: u8,
poly: u32,
reflect: bool,
) -> [[u32; 256]; L] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (32u8 - width)
} else {
poly << (32u8 - width)
};
let mut table = [[0u32; 256]; L];
// Generate first table (lane 0) directly
let mut i = 0;
while i < 256 && L > 0 {
table[0][i] = crc32(poly, reflect, i as u32);
i += 1;
}
// Generate subsequent lanes based on lane 0
if L > 1 {
let mut i = 0;
while i < 256 {
let mut e = 1;
while e < L {
let one_lower = table[e - 1][i];
if reflect {
table[e][i] = (one_lower >> 8) ^ table[0][(one_lower & 0xFF) as usize];
} else {
table[e][i] = (one_lower << 8) ^ table[0][((one_lower >> 24) & 0xFF) as usize];
}
e += 1;
}
i += 1;
}
}
table
}
pub(crate) const fn crc64_table<const L: usize>(
width: u8,
poly: u64,
reflect: bool,
) -> [[u64; 256]; L] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (64u8 - width)
} else {
poly << (64u8 - width)
};
let mut table = [[0u64; 256]; L];
// Generate first table (lane 0) directly
let mut i = 0;
while i < 256 && L > 0 {
table[0][i] = crc64(poly, reflect, i as u64);
i += 1;
}
// Generate subsequent lanes based on lane 0
if L > 1 {
let mut i = 0;
while i < 256 {
let mut e = 1;
while e < L {
let one_lower = table[e - 1][i];
if reflect {
table[e][i] = (one_lower >> 8) ^ table[0][(one_lower & 0xFF) as usize];
} else {
table[e][i] = (one_lower << 8) ^ table[0][((one_lower >> 56) & 0xFF) as usize];
}
e += 1;
}
i += 1;
}
}
table
}
pub(crate) const fn crc128_table<const L: usize>(
width: u8,
poly: u128,
reflect: bool,
) -> [[u128; 256]; L] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (128u8 - width)
} else {
poly << (128u8 - width)
};
let mut table = [[0u128; 256]; L];
// Generate first table (lane 0) directly
let mut i = 0;
while i < 256 && L > 0 {
table[0][i] = crc128(poly, reflect, i as u128);
i += 1;
}
// Generate subsequent lanes based on lane 0
if L > 1 {
let mut i = 0;
while i < 256 {
let mut e = 1;
while e < L {
let one_lower = table[e - 1][i];
if reflect {
table[e][i] = (one_lower >> 8) ^ table[0][(one_lower & 0xFF) as usize];
} else {
table[e][i] = (one_lower << 8) ^ table[0][((one_lower >> 120) & 0xFF) as usize];
}
e += 1;
}
i += 1;
}
}
table
}
+91
View File
@@ -0,0 +1,91 @@
pub(crate) const fn crc8(poly: u8, reflect: bool, mut value: u8) -> u8 {
let mut i = 0;
if reflect {
while i < 8 {
value = (value >> 1) ^ ((value & 1) * poly);
i += 1;
}
} else {
while i < 8 {
value = (value << 1) ^ (((value >> 7) & 1) * poly);
i += 1;
}
}
value
}
pub(crate) const fn crc16(poly: u16, reflect: bool, mut value: u16) -> u16 {
if reflect {
let mut i = 0;
while i < 8 {
value = (value >> 1) ^ ((value & 1) * poly);
i += 1;
}
} else {
value <<= 8;
let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> 15) & 1) * poly);
i += 1;
}
}
value
}
pub(crate) const fn crc32(poly: u32, reflect: bool, mut value: u32) -> u32 {
if reflect {
let mut i = 0;
while i < 8 {
value = (value >> 1) ^ ((value & 1) * poly);
i += 1;
}
} else {
value <<= 24;
let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> 31) & 1) * poly);
i += 1;
}
}
value
}
pub(crate) const fn crc64(poly: u64, reflect: bool, mut value: u64) -> u64 {
if reflect {
let mut i = 0;
while i < 8 {
value = (value >> 1) ^ ((value & 1) * poly);
i += 1;
}
} else {
value <<= 56;
let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> 63) & 1) * poly);
i += 1;
}
}
value
}
pub(crate) const fn crc128(poly: u128, reflect: bool, mut value: u128) -> u128 {
if reflect {
let mut i = 0;
while i < 8 {
value = (value >> 1) ^ ((value & 1) * poly);
i += 1;
}
} else {
value <<= 120;
let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> 127) & 1) * poly);
i += 1;
}
}
value
}
+181
View File
@@ -0,0 +1,181 @@
use crc::*;
const INIT: &[u8] = b"123456789";
const INIT_PART1: &[u8] = b"1234";
const INIT_PART2: &[u8] = b"56789";
#[test]
fn crc_8() {
let algs = &[
CRC_3_GSM,
CRC_3_ROHC,
CRC_4_G_704,
CRC_4_INTERLAKEN,
CRC_5_EPC_C1G2,
CRC_5_G_704,
CRC_5_USB,
CRC_6_CDMA2000_A,
CRC_6_CDMA2000_B,
CRC_6_DARC,
CRC_6_G_704,
CRC_6_GSM,
CRC_7_MMC,
CRC_7_ROHC,
CRC_7_UMTS,
CRC_8_AUTOSAR,
CRC_8_BLUETOOTH,
CRC_8_CDMA2000,
CRC_8_DARC,
CRC_8_DVB_S2,
CRC_8_GSM_A,
CRC_8_GSM_B,
CRC_8_I_432_1,
CRC_8_I_CODE,
CRC_8_LTE,
CRC_8_MAXIM_DOW,
CRC_8_MIFARE_MAD,
CRC_8_NRSC_5,
CRC_8_OPENSAFETY,
CRC_8_ROHC,
CRC_8_SAE_J1850,
CRC_8_SMBUS,
CRC_8_TECH_3250,
CRC_8_WCDMA,
];
for alg in algs.iter() {
let crc = Crc::<u8>::new(alg);
assert_eq!(alg.check, crc.checksum(INIT));
let mut digest = crc.digest();
digest.update(INIT_PART1);
digest.update(INIT_PART2);
assert_eq!(alg.check, digest.finalize());
}
}
#[test]
fn crc_16() {
let algs = &[
CRC_10_ATM,
CRC_10_CDMA2000,
CRC_10_GSM,
CRC_11_FLEXRAY,
CRC_11_UMTS,
CRC_12_CDMA2000,
CRC_12_DECT,
CRC_12_GSM,
CRC_12_UMTS,
CRC_13_BBC,
CRC_14_DARC,
CRC_14_GSM,
CRC_15_CAN,
CRC_15_MPT1327,
CRC_16_ARC,
CRC_16_CDMA2000,
CRC_16_CMS,
CRC_16_DDS_110,
CRC_16_DECT_R,
CRC_16_DECT_X,
CRC_16_DNP,
CRC_16_EN_13757,
CRC_16_GENIBUS,
CRC_16_GSM,
CRC_16_IBM_3740,
CRC_16_IBM_SDLC,
CRC_16_ISO_IEC_14443_3_A,
CRC_16_KERMIT,
CRC_16_LJ1200,
CRC_16_MAXIM_DOW,
CRC_16_MCRF4XX,
CRC_16_MODBUS,
CRC_16_NRSC_5,
CRC_16_OPENSAFETY_A,
CRC_16_OPENSAFETY_B,
CRC_16_PROFIBUS,
CRC_16_RIELLO,
CRC_16_SPI_FUJITSU,
CRC_16_T10_DIF,
CRC_16_TELEDISK,
CRC_16_TMS37157,
CRC_16_UMTS,
CRC_16_USB,
CRC_16_XMODEM,
];
for alg in algs.iter() {
let crc = Crc::<u16>::new(alg);
assert_eq!(alg.check, crc.checksum(INIT));
let mut digest = crc.digest();
digest.update(INIT_PART1);
digest.update(INIT_PART2);
assert_eq!(alg.check, digest.finalize());
}
}
#[test]
fn crc_32() {
let algs = &[
CRC_17_CAN_FD,
CRC_21_CAN_FD,
CRC_24_BLE,
CRC_24_FLEXRAY_A,
CRC_24_FLEXRAY_B,
CRC_24_INTERLAKEN,
CRC_24_LTE_A,
CRC_24_LTE_B,
CRC_24_OPENPGP,
CRC_24_OS_9,
CRC_30_CDMA,
CRC_31_PHILIPS,
CRC_32_AIXM,
CRC_32_AUTOSAR,
CRC_32_BASE91_D,
CRC_32_BZIP2,
CRC_32_CD_ROM_EDC,
CRC_32_CKSUM,
CRC_32_ISCSI,
CRC_32_ISO_HDLC,
CRC_32_JAMCRC,
CRC_32_MPEG_2,
CRC_32_XFER,
];
for alg in algs {
let crc = Crc::<u32>::new(alg);
assert_eq!(alg.check, crc.checksum(INIT));
let mut digest = crc.digest();
digest.update(INIT_PART1);
digest.update(INIT_PART2);
assert_eq!(alg.check, digest.finalize());
}
}
#[test]
fn crc_64() {
let algs = &[
CRC_40_GSM,
CRC_64_ECMA_182,
CRC_64_GO_ISO,
CRC_64_WE,
CRC_64_XZ,
];
for alg in algs {
let crc = Crc::<u64>::new(alg);
assert_eq!(alg.check, crc.checksum(INIT));
let mut digest = crc.digest();
digest.update(INIT_PART1);
digest.update(INIT_PART2);
assert_eq!(alg.check, digest.finalize());
}
}
#[test]
fn crc_128() {
let algs = &[CRC_82_DARC];
for alg in algs {
let crc = Crc::<u128>::new(alg);
assert_eq!(alg.check, crc.checksum(INIT));
let mut digest = crc.digest();
digest.update(INIT_PART1);
digest.update(INIT_PART2);
assert_eq!(alg.check, digest.finalize());
}
}