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
use crate::error::Result;
use crate::header::CodecType;
use std::ops::{Add, AddAssign};

mod avhuff;
mod cdrom;
mod ecc;
mod flac;
mod lzma;
mod none;
mod zlib;

#[cfg(feature = "flac_header")]
mod flac_header;
mod huff;

pub mod codecs {
    pub use crate::compression::avhuff::AVHuffCodec;
    pub use crate::compression::cdrom::CdLzmaCodec;
    pub use crate::compression::cdrom::CdZlibCodec;
    pub use crate::compression::flac::CdFlacCodec;
    pub use crate::compression::flac::RawFlacCodec;
    pub use crate::compression::huff::HuffmanCodec;
    pub use crate::compression::lzma::LzmaCodec;
    pub use crate::compression::none::NoneCodec;
    pub use crate::compression::zlib::ZlibCodec;
}

// unstable(trait_alias)
/// Marker trait for a codec that can be used to decompress a compressed hunk.
pub trait CompressionCodec: CodecImplementation + CompressionCodecType {}

/// Trait for a codec that implements a known CHD codec type.
pub trait CompressionCodecType {
    /// Returns the known [`CodecType`](crate::header::CodecType) that this
    /// codec implements.
    fn codec_type(&self) -> CodecType
    where
        Self: Sized;
}

/// Trait for a CHD decompression codec implementation.
pub trait CodecImplementation {
    /// Returns whethere is codec is lossy or not.
    fn is_lossy(&self) -> bool
    where
        Self: Sized;

    /// Creates a new instance of this codec for the provided hunk size.
    fn new(hunk_size: u32) -> Result<Self>
    where
        Self: Sized;

    /// Decompress compressed bytes from the input buffer into the
    /// output buffer.
    ///
    /// Usually the output buffer must have the exact
    /// length as `hunk_size`, but this may be dependent on the codec
    /// implementation.
    fn decompress(&mut self, input: &[u8], output: &mut [u8]) -> Result<DecompressResult>;
}

/// The result of a chunk decompression operation.
#[derive(Copy, Clone, Default)]
pub struct DecompressResult {
    bytes_out: usize,
    bytes_read: usize,
}

impl Add for DecompressResult {
    type Output = DecompressResult;

    fn add(self, rhs: Self) -> Self::Output {
        DecompressResult {
            bytes_out: self.total_out() + rhs.total_out(),
            bytes_read: self.total_in() + rhs.total_in(),
        }
    }
}

impl AddAssign for DecompressResult {
    fn add_assign(&mut self, rhs: Self) {
        self.bytes_read += rhs.bytes_read;
        self.bytes_out += rhs.bytes_out;
    }
}

impl DecompressResult {
    pub(crate) fn new(out: usize, read: usize) -> Self {
        DecompressResult {
            bytes_out: out,
            bytes_read: read,
        }
    }

    /// Returns the total number of decompressed bytes written to the output buffer.
    pub fn total_out(&self) -> usize {
        self.bytes_out
    }

    /// Returns the total number of bytes read from the compressed input buffer.
    pub fn total_in(&self) -> usize {
        self.bytes_read
    }
}