compression_codecs/deflate64/
decoder.rs1use crate::DecodeV2;
2use compression_core::util::{PartialBuffer, WriteBuffer};
3use deflate64::InflaterManaged;
4use std::io::{Error, ErrorKind, Result};
5
6#[derive(Debug)]
7pub struct Deflate64Decoder {
8 inflater: Box<InflaterManaged>,
9}
10
11impl Default for Deflate64Decoder {
12 fn default() -> Self {
13 Self {
14 inflater: Box::new(InflaterManaged::new()),
15 }
16 }
17}
18
19impl Deflate64Decoder {
20 pub fn new() -> Self {
21 Self::default()
22 }
23
24 fn decode(
25 &mut self,
26 input: &mut PartialBuffer<&[u8]>,
27 output: &mut WriteBuffer<'_>,
28 ) -> Result<bool> {
29 let result = self
30 .inflater
31 .inflate(input.unwritten(), output.initialize_unwritten());
32
33 input.advance(result.bytes_consumed);
34 output.advance(result.bytes_written);
35
36 if result.data_error {
37 Err(Error::new(ErrorKind::InvalidData, "invalid data"))
38 } else {
39 Ok(self.inflater.finished() && self.inflater.available_output() == 0)
40 }
41 }
42}
43
44impl DecodeV2 for Deflate64Decoder {
45 fn reinit(&mut self) -> Result<()> {
46 *self.inflater = InflaterManaged::new();
47 Ok(())
48 }
49
50 fn decode(
51 &mut self,
52 input: &mut PartialBuffer<&[u8]>,
53 output: &mut WriteBuffer<'_>,
54 ) -> Result<bool> {
55 self.decode(input, output)
56 }
57
58 fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
59 self.decode(&mut PartialBuffer::new(&[]), output)?;
60
61 loop {
62 let old_len = output.written_len();
63 self.decode(&mut PartialBuffer::new(&[]), output)?;
64 if output.written_len() == old_len {
65 break;
66 }
67 }
68
69 Ok(!output.has_no_spare_space())
70 }
71
72 fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
73 self.decode(&mut PartialBuffer::new(&[]), output)
74 }
75}