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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::action::Action;
use crate::adler32::Adler32;
use crate::core::borrow::BorrowMut;
use crate::core::hash::Hasher;
use crate::core::marker::PhantomData;
use crate::core::mem;
use crate::deflate::encoder::Inflater;
use crate::error::CompressionError;
use crate::traits::encoder::Encoder;
#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
struct ScanIterator<I: Iterator, BI: BorrowMut<I>, F: FnMut(&I::Item) -> ()> {
phantom: PhantomData<I>,
inner: BI,
closure: F,
}
impl<I: Iterator, BI: BorrowMut<I>, F: FnMut(&I::Item) -> ()> Iterator
for ScanIterator<I, BI, F>
{
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
let ret = self.inner.borrow_mut().next();
if let Some(ref s) = ret {
(self.closure)(s);
}
ret
}
}
impl<I: Iterator, BI: BorrowMut<I>, F: FnMut(&I::Item) -> ()>
ScanIterator<I, BI, F>
{
pub(crate) fn new(inner: BI, closure: F) -> Self {
Self {
inner,
closure,
phantom: PhantomData,
}
}
}
#[derive(Debug)]
pub struct ZlibEncoder {
inflater: Inflater,
adler32: Option<Adler32>,
header_len: u8,
header: Vec<u8>,
hash: Option<u32>,
hashlen: u8,
}
impl Default for ZlibEncoder {
fn default() -> Self {
Self::new()
}
}
impl ZlibEncoder {
pub fn new() -> Self {
Self {
inflater: Inflater::new(),
adler32: Some(Adler32::new()),
header: vec![0x78, 0xDA],
header_len: 2,
hash: None,
hashlen: 3,
}
}
pub fn with_dict(dict: &[u8]) -> Self {
let mut dict_idc = Adler32::new();
dict_idc.write(dict);
let dict_hash = dict_idc.finish() as u32;
Self {
inflater: Inflater::with_dict(dict),
adler32: Some(Adler32::new()),
header: vec![
0x78,
0xF9,
(dict_hash >> 24) as u8,
(dict_hash >> 16) as u8,
(dict_hash >> 8) as u8,
dict_hash as u8,
],
header_len: 6,
hash: None,
hashlen: 3,
}
}
}
impl Encoder for ZlibEncoder {
type Error = CompressionError;
type In = u8;
type Out = u8;
fn next<I: Iterator<Item = u8>>(
&mut self,
iter: &mut I,
action: Action,
) -> Option<Result<u8, CompressionError>> {
let hlen = self.header_len;
if hlen > 0 {
let hlen_all = self.header.len();
self.header_len = hlen - 1;
Some(Ok(self.header[hlen_all - hlen as usize]))
} else if let Some(hash) = self.hash {
if self.hashlen == 0 {
None
} else {
self.hashlen -= 1;
Some(Ok((hash >> (self.hashlen << 3)) as u8))
}
} else {
let mut adler32 = mem::replace(&mut self.adler32, None);
let ret = self.inflater.next(
&mut ScanIterator::<I, _, _>::new(iter, |x: &u8| {
adler32.as_mut().unwrap().write_u8(*x)
}),
action,
);
let _ = mem::replace(&mut self.adler32, adler32);
if ret.is_none() {
let hash = self.adler32.as_mut().unwrap().finish() as u32;
let ret = (hash >> 24) as u8;
self.hash = Some(hash);
Some(Ok(ret))
} else {
ret
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::encoder::EncodeExt;
#[test]
fn test_unit() {
let mut encoder = ZlibEncoder::new();
let ret = b"a"
.iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>();
assert_eq!(
ret,
Ok(vec![0x78, 0xDA, 0x4B, 0x04, 0x00, 0x00, 0x62, 0x00, 0x62])
);
}
#[test]
fn test_unit_with_dict() {
let mut encoder = ZlibEncoder::with_dict(b"a");
let ret = b"a"
.iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>();
assert_eq!(
ret,
Ok(vec![
0x78, 0xF9, 0x00, 0x62, 0x00, 0x62, 0x4B, 0x04, 0x00, 0x00,
0x62, 0x00, 0x62,
])
);
}
}