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
use std::io;
use std::io::Read;
use std::fs::File;

use flate2::FlateReadExt;
use flate2::read::ZlibDecoder;
use lzma;

use super::Signature;
use error::Error;

enum Inner<R: Read> {
    Raw(File),
    Zlib(ZlibDecoder<R>),
    Lzma(lzma::Reader<R>)
}

/// Handles decompressing swf innards and reading the results.
///
/// This is a helper struct abstracting over the various kinds of compression
/// SWF files can use, namely zlib and LZMA.
pub struct DecodedSwf {
    _inner: Inner<File>
}

impl DecodedSwf {
    /// Takes a file and a SWF signature, and handles decompressing the file
    /// accordingly, returning a reader.
    pub fn decompress(file: File, sig: Signature) -> Result<Self, super::Error> {
        let inner = match sig {
            Signature::Uncompressed => Inner::Raw(file),
            Signature::ZlibCompressed => Inner::Zlib(file.zlib_decode()),
            Signature::LzmaCompressed => Inner::Lzma(try!(lzma::Reader::from(file)))
        };
        Ok(DecodedSwf {
            _inner: inner
        })
    }
}

impl Read for DecodedSwf {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self._inner {
            Inner::Raw(ref mut f) => f.read(buf),
            Inner::Zlib(ref mut f) => f.read(buf),
            Inner::Lzma(ref mut f) => f.read(buf)
        }
    }
}