crc-1.8.1/.gitignore01006440000765000002400000000022132106275600012473 0ustar0000000000000000target Cargo.lock crc-1.8.1/.travis.yml01006440000765000002400000000060132106275600012616 0ustar0000000000000000language: rust rust: - nightly - stable crc-1.8.1/benches/bench.rs01006440000765000002400000001316132106275600013546 0ustar0000000000000000#![feature(test)] extern crate crc; extern crate test; use crc::{crc32, crc64}; use test::Bencher; #[bench] fn bench_crc32_make_table(b: &mut Bencher) { b.iter(|| crc32::make_table(crc32::IEEE)); } #[bench] fn bench_crc32_update_megabytes(b: &mut Bencher) { let table = crc32::make_table(crc32::IEEE); let bytes = Box::new([0u8; 1_000_000]); b.iter(|| crc32::update(0, &table, &*bytes)); } #[bench] fn bench_crc64_make_table(b: &mut Bencher) { b.iter(|| crc64::make_table(crc64::ECMA)); } #[bench] fn bench_crc64_update_megabytes(b: &mut Bencher) { let table = crc64::make_table(crc64::ECMA); let bytes = Box::new([0u8; 1_000_000]); b.iter(|| crc64::update(0, &table, &*bytes)); } crc-1.8.1/build.rs01006440000765000002400000003115132675651700012170 0ustar0000000000000000extern crate build_const; include!("src/util.rs"); #[allow(non_snake_case)] fn create_constants() { let mut crc16 = build_const::ConstWriter::for_build("crc16_constants") .unwrap() .finish_dependencies(); let X25: u16 = 0x8408; crc16.add_value("X25", "u16", X25); crc16.add_array("X25_TABLE", "u16", &make_table_crc16(X25)); let USB: u16 = 0xa001; crc16.add_value("USB", "u16", USB); crc16.add_array("USB_TABLE", "u16", &make_table_crc16(USB)); crc16.finish(); let mut crc32 = build_const::ConstWriter::for_build("crc32_constants") .unwrap() .finish_dependencies(); let CASTAGNOLI: u32 = 0x82f63b78; crc32.add_value("CASTAGNOLI", "u32", CASTAGNOLI); crc32.add_array("CASTAGNOLI_TABLE", "u32", &make_table_crc32(CASTAGNOLI)); let IEEE: u32 = 0xedb88320; crc32.add_value("IEEE", "u32", IEEE); crc32.add_array("IEEE_TABLE", "u32", &make_table_crc32(IEEE)); let KOOPMAN: u32 = 0xeb31d82e; crc32.add_value("KOOPMAN", "u32", KOOPMAN); crc32.add_array("KOOPMAN_TABLE", "u32", &make_table_crc32(KOOPMAN)); crc32.finish(); let mut crc64 = build_const::ConstWriter::for_build("crc64_constants") .unwrap() .finish_dependencies(); let ECMA: u64 = 0xc96c5795d7870f42; crc64.add_value("ECMA", "u64", ECMA); crc64.add_array("ECMA_TABLE", "u64", &make_table_crc64(ECMA)); let ISO: u64 = 0xd800000000000000; crc64.add_value("ISO", "u64", ISO); crc64.add_array("ISO_TABLE", "u64", &make_table_crc64(ISO)); crc64.finish(); } fn main() { create_constants(); } crc-1.8.1/Cargo.toml.orig01006440000765000002400000000701132675667470013423 0ustar0000000000000000[package] name = "crc" version = "1.8.1" authors = ["Rui Hu "] description = "Rust implementation of CRC(16, 32, 64) with support of various standards" keywords = ["crc", "crc16", "crc32", "crc64", "hash"] readme = "README.md" repository = "https://github.com/mrhooray/crc-rs.git" documentation = "https://docs.rs/crc" license = "MIT OR Apache-2.0" [build-dependencies] build_const = "0.2" [features] std = [] default = ["std"] crc-1.8.1/Cargo.toml0000644000000017240006633 0ustar00# 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 believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "crc" version = "1.8.1" authors = ["Rui Hu "] description = "Rust implementation of CRC(16, 32, 64) with support of various standards" documentation = "https://docs.rs/crc" readme = "README.md" keywords = ["crc", "crc16", "crc32", "crc64", "hash"] license = "MIT OR Apache-2.0" repository = "https://github.com/mrhooray/crc-rs.git" [build-dependencies.build_const] version = "0.2" [features] default = ["std"] std = [] crc-1.8.1/LICENSE-APACHE01006440000765000002400000025136132106275600012444 0ustar0000000000000000 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-1.8.1/LICENSE-MIT01006440000765000002400000002062132106275720012150 0ustar0000000000000000MIT 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-1.8.1/README.md01006440000765000002400000006334132675657470012022 0ustar0000000000000000# crc [![Build Status](https://travis-ci.org/mrhooray/crc-rs.svg?branch=master)](https://travis-ci.org/mrhooray/crc-rs) > Rust implementation of CRC(16, 32, 64) with support of various standards * [Crate](https://crates.io/crates/crc) * [Documentation](https://docs.rs/crc/) * [Usage](#usage) * [Benchmark](#benchmark) * [License](#license) ## Usage Add `crc` to `Cargo.toml` ```toml [dependencies] crc = "^1.0.0" ``` or ```toml [dependencies.crc] git = "https://github.com/mrhooray/crc-rs" ``` Add this to crate root ```rust extern crate crc; ``` ### Compute CRC16 ```rust use crc::{crc16, Hasher16}; assert_eq!(crc16::checksum_x25(b"123456789"), 0x906e); assert_eq!(crc16::checksum_usb(b"123456789"), 0xb4c8); // use provided or custom polynomial let mut digest = crc16::Digest::new(crc16::X25); digest.write(b"123456789"); assert_eq!(digest.sum16(), 0x906e); // with initial let mut digest = crc16::Digest::new_with_initial(crc16::X25, 0u16); digest.write(b"123456789"); assert_eq!(digest.sum16(), 0x906e); ``` ### Compute CRC32 ```rust use crc::{crc32, Hasher32}; // CRC-32-IEEE being the most commonly used one assert_eq!(crc32::checksum_ieee(b"123456789"), 0xcbf43926); assert_eq!(crc32::checksum_castagnoli(b"123456789"), 0xe3069283); assert_eq!(crc32::checksum_koopman(b"123456789"), 0x2d3dd0ae); // use provided or custom polynomial let mut digest = crc32::Digest::new(crc32::IEEE); digest.write(b"123456789"); assert_eq!(digest.sum32(), 0xcbf43926); // with initial let mut digest = crc32::Digest::new_with_initial(crc32::IEEE, 0u32); digest.write(b"123456789"); assert_eq!(digest.sum32(), 0xcbf43926); ``` ### Compute CRC64 ```rust use crc::{crc64, Hasher64}; assert_eq!(crc64::checksum_ecma(b"123456789"), 0x995dc9bbdf1939fa); assert_eq!(crc64::checksum_iso(b"123456789"), 0xb90956c775a41001); // use provided or custom polynomial let mut digest = crc64::Digest::new(crc64::ECMA); digest.write(b"123456789"); assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); // with initial let mut digest = crc64::Digest::new_with_initial(crc64::ECMA, 0u64); digest.write(b"123456789"); assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); ``` ## Benchmark > Bencher is currently not available in Rust stable releases. `cargo bench` with 2.3 GHz Intel Core i7 results ~430MB/s throughput. [Comparison](http://create.stephan-brumme.com/crc32/) ``` cargo bench Running target/release/bench-5c82e94dab3e9c79 running 4 tests test bench_crc32_make_table ... bench: 439 ns/iter (+/- 82) test bench_crc32_update_megabytes ... bench: 2327803 ns/iter (+/- 138845) test bench_crc64_make_table ... bench: 1200 ns/iter (+/- 223) test bench_crc64_update_megabytes ... bench: 2322472 ns/iter (+/- 92870) test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured ``` ## 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-1.8.1/src/crc16.rs01006440000765000002400000003221132675651700012574 0ustar0000000000000000#[cfg(feature = "std")] use std::hash::Hasher; #[cfg(not(feature = "std"))] use core::hash::Hasher; pub use util::make_table_crc16 as make_table; include!(concat!(env!("OUT_DIR"), "/crc16_constants.rs")); pub struct Digest { table: [u16; 256], initial: u16, value: u16 } pub trait Hasher16 { fn reset(&mut self); fn write(&mut self, bytes: &[u8]); fn sum16(&self) -> u16; } pub fn update(mut value: u16, table: &[u16; 256], bytes: &[u8]) -> u16 { value = !value; for &i in bytes.iter() { value = table[((value as u8) ^ i) as usize] ^ (value >> 8) } !value } pub fn checksum_x25(bytes: &[u8]) -> u16 { return update(0, &X25_TABLE, bytes); } pub fn checksum_usb(bytes: &[u8]) -> u16 { return update(0, &USB_TABLE, bytes); } impl Digest { pub fn new(poly: u16) -> Digest { Digest { table: make_table(poly), initial: 0, value: 0 } } pub fn new_with_initial(poly: u16, initial: u16) -> Digest { Digest { table: make_table(poly), initial: initial, value: initial } } } impl Hasher16 for Digest { fn reset(&mut self) { self.value = self.initial; } fn write(&mut self, bytes: &[u8]) { self.value = update(self.value, &self.table, bytes); } fn sum16(&self) -> u16 { self.value } } /// Implementation of std::hash::Hasher so that types which #[derive(Hash)] can hash with Digest. impl Hasher for Digest { fn write(&mut self, bytes: &[u8]) { Hasher16::write(self, bytes); } fn finish(&self) -> u64 { self.sum16() as u64 } } crc-1.8.1/src/crc32.rs01006440000765000002400000003400132220163170012552 0ustar0000000000000000#[cfg(feature = "std")] use std::hash::Hasher; #[cfg(not(feature = "std"))] use core::hash::Hasher; pub use util::make_table_crc32 as make_table; include!(concat!(env!("OUT_DIR"), "/crc32_constants.rs")); pub struct Digest { table: [u32; 256], initial: u32, value: u32 } pub trait Hasher32 { fn reset(&mut self); fn write(&mut self, bytes: &[u8]); fn sum32(&self) -> u32; } pub fn update(mut value: u32, table: &[u32; 256], bytes: &[u8]) -> u32 { value = !value; for &i in bytes.iter() { value = table[((value as u8) ^ i) as usize] ^ (value >> 8) } !value } pub fn checksum_ieee(bytes: &[u8]) -> u32 { return update(0, &IEEE_TABLE, bytes); } pub fn checksum_castagnoli(bytes: &[u8]) -> u32 { return update(0, &CASTAGNOLI_TABLE, bytes); } pub fn checksum_koopman(bytes: &[u8]) -> u32 { return update(0, &KOOPMAN_TABLE, bytes); } impl Digest { pub fn new(poly: u32) -> Digest { Digest { table: make_table(poly), initial: 0, value: 0 } } pub fn new_with_initial(poly: u32, initial: u32) -> Digest { Digest { table: make_table(poly), initial: initial, value: initial } } } impl Hasher32 for Digest { fn reset(&mut self) { self.value = self.initial; } fn write(&mut self, bytes: &[u8]) { self.value = update(self.value, &self.table, bytes); } fn sum32(&self) -> u32 { self.value } } /// Implementation of std::hash::Hasher so that types which #[derive(Hash)] can hash with Digest. impl Hasher for Digest { fn write(&mut self, bytes: &[u8]) { Hasher32::write(self, bytes); } fn finish(&self) -> u64 { self.sum32() as u64 } } crc-1.8.1/src/crc64.rs01006440000765000002400000003214132220163170012562 0ustar0000000000000000#[cfg(feature = "std")] use std::hash::Hasher; #[cfg(not(feature = "std"))] use core::hash::Hasher; pub use util::make_table_crc64 as make_table; include!(concat!(env!("OUT_DIR"), "/crc64_constants.rs")); pub struct Digest { table: [u64; 256], initial: u64, value: u64 } pub trait Hasher64 { fn reset(&mut self); fn write(&mut self, bytes: &[u8]); fn sum64(&self) -> u64; } pub fn update(mut value: u64, table: &[u64; 256], bytes: &[u8]) -> u64 { value = !value; for &i in bytes.iter() { value = table[((value as u8) ^ i) as usize] ^ (value >> 8) } !value } pub fn checksum_ecma(bytes: &[u8]) -> u64 { return update(0, &ECMA_TABLE, bytes); } pub fn checksum_iso(bytes: &[u8]) -> u64 { return update(0, &ISO_TABLE, bytes); } impl Digest { pub fn new(poly: u64) -> Digest { Digest { table: make_table(poly), initial: 0, value: 0 } } pub fn new_with_initial(poly: u64, initial: u64) -> Digest { Digest { table: make_table(poly), initial: initial, value: initial } } } impl Hasher64 for Digest { fn reset(&mut self) { self.value = self.initial; } fn write(&mut self, bytes: &[u8]) { self.value = update(self.value, &self.table, bytes); } fn sum64(&self) -> u64 { self.value } } /// Implementation of std::hash::Hasher so that types which #[derive(Hash)] can hash with Digest. impl Hasher for Digest { fn write(&mut self, bytes: &[u8]) { Hasher64::write(self, bytes); } fn finish(&self) -> u64 { self.sum64() } } crc-1.8.1/src/lib.rs01006440000765000002400000004026132675657700012436 0ustar0000000000000000//! # crc //! Rust implementation of CRC(16, 32, 64) //! //! ## Usage //! ### Compute CRC16 //! ```rust //! use crc::{crc16, Hasher16}; //! //! assert_eq!(crc16::checksum_x25(b"123456789"), 0x906e); //! assert_eq!(crc16::checksum_usb(b"123456789"), 0xb4c8); //! //! // use provided or custom polynomial //! let mut digest = crc16::Digest::new(crc16::X25); //! digest.write(b"123456789"); //! assert_eq!(digest.sum16(), 0x906e); //! //! // with initial //! let mut digest = crc16::Digest::new_with_initial(crc16::X25, 0u16); //! digest.write(b"123456789"); //! assert_eq!(digest.sum16(), 0x906e); //! ``` //! //! ### Compute CRC32 //! ```rust //! use crc::{crc32, Hasher32}; //! //! // CRC-32-IEEE being the most commonly used one //! assert_eq!(crc32::checksum_ieee(b"123456789"), 0xcbf43926); //! assert_eq!(crc32::checksum_castagnoli(b"123456789"), 0xe3069283); //! assert_eq!(crc32::checksum_koopman(b"123456789"), 0x2d3dd0ae); //! //! // use provided or custom polynomial //! let mut digest = crc32::Digest::new(crc32::IEEE); //! digest.write(b"123456789"); //! assert_eq!(digest.sum32(), 0xcbf43926); //! //! // with initial //! let mut digest = crc32::Digest::new_with_initial(crc32::IEEE, 0u32); //! digest.write(b"123456789"); //! assert_eq!(digest.sum32(), 0xcbf43926); //! ``` //! //! ### Compute CRC64 //! ```rust //! use crc::{crc64, Hasher64}; //! //! assert_eq!(crc64::checksum_ecma(b"123456789"), 0x995dc9bbdf1939fa); //! assert_eq!(crc64::checksum_iso(b"123456789"), 0xb90956c775a41001); //! //! // use provided or custom polynomial //! let mut digest = crc64::Digest::new(crc64::ECMA); //! digest.write(b"123456789"); //! assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); //! //! // with initial //! let mut digest = crc64::Digest::new_with_initial(crc64::ECMA, 0u64); //! digest.write(b"123456789"); //! assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); //! ``` #![cfg_attr(not(feature = "std"), no_std)] pub mod crc16; pub mod crc32; pub mod crc64; mod util; pub use self::crc16::Hasher16; pub use self::crc32::Hasher32; pub use self::crc64::Hasher64; crc-1.8.1/src/util.rs01006440000765000002400000002064132675651700012637 0ustar0000000000000000pub fn make_table_crc16(poly: u16) -> [u16; 256] { let mut table = [0u16; 256]; for i in 0..256 { let mut value = i as u16; for _ in 0..8 { value = if (value & 1) == 1 { (value >> 1) ^ poly } else { value >> 1 } } table[i] = value; } table } pub fn make_table_crc32(poly: u32) -> [u32; 256] { let mut table = [0u32; 256]; for i in 0..256 { let mut value = i as u32; for _ in 0..8 { value = if (value & 1) == 1 { (value >> 1) ^ poly } else { value >> 1 } } table[i] = value; } table } pub fn make_table_crc64(poly: u64) -> [u64; 256] { let mut table = [0u64; 256]; for i in 0..256 { let mut value = i as u64; for _ in 0..8 { value = if (value & 1) == 1 { (value >> 1) ^ poly } else { value >> 1 } } table[i] = value; } table } crc-1.8.1/tests/crc.rs01006440000765000002400000006046132675651700013010 0ustar0000000000000000extern crate crc; mod crc16 { use crc::{crc16, Hasher16}; const X25_CHECK_VALUE: u16 = 0x906e; const USB_CHECK_VALUE: u16 = 0xb4c8; #[test] fn checksum_x25() { assert_eq!(crc16::checksum_x25(b"123456789"), X25_CHECK_VALUE) } #[test] fn checksum_usb() { assert_eq!(crc16::checksum_usb(b"123456789"), USB_CHECK_VALUE) } #[test] fn digest_x25() { verify_checksum(crc16::X25, X25_CHECK_VALUE); } #[test] fn digest_usb() { verify_checksum(crc16::USB, USB_CHECK_VALUE); } fn verify_checksum(poly: u16, check_value: u16) { let mut digest = crc16::Digest::new(poly); digest.write(b"123456789"); assert_eq!(digest.sum16(), check_value); digest.reset(); for i in 1..10 { digest.write(i.to_string().as_bytes()); } assert_eq!(digest.sum16(), check_value); } } mod crc32 { use crc::{crc32, Hasher32}; const CASTAGNOLI_CHECK_VALUE: u32 = 0xe3069283; const IEEE_CHECK_VALUE: u32 = 0xcbf43926; const KOOPMAN_CHECK_VALUE: u32 = 0x2d3dd0ae; #[test] fn checksum_castagnoli() { assert_eq!(crc32::checksum_castagnoli(b"123456789"), CASTAGNOLI_CHECK_VALUE) } #[test] fn checksum_ieee() { assert_eq!(crc32::checksum_ieee(b"123456789"), IEEE_CHECK_VALUE) } #[test] fn checksum_koopman() { assert_eq!(crc32::checksum_koopman(b"123456789"), KOOPMAN_CHECK_VALUE) } #[test] fn digest_castagnoli() { verify_checksum(crc32::CASTAGNOLI, CASTAGNOLI_CHECK_VALUE); } #[test] fn digest_ieee() { verify_checksum(crc32::IEEE, IEEE_CHECK_VALUE); } #[test] fn digest_koopman() { verify_checksum(crc32::KOOPMAN, KOOPMAN_CHECK_VALUE); } fn verify_checksum(poly: u32, check_value: u32) { let mut digest = crc32::Digest::new(poly); digest.write(b"123456789"); assert_eq!(digest.sum32(), check_value); digest.reset(); for i in 1..10 { digest.write(i.to_string().as_bytes()); } assert_eq!(digest.sum32(), check_value); } } mod crc64 { use crc::{crc64, Hasher64}; const ECMA_CHECK_VALUE: u64 = 0x995dc9bbdf1939fa; const ISO_CHECK_VALUE: u64 = 0xb90956c775a41001; #[test] fn checksum_ecma() { assert_eq!(crc64::checksum_ecma(b"123456789"), ECMA_CHECK_VALUE) } #[test] fn checksum_iso() { assert_eq!(crc64::checksum_iso(b"123456789"), ISO_CHECK_VALUE) } #[test] fn digest_ecma() { verify_checksum(crc64::ECMA, ECMA_CHECK_VALUE); } #[test] fn digest_iso() { verify_checksum(crc64::ISO, ISO_CHECK_VALUE); } fn verify_checksum(poly: u64, check_value: u64) { let mut digest = crc64::Digest::new(poly); digest.write(b"123456789"); assert_eq!(digest.sum64(), check_value); digest.reset(); for i in 1..10 { digest.write(i.to_string().as_bytes()); } assert_eq!(digest.sum64(), check_value); } } crc-1.8.1/tests/hash.rs01006440000765000002400000001205132106275600013142 0ustar0000000000000000extern crate crc; mod hasher { use crc::{crc32, crc64}; use std::hash::{Hash, Hasher}; #[derive(Hash)] struct Person(&'static str, u8); #[test] fn checksum_hashcrc32() { let person = Person("John Smith", 34); let mut hasher = crc32::Digest::new(crc32::IEEE); person.hash(&mut hasher); assert_eq!(467823795u64, hasher.finish()); } #[test] fn checksum_hashcrc64() { let person = Person("John Smith", 34); let mut hasher = crc64::Digest::new(crc64::ECMA); person.hash(&mut hasher); assert_eq!(3567258626315136489u64, hasher.finish()); } }