@@ -7,11 +7,9 @@ use bzip2::write::BzEncoder;
77use flate2:: Compression as GzCompression ;
88use flate2:: read:: MultiGzDecoder ;
99use flate2:: write:: GzEncoder ;
10- use lz4:: block:: CompressionMode ;
11- use lz4:: liblz4:: BlockChecksum ;
12- use lz4:: {
13- BlockMode , BlockSize , ContentChecksum , Decoder as LZ4FrameDecoder , Encoder as LZ4FrameEncoder ,
14- EncoderBuilder as LZ4FrameEncoderBuilder ,
10+ use lz4_flex:: frame:: {
11+ BlockSize as LZ4BlockSize , FrameDecoder as LZ4FrameDecoder , FrameEncoder as LZ4FrameEncoder ,
12+ FrameInfo as LZ4FrameInfo ,
1513} ;
1614use lzma_rust2:: { CheckType , LzmaOptions , LzmaReader , LzmaWriter , XzOptions , XzReader , XzWriter } ;
1715use std:: cmp:: min;
@@ -51,9 +49,7 @@ impl<W: Write> WriteFinish<W> for BufWriter<ZopFliEncoder<W>> {
5149
5250impl < W : Write > WriteFinish < W > for LZ4FrameEncoder < W > {
5351 fn finish ( self : Box < Self > ) -> std:: io:: Result < W > {
54- let ( w, r) = Self :: finish ( * self ) ;
55- r?;
56- Ok ( w)
52+ Ok ( Self :: finish ( * self ) ?)
5753 }
5854}
5955
@@ -65,40 +61,31 @@ impl<W: Write> WriteFinish<W> for LZ4FrameEncoder<W> {
6561// LZ4BlockEncoder
6662
6763const LZ4_BLOCK_SIZE : usize = 0x800000 ;
68- const LZ4HC_CLEVEL_MAX : i32 = 12 ;
64+ const LZ4HC_CLEVEL_MAX : u8 = 12 ;
6965const LZ4_MAGIC : u32 = 0x184c2102 ;
7066
7167struct LZ4BlockEncoder < W : Write > {
7268 write : W ,
7369 chunker : Chunker ,
74- out_buf : Box < [ u8 ] > ,
7570 total : u32 ,
7671 is_lg : bool ,
7772}
7873
7974impl < W : Write > LZ4BlockEncoder < W > {
8075 fn new ( write : W , is_lg : bool ) -> Self {
81- let out_sz = lz4:: block:: compress_bound ( LZ4_BLOCK_SIZE ) . unwrap_or ( LZ4_BLOCK_SIZE ) ;
8276 LZ4BlockEncoder {
8377 write,
8478 chunker : Chunker :: new ( LZ4_BLOCK_SIZE ) ,
85- // SAFETY: all bytes will be initialized before it is used
86- out_buf : unsafe { Box :: new_uninit_slice ( out_sz) . assume_init ( ) } ,
8779 total : 0 ,
8880 is_lg,
8981 }
9082 }
9183
92- fn encode_block ( write : & mut W , out_buf : & mut [ u8 ] , chunk : & [ u8 ] ) -> std:: io:: Result < ( ) > {
93- let compressed_size = lz4:: block:: compress_to_buffer (
94- chunk,
95- Some ( CompressionMode :: HIGHCOMPRESSION ( LZ4HC_CLEVEL_MAX ) ) ,
96- false ,
97- out_buf,
98- ) ?;
99- let block_size = compressed_size as u32 ;
84+ fn encode_block ( write : & mut W , chunk : & [ u8 ] ) -> std:: io:: Result < ( ) > {
85+ let compressed = lz4_flex:: block:: compress_hc_to_vec ( chunk, LZ4HC_CLEVEL_MAX ) ;
86+ let block_size = compressed. len ( ) as u32 ;
10087 write. write_pod ( & block_size) ?;
101- write. write_all ( & out_buf [ ..compressed_size ] )
88+ write. write_all ( & compressed )
10289 }
10390}
10491
@@ -123,7 +110,7 @@ impl<W: Write> Write for LZ4BlockEncoder<W> {
123110 let ( b, chunk) = self . chunker . add_data ( buf) ;
124111 buf = b;
125112 if let Some ( chunk) = chunk {
126- Self :: encode_block ( & mut self . write , & mut self . out_buf , chunk) ?;
113+ Self :: encode_block ( & mut self . write , chunk) ?;
127114 }
128115 }
129116 Ok ( ( ) )
@@ -134,7 +121,7 @@ impl<W: Write> WriteFinish<W> for LZ4BlockEncoder<W> {
134121 fn finish ( mut self : Box < Self > ) -> std:: io:: Result < W > {
135122 let chunk = self . chunker . get_available ( ) ;
136123 if !chunk. is_empty ( ) {
137- Self :: encode_block ( & mut self . write , & mut self . out_buf , chunk) ?;
124+ Self :: encode_block ( & mut self . write , chunk) ?;
138125 }
139126 if self . is_lg {
140127 self . write . write_pod ( & self . total ) ?;
@@ -155,10 +142,12 @@ struct LZ4BlockDecoder<R: Read> {
155142
156143impl < R : Read > LZ4BlockDecoder < R > {
157144 fn new ( read : R ) -> Self {
158- let compressed_sz = lz4:: block:: compress_bound ( LZ4_BLOCK_SIZE ) . unwrap_or ( LZ4_BLOCK_SIZE ) ;
159145 Self {
160146 read,
161- in_buf : unsafe { Box :: new_uninit_slice ( compressed_sz) . assume_init ( ) } ,
147+ in_buf : unsafe {
148+ Box :: new_uninit_slice ( lz4_flex:: block:: get_maximum_output_size ( LZ4_BLOCK_SIZE ) )
149+ . assume_init ( )
150+ } ,
162151 out_buf : unsafe { Box :: new_uninit_slice ( LZ4_BLOCK_SIZE ) . assume_init ( ) } ,
163152 out_len : 0 ,
164153 out_pos : 0 ,
@@ -200,11 +189,8 @@ impl<R: Read> Read for LZ4BlockDecoder<R> {
200189 }
201190 }
202191
203- self . out_len = lz4:: block:: decompress_to_buffer (
204- compressed_block,
205- Some ( LZ4_BLOCK_SIZE as i32 ) ,
206- & mut self . out_buf ,
207- ) ?;
192+ self . out_len = lz4_flex:: block:: decompress_into ( compressed_block, & mut self . out_buf )
193+ . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , e) ) ?;
208194 self . out_pos = 0 ;
209195 }
210196 let copy_len = min ( buf. len ( ) , self . out_len - self . out_pos ) ;
@@ -232,17 +218,14 @@ pub fn get_encoder<'a, W: Write + 'a>(
232218 None ,
233219 ) ?) ,
234220 FileFormat :: BZIP2 => Box :: new ( BzEncoder :: new ( w, BzCompression :: best ( ) ) ) ,
235- FileFormat :: LZ4 => {
236- let encoder = LZ4FrameEncoderBuilder :: new ( )
237- . block_size ( BlockSize :: Max4MB )
238- . block_mode ( BlockMode :: Independent )
239- . checksum ( ContentChecksum :: ChecksumEnabled )
240- . block_checksum ( BlockChecksum :: BlockChecksumEnabled )
241- . level ( 9 )
242- . auto_flush ( true )
243- . build ( w) ?;
244- Box :: new ( encoder)
245- }
221+ FileFormat :: LZ4 => Box :: new ( LZ4FrameEncoder :: with_compression_level (
222+ LZ4FrameInfo :: new ( )
223+ . block_size ( LZ4BlockSize :: Max4MB )
224+ . block_checksums ( true )
225+ . content_checksum ( true ) ,
226+ w,
227+ LZ4HC_CLEVEL_MAX ,
228+ ) ) ,
246229 FileFormat :: LZ4_LEGACY => Box :: new ( LZ4BlockEncoder :: new ( w, false ) ) ,
247230 FileFormat :: LZ4_LG => Box :: new ( LZ4BlockEncoder :: new ( w, true ) ) ,
248231 FileFormat :: ZOPFLI => {
@@ -267,7 +250,7 @@ pub fn get_decoder<'a, R: Read + 'a>(
267250 FileFormat :: XZ => Box :: new ( XzReader :: new ( r, true ) ) ,
268251 FileFormat :: LZMA => Box :: new ( LzmaReader :: new_mem_limit ( r, u32:: MAX , None ) ?) ,
269252 FileFormat :: BZIP2 => Box :: new ( BzDecoder :: new ( r) ) ,
270- FileFormat :: LZ4 => Box :: new ( LZ4FrameDecoder :: new ( r) ? ) ,
253+ FileFormat :: LZ4 => Box :: new ( LZ4FrameDecoder :: new ( r) ) ,
271254 FileFormat :: LZ4_LG | FileFormat :: LZ4_LEGACY => Box :: new ( LZ4BlockDecoder :: new ( r) ) ,
272255 FileFormat :: ZOPFLI | FileFormat :: GZIP => Box :: new ( MultiGzDecoder :: new ( r) ) ,
273256 _ => unreachable ! ( ) ,
0 commit comments