Skip to content

Commit

Permalink
Merge pull request #13 from vitiral/no_std
Browse files Browse the repository at this point in the history
add no_std feature
  • Loading branch information
Rui Hu authored Jul 18, 2017
2 parents 1e727b3 + c014965 commit 1ae49cb
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
73 changes: 73 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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();
}

13 changes: 4 additions & 9 deletions src/crc32.rs
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
11 changes: 4 additions & 7 deletions src/crc64.rs
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1ae49cb

Please sign in to comment.