Coverage Report

Created: 2026-08-14 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/fdeflate-0.3.7/src/huffman.rs
Line
Count
Source
1
use crate::decompress::{EXCEPTIONAL_ENTRY, LITERAL_ENTRY, SECONDARY_TABLE_ENTRY};
2
3
/// Return the next code, or if the codeword is already all ones (which is the final code), return
4
/// the same code again.
5
4.76M
fn next_codeword(mut codeword: u16, table_size: u16) -> u16 {
6
4.76M
    if codeword == table_size - 1 {
7
114k
        return codeword;
8
4.64M
    }
9
10
4.64M
    let adv = (u16::BITS - 1) - (codeword ^ (table_size - 1)).leading_zeros();
11
4.64M
    let bit = 1 << adv;
12
4.64M
    codeword &= bit - 1;
13
4.64M
    codeword |= bit;
14
4.64M
    codeword
15
4.76M
}
16
17
#[allow(clippy::needless_range_loop)]
18
129k
pub fn build_table(
19
129k
    lengths: &[u8],
20
129k
    entries: &[u32],
21
129k
    codes: &mut [u16],
22
129k
    primary_table: &mut [u32],
23
129k
    secondary_table: &mut Vec<u16>,
24
129k
    is_distance_table: bool,
25
129k
    double_literal: bool,
26
129k
) -> bool {
27
    // Count the number of symbols with each code length.
28
129k
    let mut histogram = [0; 16];
29
14.2M
    for &length in lengths {
30
14.1M
        histogram[length as usize] += 1;
31
14.1M
    }
32
33
    // Determine the maximum code length.
34
129k
    let mut max_length = 15;
35
1.37M
    while max_length > 1 && histogram[max_length] == 0 {
36
1.24M
        max_length -= 1;
37
1.24M
    }
38
39
    // Handle zero and one symbol huffman codes (which are only allowed for distance codes).
40
129k
    if is_distance_table {
41
41.5k
        if max_length == 0 {
42
0
            primary_table.fill(0);
43
0
            secondary_table.clear();
44
0
            return true;
45
41.5k
        } else if max_length == 1 && histogram[1] == 1 {
46
21.1k
            let symbol = lengths.iter().position(|&l| l == 1).unwrap();
47
14.1k
            codes[symbol] = 0;
48
14.1k
            let entry = entries
49
14.1k
                .get(symbol)
50
14.1k
                .cloned()
51
14.1k
                .unwrap_or((symbol as u32) << 16)
52
14.1k
                | 1;
53
3.62M
            for chunk in primary_table.chunks_mut(2) {
54
3.62M
                chunk[0] = entry;
55
3.62M
                chunk[1] = 0;
56
3.62M
            }
57
14.1k
            return true;
58
27.3k
        }
59
88.2k
    }
60
61
    // Sort symbols by code length. Given the histogram, we can determine the starting offset
62
    // for each code length.
63
115k
    let mut offsets = [0; 16];
64
115k
    let mut codespace_used = 0;
65
115k
    offsets[1] = histogram[0];
66
571k
    for i in 1..max_length {
67
571k
        offsets[i + 1] = offsets[i] + histogram[i];
68
571k
        codespace_used = (codespace_used << 1) + histogram[i];
69
571k
    }
70
115k
    codespace_used = (codespace_used << 1) + histogram[max_length];
71
72
    // Check that the provided lengths form a valid Huffman tree.
73
115k
    if codespace_used != (1 << max_length) {
74
1.32k
        return false;
75
114k
    }
76
77
    // Sort the symbols by code length.
78
114k
    let mut next_index = offsets;
79
114k
    let mut sorted_symbols = [0; 288];
80
13.5M
    for symbol in 0..lengths.len() {
81
13.5M
        let length = lengths[symbol];
82
13.5M
        sorted_symbols[next_index[length as usize]] = symbol;
83
13.5M
        next_index[length as usize] += 1;
84
13.5M
    }
85
86
114k
    let mut codeword = 0u16;
87
114k
    let mut i = histogram[0];
88
89
    // Populate the primary decoding table
90
114k
    let primary_table_bits = primary_table.len().ilog2() as usize;
91
114k
    let primary_table_mask = (1 << primary_table_bits) - 1;
92
1.06M
    for length in 1..=primary_table_bits {
93
1.06M
        let current_table_end = 1 << length;
94
95
        // Loop over all symbols with the current code length and set their table entries.
96
4.29M
        for _ in 0..histogram[length] {
97
4.29M
            let symbol = sorted_symbols[i];
98
4.29M
            i += 1;
99
4.29M
100
4.29M
            primary_table[codeword as usize] = entries
101
4.29M
                .get(symbol)
102
4.29M
                .cloned()
103
4.29M
                .unwrap_or((symbol as u32) << 16)
104
4.29M
                | length as u32;
105
4.29M
106
4.29M
            codes[symbol] = codeword;
107
4.29M
            codeword = next_codeword(codeword, current_table_end as u16);
108
4.29M
        }
109
110
1.06M
        if double_literal {
111
2.37M
            for len1 in 1..(length - 1) {
112
2.37M
                let len2 = length - len1;
113
8.42M
                for sym1_index in offsets[len1]..next_index[len1] {
114
11.7M
                    for sym2_index in offsets[len2]..next_index[len2] {
115
11.7M
                        let sym1 = sorted_symbols[sym1_index];
116
11.7M
                        let sym2 = sorted_symbols[sym2_index];
117
11.7M
                        if sym1 < 256 && sym2 < 256 {
118
8.07M
                            let codeword1 = codes[sym1];
119
8.07M
                            let codeword2 = codes[sym2];
120
8.07M
                            let codeword = codeword1 | (codeword2 << len1);
121
8.07M
                            let entry = (sym1 as u32) << 16
122
8.07M
                                | (sym2 as u32) << 24
123
8.07M
                                | LITERAL_ENTRY
124
8.07M
                                | (2 << 8);
125
8.07M
                            primary_table[codeword as usize] = entry | (length as u32);
126
8.07M
                        }
127
                    }
128
                }
129
            }
130
551k
        }
131
132
        // If we aren't at the maximum table size, double the size of the table.
133
1.06M
        if length < primary_table_bits {
134
955k
            primary_table.copy_within(0..current_table_end, current_table_end);
135
955k
        }
136
    }
137
138
    // Populate the secondary decoding table.
139
114k
    secondary_table.clear();
140
114k
    if max_length > primary_table_bits {
141
19.0k
        let mut subtable_start = 0;
142
19.0k
        let mut subtable_prefix = !0;
143
35.7k
        for length in (primary_table_bits + 1)..=max_length {
144
35.7k
            let subtable_size = 1 << (length - primary_table_bits);
145
35.7k
            for _ in 0..histogram[length] {
146
                // If the codeword's prefix doesn't match the current subtable, create a new
147
                // subtable.
148
465k
                if codeword & primary_table_mask != subtable_prefix {
149
198k
                    subtable_prefix = codeword & primary_table_mask;
150
198k
                    subtable_start = secondary_table.len();
151
198k
                    primary_table[subtable_prefix as usize] = ((subtable_start as u32) << 16)
152
198k
                        | EXCEPTIONAL_ENTRY
153
198k
                        | SECONDARY_TABLE_ENTRY
154
198k
                        | (subtable_size as u32 - 1);
155
198k
                    secondary_table.resize(subtable_start + subtable_size, 0);
156
267k
                }
157
158
                // Lookup the symbol.
159
465k
                let symbol = sorted_symbols[i];
160
465k
                i += 1;
161
162
                // Insert the symbol into the secondary table and advance to the next codeword.
163
465k
                codes[symbol] = codeword;
164
465k
                secondary_table[subtable_start + (codeword >> primary_table_bits) as usize] =
165
465k
                    ((symbol as u16) << 4) | (length as u16);
166
465k
                codeword = next_codeword(codeword, 1 << length);
167
            }
168
169
            // If there are more codes with the same subtable prefix, extend the subtable.
170
35.7k
            if length < max_length && codeword & primary_table_mask == subtable_prefix {
171
11.2k
                secondary_table.extend_from_within(subtable_start..);
172
11.2k
                let subtable_size = secondary_table.len() - subtable_start;
173
11.2k
                primary_table[subtable_prefix as usize] = ((subtable_start as u32) << 16)
174
11.2k
                    | EXCEPTIONAL_ENTRY
175
11.2k
                    | SECONDARY_TABLE_ENTRY
176
11.2k
                    | (subtable_size as u32 - 1);
177
24.5k
            }
178
        }
179
95.2k
    }
180
181
114k
    true
182
129k
}