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
159
160
161
162
163
164
165
166
167
168
169
use super::super::{traits::CType, TableUpdateInterval, TableUpdateRate};
use lzham_sys::lzham_decompress_params;
use std::{
ops::BitOr,
os::raw::{c_uint, c_void},
};
#[derive(Clone, Debug)]
pub struct DecompressionOptions {
pub dict_size_log2: u32,
pub table_update_rate: TableUpdateRate,
pub decompression_flags: Option<DecompressionFlag>,
pub num_seed_bytes: Option<u32>,
pub p_seed_bytes: Option<Vec<u8>>,
pub table_update_interval: Option<TableUpdateInterval>,
}
#[derive(Clone, Copy, Debug)]
pub enum DecompressionFlag {
OutputUnbuffered = 1,
ComputeAdler32 = 2,
ReadZlibStream = 4,
}
#[derive(Clone, Copy, Debug)]
pub enum DecompressionStatus {
NotFinished,
HasMoreOutput,
NeedsMoreInput,
Success,
FailedInitialization,
OutputBufferTooSmall,
ExpectedMoreRawBytes,
BadCode,
Adler32,
BadRawBlock,
BadCompBlockSyncCheck,
BadZlibHeader,
NeedSeedBytes,
BadSeedBytes,
BadSyncBlock,
InvalidParameter,
Failed,
}
impl Default for DecompressionOptions {
fn default() -> Self {
Self {
dict_size_log2: 26,
table_update_rate: TableUpdateRate::DEFAULT,
decompression_flags: None,
num_seed_bytes: None,
p_seed_bytes: None,
table_update_interval: None,
}
}
}
impl BitOr for DecompressionFlag {
type Output = Self;
fn bitor(self, _rhs: Self) -> Self {
self
}
}
impl DecompressionStatus {
pub(crate) fn from_c_type(status: lzham_sys::lzham_decompress_status_t) -> Self {
match status {
0 => Self::NotFinished,
1 => DecompressionStatus::HasMoreOutput,
2 => DecompressionStatus::NeedsMoreInput,
3 => DecompressionStatus::Success,
4 => DecompressionStatus::FailedInitialization,
5 => DecompressionStatus::OutputBufferTooSmall,
6 => DecompressionStatus::ExpectedMoreRawBytes,
7 => DecompressionStatus::BadCode,
8 => DecompressionStatus::Adler32,
9 => DecompressionStatus::BadRawBlock,
10 => DecompressionStatus::BadCompBlockSyncCheck,
11 => DecompressionStatus::BadZlibHeader,
12 => DecompressionStatus::NeedSeedBytes,
13 => DecompressionStatus::BadSeedBytes,
14 => DecompressionStatus::BadSyncBlock,
15 => DecompressionStatus::InvalidParameter,
_ => panic!("decomp `status` out of bounds"),
}
}
pub fn is_success(&self) -> bool {
if let Self::Success = self {
true
} else {
false
}
}
pub fn is_success_or_first_failure(&self) -> bool {
*self as u32 >= 3
}
}
impl CType for DecompressionOptions {
type CItem = lzham_decompress_params;
fn to_c_type(self) -> Self::CItem {
let (max, slow) = self.table_update_interval.unwrap_or_default().to_c_type();
lzham_decompress_params {
m_struct_size: std::mem::size_of::<lzham_decompress_params>() as c_uint,
m_dict_size_log2: self.dict_size_log2 as c_uint,
m_table_update_rate: self.table_update_rate.to_c_type(),
m_decompress_flags: self
.decompression_flags
.map_or_else(|| 0, |f| f.to_c_type()),
m_num_seed_bytes: self.num_seed_bytes.unwrap_or(0) as c_uint,
m_pSeed_bytes: {
if let Some(p) = self.p_seed_bytes {
p.as_ptr() as *const c_void
} else {
std::ptr::null()
}
},
m_table_max_update_interval: max,
m_table_update_interval_slow_rate: slow,
}
}
}
impl CType for DecompressionFlag {
type CItem = lzham_sys::lzham_decompress_flags;
fn to_c_type(self) -> Self::CItem {
self as c_uint
}
}
impl CType for DecompressionStatus {
type CItem = lzham_sys::lzham_decompress_status_t;
fn to_c_type(self) -> Self::CItem {
self as c_uint
}
}