@@ -16,10 +16,10 @@ use super::bit_cost::{BitsEntropy, ShannonEntropy};
1616use super :: block_split:: BlockSplit ;
1717#[ allow( unused_imports) ]
1818use super :: brotli_bit_stream:: { BrotliBuildAndStoreHuffmanTreeFast , BrotliStoreHuffmanTree ,
19- BrotliStoreMetaBlock , BrotliStoreSyncMetaBlock ,
20- BrotliStoreMetaBlockFast , BrotliStoreMetaBlockTrivial ,
21- BrotliStoreUncompressedMetaBlock ,
22- BrotliWriteEmptyLastMetaBlock , BrotliWriteMetadataMetaBlock ,
19+ BrotliStoreMetaBlock , BrotliStoreMetaBlockFast ,
20+ BrotliStoreMetaBlockTrivial , BrotliStoreUncompressedMetaBlock ,
21+ BrotliWriteEmptyLastMetaBlock ,
22+ BrotliWritePaddingMetaBlock , BrotliWriteMetadataMetaBlock ,
2323 MetaBlockSplit , RecoderState , JumpToByteBoundary } ;
2424
2525use enc:: input_pair:: InputReferenceMut ;
@@ -740,7 +740,7 @@ fn EnsureInitialized<Alloc: BrotliAlloc>
740740 if ( * s) . params . quality == 0i32 || ( * s) . params . quality == 1i32 {
741741 lgwin = brotli_max_int ( lgwin, 18i32 ) ;
742742 }
743- if !( * s) . params . bare_stream {
743+ if !( ( * s) . params . catable && ( * s ) . params . bare_stream ) {
744744 EncodeWindowBits ( lgwin, s. params . large_window , & mut ( * s) . last_bytes_ , & mut ( * s) . last_bytes_bits_ ) ;
745745 }
746746 }
@@ -1969,6 +1969,15 @@ fn DecideOverLiteralContextModeling(input: &[u8],
19691969 literal_context_map) ;
19701970 }
19711971}
1972+ fn WriteEmptyLastBlocksInternal ( params : & BrotliEncoderParams , storage_ix : & mut usize , storage : & mut [ u8 ] ) {
1973+ // insert empty block for byte alignment if required
1974+ if params. byte_align {
1975+ BrotliWritePaddingMetaBlock ( storage_ix, storage) ;
1976+ }
1977+ if !params. bare_stream {
1978+ BrotliWriteEmptyLastMetaBlock ( storage_ix, storage)
1979+ }
1980+ }
19721981fn WriteMetaBlockInternal < Alloc : BrotliAlloc ,
19731982 Cb >
19741983 ( alloc : & mut Alloc ,
@@ -2007,8 +2016,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
20072016 let literal_context_lut = BROTLI_CONTEXT_LUT ( literal_context_mode) ;
20082017 let mut block_params = params. clone ( ) ;
20092018 if bytes == 0usize {
2010- BrotliWriteBits ( 2usize , 3 , storage_ix, storage) ;
2011- * storage_ix = ( * storage_ix) . wrapping_add ( 7u32 as ( usize ) ) & !7u32 as ( usize ) ;
2019+ WriteEmptyLastBlocksInternal ( params, storage_ix, storage) ;
20122020 return ;
20132021 }
20142022 if ShouldCompress ( data,
@@ -2031,13 +2039,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
20312039 false ,
20322040 cb) ;
20332041 if actual_is_last != is_last {
2034- // insert empty block for byte alignment if required
2035- if params. byte_align && ( ( * storage_ix & 7u32 as ( usize ) ) != 0 ) {
2036- BrotliStoreSyncMetaBlock ( storage_ix, storage) ;
2037- }
2038- if !params. bare_stream {
2039- BrotliWriteEmptyLastMetaBlock ( storage_ix, storage)
2040- }
2042+ WriteEmptyLastBlocksInternal ( params, storage_ix, storage) ;
20412043 }
20422044 return ;
20432045 }
@@ -2177,13 +2179,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
21772179 cb) ;
21782180 }
21792181 if actual_is_last != is_last {
2180- // insert empty block for byte alignment if required
2181- if params. byte_align && ( ( * storage_ix & 7u32 as ( usize ) ) != 0 ) {
2182- BrotliStoreSyncMetaBlock ( storage_ix, storage) ;
2183- }
2184- if !params. bare_stream {
2185- BrotliWriteEmptyLastMetaBlock ( storage_ix, storage)
2186- }
2182+ WriteEmptyLastBlocksInternal ( params, storage_ix, storage) ;
21872183 }
21882184}
21892185
@@ -2280,6 +2276,10 @@ fn EncodeData<Alloc: BrotliAlloc,
22802276 * out_size = catable_header_size;
22812277 s. is_first_mb = IsFirst :: HeaderWritten ;
22822278 }
2279+ // fixup for empty stream - note: catable is always appendable
2280+ if bytes == 0 && s. params . byte_align && s. params . appendable && !s. params . catable {
2281+ BrotliWritePaddingMetaBlock ( & mut storage_ix, ( * s) . storage_ . slice_mut ( ) ) ;
2282+ }
22832283 }
22842284 if let IsFirst :: BothCatableBytesWritten = s. is_first_mb {
22852285 // nothing to do here, move along
0 commit comments