diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
commit | a0aa2307322cd47bbf416810ac0292925e03be87 (patch) | |
tree | 37076262a026c4b48c8a0e84f44ff9187556ca35 /rust/vendor/crc | |
parent | Initial commit. (diff) | |
download | suricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip |
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/vendor/crc')
-rw-r--r-- | rust/vendor/crc/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | rust/vendor/crc/Cargo.toml | 28 | ||||
-rw-r--r-- | rust/vendor/crc/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | rust/vendor/crc/LICENSE-MIT | 21 | ||||
-rw-r--r-- | rust/vendor/crc/README.md | 113 | ||||
-rw-r--r-- | rust/vendor/crc/benches/bench.rs | 30 | ||||
-rw-r--r-- | rust/vendor/crc/build.rs | 54 | ||||
-rw-r--r-- | rust/vendor/crc/src/crc16.rs | 77 | ||||
-rw-r--r-- | rust/vendor/crc/src/crc32.rs | 81 | ||||
-rw-r--r-- | rust/vendor/crc/src/crc64.rs | 77 | ||||
-rw-r--r-- | rust/vendor/crc/src/lib.rs | 70 | ||||
-rw-r--r-- | rust/vendor/crc/src/util.rs | 47 | ||||
-rw-r--r-- | rust/vendor/crc/tests/crc.rs | 126 | ||||
-rw-r--r-- | rust/vendor/crc/tests/hash.rs | 26 |
14 files changed, 952 insertions, 0 deletions
diff --git a/rust/vendor/crc/.cargo-checksum.json b/rust/vendor/crc/.cargo-checksum.json new file mode 100644 index 0000000..1ab2c34 --- /dev/null +++ b/rust/vendor/crc/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"6c769d8d6909a8ce7e1d6e5cd3a6867849bd369eef332f67b4683c7788de8793","LICENSE-APACHE":"470355a7eed93fcc4281ec2e0f82ca3b94e7af1e4d83629f91de8cfac34d750e","LICENSE-MIT":"3488679340a49ecc34d342c4009d2dabf76f4a21f12aec2ca99b15805d656544","README.md":"1be69fa3aedd3ce9e25a13e5e457cb6f310ef1473acd17e1f10e6821cfc1b579","benches/bench.rs":"6e129032ad3c596840d0a88f369b0a86efe560b2da9c6db338e80e17876741e5","build.rs":"c5935e7aa7e6c2a4df4f34d8aedde786c24be02ddb5ede8b1125b9a108c92e80","src/crc16.rs":"b50e797e592c51482f19b413fc7f5b6e533fc34c550f3b93b769fdfbf52e809d","src/crc32.rs":"509b3b4138521c8481863a0a8421f6ffccf97d1aa9a7cd5c9221b7ebda26da66","src/crc64.rs":"fd59ca0336d798bc4b14224858ca27ce372274f21951950b38eacf36c59bd863","src/lib.rs":"8e84d94767283cb13d56ca4fd5b0aa4b8284e8dd3e97667b06af912d58db328c","src/util.rs":"805670b0d8ebe191d36ae67d105490c106581e61d89777b1afffc39efbecd3c5","tests/crc.rs":"92c8d0bb3fbb914e7832270e5a8f6d32e7b047e952bb76c21de0919b43184fd7","tests/hash.rs":"d3b314a4e50dd7669c603c0c2fdc51c47f04293c0747406dbe79c0f72a58be7d"},"package":"d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"}
\ No newline at end of file diff --git a/rust/vendor/crc/Cargo.toml b/rust/vendor/crc/Cargo.toml new file mode 100644 index 0000000..979000d --- /dev/null +++ b/rust/vendor/crc/Cargo.toml @@ -0,0 +1,28 @@ +# 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 <code@mrhooray.com>"] +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 = [] diff --git a/rust/vendor/crc/LICENSE-APACHE b/rust/vendor/crc/LICENSE-APACHE new file mode 100644 index 0000000..d368c85 --- /dev/null +++ b/rust/vendor/crc/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0 January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/rust/vendor/crc/LICENSE-MIT b/rust/vendor/crc/LICENSE-MIT new file mode 100644 index 0000000..ea21c22 --- /dev/null +++ b/rust/vendor/crc/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 crc-rs Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/rust/vendor/crc/README.md b/rust/vendor/crc/README.md new file mode 100644 index 0000000..551fbaa --- /dev/null +++ b/rust/vendor/crc/README.md @@ -0,0 +1,113 @@ +# 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. diff --git a/rust/vendor/crc/benches/bench.rs b/rust/vendor/crc/benches/bench.rs new file mode 100644 index 0000000..d2a3bf4 --- /dev/null +++ b/rust/vendor/crc/benches/bench.rs @@ -0,0 +1,30 @@ +#![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)); +} diff --git a/rust/vendor/crc/build.rs b/rust/vendor/crc/build.rs new file mode 100644 index 0000000..3c1fcc3 --- /dev/null +++ b/rust/vendor/crc/build.rs @@ -0,0 +1,54 @@ +extern 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(); +} diff --git a/rust/vendor/crc/src/crc16.rs b/rust/vendor/crc/src/crc16.rs new file mode 100644 index 0000000..f933dd5 --- /dev/null +++ b/rust/vendor/crc/src/crc16.rs @@ -0,0 +1,77 @@ +#[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 + } +} diff --git a/rust/vendor/crc/src/crc32.rs b/rust/vendor/crc/src/crc32.rs new file mode 100644 index 0000000..6093d3b --- /dev/null +++ b/rust/vendor/crc/src/crc32.rs @@ -0,0 +1,81 @@ +#[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 + } +} diff --git a/rust/vendor/crc/src/crc64.rs b/rust/vendor/crc/src/crc64.rs new file mode 100644 index 0000000..8404030 --- /dev/null +++ b/rust/vendor/crc/src/crc64.rs @@ -0,0 +1,77 @@ +#[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() + } +} diff --git a/rust/vendor/crc/src/lib.rs b/rust/vendor/crc/src/lib.rs new file mode 100644 index 0000000..c803d7c --- /dev/null +++ b/rust/vendor/crc/src/lib.rs @@ -0,0 +1,70 @@ +//! # 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; diff --git a/rust/vendor/crc/src/util.rs b/rust/vendor/crc/src/util.rs new file mode 100644 index 0000000..f5b2f29 --- /dev/null +++ b/rust/vendor/crc/src/util.rs @@ -0,0 +1,47 @@ +pub 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 +} diff --git a/rust/vendor/crc/tests/crc.rs b/rust/vendor/crc/tests/crc.rs new file mode 100644 index 0000000..e270796 --- /dev/null +++ b/rust/vendor/crc/tests/crc.rs @@ -0,0 +1,126 @@ +extern 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); + } +} diff --git a/rust/vendor/crc/tests/hash.rs b/rust/vendor/crc/tests/hash.rs new file mode 100644 index 0000000..d4c9a96 --- /dev/null +++ b/rust/vendor/crc/tests/hash.rs @@ -0,0 +1,26 @@ +extern 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()); + } + +} |