Skip to content

Commit d51804c

Browse files
committed
fn ZSTD_decompressBegin_usingDict: cleanup
1 parent 8645bcb commit d51804c

File tree

2 files changed

+37
-46
lines changed

2 files changed

+37
-46
lines changed

lib/decompress/zstd_ddict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ unsafe fn ZSTD_loadEntropy_intoDDict(
134134

135135
(*ddict).dictID = u32::from_le_bytes(*dict_id);
136136

137-
let ret = ZSTD_loadDEntropy(&mut (*ddict).entropy, dict.as_ptr().cast(), dict.len());
137+
let ret = ZSTD_loadDEntropy(&mut (*ddict).entropy, dict);
138138

139139
if ERR_isError(ret) != 0 {
140140
return Error::dictionary_corrupted.to_error_code();

lib/decompress/zstd_decompress.rs

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,30 +2172,8 @@ pub unsafe extern "C" fn ZSTD_decompressContinue(
21722172
_ => -(ZSTD_error_GENERIC as core::ffi::c_int) as size_t,
21732173
}
21742174
}
2175-
unsafe fn ZSTD_refDictContent(
2176-
dctx: *mut ZSTD_DCtx,
2177-
dict: *const core::ffi::c_void,
2178-
dictSize: size_t,
2179-
) -> size_t {
2180-
(*dctx).dictEnd = (*dctx).previousDstEnd;
2181-
(*dctx).virtualStart = (dict as *const core::ffi::c_char).offset(
2182-
-(((*dctx).previousDstEnd as *const core::ffi::c_char)
2183-
.offset_from((*dctx).prefixStart as *const core::ffi::c_char)
2184-
as core::ffi::c_long as isize),
2185-
) as *const core::ffi::c_void;
2186-
(*dctx).prefixStart = dict;
2187-
(*dctx).previousDstEnd =
2188-
(dict as *const core::ffi::c_char).add(dictSize) as *const core::ffi::c_void;
2189-
0
2190-
}
2191-
2192-
pub unsafe fn ZSTD_loadDEntropy(
2193-
entropy: &mut ZSTD_entropyDTables_t,
2194-
dict: *const core::ffi::c_void,
2195-
dictSize: size_t,
2196-
) -> size_t {
2197-
let dict = core::slice::from_raw_parts(dict.cast::<u8>(), dictSize);
21982175

2176+
pub unsafe fn ZSTD_loadDEntropy(entropy: &mut ZSTD_entropyDTables_t, dict: &[u8]) -> size_t {
21992177
let Some((_, mut dictPtr)) = dict.split_at_checked(8) else {
22002178
return Error::dictionary_corrupted.to_error_code();
22012179
};
@@ -2319,32 +2297,39 @@ pub unsafe fn ZSTD_loadDEntropy(
23192297
dict.len() - dict_content_size
23202298
}
23212299

2322-
unsafe fn ZSTD_decompress_insertDictionary(
2323-
dctx: *mut ZSTD_DCtx,
2324-
mut dict: *const core::ffi::c_void,
2325-
mut dictSize: size_t,
2326-
) -> size_t {
2327-
if dictSize < 8 {
2328-
return ZSTD_refDictContent(dctx, dict, dictSize);
2329-
}
2330-
let magic = MEM_readLE32(dict);
2300+
unsafe fn ZSTD_refDictContent(dctx: *mut ZSTD_DCtx, dict: &[u8]) -> size_t {
2301+
(*dctx).dictEnd = (*dctx).previousDstEnd;
2302+
(*dctx).virtualStart = dict
2303+
.as_ptr()
2304+
.sub((((*dctx).previousDstEnd).byte_offset_from((*dctx).prefixStart)) as usize)
2305+
.cast();
2306+
(*dctx).prefixStart = dict.as_ptr().cast();
2307+
(*dctx).previousDstEnd = dict.as_ptr_range().end.cast();
2308+
2309+
0
2310+
}
2311+
2312+
unsafe fn ZSTD_decompress_insertDictionary(dctx: *mut ZSTD_DCtx, dict: &[u8]) -> size_t {
2313+
let ([magic, dict_id, ..], _) = dict.as_chunks::<4>() else {
2314+
return ZSTD_refDictContent(dctx, dict);
2315+
};
2316+
2317+
let magic = u32::from_le_bytes(*magic);
23312318
if magic != ZSTD_MAGIC_DICTIONARY {
2332-
return ZSTD_refDictContent(dctx, dict, dictSize);
2319+
return ZSTD_refDictContent(dctx, dict);
23332320
}
2334-
(*dctx).dictID = MEM_readLE32(
2335-
(dict as *const core::ffi::c_char).offset(ZSTD_FRAMEIDSIZE as isize)
2336-
as *const core::ffi::c_void,
2337-
);
2338-
let eSize = ZSTD_loadDEntropy(&mut (*dctx).entropy, dict, dictSize);
2321+
(*dctx).dictID = u32::from_le_bytes(*dict_id);
2322+
let eSize = ZSTD_loadDEntropy(&mut (*dctx).entropy, dict);
23392323
if ERR_isError(eSize) != 0 {
23402324
return Error::dictionary_corrupted.to_error_code();
23412325
}
2342-
dict = (dict as *const core::ffi::c_char).add(eSize) as *const core::ffi::c_void;
2343-
dictSize = dictSize.wrapping_sub(eSize);
2326+
23442327
(*dctx).fseEntropy = 1;
23452328
(*dctx).litEntropy = (*dctx).fseEntropy;
2346-
ZSTD_refDictContent(dctx, dict, dictSize)
2329+
2330+
ZSTD_refDictContent(dctx, &dict[eSize..])
23472331
}
2332+
23482333
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_decompressBegin))]
23492334
pub unsafe extern "C" fn ZSTD_decompressBegin(dctx: *mut ZSTD_DCtx) -> size_t {
23502335
(*dctx).traceCtx = ZSTD_trace_decompress_begin.map_or(0, |f| f(dctx));
@@ -2373,6 +2358,7 @@ pub unsafe extern "C" fn ZSTD_decompressBegin(dctx: *mut ZSTD_DCtx) -> size_t {
23732358
(*dctx).HUFptr = &raw const (*dctx).entropy.hufTable as *const u32;
23742359
0
23752360
}
2361+
23762362
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_decompressBegin_usingDict))]
23772363
pub unsafe extern "C" fn ZSTD_decompressBegin_usingDict(
23782364
dctx: *mut ZSTD_DCtx,
@@ -2383,14 +2369,19 @@ pub unsafe extern "C" fn ZSTD_decompressBegin_usingDict(
23832369
if ERR_isError(err_code) != 0 {
23842370
return err_code;
23852371
}
2386-
if !dict.is_null()
2387-
&& dictSize != 0
2388-
&& ERR_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)) != 0
2389-
{
2372+
2373+
if dict.is_null() || dictSize == 0 {
2374+
return 0;
2375+
}
2376+
2377+
let dict = core::slice::from_raw_parts(dict.cast::<u8>(), dictSize);
2378+
if ERR_isError(ZSTD_decompress_insertDictionary(dctx, dict)) != 0 {
23902379
return Error::dictionary_corrupted.to_error_code();
23912380
}
2381+
23922382
0
23932383
}
2384+
23942385
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_decompressBegin_usingDDict))]
23952386
pub unsafe extern "C" fn ZSTD_decompressBegin_usingDDict(
23962387
dctx: *mut ZSTD_DCtx,

0 commit comments

Comments
 (0)