swf_headers/
decoded_swf.rs

1use std::io;
2use std::io::Read;
3use std::fs::File;
4
5use flate2::FlateReadExt;
6use flate2::read::ZlibDecoder;
7use lzma;
8
9use super::Signature;
10use error::Error;
11
12enum Inner<R: Read> {
13    Raw(File),
14    Zlib(ZlibDecoder<R>),
15    Lzma(lzma::Reader<R>)
16}
17
18/// Handles decompressing swf innards and reading the results.
19///
20/// This is a helper struct abstracting over the various kinds of compression
21/// SWF files can use, namely zlib and LZMA.
22pub struct DecodedSwf {
23    _inner: Inner<File>
24}
25
26impl DecodedSwf {
27    /// Takes a file and a SWF signature, and handles decompressing the file
28    /// accordingly, returning a reader.
29    pub fn decompress(file: File, sig: Signature) -> Result<Self, super::Error> {
30        let inner = match sig {
31            Signature::Uncompressed => Inner::Raw(file),
32            Signature::ZlibCompressed => Inner::Zlib(file.zlib_decode()),
33            Signature::LzmaCompressed => Inner::Lzma(try!(lzma::Reader::from(file)))
34        };
35        Ok(DecodedSwf {
36            _inner: inner
37        })
38    }
39}
40
41impl Read for DecodedSwf {
42    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
43        match self._inner {
44            Inner::Raw(ref mut f) => f.read(buf),
45            Inner::Zlib(ref mut f) => f.read(buf),
46            Inner::Lzma(ref mut f) => f.read(buf)
47        }
48    }
49}