Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use internal crate to share make table functions #16

Merged
merged 1 commit into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
45 changes: 6 additions & 39 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@

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")
.unwrap()
.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();

Expand All @@ -58,16 +26,15 @@ 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();
}

fn main() {
create_constants();
}

18 changes: 2 additions & 16 deletions src/crc32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
18 changes: 2 additions & 16 deletions src/crc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#[macro_use]
extern crate build_const;
extern crate util;

pub mod crc32;
pub mod crc64;
Expand Down
7 changes: 7 additions & 0 deletions src/util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "util"
version = "0.1.0"

[lib]
name = "util"
path = "lib.rs"
33 changes: 33 additions & 0 deletions src/util/lib.rs
Original file line number Diff line number Diff line change
@@ -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
}