summaryrefslogtreecommitdiff
path: root/src/crc32.rs
blob: 6b3f9d741b654aba56e5d87237f196e5f7a2226b (plain)
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
// https://github.com/d-e-s-o/nitrocli/blob/ac29709a644682c61a5a28d2a23f8887174fcc31/nitrocli/src/crc32.rs
//
// *************************************************************************
// * Copyright (C) 2017 Daniel Mueller (deso@posteo.net)                   *
// *                                                                       *
// * This program is free software: you can redistribute it and/or modify  *
// * it under the terms of the GNU General Public License as published by  *
// * the Free Software Foundation, either version 3 of the License, or     *
// * (at your option) any later version.                                   *
// *                                                                       *
// * This program is distributed in the hope that it will be useful,       *
// * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
// * GNU General Public License for more details.                          *
// *                                                                       *
// * You should have received a copy of the GNU General Public License     *
// * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
// *************************************************************************

/// Polynomial used in STM32.
const CRC32_POLYNOMIAL: u32 = 0x04c11db7;

fn crc32(mut crc: u32, data: u32) -> u32 {
    crc = crc ^ data;

    for _ in 0..32 {
        if crc & 0x80000000 != 0 {
            crc = (crc << 1) ^ CRC32_POLYNOMIAL;
        } else {
            crc = crc << 1;
        }
    }
    return crc;
}

/// Retrieve a u32 slice of the 'data' part.
///
/// Note that the size of the supplied data has to be a multiple of 4
/// bytes.
fn as_slice_u32(data: &[u8]) -> &[u32] {
    assert!(data.len() % ::std::mem::size_of::<u32>() == 0);

    unsafe {
        let ptr = data.as_ptr() as *const u32;
        let len = data.len() / ::std::mem::size_of::<u32>();
        return ::std::slice::from_raw_parts(ptr, len);
    }
}

/// Calculate the CRC of a byte slice.
pub fn crc(data: &[u8]) -> u32 {
    let mut crc = 0xffffffff;

    for value in as_slice_u32(data) {
        crc = crc32(crc, *value);
    }

    return crc;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_crc32() {
        let mut crc = 0;

        // The expected values were computed with the original function.
        crc = crc32(crc, 0xdeadbeef);
        assert_eq!(crc, 0x46dec763);

        crc = crc32(crc, 42);
        assert_eq!(crc, 0x7e579b45);
    }

    #[test]
    fn test_crc() {
        let data = &"thisisatextthatistobecrced..".to_string().into_bytes();
        let crc = crc(data);

        assert_eq!(crc, 0x469db4ee);
    }
}