Skip to content

Commit 04cd2f0

Browse files
committed
fn ZDICT_trainFromBuffer_unsafe_legacy: make mostly safe
1 parent 3b0bcbb commit 04cd2f0

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

lib/dictBuilder/zdict.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ unsafe fn ZDICT_addEntropyTablesFromBuffer_advanced(
12571257
///
12581258
/// - the size of the dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
12591259
/// - an error code, which can be tested with [`ZDICT_isError`]
1260-
unsafe fn ZDICT_trainFromBuffer_unsafe_legacy(
1260+
fn ZDICT_trainFromBuffer_unsafe_legacy(
12611261
dictBuffer: *mut core::ffi::c_void,
12621262
maxDictSize: size_t,
12631263
samples: &[u8],
@@ -1406,30 +1406,43 @@ unsafe fn ZDICT_trainFromBuffer_unsafe_legacy(
14061406
dictList[0].pos = n;
14071407
dictContentSize_0 = currentSize;
14081408

1409-
// build dictionary content
1409+
unsafe {
1410+
if let Err(e) = build_dictionary_content(dictBuffer, maxDictSize, samples, &dictList) {
1411+
return e.to_error_code();
1412+
}
1413+
1414+
ZDICT_addEntropyTablesFromBuffer_advanced(
1415+
dictBuffer,
1416+
dictContentSize_0 as size_t,
1417+
maxDictSize,
1418+
samples,
1419+
samplesSizes,
1420+
params.zParams,
1421+
)
1422+
}
1423+
}
1424+
1425+
unsafe fn build_dictionary_content(
1426+
dictBuffer: *mut core::ffi::c_void,
1427+
maxDictSize: size_t,
1428+
samples: &[u8],
1429+
dictList: &[DictItem],
1430+
) -> Result<(), Error> {
1431+
// convention: table[0].pos stores the number of elements
1432+
let max = dictList[0].pos;
1433+
14101434
let mut ptr = (dictBuffer as *mut u8).add(maxDictSize);
1411-
for u in 1..dictList[0].pos {
1412-
let l = (dictList[u as usize]).length;
1413-
ptr = ptr.offset(-(l as isize));
1435+
for item in &dictList[1..max as usize] {
1436+
let l = item.length;
1437+
ptr = ptr.sub(l as usize);
14141438
debug_assert!(ptr >= dictBuffer as *mut u8);
14151439
if ptr < dictBuffer as *mut u8 {
1416-
return Error::GENERIC.to_error_code(); // should not happen
1440+
return Err(Error::GENERIC); // should not happen
14171441
}
1418-
core::ptr::copy_nonoverlapping(
1419-
samples[(dictList[u as usize]).pos as usize..].as_ptr(),
1420-
ptr,
1421-
l as size_t,
1422-
);
1442+
core::ptr::copy_nonoverlapping(samples[item.pos as usize..].as_ptr(), ptr, l as size_t);
14231443
}
14241444

1425-
ZDICT_addEntropyTablesFromBuffer_advanced(
1426-
dictBuffer,
1427-
dictContentSize_0 as size_t,
1428-
maxDictSize,
1429-
samples,
1430-
samplesSizes,
1431-
params.zParams,
1432-
)
1445+
Ok(())
14331446
}
14341447

14351448
/// Train a dictionary from an array of samples.

0 commit comments

Comments
 (0)