crc-3.2.1/.cargo_vcs_info.json0000644000000001360000000000100116060ustar { "git": { "sha1": "0c81caa1075cd75c772967c0f889f374e7fadbed" }, "path_in_vcs": "" }crc-3.2.1/.github/workflows/ci.yaml000064400000000000000000000017541046102023000152610ustar 00000000000000name: 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.65.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 crc-3.2.1/.gitignore000064400000000000000000000000221046102023000123600ustar 00000000000000target Cargo.lock crc-3.2.1/Cargo.toml0000644000000021310000000000100076010ustar # 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.65" name = "crc" version = "3.2.1" authors = [ "Rui Hu ", "Akhil Velagapudi <4@4khil.com>", ] 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" [[bench]] name = "bench" harness = false [dependencies.crc-catalog] version = "2.4.0" [dev-dependencies.criterion] version = "0.4" crc-3.2.1/Cargo.toml.orig000064400000000000000000000011411046102023000132620ustar 00000000000000[package] name = "crc" version = "3.2.1" authors = [ "Rui Hu ", "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.65" [dependencies] crc-catalog = "2.4.0" [dev-dependencies] criterion = { version = "0.4" } [[bench]] name = "bench" harness = false crc-3.2.1/LICENSE-APACHE000064400000000000000000000251361046102023000123310ustar 00000000000000 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. crc-3.2.1/LICENSE-MIT000064400000000000000000000020621046102023000120320ustar 00000000000000MIT 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. crc-3.2.1/README.md000064400000000000000000000055231046102023000116620ustar 00000000000000# 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.2.1" ``` ### Examples Using a well-known algorithm: ```rust const X25: crc::Crc = crc::Crc::::new(&crc::CRC_16_IBM_SDLC); assert_eq!(X25.checksum(b"123456789"), 0x906e); ``` Using a custom algorithm: ```rust const CUSTOM_ALG: crc::Algorithm = crc::Algorithm { width: 16, poly: 0x8005, init: 0xffff, refin: false, refout: false, xorout: 0x0000, check: 0xaee7, residue: 0x0000 }; let crc = crc::Crc::::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.65. 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`. E.g.: `Crc`, `Crc>`, ... 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. crc-3.2.1/benches/bench.rs000064400000000000000000000120461046102023000134350ustar 00000000000000use crc::*; use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput}; pub const BLUETOOTH: Crc = Crc::::new(&CRC_8_BLUETOOTH); pub const BLUETOOTH_SLICE16: Crc> = Crc::>::new(&CRC_8_BLUETOOTH); pub const BLUETOOTH_BYTEWISE: Crc> = Crc::>::new(&CRC_8_BLUETOOTH); pub const BLUETOOTH_NOLOOKUP: Crc = Crc::::new(&CRC_8_BLUETOOTH); pub const X25: Crc = Crc::::new(&CRC_16_IBM_SDLC); pub const X25_SLICE16: Crc> = Crc::>::new(&CRC_16_IBM_SDLC); pub const X25_BYTEWISE: Crc> = Crc::>::new(&CRC_16_IBM_SDLC); pub const X25_NOLOOKUP: Crc = Crc::::new(&CRC_16_IBM_SDLC); pub const ISCSI: Crc = Crc::::new(&CRC_32_ISCSI); pub const ISCSI_SLICE16: Crc> = Crc::>::new(&CRC_32_ISCSI); pub const ISCSI_BYTEWISE: Crc> = Crc::>::new(&CRC_32_ISCSI); pub const ISCSI_NOLOOKUP: Crc = Crc::::new(&CRC_32_ISCSI); pub const GSM_40: Crc = Crc::::new(&CRC_40_GSM); pub const ECMA: Crc = Crc::::new(&CRC_64_ECMA_182); pub const ECMA_SLICE16: Crc> = Crc::>::new(&CRC_64_ECMA_182); pub const ECMA_BYTEWISE: Crc> = Crc::>::new(&CRC_64_ECMA_182); pub const ECMA_NOLOOKUP: Crc = Crc::::new(&CRC_64_ECMA_182); pub const DARC: Crc = Crc::::new(&CRC_82_DARC); pub const DARC_SLICE16: Crc> = Crc::>::new(&CRC_82_DARC); pub const DARC_BYTEWISE: Crc> = Crc::>::new(&CRC_82_DARC); pub const DARC_NOLOOKUP: Crc = Crc::::new(&CRC_82_DARC); static KB: usize = 1024; fn baseline(data: &[u8]) -> usize { data.iter() .fold(0usize, |acc, v| acc.wrapping_add(*v as usize)) } fn checksum(c: &mut Criterion) { let size = 16 * KB; let bytes = vec![0u8; size]; c.benchmark_group("baseline") .throughput(Throughput::Bytes(size as u64)) .bench_function("baseline", |b| b.iter(|| baseline(black_box(&bytes)))); c.benchmark_group("crc8") .throughput(Throughput::Bytes(size as u64)) .bench_function("default", |b| { b.iter(|| BLUETOOTH.checksum(black_box(&bytes))) }) .bench_function("nolookup", |b| { b.iter(|| BLUETOOTH_NOLOOKUP.checksum(black_box(&bytes))) }) .bench_function("bytewise", |b| { b.iter(|| BLUETOOTH_BYTEWISE.checksum(black_box(&bytes))) }) .bench_function("slice16", |b| { b.iter(|| BLUETOOTH_SLICE16.checksum(black_box(&bytes))) }); c.benchmark_group("crc16") .throughput(Throughput::Bytes(size as u64)) .bench_function("default", |b| b.iter(|| X25.checksum(black_box(&bytes)))) .bench_function("nolookup", |b| { b.iter(|| X25_NOLOOKUP.checksum(black_box(&bytes))) }) .bench_function("bytewise", |b| { b.iter(|| X25_BYTEWISE.checksum(black_box(&bytes))) }) .bench_function("slice16", |b| { b.iter(|| X25_SLICE16.checksum(black_box(&bytes))) }); c.benchmark_group("crc32") .throughput(Throughput::Bytes(size as u64)) .bench_function("default", |b| b.iter(|| ISCSI.checksum(black_box(&bytes)))) .bench_function("nolookup", |b| { b.iter(|| ISCSI_NOLOOKUP.checksum(black_box(&bytes))) }) .bench_function("bytewise", |b| { b.iter(|| ISCSI_BYTEWISE.checksum(black_box(&bytes))) }) .bench_function("slice16", |b| { b.iter(|| ISCSI_SLICE16.checksum(black_box(&bytes))) }); c.benchmark_group("crc64") .throughput(Throughput::Bytes(size as u64)) .bench_function("default", |b| b.iter(|| ECMA.checksum(black_box(&bytes)))) .bench_function("nolookup", |b| { b.iter(|| ECMA_NOLOOKUP.checksum(black_box(&bytes))) }) .bench_function("bytewise", |b| { b.iter(|| ECMA_BYTEWISE.checksum(black_box(&bytes))) }) .bench_function("slice16", |b| { b.iter(|| ECMA_SLICE16.checksum(black_box(&bytes))) }); c.benchmark_group("crc82") .throughput(Throughput::Bytes(size as u64)) .bench_function("default", |b| b.iter(|| DARC.checksum(black_box(&bytes)))) .bench_function("nolookup", |b| { b.iter(|| DARC_NOLOOKUP.checksum(black_box(&bytes))) }) .bench_function("bytewise", |b| { b.iter(|| DARC_BYTEWISE.checksum(black_box(&bytes))) }) .bench_function("slice16", |b| { b.iter(|| DARC_SLICE16.checksum(black_box(&bytes))) }); c.benchmark_group("checksum") .bench_function("crc8", |b| b.iter(|| BLUETOOTH.checksum(black_box(&bytes)))) .bench_function("crc40", |b| b.iter(|| GSM_40.checksum(black_box(&bytes)))); } criterion_group!(checksum_benches, checksum); criterion_main!(checksum_benches); crc-3.2.1/src/crc128/bytewise.rs000064400000000000000000000032601046102023000143710ustar 00000000000000use crate::table::crc128_table; use crate::*; use super::{finalize, init, update_bytewise}; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let table = crc128_table(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data: [table], } } 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_bytewise(crc, self.algorithm.refin, &self.data[0], bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u128, Table<1>> { const fn new(crc: &'a Crc>, value: u128) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc128/nolookup.rs000064400000000000000000000026651046102023000144140ustar 00000000000000use super::{finalize, init, update_nolookup}; use crate::*; impl Crc { pub const fn new(algorithm: &'static Algorithm) -> Self { Self { algorithm, data: [], } } 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_nolookup(crc, self.algorithm, bytes) } pub const fn digest(&self) -> Digest { 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 { let value = init(self.algorithm, initial); Digest::new(self, value) } } impl<'a> Digest<'a, u128, NoTable> { const fn new(crc: &'a Crc, value: u128) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc128/slice16.rs000064400000000000000000000032301046102023000140010ustar 00000000000000use crate::table::crc128_table_slice_16; use crate::*; use super::{finalize, init, update_slice16}; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let data = crc128_table_slice_16(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data } } 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_slice16(crc, self.algorithm.refin, &self.data, bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u128, Table<16>> { const fn new(crc: &'a Crc>, value: u128) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc128.rs000064400000000000000000000210321046102023000125330ustar 00000000000000use crate::util::crc128; use crc_catalog::Algorithm; mod bytewise; mod nolookup; mod slice16; const fn init(algorithm: &Algorithm, initial: u128) -> u128 { if algorithm.refin { initial.reverse_bits() >> (128u8 - algorithm.width) } else { initial << (128u8 - algorithm.width) } } const fn finalize(algorithm: &Algorithm, 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_nolookup(mut crc: u128, algorithm: &Algorithm, bytes: &[u8]) -> u128 { let poly = if algorithm.refin { let poly = algorithm.poly.reverse_bits(); poly >> (128u8 - algorithm.width) } else { algorithm.poly << (128u8 - algorithm.width) }; let mut i = 0; if algorithm.refin { while i < bytes.len() { let to_crc = (crc ^ bytes[i] as u128) & 0xFF; crc = crc128(poly, algorithm.refin, to_crc) ^ (crc >> 8); i += 1; } } else { while i < bytes.len() { let to_crc = ((crc >> 120) ^ bytes[i] as u128) & 0xFF; crc = crc128(poly, algorithm.refin, to_crc) ^ (crc << 8); i += 1; } } crc } const fn update_bytewise(mut crc: u128, reflect: bool, table: &[u128; 256], bytes: &[u8]) -> u128 { let mut i = 0; if reflect { while i < bytes.len() { let table_index = ((crc ^ bytes[i] as u128) & 0xFF) as usize; crc = table[table_index] ^ (crc >> 8); i += 1; } } else { while i < bytes.len() { let table_index = (((crc >> 120) ^ bytes[i] as u128) & 0xFF) as usize; crc = table[table_index] ^ (crc << 8); i += 1; } } crc } const fn update_slice16( mut crc: u128, reflect: bool, table: &[[u128; 256]; 16], bytes: &[u8], ) -> u128 { let mut i = 0; let len = bytes.len(); if reflect { while i + 16 <= len { 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[15][current0 as usize] ^ table[14][current1 as usize] ^ table[13][current2 as usize] ^ table[12][current3 as usize] ^ table[11][current4 as usize] ^ table[10][current5 as usize] ^ table[9][current6 as usize] ^ table[8][current7 as usize] ^ table[7][current8 as usize] ^ table[6][current9 as usize] ^ table[5][current10 as usize] ^ table[4][current11 as usize] ^ table[3][current12 as usize] ^ table[2][current13 as usize] ^ table[1][current14 as usize] ^ table[0][current15 as usize]; i += 16; } 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 + 16 <= len { 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[15][current0 as usize] ^ table[14][current1 as usize] ^ table[13][current2 as usize] ^ table[12][current3 as usize] ^ table[11][current4 as usize] ^ table[10][current5 as usize] ^ table[9][current6 as usize] ^ table[8][current7 as usize] ^ table[7][current8 as usize] ^ table[6][current9 as usize] ^ table[5][current10 as usize] ^ table[4][current11 as usize] ^ table[3][current12 as usize] ^ table[2][current13 as usize] ^ table[1][current14 as usize] ^ table[0][current15 as usize]; i += 16; } while i < len { let table_index = (((crc >> 120) ^ bytes[i] as u128) & 0xFF) as usize; crc = table[0][table_index] ^ (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 = 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::>::new(alg); let crc_nolookup = Crc::::new(alg); let expected = Crc::>::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); } } } } } crc-3.2.1/src/crc16/bytewise.rs000064400000000000000000000032471046102023000143120ustar 00000000000000use crate::crc16::{finalize, init, update_bytewise}; use crate::table::crc16_table; use crate::*; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let table = crc16_table(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data: [table], } } 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_bytewise(crc, self.algorithm.refin, &self.data[0], bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u16, Table<1>> { const fn new(crc: &'a Crc>, value: u16) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc16/nolookup.rs000064400000000000000000000026601046102023000143230ustar 00000000000000use crate::crc16::{finalize, init, update_nolookup}; use crate::*; impl Crc { pub const fn new(algorithm: &'static Algorithm) -> Self { Self { algorithm, data: [], } } 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_nolookup(crc, self.algorithm, bytes) } pub const fn digest(&self) -> Digest { 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 { let value = init(self.algorithm, initial); Digest::new(self, value) } } impl<'a> Digest<'a, u16, NoTable> { const fn new(crc: &'a Crc, value: u16) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc16/slice16.rs000064400000000000000000000032171046102023000137220ustar 00000000000000use crate::crc16::{finalize, init, update_slice16}; use crate::table::crc16_table_slice_16; use crate::*; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let data = crc16_table_slice_16(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data } } 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_slice16(crc, self.algorithm.refin, &self.data, bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u16, Table<16>> { const fn new(crc: &'a Crc>, value: u16) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc16.rs000064400000000000000000000155741046102023000124650ustar 00000000000000use crate::util::crc16; use crc_catalog::Algorithm; mod bytewise; mod nolookup; mod slice16; const fn init(algorithm: &Algorithm, initial: u16) -> u16 { if algorithm.refin { initial.reverse_bits() >> (16u8 - algorithm.width) } else { initial << (16u8 - algorithm.width) } } const fn finalize(algorithm: &Algorithm, 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_nolookup(mut crc: u16, algorithm: &Algorithm, bytes: &[u8]) -> u16 { let poly = if algorithm.refin { let poly = algorithm.poly.reverse_bits(); poly >> (16u8 - algorithm.width) } else { algorithm.poly << (16u8 - algorithm.width) }; let mut i = 0; if algorithm.refin { while i < bytes.len() { let to_crc = (crc ^ bytes[i] as u16) & 0xFF; crc = crc16(poly, algorithm.refin, to_crc) ^ (crc >> 8); i += 1; } } else { while i < bytes.len() { let to_crc = ((crc >> 8) ^ bytes[i] as u16) & 0xFF; crc = crc16(poly, algorithm.refin, to_crc) ^ (crc << 8); i += 1; } } crc } const fn update_bytewise(mut crc: u16, reflect: bool, table: &[u16; 256], bytes: &[u8]) -> u16 { let mut i = 0; if reflect { while i < bytes.len() { let table_index = ((crc ^ bytes[i] as u16) & 0xFF) as usize; crc = table[table_index] ^ (crc >> 8); i += 1; } } else { while i < bytes.len() { let table_index = (((crc >> 8) ^ bytes[i] as u16) & 0xFF) as usize; crc = table[table_index] ^ (crc << 8); i += 1; } } crc } const fn update_slice16( mut crc: u16, reflect: bool, table: &[[u16; 256]; 16], bytes: &[u8], ) -> u16 { let len = bytes.len(); let mut i = 0; if reflect { while i + 16 <= len { 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]; i += 16; } 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 + 16 <= len { 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; } while i < len { let table_index = (((crc >> 8) ^ bytes[i] as u16) & 0xFF) as usize; crc = table[0][table_index] ^ (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 = 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::>::new(alg); let crc_nolookup = Crc::::new(alg); let expected = Crc::>::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); } } } } } crc-3.2.1/src/crc32/bytewise.rs000064400000000000000000000032501046102023000143020ustar 00000000000000use crate::table::crc32_table; use crate::*; use crate::crc32::{finalize, init, update_bytewise}; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let table = crc32_table(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data: [table], } } 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_bytewise(crc, self.algorithm.refin, &self.data[0], bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u32, Table<1>> { const fn new(crc: &'a Crc>, value: u32) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc32/nolookup.rs000064400000000000000000000026511046102023000143210ustar 00000000000000use super::{finalize, init, update_nolookup}; use crate::*; impl Crc { pub const fn new(algorithm: &'static Algorithm) -> Self { Self { algorithm, data: [], } } 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_nolookup(crc, self.algorithm, bytes) } pub const fn digest(&self) -> Digest { 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 { let value = init(self.algorithm, initial); Digest::new(self, value) } } impl<'a> Digest<'a, u32, NoTable> { const fn new(crc: &'a Crc, value: u32) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc32/slice16.rs000064400000000000000000000032111046102023000137120ustar 00000000000000use crate::table::crc32_table_slice_16; use crate::*; use super::{finalize, init, update_slice16}; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let data = crc32_table_slice_16(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data } } 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_slice16(crc, self.algorithm.refin, &self.data, bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u32, Table<16>> { const fn new(crc: &'a Crc>, value: u32) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc32.rs000064400000000000000000000167001046102023000124530ustar 00000000000000use crate::util::crc32; use crc_catalog::Algorithm; mod bytewise; mod nolookup; mod slice16; // init is shared between all impls const fn init(algorithm: &Algorithm, initial: u32) -> u32 { if algorithm.refin { initial.reverse_bits() >> (32u8 - algorithm.width) } else { initial << (32u8 - algorithm.width) } } // finalize is shared between all impls const fn finalize(algorithm: &Algorithm, 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_nolookup(mut crc: u32, algorithm: &Algorithm, bytes: &[u8]) -> u32 { let poly = if algorithm.refin { let poly = algorithm.poly.reverse_bits(); poly >> (32u8 - algorithm.width) } else { algorithm.poly << (32u8 - algorithm.width) }; let mut i = 0; if algorithm.refin { while i < bytes.len() { let to_crc = (crc ^ bytes[i] as u32) & 0xFF; crc = crc32(poly, algorithm.refin, to_crc) ^ (crc >> 8); i += 1; } } else { while i < bytes.len() { let to_crc = ((crc >> 24) ^ bytes[i] as u32) & 0xFF; crc = crc32(poly, algorithm.refin, to_crc) ^ (crc << 8); i += 1; } } crc } const fn update_bytewise(mut crc: u32, reflect: bool, table: &[u32; 256], bytes: &[u8]) -> u32 { let mut i = 0; if reflect { while i < bytes.len() { let table_index = ((crc ^ bytes[i] as u32) & 0xFF) as usize; crc = table[table_index] ^ (crc >> 8); i += 1; } } else { while i < bytes.len() { let table_index = (((crc >> 24) ^ bytes[i] as u32) & 0xFF) as usize; crc = table[table_index] ^ (crc << 8); i += 1; } } crc } const fn update_slice16( mut crc: u32, reflect: bool, table: &[[u32; 256]; 16], bytes: &[u8], ) -> u32 { let mut i = 0; if reflect { while i + 16 <= bytes.len() { let mut current_slice = [bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]; current_slice[0] ^= crc as u8; current_slice[1] ^= (crc >> 8) as u8; current_slice[2] ^= (crc >> 16) as u8; current_slice[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]; i += 16; } // Last few bytes while i < bytes.len() { let table_index = ((crc ^ bytes[i] as u32) & 0xFF) as usize; crc = table[0][table_index] ^ (crc >> 8); i += 1; } } else { while i + 16 <= bytes.len() { let mut current_slice = [bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]; current_slice[0] ^= (crc >> 24) as u8; current_slice[1] ^= (crc >> 16) as u8; current_slice[2] ^= (crc >> 8) as u8; current_slice[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; } // Last few bytes while i < bytes.len() { let table_index = (((crc >> 24) ^ bytes[i] as u32) & 0xFF) as usize; crc = table[0][table_index] ^ (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 = 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::>::new(alg); let crc_nolookup = Crc::::new(alg); let expected = Crc::>::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); } } } } } crc-3.2.1/src/crc64/bytewise.rs000064400000000000000000000032411046102023000143070ustar 00000000000000use crate::table::crc64_table; use crate::*; use super::{finalize, init, update_bytewise}; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let table = crc64_table(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data: [table], } } 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_bytewise(crc, self.algorithm.refin, &self.data[0], bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u64, Table<1>> { const fn new(crc: &'a Crc>, value: u64) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc64/nolookup.rs000064400000000000000000000026511046102023000143260ustar 00000000000000use super::{finalize, init, update_nolookup}; use crate::*; impl Crc { pub const fn new(algorithm: &'static Algorithm) -> Self { Self { algorithm, data: [], } } 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_nolookup(crc, self.algorithm, bytes) } pub const fn digest(&self) -> Digest { 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 { let value = init(self.algorithm, initial); Digest::new(self, value) } } impl<'a> Digest<'a, u64, NoTable> { const fn new(crc: &'a Crc, value: u64) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc64/slice16.rs000064400000000000000000000032111046102023000137170ustar 00000000000000use crate::table::crc64_table_slice_16; use crate::*; use super::{finalize, init, update_slice16}; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let data = crc64_table_slice_16(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data } } 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_slice16(crc, self.algorithm.refin, &self.data, bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u64, Table<16>> { const fn new(crc: &'a Crc>, value: u64) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc64.rs000064400000000000000000000171041046102023000124570ustar 00000000000000use crate::util::crc64; use crc_catalog::Algorithm; mod bytewise; mod nolookup; mod slice16; const fn init(algorithm: &Algorithm, initial: u64) -> u64 { if algorithm.refin { initial.reverse_bits() >> (64u8 - algorithm.width) } else { initial << (64u8 - algorithm.width) } } const fn finalize(algorithm: &Algorithm, 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_nolookup(mut crc: u64, algorithm: &Algorithm, bytes: &[u8]) -> u64 { let poly = if algorithm.refin { let poly = algorithm.poly.reverse_bits(); poly >> (64u8 - algorithm.width) } else { algorithm.poly << (64u8 - algorithm.width) }; let mut i = 0; if algorithm.refin { while i < bytes.len() { let to_crc = (crc ^ bytes[i] as u64) & 0xFF; crc = crc64(poly, algorithm.refin, to_crc) ^ (crc >> 8); i += 1; } } else { while i < bytes.len() { let to_crc = ((crc >> 56) ^ bytes[i] as u64) & 0xFF; crc = crc64(poly, algorithm.refin, to_crc) ^ (crc << 8); i += 1; } } crc } const fn update_bytewise(mut crc: u64, reflect: bool, table: &[u64; 256], bytes: &[u8]) -> u64 { let mut i = 0; let len = bytes.len(); if reflect { while i < len { let table_index = ((crc ^ bytes[i] as u64) & 0xFF) as usize; crc = table[table_index] ^ (crc >> 8); i += 1; } } else { while i < len { let table_index = (((crc >> 56) ^ bytes[i] as u64) & 0xFF) as usize; crc = table[table_index] ^ (crc << 8); i += 1; } } crc } const fn update_slice16( mut crc: u64, reflect: bool, table: &[[u64; 256]; 16], bytes: &[u8], ) -> u64 { let mut i = 0; let len = bytes.len(); if reflect { while i + 16 <= len { 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]; i += 16; } 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 + 16 <= len { 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; } while i < len { let table_index = (((crc >> 56) ^ bytes[i] as u64) & 0xFF) as usize; crc = table[0][table_index] ^ (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 = 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::>::new(alg); let crc_nolookup = Crc::::new(alg); let expected = Crc::>::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); } } } } } crc-3.2.1/src/crc8/bytewise.rs000064400000000000000000000032011046102023000142210ustar 00000000000000use crate::crc8::{finalize, init, update_bytewise}; use crate::table::crc8_table; use crate::*; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let table = crc8_table(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data: [table], } } 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_bytewise(crc, &self.data[0], bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u8, Table<1>> { const fn new(crc: &'a Crc>, value: u8) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc8/nolookup.rs000064400000000000000000000026431046102023000142450ustar 00000000000000use crate::crc8::{finalize, init, update_nolookup}; use crate::*; impl Crc { pub const fn new(algorithm: &'static Algorithm) -> Self { Self { algorithm, data: [], } } 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_nolookup(crc, self.algorithm, bytes) } pub const fn digest(&self) -> Digest { 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 { let value = init(self.algorithm, initial); Digest::new(self, value) } } impl<'a> Digest<'a, u8, NoTable> { const fn new(crc: &'a Crc, value: u8) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc8/slice16.rs000064400000000000000000000031511046102023000136400ustar 00000000000000use crate::crc8::{finalize, init, update_slice16}; use crate::table::crc8_table_slice_16; use crate::*; impl Crc> { pub const fn new(algorithm: &'static Algorithm) -> Self { let data = crc8_table_slice_16(algorithm.width, algorithm.poly, algorithm.refin); Self { algorithm, data } } 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_slice16(crc, &self.data, bytes) } pub const fn digest(&self) -> Digest> { 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> { let value = init(self.algorithm, initial); Digest::new(self, value) } pub const fn table(&self) -> & as Implementation>::Data { &self.data } } impl<'a> Digest<'a, u8, Table<16>> { const fn new(crc: &'a Crc>, value: u8) -> Self { Digest { crc, value } } pub 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) } } crc-3.2.1/src/crc8.rs000064400000000000000000000113621046102023000123750ustar 00000000000000use crate::util::crc8; use crc_catalog::Algorithm; mod bytewise; mod nolookup; mod slice16; const fn init(algorithm: &Algorithm, initial: u8) -> u8 { if algorithm.refin { initial.reverse_bits() >> (8u8 - algorithm.width) } else { initial << (8u8 - algorithm.width) } } const fn finalize(algorithm: &Algorithm, 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_nolookup(mut crc: u8, algorithm: &Algorithm, bytes: &[u8]) -> u8 { let poly = if algorithm.refin { let poly = algorithm.poly.reverse_bits(); poly >> (8u8 - algorithm.width) } else { algorithm.poly << (8u8 - algorithm.width) }; let mut i = 0; while i < bytes.len() { crc = crc8(poly, algorithm.refin, crc ^ bytes[i]); i += 1; } crc } const fn update_bytewise(mut crc: u8, table: &[u8; 256], bytes: &[u8]) -> u8 { let mut i = 0; while i < bytes.len() { crc = table[(crc ^ bytes[i]) as usize]; i += 1; } crc } const fn update_slice16(mut crc: u8, table: &[[u8; 256]; 16], bytes: &[u8]) -> u8 { let len = bytes.len(); let mut i = 0; 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; } while i < len { crc = table[0][(crc ^ bytes[i]) as usize]; 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 = 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::>::new(alg); let crc_nolookup = Crc::::new(alg); let expected = Crc::>::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); } } } } } crc-3.2.1/src/lib.rs000064400000000000000000000041021046102023000122760ustar 00000000000000//! # crc //! Rust implementation of CRC. //! //! ### Examples //! Using a well-known algorithm: //! ```rust //! const X25: crc::Crc = crc::Crc::::new(&crc::CRC_16_IBM_SDLC); //! assert_eq!(X25.checksum(b"123456789"), 0x906e); //! ``` //! //! Using a custom algorithm: //! ```rust //! const CUSTOM_ALG: crc::Algorithm = crc::Algorithm { //! width: 16, //! poly: 0x8005, //! init: 0xffff, //! refin: false, //! refout: false, //! xorout: 0x0000, //! check: 0xaee7, //! residue: 0x0000 //! }; //! let crc = crc::Crc::::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: private::Sealed { /// Associated data necessary for the implementation (e.g. lookup tables). type Data; } /// 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 {} /// An implementation of the CRC algorithm with no lookup table. pub type NoTable = Table<0>; type DefaultImpl = Table<1>; impl Implementation for Table { type Data = [[W; 256]; L]; } mod private { pub trait Sealed {} impl Sealed for super::Table {} } /// Crc instance with a specific width, algorithm, and implementation. #[derive(Clone)] pub struct Crc { pub algorithm: &'static Algorithm, data: I::Data, } #[derive(Clone)] pub struct Digest<'a, W: Width, I: Implementation = DefaultImpl> { crc: &'a Crc, value: W, } #[cfg(test)] mod test { use super::{Crc, CRC_32_ISCSI}; #[test] fn test_clone() { const CRC: Crc = Crc::::new(&CRC_32_ISCSI); let _crc = CRC.clone(); } } crc-3.2.1/src/table.rs000064400000000000000000000136731046102023000126340ustar 00000000000000use crate::util::*; pub(crate) const fn crc8_table(width: u8, poly: u8, reflect: bool) -> [u8; 256] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (8u8 - width) } else { poly << (8u8 - width) }; let mut table = [0u8; 256]; let mut i = 0; while i < table.len() { table[i] = crc8(poly, reflect, i as u8); i += 1; } table } pub(crate) const fn crc8_table_slice_16(width: u8, poly: u8, reflect: bool) -> [[u8; 256]; 16] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (8u8 - width) } else { poly << (8u8 - width) }; let mut table = [[0u8; 256]; 16]; let mut i = 0; while i < 256 { table[0][i] = crc8(poly, reflect, i as u8); i += 1; } let mut i = 0; while i < 256 { let mut e = 1; while e < 16 { 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(width: u8, poly: u16, reflect: bool) -> [u16; 256] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (16u8 - width) } else { poly << (16u8 - width) }; let mut table = [0u16; 256]; let mut i = 0; while i < table.len() { table[i] = crc16(poly, reflect, i as u16); i += 1; } table } pub(crate) const fn crc16_table_slice_16(width: u8, poly: u16, reflect: bool) -> [[u16; 256]; 16] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (16u8 - width) } else { poly << (16u8 - width) }; let mut table = [[0u16; 256]; 16]; let mut i = 0; while i < 256 { table[0][i] = crc16(poly, reflect, i as u16); i += 1; } let mut i = 0; while i < 256 { let mut e = 1; while e < 16 { 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(width: u8, poly: u32, reflect: bool) -> [u32; 256] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (32u8 - width) } else { poly << (32u8 - width) }; let mut table = [0u32; 256]; let mut i = 0; while i < 256 { table[i] = crc32(poly, reflect, i as u32); i += 1; } table } pub(crate) const fn crc32_table_slice_16(width: u8, poly: u32, reflect: bool) -> [[u32; 256]; 16] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (32u8 - width) } else { poly << (32u8 - width) }; let mut table = [[0u32; 256]; 16]; let mut i = 0; while i < 256 { table[0][i] = crc32(poly, reflect, i as u32); i += 1; } let mut i = 0; while i < 256 { let mut e = 1; while e < 16 { 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(width: u8, poly: u64, reflect: bool) -> [u64; 256] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (64u8 - width) } else { poly << (64u8 - width) }; let mut table = [0u64; 256]; let mut i = 0; while i < table.len() { table[i] = crc64(poly, reflect, i as u64); i += 1; } table } pub(crate) const fn crc64_table_slice_16(width: u8, poly: u64, reflect: bool) -> [[u64; 256]; 16] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (64u8 - width) } else { poly << (64u8 - width) }; let mut table = [[0u64; 256]; 16]; let mut i = 0; while i < 256 { table[0][i] = crc64(poly, reflect, i as u64); i += 1; } let mut i = 0; while i < 256 { let mut e = 1; while e < 16 { 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(width: u8, poly: u128, reflect: bool) -> [u128; 256] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (128u8 - width) } else { poly << (128u8 - width) }; let mut table = [0u128; 256]; let mut i = 0; while i < table.len() { table[i] = crc128(poly, reflect, i as u128); i += 1; } table } pub(crate) const fn crc128_table_slice_16( width: u8, poly: u128, reflect: bool, ) -> [[u128; 256]; 16] { let poly = if reflect { let poly = poly.reverse_bits(); poly >> (128u8 - width) } else { poly << (128u8 - width) }; let mut table = [[0u128; 256]; 16]; let mut i = 0; while i < 256 { table[0][i] = crc128(poly, reflect, i as u128); i += 1; } let mut i = 0; while i < 256 { let mut e = 1; while e < 16 { 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 } crc-3.2.1/src/util.rs000064400000000000000000000040061046102023000125100ustar 00000000000000pub(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 } crc-3.2.1/tests/crc.rs000064400000000000000000000101431046102023000126540ustar 00000000000000use 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::::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::::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::::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::::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::::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()); } } crc-3.2.1/tests/test_under_all_feature_combinations.sh000075500000000000000000000006711046102023000213670ustar 00000000000000#!/bin/sh -ex cargo test cargo test --no-default-features cargo test --features slice16-mem-limit cargo test --features bytewise-mem-limit cargo test --features no-table-mem-limit cargo test --features bytewise-mem-limit,slice16-mem-limit cargo test --features no-table-mem-limit,bytewise-mem-limit cargo test --features no-table-mem-limit,slice16-mem-limit cargo test --features no-table-mem-limit,bytewise-mem-limit,slice16-mem-limit