Switch to libflate by default

Switch from flate2 to libflate by default for WebAssembly support.
flate2 can still be enabled using the "flate2" feature.
This commit is contained in:
Mike Welsh 2018-06-10 12:51:17 -07:00
parent 0983c262c5
commit ee534374c9
4 changed files with 51 additions and 15 deletions

View File

@ -10,11 +10,12 @@ description = "Read and write the Adobe Flash SWF file format."
[dependencies]
byteorder = "1.0"
flate2 = "1.0"
num-derive = "0.2"
num-traits = "0.2"
libflate = {version = "0.1", optional = true}
flate2 = {version = "1.0", optional = true}
xz2 = {version = "0.1.5", optional = true}
[features]
default = []
default = ["libflate"]
lzma-support = ["xz2"]

View File

@ -8,7 +8,10 @@
//! writing SWF data.
extern crate byteorder;
#[cfg(feature = "flate2")]
extern crate flate2;
#[cfg(feature = "libflate")]
extern crate libflate;
#[macro_use]
extern crate num_derive;
extern crate num_traits;

View File

@ -1,7 +1,6 @@
#![cfg_attr(any(feature="clippy", feature="cargo-clippy"), allow(float_cmp))]
use byteorder::{LittleEndian, ReadBytesExt};
use flate2::read::ZlibDecoder;
use std::collections::HashSet;
use std::io::{Error, ErrorKind, Read, Result};
use types::*;
@ -22,7 +21,7 @@ fn read_swf_header<'a, R: Read + 'a>(mut input: R) -> Result<(Swf, Reader<Box<Re
// Now the SWF switches to a compressed stream.
let decompressed_input: Box<Read> = match compression {
Compression::None => Box::new(input),
Compression::Zlib => Box::new(ZlibDecoder::new(input)),
Compression::Zlib => make_zlib_reader(input)?,
Compression::Lzma => make_lzma_reader(input)?,
};
@ -41,6 +40,24 @@ fn read_swf_header<'a, R: Read + 'a>(mut input: R) -> Result<(Swf, Reader<Box<Re
Ok((swf, reader))
}
#[cfg(feature = "flate2")]
fn make_zlib_reader<'a, R: Read + 'a>(input: R) -> Result<Box<Read + 'a>> {
use flate2::read::ZlibDecoder;
Ok(Box::new(ZlibDecoder::new(input)))
}
#[cfg(feature = "libflate")]
fn make_zlib_reader<'a, R: Read + 'a>(input: R) -> Result<Box<Read + 'a>> {
use libflate::zlib::Decoder;
let decoder = Decoder::new(input)?;
Ok(Box::new(decoder))
}
#[cfg(not(any(feature = "flate2",feature = "libflate")))]
fn make_zlib_reader<'a, R: Read + 'a>(_input: R) -> Result<Box<Read + 'a>> {
Err(Error::new(ErrorKind::InvalidData, "Support for Zlib compressed SWFs is not enabled."))
}
#[cfg(feature = "lzma-support")]
fn make_lzma_reader<'a, R: Read + 'a>(mut input: R) -> Result<Box<Read + 'a>> {
// Flash uses a mangled LZMA header, so we have to massage it into the normal
@ -56,11 +73,11 @@ fn make_lzma_reader<'a, R: Read + 'a>(mut input: R) -> Result<Box<Read + 'a>> {
lzma_header.write_u64::<LittleEndian>(uncompressed_length as u64)?;
let mut lzma_stream = Stream::new_lzma_decoder(u64::max_value())?;
lzma_stream.process(&lzma_header.into_inner(), &mut [0u8; 1], Action::Run)?;
Box::new(XzDecoder::new_stream(input, lzma_stream))
Ok(Box::new(XzDecoder::new_stream(input, lzma_stream)))
}
#[cfg(not(feature = "lzma-support"))]
fn make_lzma_reader<'a, R: Read + 'a>(_: R) -> Result<Box<Read + 'a>> {
fn make_lzma_reader<'a, R: Read + 'a>(_input: R) -> Result<Box<Read + 'a>> {
Err(Error::new(ErrorKind::InvalidData, "Support for LZMA compressed SWFs is not enabled."))
}

View File

@ -2,8 +2,6 @@
#![cfg_attr(any(feature="clippy", feature="cargo-clippy"), allow(float_cmp))]
use byteorder::{LittleEndian, WriteBytesExt};
use flate2::Compression as ZlibCompression;
use flate2::write::ZlibEncoder;
use std::cmp::max;
use std::collections::HashSet;
use std::io::{Error, ErrorKind, Result, Write};
@ -42,23 +40,40 @@ pub fn write_swf<W: Write>(swf: &Swf, mut output: W) -> Result<()> {
output.write_all(&swf_body)?;
}
Compression::Zlib => {
let mut encoder = ZlibEncoder::new(&mut output, ZlibCompression::best());
encoder.write_all(&swf_body)?;
}
Compression::Zlib => write_zlib_swf(&mut output, &swf_body)?,
// LZMA header.
// SWF format has a mangled LZMA header, so we have to do some magic to conver the
// standard LZMA header to SWF format.
// https://adobe.ly/2s8oYzn
Compression::Lzma => write_lzma_swf(&mut output)?,
Compression::Lzma => write_lzma_swf(&mut output, &swf_body)?,
};
Ok(())
}
#[cfg(feature = "flate2")]
fn write_zlib_swf<W: Write>(mut output: W, swf_body: &[u8]) -> Result<()> {
use flate2::Compression;
use flate2::write::ZlibEncoder;
let mut encoder = ZlibEncoder::new(&mut output, Compression::best());
encoder.write_all(&swf_body)
}
#[cfg(feature = "libflate")]
fn write_zlib_swf<W: Write>(mut output: W, swf_body: &[u8]) -> Result<()> {
use libflate::zlib::Encoder;
let mut encoder = Encoder::new(&mut output)?;
encoder.write_all(&swf_body)
}
#[cfg(not(any(feature = "flate2",feature = "libflate")))]
fn write_zlib_swf<W: Write>(_output: W, _swf_body: &[u8]) -> Result<()> {
Err(Error::new(ErrorKind::InvalidData, "Support for Zlib compressed SWFs is not enabled."))
}
#[cfg(feature = "lzma-support")]
fn write_lzma_swf<W: Write>(mut output: W) -> Result<()> {
fn write_lzma_swf<W: Write>(mut output: W, swf_body: &[u8]) -> Result<()> {
use xz2::write::XzEncoder;
use xz2::stream::{Action, LzmaOptions, Stream};
let mut stream = Stream::new_lzma_encoder(&LzmaOptions::new_preset(9)?)?;
@ -73,7 +88,7 @@ fn write_lzma_swf<W: Write>(mut output: W) -> Result<()> {
}
#[cfg(not(feature = "lzma-support"))]
fn write_lzma_swf<W: Write>(_: W) -> Result<()> {
fn write_lzma_swf<W: Write>(_output: W, _swf_body: &[u8]) -> Result<()> {
Err(Error::new(ErrorKind::InvalidData, "Support for LZMA compressed SWFs is not enabled."))
}