forked from google/zopfli
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request google#34 from shepmaster/writer
Output to a generic that implements Write
- Loading branch information
Showing
7 changed files
with
195 additions
and
120 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ readme = "README" | |
libc = "0.2" | ||
crc = "1.2" | ||
adler32 = "0.2" | ||
byteorder = "0.5.3" | ||
|
||
[profile.release] | ||
debug = true |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
use std::io::{self, Write}; | ||
use crc::crc32; | ||
use byteorder::{LittleEndian, WriteBytesExt}; | ||
|
||
use deflate::{deflate, BlockType}; | ||
use Options; | ||
|
||
/// Compresses the data according to the gzip specification, RFC 1952. | ||
pub fn gzip_compress(options: &Options, in_data: &[u8], out: &mut Vec<u8>) { | ||
out.push(31); /* ID1 */ | ||
out.push(139); /* ID2 */ | ||
out.push(8); /* CM */ | ||
out.push(0); /* FLG */ | ||
/* MTIME */ | ||
out.push(0); | ||
out.push(0); | ||
out.push(0); | ||
out.push(0); | ||
static HEADER: &'static [u8] = &[ | ||
31, // ID1 | ||
139, // ID2 | ||
8, // CM | ||
0, // FLG | ||
|
||
out.push(2); /* XFL, 2 indicates best compression. */ | ||
out.push(3); /* OS follows Unix conventions. */ | ||
0, // MTIME | ||
0, | ||
0, | ||
0, | ||
|
||
deflate(options, BlockType::Dynamic, in_data, out); | ||
2, // XFL, 2 indicates best compression. | ||
3, // OS follows Unix conventions. | ||
]; | ||
|
||
let crc = crc32::checksum_ieee(in_data); | ||
/// Compresses the data according to the gzip specification, RFC 1952. | ||
pub fn gzip_compress<W>(options: &Options, in_data: &[u8], mut out: W) -> io::Result<()> | ||
where W: Write | ||
{ | ||
try!(out.by_ref().write_all(HEADER)); | ||
|
||
out.push(crc as u8); | ||
out.push((crc >> 8) as u8); | ||
out.push((crc >> 16) as u8); | ||
out.push((crc >> 24) as u8); | ||
try!(deflate(options, BlockType::Dynamic, in_data, out.by_ref())); | ||
|
||
let input_size = in_data.len() as u32; | ||
out.push(input_size as u8); | ||
out.push((input_size >> 8) as u8); | ||
out.push((input_size >> 16) as u8); | ||
out.push((input_size >> 24) as u8); | ||
try!(out.by_ref().write_u32::<LittleEndian>(crc32::checksum_ieee(in_data))); | ||
out.write_u32::<LittleEndian>(in_data.len() as u32) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
use std::io; | ||
|
||
use std::io::{self, Write}; | ||
use adler32::adler32; | ||
use byteorder::{LittleEndian, WriteBytesExt}; | ||
|
||
use deflate::{deflate, BlockType}; | ||
use Options; | ||
|
||
pub fn zlib_compress(options: &Options, in_data: &[u8], out: &mut Vec<u8>) { | ||
let checksum = adler32(io::Cursor::new(&in_data)).expect("Error with adler32"); | ||
pub fn zlib_compress<W>(options: &Options, in_data: &[u8], mut out: W) -> io::Result<()> | ||
where W: Write | ||
{ | ||
let cmf = 120; /* CM 8, CINFO 7. See zlib spec.*/ | ||
let flevel = 3; | ||
let fdict = 0; | ||
let mut cmfflg = 256 * cmf + fdict * 32 + flevel * 64; | ||
let fcheck = 31 - cmfflg % 31; | ||
cmfflg += fcheck; | ||
|
||
out.push((cmfflg / 256) as u8); | ||
out.push((cmfflg % 256) as u8); | ||
try!(out.by_ref().write_u16::<LittleEndian>(cmfflg)); | ||
|
||
deflate(options, BlockType::Dynamic, in_data, out); | ||
try!(deflate(options, BlockType::Dynamic, in_data, out.by_ref())); | ||
|
||
out.push(((checksum >> 24) % 256) as u8); | ||
out.push(((checksum >> 16) % 256) as u8); | ||
out.push(((checksum >> 8) % 256) as u8); | ||
out.push((checksum % 256) as u8); | ||
let checksum = adler32(io::Cursor::new(&in_data)).expect("Error with adler32"); | ||
out.write_u32::<LittleEndian>(checksum) | ||
} |