xref: /DragonOS/kernel/crates/crc/src/crc64.rs (revision 4935c74f326cd4e0854959c0ec8ab1d726c05e41)
1*4935c74fSLoGin //! * Normal 64-bit CRC calculation.
2*4935c74fSLoGin //!
3*4935c74fSLoGin //! Taken from Linux Kernel 6.1.9
4*4935c74fSLoGin //!
5*4935c74fSLoGin //! This is a basic crc64 implementation following ECMA-182 specification,
6*4935c74fSLoGin //! which can be found from,
7*4935c74fSLoGin //! https://www.ecma-international.org/publications/standards/Ecma-182.htm
8*4935c74fSLoGin //!
9*4935c74fSLoGin //! Dr. Ross N. Williams has a great document to introduce the idea of CRC
10*4935c74fSLoGin //! algorithm, here the CRC64 code is also inspired by the table-driven
11*4935c74fSLoGin //! algorithm and detail example from this paper. This paper can be found
12*4935c74fSLoGin //! from,
13*4935c74fSLoGin //! http://www.ross.net/crc/download/crc_v3.txt
14*4935c74fSLoGin //!
15*4935c74fSLoGin //! crc64table[256] is the lookup table of a table-driven 64-bit CRC
16*4935c74fSLoGin //! calculation, which is generated by gen_crc64table.c in kernel build
17*4935c74fSLoGin //! time. The polynomial of crc64 arithmetic is from ECMA-182 specification
18*4935c74fSLoGin //! as well, which is defined as,
19*4935c74fSLoGin //!
20*4935c74fSLoGin //! x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
21*4935c74fSLoGin //! x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
22*4935c74fSLoGin //! x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
23*4935c74fSLoGin //! x^7 + x^4 + x + 1
24*4935c74fSLoGin //!
25*4935c74fSLoGin //! crc64rocksoft[256] table is from the Rocksoft specification polynomial
26*4935c74fSLoGin //! defined as,
27*4935c74fSLoGin //!
28*4935c74fSLoGin //! x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 +
29*4935c74fSLoGin //! x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 +
30*4935c74fSLoGin //! x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 1
31*4935c74fSLoGin 
32*4935c74fSLoGin use crate::tables::crc64::{CRC64_ROCKSOFT_TABLE, CRC64_TABLE};
33*4935c74fSLoGin 
34*4935c74fSLoGin /// crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
35*4935c74fSLoGin ///
36*4935c74fSLoGin /// ## 参数
37*4935c74fSLoGin ///
38*4935c74fSLoGin /// - `crc`: seed value for computation. 0 or (u64)~0 for a new CRC calculation,
39*4935c74fSLoGin ///             or the previous crc64 value if computing incrementally.
40*4935c74fSLoGin /// - `buf`: pointer to buffer over which CRC64 is run
crc64_be(mut crc: u64, buf: &[u8]) -> u6441*4935c74fSLoGin pub fn crc64_be(mut crc: u64, buf: &[u8]) -> u64 {
42*4935c74fSLoGin     for &byte in buf {
43*4935c74fSLoGin         let t = ((crc >> 56) ^ (byte as u64)) & 0xff;
44*4935c74fSLoGin         crc = CRC64_TABLE[t as usize] ^ (crc << 8);
45*4935c74fSLoGin     }
46*4935c74fSLoGin     crc
47*4935c74fSLoGin }
48*4935c74fSLoGin 
49*4935c74fSLoGin ///
50*4935c74fSLoGin /// crc64_rocksoft_generic - Calculate bitwise Rocksoft CRC64
51*4935c74fSLoGin ///
52*4935c74fSLoGin /// ## 参数
53*4935c74fSLoGin ///
54*4935c74fSLoGin /// - `crc`: seed value for computation. 0 for a new CRC calculation, or the
55*4935c74fSLoGin ///            previous crc64 value if computing incrementally.
56*4935c74fSLoGin /// - `buf`: pointer to buffer over which CRC64 is run
crc64_rocksoft_generic(mut crc: u64, buf: &[u8]) -> u6457*4935c74fSLoGin pub fn crc64_rocksoft_generic(mut crc: u64, buf: &[u8]) -> u64 {
58*4935c74fSLoGin     crc = !crc;
59*4935c74fSLoGin 
60*4935c74fSLoGin     for &byte in buf {
61*4935c74fSLoGin         crc = (crc >> 8) ^ CRC64_ROCKSOFT_TABLE[(((crc & 0xff) as u8) ^ (byte)) as usize];
62*4935c74fSLoGin     }
63*4935c74fSLoGin 
64*4935c74fSLoGin     return !crc;
65*4935c74fSLoGin }
66*4935c74fSLoGin 
67*4935c74fSLoGin #[cfg(test)]
68*4935c74fSLoGin mod tests {
69*4935c74fSLoGin     use super::*;
70*4935c74fSLoGin 
71*4935c74fSLoGin     #[test]
crc64_be_single()72*4935c74fSLoGin     fn crc64_be_single() {
73*4935c74fSLoGin         let mut buf = [0u8; 1];
74*4935c74fSLoGin         buf[0] = 0xaa;
75*4935c74fSLoGin         let crc = crc64_be(0, &buf);
76*4935c74fSLoGin         assert_eq!(crc, 0x2d071b6213a0cb8b);
77*4935c74fSLoGin     }
78*4935c74fSLoGin 
79*4935c74fSLoGin     #[test]
crc64_be_multibytes()80*4935c74fSLoGin     fn crc64_be_multibytes() {
81*4935c74fSLoGin         let buf = b"0123456789";
82*4935c74fSLoGin         let crc = crc64_be(0, buf);
83*4935c74fSLoGin         assert_eq!(crc, 0x2a71ab4164c3bbe8);
84*4935c74fSLoGin     }
85*4935c74fSLoGin }
86