diff --git a/Cargo.toml b/Cargo.toml index d9d6c1d..fde20ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,12 +10,11 @@ documentation = "http://mrhooray.github.io/crc-rs/crc/index.html" license = "MIT" [dependencies] - -[dependencies.build_const] -version = "0.2" -default-features = false +util = { path = "./src/util" } +build_const = { version = "0.2", default-features = false } [build-dependencies] +util = { path = "./src/util" } build_const = "0.2" [features] diff --git a/build.rs b/build.rs index 45c1627..a4a1d12 100644 --- a/build.rs +++ b/build.rs @@ -1,38 +1,6 @@ - +extern crate util; extern crate build_const; -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 -} - #[allow(non_snake_case)] fn create_constants() { let mut crc32 = build_const::ConstWriter::for_build("crc32_constants") @@ -40,15 +8,15 @@ fn create_constants() { .finish_dependencies(); let CASTAGNOLI: u32 = 0x82f63b78; crc32.add_value("CASTAGNOLI", "u32", CASTAGNOLI); - crc32.add_array("CASTAGNOLI_TABLE", "u32", &make_table_crc32(CASTAGNOLI)); + crc32.add_array("CASTAGNOLI_TABLE", "u32", &util::make_table_crc32(CASTAGNOLI)); let IEEE: u32 = 0xedb88320; crc32.add_value("IEEE", "u32", IEEE); - crc32.add_array("IEEE_TABLE", "u32", &make_table_crc32(IEEE)); + crc32.add_array("IEEE_TABLE", "u32", &util::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.add_array("KOOPMAN_TABLE", "u32", &util::make_table_crc32(KOOPMAN)); crc32.finish(); @@ -58,11 +26,11 @@ fn create_constants() { let ECMA: u64 = 0xc96c5795d7870f42; crc64.add_value("ECMA", "u64", ECMA); - crc64.add_array("ECMA_TABLE", "u64", &make_table_crc64(ECMA)); + crc64.add_array("ECMA_TABLE", "u64", &util::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.add_array("ISO_TABLE", "u64", &util::make_table_crc64(ISO)); crc64.finish(); } @@ -70,4 +38,3 @@ fn create_constants() { fn main() { create_constants(); } - diff --git a/src/crc32.rs b/src/crc32.rs index 48302fc..a1de5c7 100644 --- a/src/crc32.rs +++ b/src/crc32.rs @@ -3,6 +3,8 @@ use std::hash::Hasher; #[cfg(not(feature = "std"))] use core::hash::Hasher; +pub use util::make_table_crc32 as make_table; + build_const!("crc32_constants"); pub struct Digest { @@ -17,22 +19,6 @@ pub trait Hasher32 { fn sum32(&self) -> u32; } -pub fn make_table(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 update(mut value: u32, table: &[u32; 256], bytes: &[u8]) -> u32 { value = !value; for &i in bytes.iter() { diff --git a/src/crc64.rs b/src/crc64.rs index 9df484b..6d9b894 100644 --- a/src/crc64.rs +++ b/src/crc64.rs @@ -3,6 +3,8 @@ use std::hash::Hasher; #[cfg(not(feature = "std"))] use core::hash::Hasher; +pub use util::make_table_crc64 as make_table; + build_const!("crc64_constants"); pub struct Digest { @@ -17,22 +19,6 @@ pub trait Hasher64 { fn sum64(&self) -> u64; } -pub fn make_table(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 -} - pub fn update(mut value: u64, table: &[u64; 256], bytes: &[u8]) -> u64 { value = !value; for &i in bytes.iter() { diff --git a/src/lib.rs b/src/lib.rs index 2dbe8a6..c04643d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,7 @@ #[macro_use] extern crate build_const; +extern crate util; pub mod crc32; pub mod crc64; diff --git a/src/util/Cargo.toml b/src/util/Cargo.toml new file mode 100644 index 0000000..c30a505 --- /dev/null +++ b/src/util/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "util" +version = "0.1.0" + +[lib] +name = "util" +path = "lib.rs" diff --git a/src/util/lib.rs b/src/util/lib.rs new file mode 100644 index 0000000..ec9d617 --- /dev/null +++ b/src/util/lib.rs @@ -0,0 +1,33 @@ +//! utility functions shared between source and build script + +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 +}