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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use core::hash::Hasher;

pub use crate::util::make_table_crc64 as make_table;
use crate::CalcType;

pub const ECMA: u64 = 0x42F0E1EBA9EA3693;
pub const ECMA_TABLE: [u64; 256] = make_table(ECMA, true);
pub const ISO: u64 = 0x000000000000001B;
pub const ISO_TABLE: [u64; 256] = make_table(ISO, true);

/// `Digest` struct for CRC calculation
/// - `table`: Calculation table generated from input parameters.
/// - `initial`: Initial value.
/// - `value`: Current value of the CRC calculation.
/// - `final_xor`: Final value to XOR with when calling `Digest::sum64()`.
/// - `calc`: Type of calculation. See its documentation for details.
pub struct Digest {
    table: [u64; 256],
    initial: u64,
    value: u64,
    final_xor: u64,
    calc: CalcType,
}

pub trait Hasher64 {
    /// Resets CRC calculation to `initial` value
    fn reset(&mut self);
    /// Updates CRC calculation with input byte array `bytes`
    fn write(&mut self, bytes: &[u8]);
    /// Returns checksum after being XOR'd with `final_xor`
    fn sum64(&self) -> u64;
}

/// Updates input CRC value `value` using CRC table `table` with byte array `bytes`.
pub fn update(mut value: u64, table: &[u64; 256], bytes: &[u8], calc: &CalcType) -> u64 {
    match calc {
        CalcType::Normal => {
            value = bytes.iter().fold(value, |acc, &x| {
                (acc << 8) ^ (table[((u64::from(x)) ^ (acc >> 56)) as usize])
            })
        }
        CalcType::Reverse => {
            value = bytes.iter().fold(value, |acc, &x| {
                (acc >> 8) ^ (table[((acc ^ (u64::from(x))) & 0xFF) as usize])
            })
        }
        CalcType::Compat => {
            value = !value;
            value = bytes.iter().fold(value, |acc, &x| {
                (acc >> 8) ^ (table[((acc ^ (u64::from(x))) & 0xFF) as usize])
            });
            value = !value;
        }
    }

    value
}

/// Generates a ECMA-188 64 bit CRC checksum (AKA CRC-64-ECMA).
pub fn checksum_ecma(bytes: &[u8]) -> u64 {
    update(0u64, &ECMA_TABLE, bytes, &CalcType::Compat)
}

/// Generates a ISO 3309 32 bit CRC checksum (AKA CRC-64-ISO).
pub fn checksum_iso(bytes: &[u8]) -> u64 {
    update(0u64, &ISO_TABLE, bytes, &CalcType::Compat)
}

impl Digest {
    /// Creates a new Digest from input polynomial.
    ///
    /// # Example
    ///
    /// ```rust
    /// use crc::{crc64, Hasher64};
    /// let mut digest = crc64::Digest::new(crc64::ECMA);
    /// digest.write(b"123456789");
    /// assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa);;
    /// ```
    pub const fn new(poly: u64) -> Digest {
        Digest {
            table: make_table(poly, true),
            initial: 0u64,
            value: 0u64,
            final_xor: 0u64,
            calc: CalcType::Compat,
        }
    }

    /// Creates a new Digest from input polynomial and initial value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use crc::{crc64, Hasher64};
    /// let mut digest = crc64::Digest::new_with_initial(crc64::ECMA, 0u64);
    /// digest.write(b"123456789");
    /// assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa);
    /// ```
    pub const fn new_with_initial(poly: u64, initial: u64) -> Digest {
        Digest {
            table: make_table(poly, true),
            initial,
            value: initial,
            final_xor: 0u64,
            calc: CalcType::Compat,
        }
    }

    /// Creates a fully customized Digest from input parameters.
    ///
    /// # Example
    ///
    /// ```rust
    /// use crc::{crc64, Hasher64};
    /// let mut digest = crc64::Digest::new_custom(crc64::ECMA, !0u64, !0u64, crc::CalcType::Reverse);
    /// digest.write(b"123456789");
    /// assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa);
    /// ```
    pub fn new_custom(poly: u64, initial: u64, final_xor: u64, calc: CalcType) -> Digest {
        let mut rfl: bool = true;
        if let CalcType::Normal = calc {
            rfl = false;
        }

        Digest {
            table: make_table(poly, rfl),
            initial,
            value: initial,
            final_xor,
            calc,
        }
    }
}

impl Hasher64 for Digest {
    fn reset(&mut self) {
        self.value = self.initial;
    }

    fn write(&mut self, bytes: &[u8]) {
        self.value = update(self.value, &self.table, bytes, &self.calc);
    }

    fn sum64(&self) -> u64 {
        self.value ^ self.final_xor
    }
}

impl Hasher for Digest {
    fn finish(&self) -> u64 {
        self.sum64()
    }

    fn write(&mut self, bytes: &[u8]) {
        Hasher64::write(self, bytes);
    }
}