diff --git a/Cargo.toml b/Cargo.toml index d18ed39..d9d6c1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,14 @@ documentation = "http://mrhooray.github.io/crc-rs/crc/index.html" license = "MIT" [dependencies] -lazy_static = "0.2" + +[dependencies.build_const] +version = "0.2" +default-features = false + +[build-dependencies] +build_const = "0.2" + +[features] +std = [] +default = ["std"] diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..45c1627 --- /dev/null +++ b/build.rs @@ -0,0 +1,73 @@ + +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") + .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/src/crc32.rs b/src/crc32.rs index b37f8cd..48302fc 100644 --- a/src/crc32.rs +++ b/src/crc32.rs @@ -1,14 +1,9 @@ +#[cfg(feature = "std")] use std::hash::Hasher; +#[cfg(not(feature = "std"))] +use core::hash::Hasher; -pub const CASTAGNOLI: u32 = 0x82f63b78; -pub const IEEE: u32 = 0xedb88320; -pub const KOOPMAN: u32 = 0xeb31d82e; - -lazy_static! { - pub static ref IEEE_TABLE: [u32; 256] = make_table(IEEE); - pub static ref CASTAGNOLI_TABLE: [u32; 256] = make_table(CASTAGNOLI); - pub static ref KOOPMAN_TABLE: [u32; 256] = make_table(KOOPMAN); -} +build_const!("crc32_constants"); pub struct Digest { table: [u32; 256], diff --git a/src/crc64.rs b/src/crc64.rs index 0b6bd73..9df484b 100644 --- a/src/crc64.rs +++ b/src/crc64.rs @@ -1,12 +1,9 @@ +#[cfg(feature = "std")] use std::hash::Hasher; +#[cfg(not(feature = "std"))] +use core::hash::Hasher; -pub const ECMA: u64 = 0xc96c5795d7870f42; -pub const ISO: u64 = 0xd800000000000000; - -lazy_static! { - pub static ref ECMA_TABLE: [u64; 256] = make_table(ECMA); - pub static ref ISO_TABLE: [u64; 256] = make_table(ISO); -} +build_const!("crc64_constants"); pub struct Digest { table: [u64; 256], diff --git a/src/lib.rs b/src/lib.rs index ee44661..2dbe8a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,8 +40,10 @@ //! assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); //! ``` +#![cfg_attr(not(feature = "std"), no_std)] + #[macro_use] -extern crate lazy_static; +extern crate build_const; pub mod crc32; pub mod crc64;