feat(rust): port Huffman compression

Move Huffman table construction, table serialization, and one- and
four-stream payload encoding from huf_compress.c into the Rust compatibility
archive.  The declaration-only C shim preserves the existing internal ABI,
so the remaining C compressor can call the migrated implementation unchanged.

The translation keeps CTable layouts, workspace checks, repeat-table
selection, and bitstream output compatible with the original encoder.  This
lets native C consumers exercise the Rust implementation through libzstd.

Test Plan:
- cargo clippy, cargo clippy --benches, and cargo clippy --tests
- cargo +nightly fmt and cargo test --all-targets (88 passed)
- cargo check --no-default-features --features compression
- rebuild and run tests fuzzer, zstreamtest, and invalidDictionaries
- compare tables, headers, and 1X/4X streams with a renamed pristine C build

Refs: rust/README.md component map
This commit is contained in:
2026-07-10 21:54:23 +02:00
parent d784406374
commit 0a6a5f5267
4 changed files with 1552 additions and 1463 deletions
+1546
View File
@@ -0,0 +1,1546 @@
#![allow(non_snake_case)]
//! Huffman compression tables, headers, and single/four-stream encoders.
//!
//! This is the Rust implementation of `lib/compress/huf_compress.c`. The
//! externally supplied workspaces remain part of the C ABI: their alignment
//! and size checks are preserved even though the translation keeps temporary
//! state in fixed Rust arrays.
use crate::bits::ZSTD_highbit32;
use crate::entropy_common::{HUF_readStats, HUF_TABLELOG_MAX};
use crate::errors::{ERR_isError, ZstdErrorCode, ERROR};
use crate::fse_compress::{
FSE_buildCTable_wksp, FSE_compress_usingCTable, FSE_normalizeCount, FSE_optimalTableLog,
FSE_optimalTableLog_internal, FSE_writeNCount,
};
use crate::hist::{HIST_count_simple, HIST_count_wksp, HIST_WKSP_SIZE_U32};
use crate::mem::MEM_writeLE16;
use std::mem::size_of;
use std::os::raw::{c_int, c_short, c_uint, c_void};
const HUF_BLOCKSIZE_MAX: usize = 128 * 1024;
const HUF_TABLELOG_DEFAULT: u32 = 11;
const HUF_TABLELOG_ABSOLUTEMAX: u32 = 12;
const HUF_SYMBOLVALUE_MAX: u32 = 255;
const HUF_CTABLE_SIZE_ST: usize = HUF_SYMBOLVALUE_MAX as usize + 2;
const HUF_CTABLE_WORKSPACE_SIZE: usize = (4 * (HUF_SYMBOLVALUE_MAX as usize + 1) + 192) * 4;
const HUF_WORKSPACE_SIZE: usize = (8 << 10) + 512;
const HUF_WORKSPACE_MAX_ALIGNMENT: usize = 8;
const MAX_FSE_TABLELOG_FOR_HUFF_HEADER: u32 = 6;
const HUF_FLAGS_OPTIMAL_DEPTH: c_int = 1 << 1;
const HUF_FLAGS_PREFER_REPEAT: c_int = 1 << 2;
const HUF_FLAGS_SUSPECT_UNCOMPRESSIBLE: c_int = 1 << 3;
const HUF_REPEAT_NONE: c_int = 0;
const HUF_REPEAT_CHECK: c_int = 1;
const HUF_REPEAT_VALID: c_int = 2;
const SUSPECT_INCOMPRESSIBLE_SAMPLE_SIZE: usize = 4096;
const SUSPECT_INCOMPRESSIBLE_SAMPLE_RATIO: usize = 10;
const STARTNODE: usize = HUF_SYMBOLVALUE_MAX as usize + 1;
const RANK_POSITION_TABLE_SIZE: usize = 192;
const RANK_POSITION_LOG_BUCKETS_BEGIN: u32 = (RANK_POSITION_TABLE_SIZE as u32 - 1) - 32 - 1;
const RANK_POSITION_DISTINCT_COUNT_CUTOFF: u32 = RANK_POSITION_LOG_BUCKETS_BEGIN + 7;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct HUF_CTableHeader {
pub tableLog: u8,
pub maxSymbolValue: u8,
pub unused: [u8; size_of::<usize>() - 2],
}
#[repr(C)]
#[derive(Copy, Clone, Default)]
struct NodeElt {
count: u32,
parent: u16,
byte: u8,
nb_bits: u8,
}
#[repr(C)]
#[derive(Copy, Clone, Default)]
struct RankPos {
base: u16,
curr: u16,
}
#[inline]
unsafe fn ctable_read(table: *const usize, index: usize) -> usize {
table.add(index).read_unaligned()
}
#[inline]
unsafe fn ctable_write(table: *mut usize, index: usize, value: usize) {
table.add(index).write_unaligned(value);
}
#[inline]
unsafe fn huf_read_ctable_header(table: *const usize) -> HUF_CTableHeader {
let mut header = HUF_CTableHeader {
tableLog: 0,
maxSymbolValue: 0,
unused: [0; size_of::<usize>() - 2],
};
std::ptr::copy_nonoverlapping(
table.cast::<u8>(),
(&mut header as *mut HUF_CTableHeader).cast::<u8>(),
size_of::<HUF_CTableHeader>(),
);
header
}
#[inline]
unsafe fn huf_write_ctable_header(table: *mut usize, table_log: u32, max_symbol_value: u32) {
let header = HUF_CTableHeader {
tableLog: table_log as u8,
maxSymbolValue: max_symbol_value as u8,
unused: [0; size_of::<usize>() - 2],
};
std::ptr::copy_nonoverlapping(
(&header as *const HUF_CTableHeader).cast::<u8>(),
table.cast::<u8>(),
size_of::<HUF_CTableHeader>(),
);
}
#[inline]
fn huf_get_nb_bits(elt: usize) -> usize {
elt & 0xff
}
#[inline]
fn huf_get_value(elt: usize) -> usize {
elt & !0xffusize
}
#[inline]
unsafe fn huf_set_nb_bits(table: *mut usize, index: usize, nb_bits: usize) {
debug_assert!(nb_bits <= HUF_TABLELOG_ABSOLUTEMAX as usize);
ctable_write(table, index, nb_bits);
}
#[inline]
unsafe fn huf_set_value(table: *mut usize, index: usize, value: usize) {
let elt = ctable_read(table, index);
let nb_bits = huf_get_nb_bits(elt);
if nb_bits != 0 {
debug_assert!(value >> nb_bits == 0);
ctable_write(
table,
index,
elt | (value << (usize::BITS as usize - nb_bits)),
);
}
}
#[inline]
fn huf_aligned_workspace_size(
workspace: *mut c_void,
workspace_size: usize,
align: usize,
) -> usize {
debug_assert!(align.is_power_of_two());
debug_assert!(align <= HUF_WORKSPACE_MAX_ALIGNMENT);
let add = (align - ((workspace as usize) & (align - 1))) & (align - 1);
workspace_size.saturating_sub(add)
}
#[no_mangle]
pub unsafe extern "C" fn HUF_readCTableHeader(ctable: *const usize) -> HUF_CTableHeader {
huf_read_ctable_header(ctable)
}
unsafe fn huf_compress_weights(
dst: *mut u8,
dst_size: usize,
weight_table: *const u8,
wt_size: usize,
) -> usize {
if wt_size <= 1 {
return 0;
}
let mut count = [0u32; HUF_TABLELOG_MAX as usize + 1];
let mut max_symbol_value = HUF_TABLELOG_MAX;
let max_count = HIST_count_simple(
count.as_mut_ptr(),
&mut max_symbol_value,
weight_table.cast::<c_void>(),
wt_size,
);
if max_count as usize == wt_size {
return 1;
}
if max_count == 1 {
return 0;
}
let table_log =
FSE_optimalTableLog(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, wt_size, max_symbol_value);
let mut norm = [0i16; HUF_TABLELOG_MAX as usize + 1];
let result = FSE_normalizeCount(
norm.as_mut_ptr().cast::<c_short>(),
table_log,
count.as_ptr(),
wt_size,
max_symbol_value,
0,
);
if ERR_isError(result) {
return result;
}
let h_size = FSE_writeNCount(
dst.cast::<c_void>(),
dst_size,
norm.as_ptr().cast::<c_short>(),
max_symbol_value,
table_log,
);
if ERR_isError(h_size) {
return h_size;
}
if h_size > dst_size {
return ERROR(ZstdErrorCode::DstSizeTooSmall);
}
// FSE_CTABLE_SIZE_U32(6, HUF_TABLELOG_MAX) and the corresponding build
// workspace macro from fse.h.
let mut ctable = [0u32; 59];
let mut scratch = [0u32; 41];
let result = FSE_buildCTable_wksp(
ctable.as_mut_ptr(),
norm.as_ptr().cast::<c_short>(),
max_symbol_value,
table_log,
scratch.as_mut_ptr().cast::<c_void>(),
size_of::<[u32; 41]>(),
);
if ERR_isError(result) {
return result;
}
let c_size = FSE_compress_usingCTable(
dst.add(h_size).cast::<c_void>(),
dst_size - h_size,
weight_table.cast::<c_void>(),
wt_size,
ctable.as_ptr(),
);
if ERR_isError(c_size) {
return c_size;
}
if c_size == 0 {
return 0;
}
h_size + c_size
}
#[no_mangle]
pub unsafe extern "C" fn HUF_writeCTable_wksp(
dst: *mut c_void,
max_dst_size: usize,
ctable: *const usize,
max_symbol_value: c_uint,
huff_log: c_uint,
workspace: *mut c_void,
workspace_size: usize,
) -> usize {
let header = huf_read_ctable_header(ctable);
debug_assert_eq!(u32::from(header.maxSymbolValue), max_symbol_value);
debug_assert_eq!(u32::from(header.tableLog), huff_log);
if huf_aligned_workspace_size(workspace, workspace_size, size_of::<u32>())
< HUF_CTABLE_WORKSPACE_SIZE
{
return ERROR(ZstdErrorCode::Generic);
}
if max_symbol_value > HUF_SYMBOLVALUE_MAX {
return ERROR(ZstdErrorCode::MaxSymbolValueTooLarge);
}
if max_dst_size < 1 {
return ERROR(ZstdErrorCode::DstSizeTooSmall);
}
let mut bits_to_weight = [0u8; HUF_TABLELOG_MAX as usize + 1];
for (n, weight) in bits_to_weight
.iter_mut()
.enumerate()
.take(huff_log as usize + 1)
.skip(1)
{
*weight = (huff_log as usize + 1 - n) as u8;
}
let mut huff_weight = [0u8; HUF_SYMBOLVALUE_MAX as usize + 1];
for (n, weight) in huff_weight
.iter_mut()
.enumerate()
.take(max_symbol_value as usize)
{
let bits = huf_get_nb_bits(ctable_read(ctable, n + 1));
if bits > huff_log as usize {
return ERROR(ZstdErrorCode::TableLogTooLarge);
}
*weight = bits_to_weight[bits];
}
let output = dst.cast::<u8>();
let h_size = huf_compress_weights(
output.add(1),
max_dst_size - 1,
huff_weight.as_ptr(),
max_symbol_value as usize,
);
if ERR_isError(h_size) {
return h_size;
}
if h_size > 1 && h_size < max_symbol_value as usize / 2 {
*output = h_size as u8;
return h_size + 1;
}
if max_symbol_value > 256 - 128 {
return ERROR(ZstdErrorCode::Generic);
}
let raw_size = (max_symbol_value as usize).div_ceil(2) + 1;
if raw_size > max_dst_size {
return ERROR(ZstdErrorCode::DstSizeTooSmall);
}
*output = (128 + (max_symbol_value - 1)) as u8;
huff_weight[max_symbol_value as usize] = 0;
for n in (0..max_symbol_value as usize).step_by(2) {
*output.add(n / 2 + 1) = (huff_weight[n] << 4).wrapping_add(huff_weight[n + 1]);
}
raw_size
}
#[no_mangle]
pub unsafe extern "C" fn HUF_readCTable(
ctable: *mut usize,
max_symbol_value_ptr: *mut c_uint,
src: *const c_void,
src_size: usize,
has_zero_weights: *mut c_uint,
) -> usize {
let mut huff_weight = [0u8; HUF_SYMBOLVALUE_MAX as usize + 1];
let mut rank_val = [0u32; HUF_TABLELOG_ABSOLUTEMAX as usize + 1];
let mut table_log = 0u32;
let mut nb_symbols = 0u32;
let read_size = HUF_readStats(
huff_weight.as_mut_ptr(),
huff_weight.len(),
rank_val.as_mut_ptr(),
&mut nb_symbols,
&mut table_log,
src,
src_size,
);
if ERR_isError(read_size) {
return read_size;
}
*has_zero_weights = u32::from(rank_val[0] > 0);
if table_log > HUF_TABLELOG_MAX {
return ERROR(ZstdErrorCode::TableLogTooLarge);
}
if nb_symbols > *max_symbol_value_ptr + 1 {
return ERROR(ZstdErrorCode::MaxSymbolValueTooSmall);
}
*max_symbol_value_ptr = nb_symbols - 1;
huf_write_ctable_header(ctable, table_log, *max_symbol_value_ptr);
let mut next_rank_start = 0u32;
for (rank, rank_value) in rank_val
.iter_mut()
.enumerate()
.take(table_log as usize + 1)
.skip(1)
{
let current = next_rank_start;
next_rank_start = next_rank_start.wrapping_add(*rank_value << (rank - 1));
*rank_value = current;
}
for (symbol, weight) in huff_weight
.iter()
.copied()
.take(nb_symbols as usize)
.enumerate()
{
let weight = weight as u32;
let bits = if weight == 0 {
0
} else {
table_log + 1 - weight
};
huf_set_nb_bits(ctable, symbol + 1, bits as usize);
}
let mut nb_per_rank = [0u16; HUF_TABLELOG_MAX as usize + 2];
let mut val_per_rank = [0u16; HUF_TABLELOG_MAX as usize + 2];
for symbol in 0..nb_symbols as usize {
let bits = huf_get_nb_bits(ctable_read(ctable, symbol + 1));
nb_per_rank[bits] = nb_per_rank[bits].wrapping_add(1);
}
val_per_rank[table_log as usize + 1] = 0;
let mut min = 0u16;
for rank in (1..=table_log as usize).rev() {
val_per_rank[rank] = min;
min = min.wrapping_add(nb_per_rank[rank]);
min >>= 1;
}
for symbol in 0..nb_symbols as usize {
let bits = huf_get_nb_bits(ctable_read(ctable, symbol + 1));
let value = val_per_rank[bits];
huf_set_value(ctable, symbol + 1, value as usize);
val_per_rank[bits] = val_per_rank[bits].wrapping_add(1);
}
read_size
}
#[no_mangle]
pub unsafe extern "C" fn HUF_getNbBitsFromCTable(ctable: *const usize, symbol_value: u32) -> u32 {
if symbol_value > HUF_SYMBOLVALUE_MAX {
return 0;
}
if symbol_value > u32::from(huf_read_ctable_header(ctable).maxSymbolValue) {
return 0;
}
huf_get_nb_bits(ctable_read(ctable, symbol_value as usize + 1)) as u32
}
#[inline]
fn huf_get_index(count: u32) -> usize {
if count < RANK_POSITION_DISTINCT_COUNT_CUTOFF {
count as usize
} else {
(ZSTD_highbit32(count) + RANK_POSITION_LOG_BUCKETS_BEGIN) as usize
}
}
fn huf_insertion_sort(nodes: &mut [NodeElt], low: i32, high: i32) {
let size = high - low + 1;
if size <= 1 {
return;
}
for offset in 1..size {
let key = nodes[(low + offset) as usize];
let mut index = offset - 1;
while index >= 0 && nodes[(low + index) as usize].count < key.count {
nodes[(low + index + 1) as usize] = nodes[(low + index) as usize];
index -= 1;
}
nodes[(low + index + 1) as usize] = key;
}
}
fn huf_quick_sort_partition(nodes: &mut [NodeElt], low: i32, high: i32) -> i32 {
let pivot = nodes[high as usize].count;
let mut i = low - 1;
for j in low..high {
if nodes[j as usize].count > pivot {
i += 1;
nodes.swap(i as usize, j as usize);
}
}
nodes.swap((i + 1) as usize, high as usize);
i + 1
}
fn huf_simple_quick_sort(nodes: &mut [NodeElt], mut low: i32, mut high: i32) {
const INSERTION_SORT_THRESHOLD: i32 = 8;
if high - low < INSERTION_SORT_THRESHOLD {
huf_insertion_sort(nodes, low, high);
return;
}
while low < high {
let index = huf_quick_sort_partition(nodes, low, high);
if index - low < high - index {
huf_simple_quick_sort(nodes, low, index - 1);
low = index + 1;
} else {
huf_simple_quick_sort(nodes, index + 1, high);
high = index - 1;
}
}
}
fn huf_sort(
nodes: &mut [NodeElt; 2 * (HUF_SYMBOLVALUE_MAX as usize + 1)],
count: *const c_uint,
max_symbol_value: u32,
rank_position: &mut [RankPos; RANK_POSITION_TABLE_SIZE],
) {
let max_symbol_value1 = max_symbol_value as usize + 1;
*rank_position = [RankPos::default(); RANK_POSITION_TABLE_SIZE];
for symbol in 0..max_symbol_value1 {
let lower_rank = huf_get_index(unsafe { *count.add(symbol) });
debug_assert!(lower_rank < RANK_POSITION_TABLE_SIZE - 1);
rank_position[lower_rank].base = rank_position[lower_rank].base.wrapping_add(1);
}
for rank in (1..RANK_POSITION_TABLE_SIZE).rev() {
rank_position[rank - 1].base = rank_position[rank - 1]
.base
.wrapping_add(rank_position[rank].base);
rank_position[rank - 1].curr = rank_position[rank - 1].base;
}
for symbol in 0..max_symbol_value1 {
let current_count = unsafe { *count.add(symbol) };
let rank = huf_get_index(current_count) + 1;
let position = rank_position[rank].curr as usize;
debug_assert!(position < max_symbol_value1);
nodes[position + 1].count = current_count;
nodes[position + 1].byte = symbol as u8;
rank_position[rank].curr = rank_position[rank].curr.wrapping_add(1);
}
for position in rank_position
.iter()
.take(RANK_POSITION_TABLE_SIZE - 1)
.skip(RANK_POSITION_DISTINCT_COUNT_CUTOFF as usize)
{
let bucket_size = i32::from(position.curr) - i32::from(position.base);
let bucket_start = position.base as usize;
if bucket_size > 1 {
huf_simple_quick_sort(
&mut nodes[bucket_start + 1..bucket_start + 1 + bucket_size as usize],
0,
bucket_size - 1,
);
}
}
}
#[inline]
fn huff_node_index(index: usize) -> usize {
index + 1
}
#[inline]
fn huff_node_index_signed(index: i32) -> usize {
debug_assert!(index >= -1);
(index + 1) as usize
}
fn huf_build_tree(
nodes: &mut [NodeElt; 2 * (HUF_SYMBOLVALUE_MAX as usize + 1)],
max_symbol_value: u32,
) -> Option<usize> {
let mut non_null_rank = max_symbol_value as i32;
while non_null_rank >= 0 && nodes[huff_node_index(non_null_rank as usize)].count == 0 {
non_null_rank -= 1;
}
if non_null_rank < 1 {
return None;
}
let mut low_s = non_null_rank;
let node_root = STARTNODE as i32 + low_s - 1;
let mut low_n = STARTNODE as i32;
let mut node_nb = STARTNODE as i32;
let first_count = nodes[huff_node_index_signed(low_s)].count;
let second_count = nodes[huff_node_index_signed(low_s - 1)].count;
nodes[huff_node_index(node_nb as usize)].count = first_count.wrapping_add(second_count);
nodes[huff_node_index_signed(low_s)].parent = node_nb as u16;
nodes[huff_node_index_signed(low_s - 1)].parent = node_nb as u16;
node_nb += 1;
low_s -= 2;
for node in node_nb..=node_root {
nodes[huff_node_index(node as usize)].count = 1 << 30;
}
nodes[0].count = 1 << 31;
while node_nb <= node_root {
let n1 = if nodes[huff_node_index_signed(low_s)].count
< nodes[huff_node_index_signed(low_n)].count
{
let node = low_s;
low_s -= 1;
node
} else {
let node = low_n;
low_n += 1;
node
};
let n2 = if nodes[huff_node_index_signed(low_s)].count
< nodes[huff_node_index_signed(low_n)].count
{
let node = low_s;
low_s -= 1;
node
} else {
let node = low_n;
low_n += 1;
node
};
nodes[huff_node_index(node_nb as usize)].count = nodes[huff_node_index_signed(n1)]
.count
.wrapping_add(nodes[huff_node_index_signed(n2)].count);
nodes[huff_node_index_signed(n1)].parent = node_nb as u16;
nodes[huff_node_index_signed(n2)].parent = node_nb as u16;
node_nb += 1;
}
nodes[huff_node_index(node_root as usize)].nb_bits = 0;
for node in (STARTNODE as i32..node_root).rev() {
let parent = nodes[huff_node_index(node as usize)].parent as usize;
nodes[huff_node_index(node as usize)].nb_bits =
nodes[huff_node_index(parent)].nb_bits.wrapping_add(1);
}
for node in 0..=non_null_rank as usize {
let parent = nodes[huff_node_index(node)].parent as usize;
nodes[huff_node_index(node)].nb_bits =
nodes[huff_node_index(parent)].nb_bits.wrapping_add(1);
}
Some(non_null_rank as usize)
}
fn huf_set_max_height(
nodes: &mut [NodeElt; 2 * (HUF_SYMBOLVALUE_MAX as usize + 1)],
last_non_null: usize,
target_nb_bits: u32,
) -> u32 {
let largest_bits = u32::from(nodes[huff_node_index(last_non_null)].nb_bits);
if largest_bits <= target_nb_bits {
return largest_bits;
}
let mut total_cost = 0i32;
let base_cost = 1i32 << (largest_bits - target_nb_bits);
let mut n = last_non_null as i32;
while u32::from(nodes[huff_node_index(n as usize)].nb_bits) > target_nb_bits {
let bits = u32::from(nodes[huff_node_index(n as usize)].nb_bits);
total_cost += base_cost - (1i32 << (largest_bits - bits));
nodes[huff_node_index(n as usize)].nb_bits = target_nb_bits as u8;
n -= 1;
}
while n >= 0 && u32::from(nodes[huff_node_index(n as usize)].nb_bits) == target_nb_bits {
n -= 1;
}
if n < 0 {
return target_nb_bits;
}
total_cost >>= largest_bits - target_nb_bits;
const NO_SYMBOL: u32 = 0xf0f0_f0f0;
let mut rank_last = [NO_SYMBOL; HUF_TABLELOG_MAX as usize + 2];
let mut current_nb_bits = target_nb_bits;
for position in (0..=n as usize).rev() {
let bits = u32::from(nodes[huff_node_index(position)].nb_bits);
if bits >= current_nb_bits {
continue;
}
current_nb_bits = bits;
rank_last[(target_nb_bits - current_nb_bits) as usize] = position as u32;
}
while total_cost > 0 {
let mut bits_to_decrease = ZSTD_highbit32(total_cost as u32) + 1;
while bits_to_decrease > 1 {
let high_position = rank_last[bits_to_decrease as usize];
let low_position = rank_last[(bits_to_decrease - 1) as usize];
if high_position == NO_SYMBOL {
bits_to_decrease -= 1;
continue;
}
if low_position == NO_SYMBOL {
break;
}
let high_total = nodes[huff_node_index(high_position as usize)].count;
let low_total = nodes[huff_node_index(low_position as usize)]
.count
.wrapping_mul(2);
if high_total <= low_total {
break;
}
bits_to_decrease -= 1;
}
while bits_to_decrease <= HUF_TABLELOG_MAX
&& rank_last[bits_to_decrease as usize] == NO_SYMBOL
{
bits_to_decrease += 1;
}
if bits_to_decrease > HUF_TABLELOG_MAX + 1 {
return target_nb_bits;
}
let position = rank_last[bits_to_decrease as usize] as usize;
total_cost -= 1i32 << (bits_to_decrease - 1);
nodes[huff_node_index(position)].nb_bits =
nodes[huff_node_index(position)].nb_bits.wrapping_add(1);
if rank_last[(bits_to_decrease - 1) as usize] == NO_SYMBOL {
rank_last[(bits_to_decrease - 1) as usize] = rank_last[bits_to_decrease as usize];
}
if rank_last[bits_to_decrease as usize] == 0 {
rank_last[bits_to_decrease as usize] = NO_SYMBOL;
} else {
rank_last[bits_to_decrease as usize] -= 1;
let previous = rank_last[bits_to_decrease as usize] as usize;
if u32::from(nodes[huff_node_index(previous)].nb_bits)
!= target_nb_bits - bits_to_decrease
{
rank_last[bits_to_decrease as usize] = NO_SYMBOL;
}
}
}
while total_cost < 0 {
if rank_last[1] == NO_SYMBOL {
while n >= 0 && u32::from(nodes[huff_node_index(n as usize)].nb_bits) == target_nb_bits
{
n -= 1;
}
if n < 0 {
return target_nb_bits;
}
let position = n as usize + 1;
nodes[huff_node_index(position)].nb_bits =
nodes[huff_node_index(position)].nb_bits.wrapping_sub(1);
rank_last[1] = position as u32;
total_cost += 1;
continue;
}
let position = rank_last[1] as usize + 1;
nodes[huff_node_index(position)].nb_bits =
nodes[huff_node_index(position)].nb_bits.wrapping_sub(1);
rank_last[1] = rank_last[1].wrapping_add(1);
total_cost += 1;
}
target_nb_bits
}
unsafe fn huf_build_ctable_from_tree(
ctable: *mut usize,
nodes: &[NodeElt; 2 * (HUF_SYMBOLVALUE_MAX as usize + 1)],
non_null_rank: usize,
max_symbol_value: u32,
max_nb_bits: u32,
) {
let mut nb_per_rank = [0u16; HUF_TABLELOG_MAX as usize + 1];
let mut val_per_rank = [0u16; HUF_TABLELOG_MAX as usize + 1];
for node in 0..=non_null_rank {
let bits = nodes[huff_node_index(node)].nb_bits as usize;
nb_per_rank[bits] = nb_per_rank[bits].wrapping_add(1);
}
let mut min = 0u16;
for rank in (1..=max_nb_bits as usize).rev() {
val_per_rank[rank] = min;
min = min.wrapping_add(nb_per_rank[rank]);
min >>= 1;
}
for node in 0..=max_symbol_value as usize {
let symbol = nodes[huff_node_index(node)].byte as usize;
huf_set_nb_bits(
ctable,
symbol + 1,
nodes[huff_node_index(node)].nb_bits as usize,
);
}
for symbol in 0..=max_symbol_value as usize {
let bits = huf_get_nb_bits(ctable_read(ctable, symbol + 1));
let value = val_per_rank[bits];
huf_set_value(ctable, symbol + 1, value as usize);
val_per_rank[bits] = val_per_rank[bits].wrapping_add(1);
}
huf_write_ctable_header(ctable, max_nb_bits, max_symbol_value);
}
#[no_mangle]
pub unsafe extern "C" fn HUF_buildCTable_wksp(
ctable: *mut usize,
count: *const c_uint,
max_symbol_value: u32,
mut max_nb_bits: u32,
workspace: *mut c_void,
workspace_size: usize,
) -> usize {
if huf_aligned_workspace_size(workspace, workspace_size, size_of::<u32>())
< HUF_CTABLE_WORKSPACE_SIZE
{
return ERROR(ZstdErrorCode::WorkSpaceTooSmall);
}
if max_nb_bits == 0 {
max_nb_bits = HUF_TABLELOG_DEFAULT;
}
if max_symbol_value > HUF_SYMBOLVALUE_MAX {
return ERROR(ZstdErrorCode::MaxSymbolValueTooLarge);
}
let mut nodes = [NodeElt::default(); 2 * (HUF_SYMBOLVALUE_MAX as usize + 1)];
let mut rank_position = [RankPos::default(); RANK_POSITION_TABLE_SIZE];
huf_sort(&mut nodes, count, max_symbol_value, &mut rank_position);
let non_null_rank = match huf_build_tree(&mut nodes, max_symbol_value) {
Some(rank) => rank,
None => return ERROR(ZstdErrorCode::Generic),
};
max_nb_bits = huf_set_max_height(&mut nodes, non_null_rank, max_nb_bits);
if max_nb_bits > HUF_TABLELOG_MAX {
return ERROR(ZstdErrorCode::Generic);
}
huf_build_ctable_from_tree(ctable, &nodes, non_null_rank, max_symbol_value, max_nb_bits);
max_nb_bits as usize
}
#[no_mangle]
pub unsafe extern "C" fn HUF_estimateCompressedSize(
ctable: *const usize,
count: *const c_uint,
max_symbol_value: c_uint,
) -> usize {
let mut bits = 0usize;
for symbol in 0..=max_symbol_value as usize {
bits = bits.wrapping_add(
huf_get_nb_bits(ctable_read(ctable, symbol + 1))
.wrapping_mul(*count.add(symbol) as usize),
);
}
bits >> 3
}
#[no_mangle]
pub unsafe extern "C" fn HUF_validateCTable(
ctable: *const usize,
count: *const c_uint,
max_symbol_value: c_uint,
) -> c_int {
let header = huf_read_ctable_header(ctable);
if u32::from(header.maxSymbolValue) < max_symbol_value {
return 0;
}
for symbol in 0..=max_symbol_value as usize {
if *count.add(symbol) != 0 && huf_get_nb_bits(ctable_read(ctable, symbol + 1)) == 0 {
return 0;
}
}
1
}
#[no_mangle]
pub extern "C" fn HUF_compressBound(size: usize) -> usize {
129usize
.wrapping_add(size)
.wrapping_add(size >> 8)
.wrapping_add(8)
}
struct HufCStream {
bit_container: usize,
bit_pos: usize,
dst: *mut u8,
ptr: usize,
end: usize,
}
impl HufCStream {
unsafe fn init(dst: *mut u8, dst_capacity: usize) -> Option<Self> {
let container_size = size_of::<usize>();
if dst_capacity <= container_size {
return None;
}
Some(Self {
bit_container: 0,
bit_pos: 0,
dst,
ptr: 0,
end: dst_capacity - container_size,
})
}
#[inline]
fn add_bits(&mut self, elt: usize) {
let nb_bits = huf_get_nb_bits(elt);
self.bit_container >>= nb_bits;
self.bit_container |= huf_get_value(elt);
self.bit_pos = self.bit_pos.wrapping_add(nb_bits);
}
unsafe fn flush_bits(&mut self) {
let nb_bits = self.bit_pos & 0xff;
if nb_bits == 0 {
return;
}
let nb_bytes = nb_bits >> 3;
let bit_container = self.bit_container >> (usize::BITS as usize - nb_bits);
for byte in 0..nb_bytes {
// The C encoder writes a full native word at `ptr`, then clamps the
// logical cursor. Only materialize the bytes that are logically
// part of the stream; `ptr <= end` guarantees those are in bounds.
*self.dst.add(self.ptr + byte) = (bit_container >> (8 * byte)) as u8;
}
self.ptr = self.ptr.saturating_add(nb_bytes);
if self.ptr > self.end {
self.ptr = self.end;
}
self.bit_pos &= 7;
}
unsafe fn close(mut self) -> usize {
let end_mark = 1usize | (1usize << (usize::BITS as usize - 1));
self.add_bits(end_mark);
self.flush_bits();
if self.ptr >= self.end {
return 0;
}
if self.bit_pos & 0xff != 0 {
let partial = self.bit_container >> (usize::BITS as usize - (self.bit_pos & 0xff));
*self.dst.add(self.ptr) = partial as u8;
}
self.ptr + usize::from((self.bit_pos & 0xff) > 0)
}
}
unsafe fn huf_compress1x_using_ctable_internal(
dst: *mut u8,
dst_size: usize,
src: *const u8,
src_size: usize,
ctable: *const usize,
) -> usize {
if dst_size < 8 {
return 0;
}
let mut stream = match HufCStream::init(dst, dst_size) {
Some(stream) => stream,
None => return 0,
};
for index in (0..src_size).rev() {
let symbol = *src.add(index) as usize;
stream.add_bits(ctable_read(ctable, symbol + 1));
// Keeping the pending tail below one byte makes the portable Rust
// stream independent of the C implementation's BMI2-oriented unroll
// schedule while retaining the exact HUF bitstream representation.
stream.flush_bits();
}
stream.close()
}
#[no_mangle]
pub unsafe extern "C" fn HUF_compress1X_usingCTable(
dst: *mut c_void,
dst_size: usize,
src: *const c_void,
src_size: usize,
ctable: *const usize,
_flags: c_int,
) -> usize {
huf_compress1x_using_ctable_internal(dst.cast(), dst_size, src.cast(), src_size, ctable)
}
unsafe fn huf_compress4x_using_ctable_internal(
dst: *mut u8,
dst_size: usize,
src: *const u8,
src_size: usize,
ctable: *const usize,
) -> usize {
if dst_size < 6 + 1 + 1 + 1 + 8 || src_size < 12 {
return 0;
}
let segment_size = src_size.div_ceil(4);
let mut input_offset = 0usize;
let mut output_offset = 6usize;
for segment in 0..4 {
let input_size = if segment < 3 {
segment_size
} else {
src_size - input_offset
};
let c_size = huf_compress1x_using_ctable_internal(
dst.add(output_offset),
dst_size - output_offset,
src.add(input_offset),
input_size,
ctable,
);
if c_size == 0 || c_size > u16::MAX as usize {
return 0;
}
if segment < 3 {
MEM_writeLE16(dst.add(segment * 2).cast::<c_void>(), c_size as u16);
}
output_offset += c_size;
input_offset += input_size;
}
output_offset
}
#[no_mangle]
pub unsafe extern "C" fn HUF_compress4X_usingCTable(
dst: *mut c_void,
dst_size: usize,
src: *const c_void,
src_size: usize,
ctable: *const usize,
_flags: c_int,
) -> usize {
huf_compress4x_using_ctable_internal(dst.cast(), dst_size, src.cast(), src_size, ctable)
}
#[allow(clippy::too_many_arguments)]
unsafe fn huf_compress_ctable_internal(
dst: *mut u8,
dst_size: usize,
prefix_size: usize,
src: *const u8,
src_size: usize,
four_streams: bool,
ctable: *const usize,
flags: c_int,
) -> usize {
if prefix_size > dst_size {
return 0;
}
let c_size = if four_streams {
HUF_compress4X_usingCTable(
dst.add(prefix_size).cast::<c_void>(),
dst_size - prefix_size,
src.cast::<c_void>(),
src_size,
ctable,
flags,
)
} else {
HUF_compress1X_usingCTable(
dst.add(prefix_size).cast::<c_void>(),
dst_size - prefix_size,
src.cast::<c_void>(),
src_size,
ctable,
flags,
)
};
if ERR_isError(c_size) {
return c_size;
}
let total_size = prefix_size.wrapping_add(c_size);
if c_size == 0 || total_size >= src_size.saturating_sub(1) {
return 0;
}
total_size
}
#[no_mangle]
pub unsafe extern "C" fn HUF_cardinality(count: *const c_uint, max_symbol_value: c_uint) -> c_uint {
let mut cardinality = 0u32;
for symbol in 0..=max_symbol_value as usize {
if *count.add(symbol) != 0 {
cardinality += 1;
}
}
cardinality
}
#[no_mangle]
pub extern "C" fn HUF_minTableLog(symbol_cardinality: c_uint) -> c_uint {
ZSTD_highbit32(symbol_cardinality) + 1
}
#[no_mangle]
pub unsafe extern "C" fn HUF_optimalTableLog(
max_table_log: c_uint,
src_size: usize,
max_symbol_value: c_uint,
workspace: *mut c_void,
workspace_size: usize,
table: *mut usize,
count: *const c_uint,
flags: c_int,
) -> c_uint {
if flags & HUF_FLAGS_OPTIMAL_DEPTH == 0 {
return FSE_optimalTableLog_internal(max_table_log, src_size, max_symbol_value, 1);
}
let symbol_cardinality = HUF_cardinality(count, max_symbol_value);
if symbol_cardinality == 0 {
return max_table_log;
}
let min_table_log = HUF_minTableLog(symbol_cardinality);
let mut opt_size = usize::MAX - 1;
let mut opt_log = max_table_log;
let mut probe_dst = [0u8; HUF_WORKSPACE_SIZE];
let write_workspace_size = workspace_size.saturating_sub(748);
for guess in min_table_log..=max_table_log {
let max_bits = HUF_buildCTable_wksp(
table,
count,
max_symbol_value,
guess,
workspace,
workspace_size,
);
if ERR_isError(max_bits) {
continue;
}
if max_bits < guess as usize && guess > min_table_log {
break;
}
let h_size = HUF_writeCTable_wksp(
probe_dst.as_mut_ptr().cast::<c_void>(),
write_workspace_size.min(probe_dst.len()),
table,
max_symbol_value,
max_bits as u32,
workspace,
workspace_size,
);
if ERR_isError(h_size) {
continue;
}
let new_size =
HUF_estimateCompressedSize(table, count, max_symbol_value).wrapping_add(h_size);
if new_size > opt_size.wrapping_add(1) {
break;
}
if new_size < opt_size {
opt_size = new_size;
opt_log = guess;
}
}
opt_log
}
#[inline]
fn huf_compress_tables_size() -> usize {
(HUF_SYMBOLVALUE_MAX as usize + 1) * size_of::<u32>()
+ HUF_CTABLE_SIZE_ST * size_of::<usize>()
+ HUF_CTABLE_WORKSPACE_SIZE
}
#[allow(clippy::too_many_arguments)]
unsafe fn huf_compress_internal(
dst: *mut u8,
dst_size: usize,
src: *const u8,
src_size: usize,
mut max_symbol_value: u32,
mut huff_log: u32,
four_streams: bool,
workspace: *mut c_void,
workspace_size: usize,
old_huf_table: *mut usize,
repeat: *mut c_int,
flags: c_int,
) -> usize {
if huf_aligned_workspace_size(workspace, workspace_size, size_of::<usize>())
< huf_compress_tables_size()
{
return ERROR(ZstdErrorCode::WorkSpaceTooSmall);
}
if src_size == 0 || dst_size == 0 {
return 0;
}
if src_size > HUF_BLOCKSIZE_MAX {
return ERROR(ZstdErrorCode::SrcSizeWrong);
}
if huff_log > HUF_TABLELOG_MAX {
return ERROR(ZstdErrorCode::TableLogTooLarge);
}
if max_symbol_value > HUF_SYMBOLVALUE_MAX {
return ERROR(ZstdErrorCode::MaxSymbolValueTooLarge);
}
if max_symbol_value == 0 {
max_symbol_value = HUF_SYMBOLVALUE_MAX;
}
if huff_log == 0 {
huff_log = HUF_TABLELOG_DEFAULT;
}
if flags & HUF_FLAGS_PREFER_REPEAT != 0
&& !repeat.is_null()
&& *repeat == HUF_REPEAT_VALID
&& !old_huf_table.is_null()
{
return huf_compress_ctable_internal(
dst,
dst_size,
0,
src,
src_size,
four_streams,
old_huf_table,
flags,
);
}
let mut count = [0u32; HUF_SYMBOLVALUE_MAX as usize + 1];
if flags & HUF_FLAGS_SUSPECT_UNCOMPRESSIBLE != 0
&& src_size >= SUSPECT_INCOMPRESSIBLE_SAMPLE_SIZE * SUSPECT_INCOMPRESSIBLE_SAMPLE_RATIO
{
let mut largest_total = 0usize;
let mut sample_max = max_symbol_value;
largest_total += HIST_count_simple(
count.as_mut_ptr(),
&mut sample_max,
src.cast::<c_void>(),
SUSPECT_INCOMPRESSIBLE_SAMPLE_SIZE,
) as usize;
sample_max = max_symbol_value;
largest_total += HIST_count_simple(
count.as_mut_ptr(),
&mut sample_max,
src.add(src_size - SUSPECT_INCOMPRESSIBLE_SAMPLE_SIZE)
.cast::<c_void>(),
SUSPECT_INCOMPRESSIBLE_SAMPLE_SIZE,
) as usize;
if largest_total <= ((2 * SUSPECT_INCOMPRESSIBLE_SAMPLE_SIZE) >> 7).wrapping_add(4) {
return 0;
}
}
let mut hist_workspace = [0u32; HIST_WKSP_SIZE_U32];
let largest = HIST_count_wksp(
count.as_mut_ptr(),
&mut max_symbol_value,
src.cast::<c_void>(),
src_size,
hist_workspace.as_mut_ptr().cast::<c_void>(),
size_of::<[u32; HIST_WKSP_SIZE_U32]>(),
);
if ERR_isError(largest) {
return largest;
}
if largest == src_size {
*dst = *src;
return 1;
}
if largest <= (src_size >> 7).wrapping_add(4) {
return 0;
}
if !repeat.is_null()
&& *repeat == HUF_REPEAT_CHECK
&& (old_huf_table.is_null()
|| HUF_validateCTable(old_huf_table, count.as_ptr(), max_symbol_value) == 0)
{
*repeat = HUF_REPEAT_NONE;
}
if flags & HUF_FLAGS_PREFER_REPEAT != 0
&& !repeat.is_null()
&& *repeat != HUF_REPEAT_NONE
&& !old_huf_table.is_null()
{
return huf_compress_ctable_internal(
dst,
dst_size,
0,
src,
src_size,
four_streams,
old_huf_table,
flags,
);
}
let mut ctable = [0usize; HUF_CTABLE_SIZE_ST];
huff_log = HUF_optimalTableLog(
huff_log,
src_size,
max_symbol_value,
workspace,
HUF_CTABLE_WORKSPACE_SIZE,
ctable.as_mut_ptr(),
count.as_ptr(),
flags,
);
let max_bits = HUF_buildCTable_wksp(
ctable.as_mut_ptr(),
count.as_ptr(),
max_symbol_value,
huff_log,
workspace,
HUF_CTABLE_WORKSPACE_SIZE,
);
if ERR_isError(max_bits) {
return max_bits;
}
huff_log = max_bits as u32;
let header_size = HUF_writeCTable_wksp(
dst.cast::<c_void>(),
dst_size,
ctable.as_ptr(),
max_symbol_value,
huff_log,
workspace,
HUF_CTABLE_WORKSPACE_SIZE,
);
if ERR_isError(header_size) {
return header_size;
}
if !repeat.is_null() && *repeat != HUF_REPEAT_NONE && !old_huf_table.is_null() {
let old_size = HUF_estimateCompressedSize(old_huf_table, count.as_ptr(), max_symbol_value);
let new_size =
HUF_estimateCompressedSize(ctable.as_ptr(), count.as_ptr(), max_symbol_value);
if old_size <= header_size.wrapping_add(new_size)
|| header_size.wrapping_add(12) >= src_size
{
return huf_compress_ctable_internal(
dst,
dst_size,
0,
src,
src_size,
four_streams,
old_huf_table,
flags,
);
}
}
if header_size.wrapping_add(12) >= src_size {
return 0;
}
if !repeat.is_null() {
*repeat = HUF_REPEAT_NONE;
}
if !old_huf_table.is_null() {
std::ptr::copy_nonoverlapping(
ctable.as_ptr().cast::<u8>(),
old_huf_table.cast::<u8>(),
size_of::<[usize; HUF_CTABLE_SIZE_ST]>(),
);
}
huf_compress_ctable_internal(
dst,
dst_size,
header_size,
src,
src_size,
four_streams,
ctable.as_ptr(),
flags,
)
}
#[no_mangle]
pub unsafe extern "C" fn HUF_compress1X_repeat(
dst: *mut c_void,
dst_size: usize,
src: *const c_void,
src_size: usize,
max_symbol_value: c_uint,
huff_log: c_uint,
workspace: *mut c_void,
workspace_size: usize,
huf_table: *mut usize,
repeat: *mut c_int,
flags: c_int,
) -> usize {
huf_compress_internal(
dst.cast(),
dst_size,
src.cast(),
src_size,
max_symbol_value,
huff_log,
false,
workspace,
workspace_size,
huf_table,
repeat,
flags,
)
}
#[no_mangle]
pub unsafe extern "C" fn HUF_compress4X_repeat(
dst: *mut c_void,
dst_size: usize,
src: *const c_void,
src_size: usize,
max_symbol_value: c_uint,
huff_log: c_uint,
workspace: *mut c_void,
workspace_size: usize,
huf_table: *mut usize,
repeat: *mut c_int,
flags: c_int,
) -> usize {
huf_compress_internal(
dst.cast(),
dst_size,
src.cast(),
src_size,
max_symbol_value,
huff_log,
true,
workspace,
workspace_size,
huf_table,
repeat,
flags,
)
}
#[cfg(all(test, feature = "decompression"))]
mod tests {
use super::*;
use crate::huf_decompress::{
HUF_decompress1X_usingDTable, HUF_decompress4X_usingDTable, HUF_readDTableX1_wksp,
};
const WORKSPACE_SIZE: usize = HUF_WORKSPACE_SIZE;
fn source() -> Vec<u8> {
let mut source = Vec::with_capacity(4096);
for index in 0..4096 {
source.push(match index % 17 {
0..=8 => b'a',
9..=12 => b'b',
13..=14 => b'c',
15 => b'd',
_ => b'e',
});
}
source
}
unsafe fn make_table(
source: &[u8],
table: &mut [usize; HUF_CTABLE_SIZE_ST],
workspace: &mut [u64; WORKSPACE_SIZE / size_of::<u64>()],
) -> u32 {
let mut count = [0u32; HUF_SYMBOLVALUE_MAX as usize + 1];
for byte in source {
count[*byte as usize] += 1;
}
let mut max_symbol = HUF_SYMBOLVALUE_MAX;
while count[max_symbol as usize] == 0 {
max_symbol -= 1;
}
let bits = HUF_buildCTable_wksp(
table.as_mut_ptr(),
count.as_ptr(),
max_symbol,
0,
workspace.as_mut_ptr().cast::<c_void>(),
std::mem::size_of_val(workspace),
);
assert!(!ERR_isError(bits));
bits as u32
}
unsafe fn dtable_from_header(
header: &[u8],
workspace: &mut [u64; WORKSPACE_SIZE / size_of::<u64>()],
) -> [u32; 1 + (1 << 11)] {
let mut dtable = [0u32; 1 + (1 << 11)];
dtable[0] = 11 * 0x0100_0001;
let read = HUF_readDTableX1_wksp(
dtable.as_mut_ptr(),
header.as_ptr().cast::<c_void>(),
header.len(),
workspace.as_mut_ptr().cast::<c_void>(),
std::mem::size_of_val(workspace),
0,
);
assert!(!ERR_isError(read));
dtable
}
#[test]
fn tables_and_single_stream_round_trip() {
let source = source();
let mut workspace = [0u64; WORKSPACE_SIZE / size_of::<u64>()];
let mut table = [0usize; HUF_CTABLE_SIZE_ST];
let bits = unsafe { make_table(&source, &mut table, &mut workspace) };
assert!((1..=HUF_TABLELOG_MAX).contains(&bits));
let max_symbol = unsafe { HUF_readCTableHeader(table.as_ptr()).maxSymbolValue as u32 };
let mut header = [0u8; 512];
let header_size = unsafe {
HUF_writeCTable_wksp(
header.as_mut_ptr().cast::<c_void>(),
header.len(),
table.as_ptr(),
max_symbol,
bits,
workspace.as_mut_ptr().cast::<c_void>(),
std::mem::size_of_val(&workspace),
)
};
assert!(!ERR_isError(header_size));
let dtable = unsafe { dtable_from_header(&header[..header_size], &mut workspace) };
let mut compressed = vec![0u8; source.len() + 64];
let c_size = unsafe {
HUF_compress1X_usingCTable(
compressed.as_mut_ptr().cast::<c_void>(),
compressed.len(),
source.as_ptr().cast::<c_void>(),
source.len(),
table.as_ptr(),
0,
)
};
assert!(c_size > 0);
let mut decoded = vec![0u8; source.len()];
let d_size = unsafe {
HUF_decompress1X_usingDTable(
decoded.as_mut_ptr().cast::<c_void>(),
decoded.len(),
compressed.as_ptr().cast::<c_void>(),
c_size,
dtable.as_ptr(),
0,
)
};
assert_eq!(d_size, source.len());
assert_eq!(decoded, source);
}
#[test]
fn four_stream_and_repeat_round_trip() {
let source = source();
let mut workspace = [0u64; WORKSPACE_SIZE / size_of::<u64>()];
let mut old_table = [0usize; HUF_CTABLE_SIZE_ST];
let mut repeat = HUF_REPEAT_NONE;
let mut compressed = vec![0u8; source.len() + 256];
let c_size = unsafe {
HUF_compress4X_repeat(
compressed.as_mut_ptr().cast::<c_void>(),
compressed.len(),
source.as_ptr().cast::<c_void>(),
source.len(),
HUF_SYMBOLVALUE_MAX,
HUF_TABLELOG_DEFAULT,
workspace.as_mut_ptr().cast::<c_void>(),
std::mem::size_of_val(&workspace),
old_table.as_mut_ptr(),
&mut repeat,
HUF_FLAGS_OPTIMAL_DEPTH,
)
};
assert!(c_size > 0);
let mut dtable = [0u32; 1 + (1 << 11)];
dtable[0] = 11 * 0x0100_0001;
let header_size = unsafe {
HUF_readDTableX1_wksp(
dtable.as_mut_ptr(),
compressed.as_ptr().cast::<c_void>(),
c_size,
workspace.as_mut_ptr().cast::<c_void>(),
std::mem::size_of_val(&workspace),
0,
)
};
assert!(!ERR_isError(header_size));
let mut decoded = vec![0u8; source.len()];
let d_size = unsafe {
HUF_decompress4X_usingDTable(
decoded.as_mut_ptr().cast::<c_void>(),
decoded.len(),
compressed.as_ptr().add(header_size).cast::<c_void>(),
c_size - header_size,
dtable.as_ptr(),
0,
)
};
assert_eq!(d_size, source.len());
assert_eq!(decoded, source);
}
}
+2
View File
@@ -12,6 +12,8 @@ pub mod fse_compress;
pub mod fse_decompress;
#[cfg(feature = "compression")]
pub mod hist;
#[cfg(feature = "compression")]
pub mod huf_compress;
#[cfg(feature = "decompression")]
pub mod huf_decompress;
pub mod mem;