Coverage Report

Created: 2025-06-24 06:36

/rust/registry/src/index.crates.io-6f17d22bba15001f/crc32fast-1.4.2/src/combine.rs
Line
Count
Source (jump to first uncovered line)
1
const GF2_DIM: usize = 32;
2
3
0
fn gf2_matrix_times(mat: &[u32; GF2_DIM], mut vec: u32) -> u32 {
4
0
    let mut sum = 0;
5
0
    let mut idx = 0;
6
0
    while vec > 0 {
7
0
        if vec & 1 == 1 {
8
0
            sum ^= mat[idx];
9
0
        }
10
0
        vec >>= 1;
11
0
        idx += 1;
12
    }
13
0
    return sum;
14
0
}
15
16
0
fn gf2_matrix_square(square: &mut [u32; GF2_DIM], mat: &[u32; GF2_DIM]) {
17
0
    for n in 0..GF2_DIM {
18
0
        square[n] = gf2_matrix_times(mat, mat[n]);
19
0
    }
20
0
}
21
22
0
pub(crate) fn combine(mut crc1: u32, crc2: u32, mut len2: u64) -> u32 {
23
0
    let mut row: u32;
24
0
    let mut even = [0u32; GF2_DIM]; /* even-power-of-two zeros operator */
25
0
    let mut odd = [0u32; GF2_DIM]; /* odd-power-of-two zeros operator */
26
0
27
0
    /* degenerate case (also disallow negative lengths) */
28
0
    if len2 <= 0 {
29
0
        return crc1;
30
0
    }
31
0
32
0
    /* put operator for one zero bit in odd */
33
0
    odd[0] = 0xedb88320; /* CRC-32 polynomial */
34
0
    row = 1;
35
0
    for n in 1..GF2_DIM {
36
0
        odd[n] = row;
37
0
        row <<= 1;
38
0
    }
39
40
    /* put operator for two zero bits in even */
41
0
    gf2_matrix_square(&mut even, &odd);
42
0
43
0
    /* put operator for four zero bits in odd */
44
0
    gf2_matrix_square(&mut odd, &even);
45
46
    /* apply len2 zeros to crc1 (first square will put the operator for one
47
       zero byte, eight zero bits, in even) */
48
    loop {
49
        /* apply zeros operator for this bit of len2 */
50
0
        gf2_matrix_square(&mut even, &odd);
51
0
        if len2 & 1 == 1 {
52
0
            crc1 = gf2_matrix_times(&even, crc1);
53
0
        }
54
0
        len2 >>= 1;
55
0
56
0
        /* if no more bits set, then done */
57
0
        if len2 == 0 {
58
0
            break;
59
0
        }
60
0
61
0
        /* another iteration of the loop with odd and even swapped */
62
0
        gf2_matrix_square(&mut odd, &even);
63
0
        if len2 & 1 == 1 {
64
0
            crc1 = gf2_matrix_times(&odd, crc1);
65
0
        }
66
0
        len2 >>= 1;
67
0
68
0
        /* if no more bits set, then done */
69
0
        if len2 == 0 {
70
0
            break;
71
0
        }
72
    }
73
74
    /* return combined crc */
75
0
    crc1 ^= crc2;
76
0
    return crc1;
77
0
}