diff --git a/justfile b/justfile index 8334fbb3..dd619c0a 100644 --- a/justfile +++ b/justfile @@ -20,6 +20,8 @@ build-simd: # Build the brotli-ffi crate (in ./c dir) build-ffi: + # TODO: The c/Cargo.toml does not depend on the **unpublished** main crate, so its build never actually gets tested + RUSTFLAGS='-D warnings' cargo build --features ffi-api RUSTFLAGS='-D warnings' cargo build --workspace --all-targets --bins --tests --lib --benches --examples --manifest-path c/Cargo.toml # For now, use original make file for building/testing the FFI crate cd c && make diff --git a/src/enc/backward_references/hq.rs b/src/enc/backward_references/hq.rs index 1b1205a0..6e4857b2 100644 --- a/src/enc/backward_references/hq.rs +++ b/src/enc/backward_references/hq.rs @@ -1030,7 +1030,7 @@ pub fn BrotliCreateZopfliBackwardReferences< } } -fn SetCost(histogram: &[u32], histogram_size: usize, literal_histogram: i32, cost: &mut [floatX]) { +fn SetCost(histogram: &[u32], histogram_size: usize, literal_histogram: bool, cost: &mut [floatX]) { let mut sum: u64 = 0; for i in 0..histogram_size { sum = sum.wrapping_add(u64::from(histogram[i])); @@ -1038,7 +1038,7 @@ fn SetCost(histogram: &[u32], histogram_size: usize, literal_histogram: i32, cos let log2sum = FastLog2(sum); let mut missing_symbol_sum = sum; - if literal_histogram == 0 { + if !literal_histogram { for i in 0..histogram_size { if histogram[i] == 0 { missing_symbol_sum = missing_symbol_sum.wrapping_add(1); @@ -1107,19 +1107,19 @@ impl> ZopfliCostModel { SetCost( &histogram_literal[..], BROTLI_NUM_LITERAL_SYMBOLS, - 1i32, + true, &mut cost_literal, ); SetCost( &histogram_cmd[..], BROTLI_NUM_COMMAND_SYMBOLS, - 0i32, + false, &mut cost_cmd[..], ); SetCost( &histogram_dist[..], self.distance_histogram_size as usize, - 0i32, + false, self.cost_dist_.slice_mut(), ); for i in 0usize..704usize { diff --git a/src/enc/brotli_bit_stream.rs b/src/enc/brotli_bit_stream.rs index 49799da5..7b0312a9 100644 --- a/src/enc/brotli_bit_stream.rs +++ b/src/enc/brotli_bit_stream.rs @@ -1555,7 +1555,7 @@ fn StoreBlockSwitch( code: &mut BlockSplitCode, block_len: u32, block_type: u8, - is_first_block: i32, + is_first_block: bool, storage_ix: &mut usize, storage: &mut [u8], ) { @@ -1563,7 +1563,7 @@ fn StoreBlockSwitch( let mut lencode: usize = 0; let mut len_nextra: u32 = 0; let mut len_extra: u32 = 0; - if is_first_block == 0 { + if !is_first_block { BrotliWriteBits( code.type_depths[typecode] as u8, code.type_bits[typecode] as (u64), @@ -1634,7 +1634,7 @@ fn BuildAndStoreBlockSplitCode( storage_ix, storage, ); - StoreBlockSwitch(code, lengths[0], types[0], 1i32, storage_ix, storage); + StoreBlockSwitch(code, lengths[0], types[0], true, storage_ix, storage); } } @@ -1962,7 +1962,7 @@ impl + Allocator> BlockEncoder<'_, Alloc> { &mut self.block_split_code_, block_len, block_type, - 0i32, + false, storage_ix, storage, ); @@ -2059,7 +2059,7 @@ impl + Allocator> BlockEncoder<'_, Alloc> { &mut self.block_split_code_, block_len, block_type, - 0, + false, storage_ix, storage, ); diff --git a/src/enc/cluster.rs b/src/enc/cluster.rs index 7cb6b51e..c01c0908 100644 --- a/src/enc/cluster.rs +++ b/src/enc/cluster.rs @@ -62,7 +62,7 @@ fn BrotliCompareAndPushToQueue< pairs: &mut [HistogramPair], num_pairs: &mut usize, ) { - let mut is_good_pair: i32 = 0i32; + let mut is_good_pair = false; let mut p: HistogramPair = HistogramPair { idx1: 0, idx2: 0, @@ -85,10 +85,10 @@ fn BrotliCompareAndPushToQueue< p.cost_diff -= (out[idx2 as usize]).bit_cost(); if (out[idx1 as usize]).total_count() == 0usize { p.cost_combo = (out[idx2 as usize]).bit_cost(); - is_good_pair = 1i32; + is_good_pair = true; } else if (out[idx2 as usize]).total_count() == 0usize { p.cost_combo = (out[idx1 as usize]).bit_cost(); - is_good_pair = 1i32; + is_good_pair = true; } else { let threshold: super::util::floatX = if *num_pairs == 0usize { 1e38 as super::util::floatX @@ -101,10 +101,10 @@ fn BrotliCompareAndPushToQueue< let cost_combo: super::util::floatX = BrotliPopulationCost(&combo, scratch_space); if cost_combo < threshold - p.cost_diff { p.cost_combo = cost_combo; - is_good_pair = 1i32; + is_good_pair = true; } } - if is_good_pair != 0 { + if is_good_pair { p.cost_diff += p.cost_combo; if *num_pairs > 0usize && HistogramPairIsLess(&pairs[0], &p) { /* Replace the top of the queue if needed. */ diff --git a/src/enc/compress_fragment.rs b/src/enc/compress_fragment.rs index 5bd6d593..0e8420c9 100644 --- a/src/enc/compress_fragment.rs +++ b/src/enc/compress_fragment.rs @@ -7,11 +7,11 @@ use core::cmp::min; // examples: IsMatch checks p1[4] and p1[5] // the hoops that BuildAndStoreCommandPrefixCode goes through are subtly different in order // (eg memcpy x+24, y instead of +24, y+40 -// pretty much assume compress_fragment_two_pass is a trap! except for BrotliStoreMetaBlockHeader +// pretty much assume compress_fragment_two_pass is a trap! except for store_meta_block_header use super::super::alloc; use super::backward_references::kHashMul32; use super::brotli_bit_stream::{BrotliBuildAndStoreHuffmanTreeFast, BrotliStoreHuffmanTree}; -use super::compress_fragment_two_pass::{memcpy, BrotliStoreMetaBlockHeader, BrotliWriteBits}; +use super::compress_fragment_two_pass::{memcpy, BrotliWriteBits}; use super::entropy_encode::{ BrotliConvertBitDepthsToSymbols, BrotliCreateHuffmanTree, HuffmanTree, }; @@ -19,6 +19,7 @@ use super::static_dict::{ FindMatchLengthWithLimit, BROTLI_UNALIGNED_LOAD32, BROTLI_UNALIGNED_LOAD64, }; use super::util::{FastLog2, Log2FloorNonZero}; +use crate::enc::compress_fragment_two_pass::store_meta_block_header; //use super::super::alloc::{SliceWrapper, SliceWrapperMut}; //static kHashMul32: u32 = 0x1e35a7bdu32; @@ -242,7 +243,7 @@ fn EmitUncompressedMetaBlock( storage: &mut [u8], ) { RewindBitPosition(storage_ix_start, storage_ix, storage); - BrotliStoreMetaBlockHeader(len, 1i32, storage_ix, storage); + store_meta_block_header(len, true, storage_ix, storage); *storage_ix = storage_ix.wrapping_add(7u32 as usize) & !7u32 as usize; memcpy(storage, (*storage_ix >> 3), begin, 0, len); *storage_ix = storage_ix.wrapping_add(len << 3); @@ -683,7 +684,7 @@ fn BrotliCompressFragmentFastImpl>( let mut input_index = 0usize; let mut last_distance: i32; let shift: usize = (64u32 as usize).wrapping_sub(table_bits); - BrotliStoreMetaBlockHeader(block_size, 0i32, storage_ix, storage); + store_meta_block_header(block_size, false, storage_ix, storage); BrotliWriteBits(13usize, 0, storage_ix, storage); literal_ratio = BuildAndStoreLiteralPrefixCode( m, @@ -1026,7 +1027,7 @@ fn BrotliCompressFragmentFastImpl>( block_size = min(input_size, kFirstBlockSize); total_block_size = block_size; mlen_storage_ix = storage_ix.wrapping_add(3); - BrotliStoreMetaBlockHeader(block_size, 0i32, storage_ix, storage); + store_meta_block_header(block_size, false, storage_ix, storage); BrotliWriteBits(13usize, 0, storage_ix, storage); literal_ratio = BuildAndStoreLiteralPrefixCode( m, diff --git a/src/enc/compress_fragment_two_pass.rs b/src/enc/compress_fragment_two_pass.rs index 29c1044a..0d8e8059 100644 --- a/src/enc/compress_fragment_two_pass.rs +++ b/src/enc/compress_fragment_two_pass.rs @@ -185,7 +185,7 @@ fn CreateCommands( ); let ip_limit: usize = input_index.wrapping_add(len_limit); let mut next_hash: u32; - let mut goto_emit_remainder: i32 = 0i32; + let mut goto_emit_remainder = false; next_hash = Hash( &base_ip[{ ip_index = ip_index.wrapping_add(1); @@ -194,7 +194,7 @@ fn CreateCommands( shift, min_match, ); - while goto_emit_remainder == 0 { + while !goto_emit_remainder { let mut skip: u32 = 32u32; let mut next_ip: usize = ip_index; let mut candidate: usize = 0; @@ -208,7 +208,7 @@ fn CreateCommands( ip_index = next_ip; next_ip = ip_index.wrapping_add(bytes_between_hash_lookups as usize); if next_ip > ip_limit { - goto_emit_remainder = 1i32; + goto_emit_remainder = true; { break 'break3; } @@ -233,12 +233,12 @@ fn CreateCommands( } if !(ip_index.wrapping_sub(candidate) > (1usize << 18).wrapping_sub(16) as isize as usize - && (goto_emit_remainder == 0)) + && !goto_emit_remainder) { break; } } - if goto_emit_remainder != 0 { + if goto_emit_remainder { break; } { @@ -269,7 +269,7 @@ fn CreateCommands( *num_commands += EmitCopyLenLastDistance(matched, commands); next_emit = ip_index; if ip_index >= ip_limit { - goto_emit_remainder = 1i32; + goto_emit_remainder = true; { break; } @@ -325,7 +325,7 @@ fn CreateCommands( *num_commands += EmitDistance(last_distance as u32, commands); next_emit = ip_index; if ip_index >= ip_limit { - goto_emit_remainder = 1i32; + goto_emit_remainder = true; { break; } @@ -365,7 +365,7 @@ fn CreateCommands( table[(cur_hash as usize)] = ip_index as i32; } } - if goto_emit_remainder == 0 { + if !goto_emit_remainder { next_hash = Hash( &base_ip[{ ip_index = ip_index.wrapping_add(1); @@ -418,11 +418,22 @@ pub fn BrotliWriteBits(n_bits: usize, bits: u64, pos: &mut usize, array: &mut [u BROTLI_UNALIGNED_STORE64(p, v); *pos = pos.wrapping_add(n_bits); } + +#[deprecated(note = "use store_meta_block_header instead")] pub fn BrotliStoreMetaBlockHeader( len: usize, is_uncompressed: i32, storage_ix: &mut usize, storage: &mut [u8], +) { + store_meta_block_header(len, is_uncompressed != 0, storage_ix, storage); +} + +pub(crate) fn store_meta_block_header( + len: usize, + is_uncompressed: bool, + storage_ix: &mut usize, + storage: &mut [u8], ) { let mut nibbles: u64 = 6; BrotliWriteBits(1, 0, storage_ix, storage); @@ -438,7 +449,7 @@ pub fn BrotliStoreMetaBlockHeader( storage_ix, storage, ); - BrotliWriteBits(1usize, is_uncompressed as (u64), storage_ix, storage); + BrotliWriteBits(1, u64::from(is_uncompressed), storage_ix, storage); } pub fn memcpy( @@ -639,7 +650,7 @@ fn EmitUncompressedMetaBlock( storage_ix: &mut usize, storage: &mut [u8], ) { - BrotliStoreMetaBlockHeader(input_size, 1i32, storage_ix, storage); + store_meta_block_header(input_size, true, storage_ix, storage); *storage_ix = storage_ix.wrapping_add(7u32 as usize) & !7u32 as usize; memcpy(storage, (*storage_ix >> 3), input, 0, input_size); *storage_ix = storage_ix.wrapping_add(input_size << 3); @@ -683,7 +694,7 @@ fn BrotliCompressFragmentTwoPassImpl>( ); } if ShouldCompress(&base_ip[input_index..], block_size, num_literals) { - BrotliStoreMetaBlockHeader(block_size, 0i32, storage_ix, storage); + store_meta_block_header(block_size, false, storage_ix, storage); BrotliWriteBits(13usize, 0, storage_ix, storage); StoreCommands( m, diff --git a/src/enc/encode.rs b/src/enc/encode.rs index b9928621..8ee6a267 100644 --- a/src/enc/encode.rs +++ b/src/enc/encode.rs @@ -1402,6 +1402,8 @@ fn MakeUncompressedStream(input: &[u8], input_size: usize, output: &mut [u8]) -> result = result.wrapping_add(1); result } + +#[deprecated(note = "Use encoder_compress instead")] pub fn BrotliEncoderCompress< Alloc: BrotliAlloc, MetablockCallback: FnMut( @@ -1422,23 +1424,58 @@ pub fn BrotliEncoderCompress< encoded_buffer: &mut [u8], metablock_callback: &mut MetablockCallback, ) -> i32 { + encoder_compress( + empty_m8, + m8, + quality, + lgwin, + mode, + input_size, + input_buffer, + encoded_size, + encoded_buffer, + metablock_callback, + ) + .into() +} + +pub(crate) fn encoder_compress< + Alloc: BrotliAlloc, + MetablockCallback: FnMut( + &mut interface::PredictionModeContextMap, + &mut [interface::StaticCommand], + interface::InputPair, + &mut Alloc, + ), +>( + empty_m8: Alloc, + m8: &mut Alloc, + quality: i32, + lgwin: i32, + mode: BrotliEncoderMode, + input_size: usize, + input_buffer: &[u8], + encoded_size: &mut usize, + encoded_buffer: &mut [u8], + metablock_callback: &mut MetablockCallback, +) -> bool { let out_size: usize = *encoded_size; let input_start = input_buffer; let output_start = encoded_buffer; let max_out_size: usize = BrotliEncoderMaxCompressedSize(input_size); - if out_size == 0usize { - return 0i32; + if out_size == 0 { + return false; } - if input_size == 0usize { + if input_size == 0 { *encoded_size = 1; output_start[0] = 6; - return 1i32; + return true; } - let mut is_fallback: i32 = 0i32; - if quality == 10i32 { - panic!("Unimplemented: need to set 9.5 here"); + let mut is_fallback = false; + if quality == 10 { + unimplemented!("need to set 9.5 here"); } - if is_fallback == 0 { + if !is_fallback { let mut s_orig = BrotliEncoderStateStruct::new(core::mem::replace(m8, empty_m8)); let mut result: bool; { @@ -1480,21 +1517,21 @@ pub fn BrotliEncoderCompress< } let _ = core::mem::replace(m8, s_orig.m8); if !result || max_out_size != 0 && (*encoded_size > max_out_size) { - is_fallback = 1i32; + is_fallback = true; } else { - return 1i32; + return true; } } - assert_ne!(is_fallback, 0); - *encoded_size = 0usize; + assert_ne!(is_fallback, false); + *encoded_size = 0; if max_out_size == 0 { - return 0i32; + return false; } if out_size >= max_out_size { *encoded_size = MakeUncompressedStream(input_start, input_size, output_start); - return 1i32; + return true; } - 0i32 + false } impl BrotliEncoderStateStruct { @@ -1531,13 +1568,13 @@ impl BrotliEncoderStateStruct { next_out_array: &mut [u8], next_out_offset: &mut usize, total_out: &mut Option, - ) -> i32 { + ) -> bool { if self.stream_state_ as i32 == BrotliEncoderStreamState::BROTLI_STREAM_FLUSH_REQUESTED as i32 && (self.last_bytes_bits_ as i32 != 0i32) { self.inject_byte_padding_block(); - return 1i32; + return true; } if self.available_out_ != 0usize && (*available_out != 0usize) { let copy_output_size: usize = min(self.available_out_, *available_out); @@ -1552,9 +1589,9 @@ impl BrotliEncoderStateStruct { if let &mut Some(ref mut total_out_inner) = total_out { *total_out_inner = self.total_out_ as usize; } - return 1i32; + return true; } - 0i32 + false } fn unprocessed_input_size(&self) -> u64 { @@ -2566,8 +2603,7 @@ impl BrotliEncoderStateStruct { next_out_array, next_out_offset, total_out, - ) != 0 - { + ) { continue; } if self.available_out_ != 0usize { @@ -2707,8 +2743,7 @@ impl BrotliEncoderStateStruct { next_out_array, next_out_offset, total_out, - ) != 0 - { + ) { continue; } if self.available_out_ == 0usize @@ -2912,8 +2947,7 @@ impl BrotliEncoderStateStruct { next_out_array, next_out_offset, total_out, - ) != 0 - { + ) { continue; } if self.available_out_ == 0usize diff --git a/src/enc/metablock.rs b/src/enc/metablock.rs index ee667506..c9e871bc 100644 --- a/src/enc/metablock.rs +++ b/src/enc/metablock.rs @@ -565,7 +565,7 @@ fn BlockSplitterFinishBlock< split: &mut BlockSplit, histograms: &mut [HistogramType], histograms_size: &mut usize, - is_final: i32, + is_final: bool, ) { xself.block_size_ = max(xself.block_size_, xself.min_block_size_); if xself.num_blocks_ == 0usize { @@ -660,7 +660,7 @@ fn BlockSplitterFinishBlock< } } } - if is_final != 0 { + if is_final { *histograms_size = split.num_types; split.num_blocks = xself.num_blocks_; } @@ -676,7 +676,7 @@ fn ContextBlockSplitterFinishBlock< split: &mut BlockSplit, histograms: &mut [HistogramLiteral], histograms_size: &mut usize, - is_final: i32, + is_final: bool, ) { let num_contexts: usize = xself.num_contexts_; if xself.block_size_ < xself.min_block_size_ { @@ -796,7 +796,7 @@ fn ContextBlockSplitterFinishBlock< } m.free_cell(combined_histo); } - if is_final != 0 { + if is_final { *histograms_size = split.num_types.wrapping_mul(num_contexts); split.num_blocks = xself.num_blocks_; } @@ -815,7 +815,7 @@ fn BlockSplitterAddSymbol< HistogramAddItem(&mut histograms[xself.curr_histogram_ix_], symbol); xself.block_size_ = xself.block_size_.wrapping_add(1); if xself.block_size_ == xself.target_block_size_ { - BlockSplitterFinishBlock(xself, split, histograms, histograms_size, 0i32); + BlockSplitterFinishBlock(xself, split, histograms, histograms_size, false); } } @@ -836,7 +836,7 @@ fn ContextBlockSplitterAddSymbol< ); xself.block_size_ = xself.block_size_.wrapping_add(1); if xself.block_size_ == xself.target_block_size_ { - ContextBlockSplitterFinishBlock(xself, m, split, histograms, histograms_size, 0i32); + ContextBlockSplitterFinishBlock(xself, m, split, histograms, histograms_size, false); } } @@ -1002,7 +1002,7 @@ pub fn BrotliBuildMetaBlockGreedyInternal< &mut mb.literal_split, mb.literal_histograms.slice_mut(), &mut mb.literal_histograms_size, - 1i32, + true, ), &mut LitBlocks::ctx(ref mut lit_blocks_ctx) => ContextBlockSplitterFinishBlock( lit_blocks_ctx, @@ -1010,7 +1010,7 @@ pub fn BrotliBuildMetaBlockGreedyInternal< &mut mb.literal_split, mb.literal_histograms.slice_mut(), &mut mb.literal_histograms_size, - 1i32, + true, ), } BlockSplitterFinishBlock( @@ -1018,14 +1018,14 @@ pub fn BrotliBuildMetaBlockGreedyInternal< &mut mb.command_split, mb.command_histograms.slice_mut(), &mut mb.command_histograms_size, - 1i32, + true, ); BlockSplitterFinishBlock( &mut dist_blocks, &mut mb.distance_split, mb.distance_histograms.slice_mut(), &mut mb.distance_histograms_size, - 1i32, + true, ); if num_contexts > 1 { MapStaticContexts(alloc, num_contexts, static_context_map, mb); diff --git a/src/ffi/compressor.rs b/src/ffi/compressor.rs index 975d0400..1a41631e 100644 --- a/src/ffi/compressor.rs +++ b/src/ffi/compressor.rs @@ -231,7 +231,7 @@ pub unsafe extern "C" fn BrotliEncoderCompress( let empty_m8 = BrotliSubclassableAllocator::new(SubclassableAllocator::new(allocators.clone())); - ::enc::encode::BrotliEncoderCompress( + crate::enc::encode::encoder_compress( empty_m8, &mut m8, quality, @@ -243,6 +243,7 @@ pub unsafe extern "C" fn BrotliEncoderCompress( encoded_buf, &mut |_a, _b, _c, _d| (), ) + .into() }) .unwrap_or_else(|panic_err| { error_print(panic_err); diff --git a/src/ffi/multicompress/mod.rs b/src/ffi/multicompress/mod.rs index ef9038d7..4c48850e 100644 --- a/src/ffi/multicompress/mod.rs +++ b/src/ffi/multicompress/mod.rs @@ -59,7 +59,7 @@ fn help_brotli_encoder_compress_single( output: &mut [u8], encoded_size: &mut usize, m8: BrotliSubclassableAllocator, -) -> i32 { +) -> bool { let mut encoder = BrotliEncoderStateStruct::new(m8); for (p, v) in param_keys.iter().zip(param_values.iter()) { encoder.set_parameter(*p, *v); @@ -85,11 +85,7 @@ fn help_brotli_encoder_compress_single( } *encoded_size = total_out.unwrap(); - if result { - 1 - } else { - 0 - } + result } #[no_mangle] @@ -134,7 +130,8 @@ pub unsafe extern "C" fn BrotliEncoderCompressMulti( output_slice, &mut *encoded_size, m8, - ); + ) + .into(); } let null_opaques = [core::ptr::null_mut::(); MAX_THREADS]; let alloc_opaque = if alloc_opaque_per_thread.is_null() {