| 1 |
const LENGTH_SYMBOL_TABLE: [usize; 288 /* Originally, 259 entries */] = [ |
| 2 |
0, 0, 0, 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, 268, 269, |
| 3 |
269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272, 272, 272, 273, 273, 273, 273, |
| 4 |
273, 273, 273, 273, 274, 274, 274, 274, 274, 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, |
| 5 |
275, 276, 276, 276, 276, 276, 276, 276, 276, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, |
| 6 |
277, 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, |
| 7 |
278, 278, 278, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, |
| 8 |
280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 281, 281, 281, |
| 9 |
281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, |
| 10 |
281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 282, 282, 282, 282, 282, 282, 282, 282, 282, |
| 11 |
282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, |
| 12 |
282, 282, 282, 282, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, |
| 13 |
283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 284, 284, |
| 14 |
284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, |
| 15 |
284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 285, |
| 16 |
// Padding to 288 entries for reduced bounds checking |
| 17 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 18 |
]; |
| 19 |
|
| 20 |
/// Gets the symbol for the given length, cfr. the DEFLATE spec. |
| 21 |
/// Returns symbol in range [257-285] (inclusive). |
| 22 |
pub const fn get_length_symbol(length: usize) -> usize { |
| 23 |
LENGTH_SYMBOL_TABLE[length] |
| 24 |
} |
| 25 |
|
| 26 |
/// Gets the amount of extra bits for the given dist, cfr. the DEFLATE spec. |
| 27 |
pub const fn get_dist_extra_bits(dist: u16) -> u32 { |
| 28 |
if dist < 5 { |
| 29 |
return 0; |
| 30 |
} |
| 31 |
(dist - 1).ilog2() - 1 |
| 32 |
} |
| 33 |
|
| 34 |
/// Gets value of the extra bits for the given dist, cfr. the DEFLATE spec. |
| 35 |
pub const fn get_dist_extra_bits_value(dist: u16) -> u16 { |
| 36 |
if dist < 5 { |
| 37 |
return 0; |
| 38 |
} |
| 39 |
let l = (dist - 1).ilog2(); |
| 40 |
(dist - (1 + (1 << l))) & ((1 << (l - 1)) - 1) |
| 41 |
} |
| 42 |
|
| 43 |
pub const fn get_dist_symbol(dist: u16) -> u16 { |
| 44 |
if dist < 5 { |
| 45 |
// dist should never equal zero, and wrapping generates more efficient code |
| 46 |
return dist.wrapping_sub(1); |
| 47 |
} |
| 48 |
let l = (dist - 1).ilog2(); |
| 49 |
let r = ((dist - 1) >> (l - 1)) & 1; |
| 50 |
l as u16 * 2 + r |
| 51 |
} |
| 52 |
|
| 53 |
const LENGTH_EXTRA_BITS: [u32; 288 /* Originally, 259 entries */] = [ |
| 54 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 55 |
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 56 |
3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 57 |
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 58 |
4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 59 |
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 60 |
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 61 |
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 62 |
5, 5, 0, // Padding to 288 entries for reduced bounds checking |
| 63 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 64 |
]; |
| 65 |
|
| 66 |
/// Gets the amount of extra bits for the given length, cfr. the DEFLATE spec. |
| 67 |
pub const fn get_length_extra_bits(l: usize) -> u32 { |
| 68 |
LENGTH_EXTRA_BITS[l] |
| 69 |
} |
| 70 |
|
| 71 |
const LENGTH_EXTRA_BITS_VALUE: [u32; 288 /* Originally, 259 entries */] = [ |
| 72 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, |
| 73 |
1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, |
| 74 |
5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 75 |
10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, |
| 76 |
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
| 77 |
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 78 |
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, |
| 79 |
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, |
| 80 |
29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, |
| 81 |
23, 24, 25, 26, 27, 28, 29, 30, 0, |
| 82 |
// Padding to 288 entries for reduced bounds checking |
| 83 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 84 |
]; |
| 85 |
|
| 86 |
/// Gets value of the extra bits for the given length, cfr. the DEFLATE spec. |
| 87 |
pub const fn get_length_extra_bits_value(l: usize) -> u32 { |
| 88 |
LENGTH_EXTRA_BITS_VALUE[l] |
| 89 |
} |
| 90 |
|
| 91 |
const LENGTH_SYMBOL_EXTRA_BITS_TABLE: [u32; 29] = [ |
| 92 |
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, |
| 93 |
]; |
| 94 |
|
| 95 |
/// Gets the amount of extra bits for the given length symbol. |
| 96 |
pub const fn get_length_symbol_extra_bits(s: usize) -> u32 { |
| 97 |
LENGTH_SYMBOL_EXTRA_BITS_TABLE[s - 257] |
| 98 |
} |
| 99 |
|
| 100 |
const DIST_SYMBOL_EXTRA_BITS_TABLE: [u32; 30] = [ |
| 101 |
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, |
| 102 |
13, |
| 103 |
]; |
| 104 |
|
| 105 |
/// Gets the amount of extra bits for the given distance symbol. |
| 106 |
pub const fn get_dist_symbol_extra_bits(s: usize) -> u32 { |
| 107 |
DIST_SYMBOL_EXTRA_BITS_TABLE[s] |
| 108 |
} |