1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#![feature(test, cfg_target_feature)]
extern crate libc;
#[cfg(test)]
extern crate rand;
#[cfg(test)]
extern crate test;

use libc::{c_void, uint32_t, size_t};

#[allow(dead_code)]
extern {
    fn crc32c_hw(crc: uint32_t, buf: *const c_void, len: size_t) -> uint32_t;
    fn crc32c_sw(crc: uint32_t, buf: *const c_void, len: size_t) -> uint32_t;
}

/// Computes the crc32c for the data payload.
#[cfg(target_feature="sse4.2")]
#[inline]
pub fn crc32c(data: &[u8]) -> u32 {
    let len = data.len();
    unsafe {
        crc32c_hw(0, data.as_ptr() as *const c_void, len) as u32
    }
}

/// Computes the crc32c for the data payload.
#[cfg(target_feature="sse4.2")]
#[inline]
pub fn crc32c_append(crc: u32, data: &[u8]) -> u32 {
    let len = data.len();
    unsafe {
        crc32c_hw(crc, data.as_ptr() as *const c_void, len) as u32
    }
}

/// Computes the crc32c for the data payload.
#[cfg(not(target_feature="sse4.2"))]
#[inline]
pub fn crc32c(data: &[u8]) -> u32 {
    let len = data.len();
    unsafe {
        crc32c_sw(0, data.as_ptr() as *const c_void, len) as u32
    }
}

/// Computes the crc32c for the data payload, starting with a previous CRC32C value.
#[cfg(not(target_feature="sse4.2"))]
#[inline]
pub fn crc32c_append(crc: u32, data: &[u8]) -> u32 {
    let len = data.len();
    unsafe {
        crc32c_sw(crc, data.as_ptr() as *const c_void, len) as u32
    }
}


#[cfg(test)]
mod tests {
    use rand::{OsRng, Rng};
    use super::*;
    use test::Bencher;

    #[test]
    fn test_crc() {
        let v = crc32c(b"012345678910");
        assert_eq!(0x8412e281, v);
    }

    #[test]
    fn test_crc_append() {
        let v = crc32c(b"01234");
        let v = crc32c_append(v, b"5678910");
        assert_eq!(0x8412e281, v);
    }

    #[bench]
    fn crc(b: &mut Bencher) {
        let mut bytes = [0u8; 8000];

        let mut r = OsRng::new().unwrap();
        r.fill_bytes(&mut bytes);

        b.iter(|| crc32c(&bytes));
    }
}