diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 48de39601..1107dc758 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -481,7 +481,8 @@ typedef struct { ZSTD_rust_copyCCtxMarkTables_f markTablesClean; ZSTD_rust_copyCCtxCopyState_f copyMatchState; ZSTD_rust_copyCCtxCopyState_f copyDictState; - ZSTD_rust_copyCCtxCopyState_f copyBlockState; + ZSTD_compressedBlockState_t** destinationBlockState; + const ZSTD_compressedBlockState_t* sourceBlockState; int zbuff; } ZSTD_rust_copyCCtxInternalState; size_t ZSTD_rust_copyCCtxInternal( @@ -522,7 +523,7 @@ typedef char ZSTD_rust_copy_cctx_internal_state_layout[ == 3 * sizeof(void*) + sizeof(U64) && offsetof(ZSTD_rust_copyCCtxInternalState, zbuff) == 3 * sizeof(void*) + sizeof(U64) - + 9 * sizeof(void*) + + 10 * sizeof(void*) && sizeof(ZSTD_rust_copyCCtxInternalState) == ((offsetof(ZSTD_rust_copyCCtxInternalState, zbuff) + sizeof(int) + sizeof(void*) - 1) / sizeof(void*)) @@ -4854,16 +4855,6 @@ static void ZSTD_rust_copyCCtx_copy_dict_state( dst->dictContentSize = src->dictContentSize; } -static void ZSTD_rust_copyCCtx_copy_block_state( - void* context, const void* srcCCtx) -{ - ZSTD_CCtx* const dst = (ZSTD_CCtx*)context; - const ZSTD_CCtx* const src = (const ZSTD_CCtx*)srcCCtx; - ZSTD_memcpy(dst->blockState.prevCBlock, - src->blockState.prevCBlock, - sizeof(*src->blockState.prevCBlock)); -} - static size_t ZSTD_rust_copyCCtx_internal_callback( void* context, const void* srcCCtx, const ZSTD_frameParameters* fParams, @@ -4882,7 +4873,8 @@ static size_t ZSTD_rust_copyCCtx_internal_callback( state.markTablesClean = ZSTD_rust_copyCCtx_mark_tables_clean; state.copyMatchState = ZSTD_rust_copyCCtx_copy_match_state; state.copyDictState = ZSTD_rust_copyCCtx_copy_dict_state; - state.copyBlockState = ZSTD_rust_copyCCtx_copy_block_state; + state.destinationBlockState = &((ZSTD_CCtx*)context)->blockState.prevCBlock; + state.sourceBlockState = ((const ZSTD_CCtx*)srcCCtx)->blockState.prevCBlock; state.zbuff = zbuff; return ZSTD_rust_copyCCtxInternal(&state); } diff --git a/rust/src/zstd_compress.rs b/rust/src/zstd_compress.rs index 398254e7d..1f2748e20 100644 --- a/rust/src/zstd_compress.rs +++ b/rust/src/zstd_compress.rs @@ -1703,7 +1703,8 @@ pub struct ZSTD_rust_copyCCtxInternalState { mark_tables_clean: Option, copy_match_state: Option, copy_dict_state: Option, - copy_block_state: Option, + destination_block_state: *mut *mut ZSTD_compressedBlockState_t, + source_block_state: *const ZSTD_compressedBlockState_t, zbuff: c_int, } @@ -1725,7 +1726,7 @@ const _: () = { ); assert!( offset_of!(ZSTD_rust_copyCCtxInternalState, zbuff) - == 3 * size_of::() + size_of::() + 9 * size_of::() + == 3 * size_of::() + size_of::() + 10 * size_of::() ); assert!( size_of::() @@ -1735,7 +1736,8 @@ const _: () = { ); }; -/// Run the private context-copy operation through C-owned layout callbacks. +/// Run the private context-copy operation through C-owned layout callbacks +/// and copy the compressed-block state directly in Rust. #[no_mangle] pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal( state: *const ZSTD_rust_copyCCtxInternalState, @@ -1753,7 +1755,6 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal( Some(mark_tables_clean), Some(copy_match_state), Some(copy_dict_state), - Some(copy_block_state), ) = ( state.check_stage, state.copy_custom_mem, @@ -1763,12 +1764,16 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal( state.mark_tables_clean, state.copy_match_state, state.copy_dict_state, - state.copy_block_state, ) else { return ERROR(ZstdErrorCode::Generic); }; - if state.callback_context.is_null() || state.src_cctx.is_null() || state.f_params.is_null() { + if state.callback_context.is_null() + || state.src_cctx.is_null() + || state.f_params.is_null() + || state.destination_block_state.is_null() + || state.source_block_state.is_null() + { return ERROR(ZstdErrorCode::Generic); } @@ -1794,7 +1799,11 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal( mark_tables_clean(state.callback_context); copy_match_state(state.callback_context, state.src_cctx); copy_dict_state(state.callback_context, state.src_cctx); - copy_block_state(state.callback_context, state.src_cctx); + let destination_block_state = *state.destination_block_state; + if destination_block_state.is_null() { + return ERROR(ZstdErrorCode::Generic); + } + ptr::copy_nonoverlapping(state.source_block_state, destination_block_state, 1); } 0 } @@ -13863,7 +13872,6 @@ mod tests { 1 => "custom", 6 => "match", 7 => "dict", - 8 => "block", _ => panic!("unexpected copy-state callback order"), }; context.events.push(event); @@ -13908,6 +13916,8 @@ mod tests { context: &mut CopyCCtxInternalTestContext, src_cctx: *const c_void, f_params: &ZSTD_frameParameters, + destination_block_state: *mut *mut ZSTD_compressedBlockState_t, + source_block_state: *const ZSTD_compressedBlockState_t, ) -> ZSTD_rust_copyCCtxInternalState { ZSTD_rust_copyCCtxInternalState { callback_context: (context as *mut CopyCCtxInternalTestContext).cast(), @@ -13922,7 +13932,8 @@ mod tests { mark_tables_clean: Some(copy_cctx_internal_test_mark_tables_clean), copy_match_state: Some(copy_cctx_internal_test_copy_state), copy_dict_state: Some(copy_cctx_internal_test_copy_state), - copy_block_state: Some(copy_cctx_internal_test_copy_state), + destination_block_state, + source_block_state, zbuff: 7, } } @@ -13935,19 +13946,42 @@ mod tests { checksumFlag: 1, noDictIDFlag: 1, }; - let state = - copy_cctx_internal_test_state(&mut context, 0x5000usize as *const c_void, &f_params); + let mut source_block_state = + unsafe { MaybeUninit::::zeroed().assume_init() }; + source_block_state.rep = [15, 16, 17]; + source_block_state.entropy.huf.repeatMode = 2; + source_block_state.entropy.fse.offcode_repeatMode = FSE_REPEAT_VALID; + let mut destination_block_state = + unsafe { MaybeUninit::::zeroed().assume_init() }; + let mut destination_block_state_slot = + &mut destination_block_state as *mut ZSTD_compressedBlockState_t; + let state = copy_cctx_internal_test_state( + &mut context, + 0x5000usize as *const c_void, + &f_params, + &mut destination_block_state_slot, + &source_block_state, + ); let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) }; assert_eq!(result, 0); assert_eq!( context.events, - ["check", "custom", "reset", "dirty", "tables", "clean", "match", "dict", "block"] + ["check", "custom", "reset", "dirty", "tables", "clean", "match", "dict"] ); assert_eq!(context.frame_params, f_params); assert_eq!(context.pledged_src_size, 123); assert_eq!(context.zbuff, 7); + assert_eq!(destination_block_state.rep, source_block_state.rep); + assert_eq!( + destination_block_state.entropy.huf.repeatMode, + source_block_state.entropy.huf.repeatMode + ); + assert_eq!( + destination_block_state.entropy.fse.offcode_repeatMode, + source_block_state.entropy.fse.offcode_repeatMode + ); } #[test] @@ -13957,8 +13991,19 @@ mod tests { ..CopyCCtxInternalTestContext::default() }; let f_params = ZSTD_frameParameters::default(); - let state = - copy_cctx_internal_test_state(&mut context, 0x5000usize as *const c_void, &f_params); + let mut destination_block_state = + unsafe { MaybeUninit::::zeroed().assume_init() }; + let mut destination_block_state_slot = + &mut destination_block_state as *mut ZSTD_compressedBlockState_t; + let source_block_state = + unsafe { MaybeUninit::::zeroed().assume_init() }; + let state = copy_cctx_internal_test_state( + &mut context, + 0x5000usize as *const c_void, + &f_params, + &mut destination_block_state_slot, + &source_block_state, + ); let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) }; @@ -13973,8 +14018,19 @@ mod tests { ..CopyCCtxInternalTestContext::default() }; let f_params = ZSTD_frameParameters::default(); - let state = - copy_cctx_internal_test_state(&mut context, 0x5000usize as *const c_void, &f_params); + let mut destination_block_state = + unsafe { MaybeUninit::::zeroed().assume_init() }; + let mut destination_block_state_slot = + &mut destination_block_state as *mut ZSTD_compressedBlockState_t; + let source_block_state = + unsafe { MaybeUninit::::zeroed().assume_init() }; + let state = copy_cctx_internal_test_state( + &mut context, + 0x5000usize as *const c_void, + &f_params, + &mut destination_block_state_slot, + &source_block_state, + ); let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) };