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
use byteorder::ReadBytesExt;
use std::io::{Read, Seek, SeekFrom};

use header::Yaz0Header;
use Error;

/// Wraps a reader of Yaz0 data, providing decompression methods.
#[derive(Debug)]
pub struct Yaz0Archive<R>
where
    R: Read + Seek,
{
    reader: R,

    data_start: usize,
    header: Yaz0Header,
}

impl<R> Yaz0Archive<R>
where
    R: Read + Seek,
{
    /// Creates a new `Yaz0` from a reader.
    pub fn new(mut reader: R) -> Result<Yaz0Archive<R>, Error> {
        // Parses header and advances reader to start of data
        let header = Yaz0Header::parse(&mut reader)?;

        let data_start = reader.seek(SeekFrom::Current(0))?;

        Ok(Yaz0Archive {
            reader,
            header,
            data_start: data_start as usize,
        })
    }

    /// Decompresses the Yaz0 file, producing a `Vec<u8>` of the decompressed data.
    pub fn decompress(&mut self) -> Result<Vec<u8>, Error> {
        let mut dest: Vec<u8> = Vec::with_capacity(self.header.expected_size);
        dest.resize(self.header.expected_size, 0x00);
        let mut dest_pos: usize = 0;

        let mut ops_left: u8 = 0;
        let mut code_byte: u8 = 0;

        while dest_pos < self.header.expected_size {
            if ops_left == 0 {
                code_byte = self.reader.read_u8()?;
                ops_left = 8;
            }

            if code_byte & 0x80 != 0 {
                dest[dest_pos] = self.reader.read_u8()?;
                dest_pos += 1;
            } else {
                let byte1: u8 = self.reader.read_u8()?;
                let byte2: u8 = self.reader.read_u8()?;

                // Calculate where the copy should start
                let dist = (((byte1 & 0xf) as usize) << 8) | (byte2 as usize);
                let run_base = dest_pos - (dist + 1);

                // Figure out how many bytes we have to copy
                let copy_len: usize = match byte1 >> 4 {
                    0 => self.reader.read_u8()? as usize + 0x12, // read the next input byte and add 0x12
                                                                 // to get the length to copy
                    n => n as usize + 2 // otherwise, just take the upper nybble of byte1 and add 2 to get the length
                };

                for i in 0..copy_len {
                    dest[dest_pos] = dest[run_base + i];
                    dest_pos += 1;
                }
            }

            // use next operation bit from the code byte
            code_byte <<= 1;
            ops_left -= 1;
        }

        Ok(dest)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    /// Deflate the Bianco Hills .szs file, and compare to the decompressed file produced by yaz0dec.
    #[test]
    fn test_deflate_bianco() {
        let data: &[u8] = include_bytes!("../data/bianco0.szs");
        let reference_decompressed: &[u8] = include_bytes!("../data/bianco0");

        let reader = Cursor::new(data);

        let mut f = Yaz0Archive::new(reader).unwrap();

        let deflated = f.decompress().unwrap();

        println!("{} :: {}", deflated.len(), reference_decompressed.len());

        assert!(deflated == reference_decompressed, "deflated bianco0 did not match reference deflation!");
    }

    /// Test loading a small constructed Yaz0 file containing random data.
    /// Note: this file will almost certainly error if decompression is attempted.
    #[test]
    fn test_load() {
        let data: &[u8] = &[
            // 'Yaz0'
            0x59, 0x61, 0x7a, 0x30,
            // 13371337 bytes, when deflated
            0x00, 0xcc, 0x07, 0xc9,
            // 8 bytes of zeros
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,

            // 20 bytes of (random) data
            0x69, 0x95, 0xa4, 0xa3,
            0x5f, 0xfd, 0xf6, 0x8c,
            0x7d, 0xee, 0x93, 0xc5,
            0x4a, 0x1f, 0xd3, 0x19,
            0xdc, 0x78, 0xfd, 0x3f,
        ];

        let cursor = Cursor::new(&data);
        let f = Yaz0Archive::new(cursor).unwrap();

        assert_eq!(f.header.expected_size, 13371337);
    }

    /// Check that the Yaz0 header parsing fails when provided with a file not starting with the Yaz0 magic.
    #[test]
    fn test_bad_magic() {
        let data: &[u8] = &[
            // 'Foo0'
            0x46, 0x6f, 0x6f, 0x30,
            // 13371337 bytes, when deflated
            0x00, 0xcc, 0x07, 0xc9,
            // 8 bytes of zeros
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,
        ];

        let cursor = Cursor::new(&data);
        let result = Yaz0Archive::new(cursor);

        assert!(result.is_err());
    }
}