@@ -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 }
@@ -1973,6 +1973,15 @@ fn DecideOverLiteralContextModeling(input: &[u8],
19731973 literal_context_map) ;
19741974 }
19751975}
1976+ fn WriteEmptyLastBlocksInternal ( params : & BrotliEncoderParams , storage_ix : & mut usize , storage : & mut [ u8 ] ) {
1977+ // insert empty block for byte alignment if required
1978+ if params. byte_align {
1979+ BrotliWritePaddingMetaBlock ( storage_ix, storage) ;
1980+ }
1981+ if !params. bare_stream {
1982+ BrotliWriteEmptyLastMetaBlock ( storage_ix, storage)
1983+ }
1984+ }
19761985fn WriteMetaBlockInternal < Alloc : BrotliAlloc ,
19771986 Cb >
19781987 ( alloc : & mut Alloc ,
@@ -2011,8 +2020,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
20112020 let literal_context_lut = BROTLI_CONTEXT_LUT ( literal_context_mode) ;
20122021 let mut block_params = params. clone ( ) ;
20132022 if bytes == 0usize {
2014- BrotliWriteBits ( 2usize , 3 , storage_ix, storage) ;
2015- * storage_ix = ( * storage_ix) . wrapping_add ( 7u32 as ( usize ) ) & !7u32 as ( usize ) ;
2023+ WriteEmptyLastBlocksInternal ( params, storage_ix, storage) ;
20162024 return ;
20172025 }
20182026 if ShouldCompress ( data,
@@ -2035,13 +2043,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
20352043 false ,
20362044 cb) ;
20372045 if actual_is_last != is_last {
2038- // insert empty block for byte alignment if required
2039- if params. byte_align && ( ( * storage_ix & 7u32 as ( usize ) ) != 0 ) {
2040- BrotliStoreSyncMetaBlock ( storage_ix, storage) ;
2041- }
2042- if !params. bare_stream {
2043- BrotliWriteEmptyLastMetaBlock ( storage_ix, storage)
2044- }
2046+ WriteEmptyLastBlocksInternal ( params, storage_ix, storage) ;
20452047 }
20462048 return ;
20472049 }
@@ -2181,13 +2183,7 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
21812183 cb) ;
21822184 }
21832185 if actual_is_last != is_last {
2184- // insert empty block for byte alignment if required
2185- if params. byte_align && ( ( * storage_ix & 7u32 as ( usize ) ) != 0 ) {
2186- BrotliStoreSyncMetaBlock ( storage_ix, storage) ;
2187- }
2188- if !params. bare_stream {
2189- BrotliWriteEmptyLastMetaBlock ( storage_ix, storage)
2190- }
2186+ WriteEmptyLastBlocksInternal ( params, storage_ix, storage) ;
21912187 }
21922188}
21932189
@@ -2284,6 +2280,10 @@ fn EncodeData<Alloc: BrotliAlloc,
22842280 * out_size = catable_header_size;
22852281 s. is_first_mb = IsFirst :: HeaderWritten ;
22862282 }
2283+ // fixup for empty stream - note: catable is always appendable
2284+ if bytes == 0 && s. params . byte_align && s. params . appendable && !s. params . catable {
2285+ BrotliWritePaddingMetaBlock ( & mut storage_ix, ( * s) . storage_ . slice_mut ( ) ) ;
2286+ }
22872287 }
22882288 if let IsFirst :: BothCatableBytesWritten = s. is_first_mb {
22892289 // nothing to do here, move along
0 commit comments