Skip to main content

libzstd_rs_sys/lib/compress/
zstd_compress_superblock.rs

1use libc::size_t;
2
3use crate::lib::common::error_private::{ERR_isError, Error};
4use crate::lib::common::fse::FSE_CTable;
5use crate::lib::common::huf::{HUF_CElt, HUF_flags_bmi2};
6use crate::lib::common::mem::{MEM_32bits, MEM_writeLE16, MEM_writeLE24, MEM_writeLE32};
7use crate::lib::common::zstd_internal::{
8    bt_compressed, bt_raw, DefaultMaxOff, LL_bits, LL_defaultNorm, LL_defaultNormLog, ML_bits,
9    ML_defaultNorm, ML_defaultNormLog, MaxLL, MaxML, MaxOff, OF_defaultNorm, OF_defaultNormLog,
10    MINMATCH, ZSTD_REP_NUM,
11};
12use crate::lib::compress::hist::{HIST_countFast_wksp, HIST_count_wksp};
13use crate::lib::compress::huf_compress::{
14    HUF_compress1X_usingCTable, HUF_compress4X_usingCTable, HUF_estimateCompressedSize,
15};
16use crate::lib::compress::zstd_compress::{
17    SeqDef, SeqStore_t, ZSTD_CCtx, ZSTD_CCtx_params, ZSTD_CDict, ZSTD_MatchState_t,
18    ZSTD_buildBlockEntropyStats, ZSTD_compressedBlockState_t, ZSTD_entropyCTablesMetadata_t,
19    ZSTD_entropyCTables_t, ZSTD_fseCTablesMetadata_t, ZSTD_fseCTables_t, ZSTD_hufCTablesMetadata_t,
20    ZSTD_hufCTables_t, ZSTD_match_t, ZSTD_optimal_t,
21};
22use crate::lib::compress::zstd_compress_literals::{
23    ZSTD_compressRleLiteralsBlock, ZSTD_noCompressLiterals,
24};
25use crate::lib::compress::zstd_compress_sequences::{
26    ZSTD_crossEntropyCost, ZSTD_encodeSequences, ZSTD_fseBitCost,
27};
28
29#[derive(Copy, Clone)]
30#[repr(C)]
31pub struct ZSTD_Sequence {
32    pub offset: core::ffi::c_uint,
33    pub litLength: core::ffi::c_uint,
34    pub matchLength: core::ffi::c_uint,
35    pub rep: core::ffi::c_uint,
36}
37#[derive(Copy, Clone)]
38#[repr(C)]
39pub struct ZSTD_blockSplitCtx {
40    pub fullSeqStoreChunk: SeqStore_t,
41    pub firstHalfSeqStore: SeqStore_t,
42    pub secondHalfSeqStore: SeqStore_t,
43    pub currSeqStore: SeqStore_t,
44    pub nextSeqStore: SeqStore_t,
45    pub partitions: [u32; 196],
46    pub entropyMetadata: ZSTD_entropyCTablesMetadata_t,
47}
48pub type SymbolEncodingType_e = core::ffi::c_uint;
49pub const set_repeat: SymbolEncodingType_e = 3;
50pub const set_compressed: SymbolEncodingType_e = 2;
51pub const set_rle: SymbolEncodingType_e = 1;
52pub const set_basic: SymbolEncodingType_e = 0;
53pub type ZSTD_longLengthType_e = core::ffi::c_uint;
54pub const ZSTD_llt_matchLength: ZSTD_longLengthType_e = 2;
55pub const ZSTD_llt_literalLength: ZSTD_longLengthType_e = 1;
56pub const ZSTD_llt_none: ZSTD_longLengthType_e = 0;
57pub type ZSTD_prefixDict = ZSTD_prefixDict_s;
58#[repr(C)]
59pub struct ZSTD_prefixDict_s {
60    pub dict: *const core::ffi::c_void,
61    pub dictSize: size_t,
62    pub dictContentType: ZSTD_dictContentType_e,
63}
64pub type ZSTD_dictContentType_e = core::ffi::c_uint;
65pub const ZSTD_dct_fullDict: ZSTD_dictContentType_e = 2;
66pub const ZSTD_dct_rawContent: ZSTD_dictContentType_e = 1;
67pub const ZSTD_dct_auto: ZSTD_dictContentType_e = 0;
68#[repr(C)]
69pub struct ZSTD_localDict {
70    pub dictBuffer: *mut core::ffi::c_void,
71    pub dict: *const core::ffi::c_void,
72    pub dictSize: size_t,
73    pub dictContentType: ZSTD_dictContentType_e,
74    pub cdict: *mut ZSTD_CDict,
75}
76pub type ZSTD_inBuffer = ZSTD_inBuffer_s;
77#[repr(C)]
78pub struct ZSTD_inBuffer_s {
79    pub src: *const core::ffi::c_void,
80    pub size: size_t,
81    pub pos: size_t,
82}
83pub type ZSTD_cStreamStage = core::ffi::c_uint;
84pub const zcss_flush: ZSTD_cStreamStage = 2;
85pub const zcss_load: ZSTD_cStreamStage = 1;
86pub const zcss_init: ZSTD_cStreamStage = 0;
87pub type ZSTD_buffered_policy_e = core::ffi::c_uint;
88pub const ZSTDb_buffered: ZSTD_buffered_policy_e = 1;
89pub const ZSTDb_not_buffered: ZSTD_buffered_policy_e = 0;
90#[repr(C)]
91pub struct ZSTD_blockState_t {
92    pub prevCBlock: *mut ZSTD_compressedBlockState_t,
93    pub nextCBlock: *mut ZSTD_compressedBlockState_t,
94    pub matchState: ZSTD_MatchState_t,
95}
96#[repr(C)]
97pub struct optState_t {
98    pub litFreq: *mut core::ffi::c_uint,
99    pub litLengthFreq: *mut core::ffi::c_uint,
100    pub matchLengthFreq: *mut core::ffi::c_uint,
101    pub offCodeFreq: *mut core::ffi::c_uint,
102    pub matchTable: *mut ZSTD_match_t,
103    pub priceTable: *mut ZSTD_optimal_t,
104    pub litSum: u32,
105    pub litLengthSum: u32,
106    pub matchLengthSum: u32,
107    pub offCodeSum: u32,
108    pub litSumBasePrice: u32,
109    pub litLengthSumBasePrice: u32,
110    pub matchLengthSumBasePrice: u32,
111    pub offCodeSumBasePrice: u32,
112    pub priceType: ZSTD_OptPrice_e,
113    pub symbolCosts: *const ZSTD_entropyCTables_t,
114    pub literalCompressionMode: ZSTD_ParamSwitch_e,
115}
116pub type ZSTD_ParamSwitch_e = core::ffi::c_uint;
117pub const ZSTD_ps_disable: ZSTD_ParamSwitch_e = 2;
118pub const ZSTD_ps_enable: ZSTD_ParamSwitch_e = 1;
119pub const ZSTD_ps_auto: ZSTD_ParamSwitch_e = 0;
120pub type ZSTD_OptPrice_e = core::ffi::c_uint;
121pub const zop_predef: ZSTD_OptPrice_e = 1;
122pub const zop_dynamic: ZSTD_OptPrice_e = 0;
123#[repr(C)]
124pub struct ZSTD_window_t {
125    pub nextSrc: *const u8,
126    pub base: *const u8,
127    pub dictBase: *const u8,
128    pub dictLimit: u32,
129    pub lowLimit: u32,
130    pub nbOverflowCorrections: u32,
131}
132#[repr(C)]
133pub struct ldmState_t {
134    pub window: ZSTD_window_t,
135    pub hashTable: *mut ldmEntry_t,
136    pub loadedDictEnd: u32,
137    pub bucketOffsets: *mut u8,
138    pub splitIndices: [size_t; 64],
139    pub matchCandidates: [ldmMatchCandidate_t; 64],
140}
141#[repr(C)]
142pub struct ldmMatchCandidate_t {
143    pub split: *const u8,
144    pub hash: u32,
145    pub checksum: u32,
146    pub bucket: *mut ldmEntry_t,
147}
148#[derive(Copy, Clone)]
149#[repr(C)]
150pub struct ldmEntry_t {
151    pub offset: u32,
152    pub checksum: u32,
153}
154#[derive(Copy, Clone)]
155#[repr(C)]
156pub struct SeqCollector {
157    pub collectSequences: core::ffi::c_int,
158    pub seqStart: *mut ZSTD_Sequence,
159    pub seqIndex: size_t,
160    pub maxSequences: size_t,
161}
162#[derive(Copy, Clone)]
163#[repr(C)]
164pub struct XXH64_state_s {
165    pub total_len: XXH64_hash_t,
166    pub v: [XXH64_hash_t; 4],
167    pub mem64: [XXH64_hash_t; 4],
168    pub memsize: XXH32_hash_t,
169    pub reserved32: XXH32_hash_t,
170    pub reserved64: XXH64_hash_t,
171}
172type XXH64_hash_t = u64;
173type XXH32_hash_t = u32;
174#[repr(C)]
175pub struct ZSTD_cwksp {
176    pub workspace: *mut core::ffi::c_void,
177    pub workspaceEnd: *mut core::ffi::c_void,
178    pub objectEnd: *mut core::ffi::c_void,
179    pub tableEnd: *mut core::ffi::c_void,
180    pub tableValidEnd: *mut core::ffi::c_void,
181    pub allocStart: *mut core::ffi::c_void,
182    pub initOnceStart: *mut core::ffi::c_void,
183    pub allocFailed: u8,
184    pub workspaceOversizedDuration: core::ffi::c_int,
185    pub phase: ZSTD_cwksp_alloc_phase_e,
186    pub isStatic: ZSTD_cwksp_static_alloc_e,
187}
188pub type ZSTD_cwksp_static_alloc_e = core::ffi::c_uint;
189pub const ZSTD_cwksp_static_alloc: ZSTD_cwksp_static_alloc_e = 1;
190pub const ZSTD_cwksp_dynamic_alloc: ZSTD_cwksp_static_alloc_e = 0;
191pub type ZSTD_cwksp_alloc_phase_e = core::ffi::c_uint;
192pub const ZSTD_cwksp_alloc_buffers: ZSTD_cwksp_alloc_phase_e = 3;
193pub const ZSTD_cwksp_alloc_aligned: ZSTD_cwksp_alloc_phase_e = 2;
194pub const ZSTD_cwksp_alloc_aligned_init_once: ZSTD_cwksp_alloc_phase_e = 1;
195pub const ZSTD_cwksp_alloc_objects: ZSTD_cwksp_alloc_phase_e = 0;
196pub type ZSTD_sequenceProducer_F = Option<
197    unsafe extern "C" fn(
198        *mut core::ffi::c_void,
199        *mut ZSTD_Sequence,
200        size_t,
201        *const core::ffi::c_void,
202        size_t,
203        *const core::ffi::c_void,
204        size_t,
205        core::ffi::c_int,
206        size_t,
207    ) -> size_t,
208>;
209pub type ZSTD_SequenceFormat_e = core::ffi::c_uint;
210pub const ZSTD_sf_explicitBlockDelimiters: ZSTD_SequenceFormat_e = 1;
211pub const ZSTD_sf_noBlockDelimiters: ZSTD_SequenceFormat_e = 0;
212#[derive(Copy, Clone)]
213#[repr(C)]
214pub struct ldmParams_t {
215    pub enableLdm: ZSTD_ParamSwitch_e,
216    pub hashLog: u32,
217    pub bucketSizeLog: u32,
218    pub minMatchLength: u32,
219    pub hashRateLog: u32,
220    pub windowLog: u32,
221}
222pub type ZSTD_dictAttachPref_e = core::ffi::c_uint;
223pub const ZSTD_dictForceLoad: ZSTD_dictAttachPref_e = 3;
224pub const ZSTD_dictForceCopy: ZSTD_dictAttachPref_e = 2;
225pub const ZSTD_dictForceAttach: ZSTD_dictAttachPref_e = 1;
226pub const ZSTD_dictDefaultAttach: ZSTD_dictAttachPref_e = 0;
227#[derive(Copy, Clone)]
228#[repr(C)]
229pub struct ZSTD_frameParameters {
230    pub contentSizeFlag: core::ffi::c_int,
231    pub checksumFlag: core::ffi::c_int,
232    pub noDictIDFlag: core::ffi::c_int,
233}
234pub type ZSTD_compressionStage_e = core::ffi::c_uint;
235pub const ZSTDcs_ending: ZSTD_compressionStage_e = 3;
236pub const ZSTDcs_ongoing: ZSTD_compressionStage_e = 2;
237pub const ZSTDcs_init: ZSTD_compressionStage_e = 1;
238pub const ZSTDcs_created: ZSTD_compressionStage_e = 0;
239pub type Repcodes_t = repcodes_s;
240#[derive(Copy, Clone)]
241#[repr(C)]
242pub struct repcodes_s {
243    pub rep: [u32; 3],
244}
245#[derive(Copy, Clone)]
246#[repr(C)]
247pub struct ZSTD_SequenceLength {
248    pub litLength: u32,
249    pub matchLength: u32,
250}
251#[derive(Copy, Clone)]
252#[repr(C)]
253pub struct EstimatedBlockSize {
254    pub estLitSize: size_t,
255    pub estBlockSize: size_t,
256}
257pub const ZSTD_TARGETCBLOCKSIZE_MIN: core::ffi::c_int = 1340;
258#[inline]
259unsafe fn ZSTD_getSequenceLength(
260    seqStore: *const SeqStore_t,
261    seq: *const SeqDef,
262) -> ZSTD_SequenceLength {
263    let mut seqLen = ZSTD_SequenceLength {
264        litLength: 0,
265        matchLength: 0,
266    };
267    seqLen.litLength = (*seq).litLength as u32;
268    seqLen.matchLength = ((*seq).mlBase as core::ffi::c_int + MINMATCH) as u32;
269    if (*seqStore).longLengthPos
270        == seq.offset_from((*seqStore).sequencesStart) as core::ffi::c_long as u32
271    {
272        if (*seqStore).longLengthType as core::ffi::c_uint
273            == ZSTD_llt_literalLength as core::ffi::c_int as core::ffi::c_uint
274        {
275            seqLen.litLength = (seqLen.litLength).wrapping_add(0x10000 as core::ffi::c_int as u32);
276        }
277        if (*seqStore).longLengthType as core::ffi::c_uint
278            == ZSTD_llt_matchLength as core::ffi::c_int as core::ffi::c_uint
279        {
280            seqLen.matchLength =
281                (seqLen.matchLength).wrapping_add(0x10000 as core::ffi::c_int as u32);
282        }
283    }
284    seqLen
285}
286#[inline]
287unsafe fn ZSTD_noCompressBlock(
288    dst: *mut core::ffi::c_void,
289    dstCapacity: size_t,
290    src: *const core::ffi::c_void,
291    srcSize: size_t,
292    lastBlock: u32,
293) -> size_t {
294    let cBlockHeader24 = lastBlock
295        .wrapping_add((bt_raw as core::ffi::c_int as u32) << 1)
296        .wrapping_add((srcSize << 3) as u32);
297    if srcSize.wrapping_add(ZSTD_blockHeaderSize) > dstCapacity {
298        return Error::dstSize_tooSmall.to_error_code();
299    }
300    MEM_writeLE24(dst, cBlockHeader24);
301    libc::memcpy(
302        (dst as *mut u8).add(ZSTD_blockHeaderSize) as *mut core::ffi::c_void,
303        src,
304        srcSize as libc::size_t,
305    );
306    ZSTD_blockHeaderSize.wrapping_add(srcSize)
307}
308#[inline]
309unsafe fn ZSTD_updateRep(rep: *mut u32, offBase: u32, ll0: u32) {
310    if offBase > ZSTD_REP_NUM as u32 {
311        *rep.offset(2) = *rep.offset(1);
312        *rep.offset(1) = *rep.offset(0);
313        *rep.offset(0) = offBase.wrapping_sub(ZSTD_REP_NUM as u32);
314    } else {
315        let repCode = offBase.wrapping_sub(1).wrapping_add(ll0);
316        if repCode > 0 {
317            let currentOffset = if repCode == ZSTD_REP_NUM as u32 {
318                (*rep.offset(0)).wrapping_sub(1)
319            } else {
320                *rep.offset(repCode as isize)
321            };
322            *rep.offset(2) = if repCode >= 2 {
323                *rep.offset(1)
324            } else {
325                *rep.offset(2)
326            };
327            *rep.offset(1) = *rep.offset(0);
328            *rep.offset(0) = currentOffset;
329        }
330    };
331}
332pub const ZSTD_BLOCKHEADERSIZE: core::ffi::c_int = 3;
333static ZSTD_blockHeaderSize: size_t = ZSTD_BLOCKHEADERSIZE as size_t;
334pub const LONGNBSEQ: core::ffi::c_int = 0x7f00 as core::ffi::c_int;
335pub const STREAM_ACCUMULATOR_MIN_32: core::ffi::c_int = 25;
336pub const STREAM_ACCUMULATOR_MIN_64: core::ffi::c_int = 57;
337unsafe fn ZSTD_compressSubBlock_literal(
338    hufTable: *const HUF_CElt,
339    hufMetadata: *const ZSTD_hufCTablesMetadata_t,
340    literals: *const u8,
341    litSize: size_t,
342    dst: *mut core::ffi::c_void,
343    dstSize: size_t,
344    bmi2: core::ffi::c_int,
345    writeEntropy: core::ffi::c_int,
346    entropyWritten: *mut core::ffi::c_int,
347) -> size_t {
348    let header = (if writeEntropy != 0 { 200 } else { 0 }) as size_t;
349    let lhSize = (3
350        + (litSize >= (((1) << 10) as size_t).wrapping_sub(header)) as core::ffi::c_int
351        + (litSize >= ((16 * ((1) << 10)) as size_t).wrapping_sub(header)) as core::ffi::c_int)
352        as size_t;
353    let ostart = dst as *mut u8;
354    let oend = ostart.add(dstSize);
355    let mut op = ostart.add(lhSize);
356    let singleStream = (lhSize == 3) as core::ffi::c_int as u32;
357    let hType = (if writeEntropy != 0 {
358        (*hufMetadata).hType as core::ffi::c_uint
359    } else {
360        set_repeat as core::ffi::c_int as core::ffi::c_uint
361    }) as SymbolEncodingType_e;
362    let mut cLitSize = 0 as size_t;
363    *entropyWritten = 0;
364    if litSize == 0
365        || (*hufMetadata).hType as core::ffi::c_uint
366            == set_basic as core::ffi::c_int as core::ffi::c_uint
367    {
368        return ZSTD_noCompressLiterals(
369            dst,
370            dstSize,
371            literals as *const core::ffi::c_void,
372            litSize,
373        );
374    } else if (*hufMetadata).hType as core::ffi::c_uint
375        == set_rle as core::ffi::c_int as core::ffi::c_uint
376    {
377        return ZSTD_compressRleLiteralsBlock(
378            dst,
379            dstSize,
380            literals as *const core::ffi::c_void,
381            litSize,
382        );
383    }
384    if writeEntropy != 0
385        && (*hufMetadata).hType as core::ffi::c_uint
386            == set_compressed as core::ffi::c_int as core::ffi::c_uint
387    {
388        libc::memcpy(
389            op as *mut core::ffi::c_void,
390            ((*hufMetadata).hufDesBuffer).as_ptr() as *const core::ffi::c_void,
391            (*hufMetadata).hufDesSize as libc::size_t,
392        );
393        op = op.add((*hufMetadata).hufDesSize);
394        cLitSize = cLitSize.wrapping_add((*hufMetadata).hufDesSize);
395    }
396    let flags = if bmi2 != 0 {
397        HUF_flags_bmi2 as core::ffi::c_int
398    } else {
399        0
400    };
401    let cSize = if singleStream != 0 {
402        HUF_compress1X_usingCTable(
403            op as *mut core::ffi::c_void,
404            oend.offset_from(op) as size_t,
405            literals as *const core::ffi::c_void,
406            litSize,
407            hufTable,
408            flags,
409        )
410    } else {
411        HUF_compress4X_usingCTable(
412            op as *mut core::ffi::c_void,
413            oend.offset_from(op) as size_t,
414            literals as *const core::ffi::c_void,
415            litSize,
416            hufTable,
417            flags,
418        )
419    };
420    op = op.add(cSize);
421    cLitSize = cLitSize.wrapping_add(cSize);
422    if cSize == 0 || ERR_isError(cSize) {
423        return 0;
424    }
425    if writeEntropy == 0 && cLitSize >= litSize {
426        return ZSTD_noCompressLiterals(
427            dst,
428            dstSize,
429            literals as *const core::ffi::c_void,
430            litSize,
431        );
432    }
433    if lhSize
434        < (3 + (cLitSize >= ((1) << 10) as size_t) as core::ffi::c_int
435            + (cLitSize >= (16 * ((1) << 10)) as size_t) as core::ffi::c_int) as size_t
436    {
437        return ZSTD_noCompressLiterals(
438            dst,
439            dstSize,
440            literals as *const core::ffi::c_void,
441            litSize,
442        );
443    }
444    match lhSize {
445        3 => {
446            let lhc = (hType as core::ffi::c_uint)
447                .wrapping_add(((singleStream == 0) as core::ffi::c_int as u32) << 2)
448                .wrapping_add((litSize as u32) << 4)
449                .wrapping_add((cLitSize as u32) << 14);
450            MEM_writeLE24(ostart as *mut core::ffi::c_void, lhc);
451        }
452        4 => {
453            let lhc_0 = (hType as core::ffi::c_uint)
454                .wrapping_add(((2) << 2) as core::ffi::c_uint)
455                .wrapping_add((litSize as u32) << 4)
456                .wrapping_add((cLitSize as u32) << 18);
457            MEM_writeLE32(ostart as *mut core::ffi::c_void, lhc_0);
458        }
459        5 => {
460            let lhc_1 = (hType as core::ffi::c_uint)
461                .wrapping_add(((3) << 2) as core::ffi::c_uint)
462                .wrapping_add((litSize as u32) << 4)
463                .wrapping_add((cLitSize as u32) << 22);
464            MEM_writeLE32(ostart as *mut core::ffi::c_void, lhc_1);
465            *ostart.offset(4) = (cLitSize >> 10) as u8;
466        }
467        _ => {}
468    }
469    *entropyWritten = 1;
470    op.offset_from(ostart) as size_t
471}
472unsafe fn ZSTD_seqDecompressedSize(
473    seqStore: *const SeqStore_t,
474    sequences: *const SeqDef,
475    nbSeqs: size_t,
476    litSize: size_t,
477    lastSubBlock: core::ffi::c_int,
478) -> size_t {
479    let mut matchLengthSum = 0 as size_t;
480    let mut litLengthSum = 0 as size_t;
481    let mut n: size_t = 0;
482    n = 0;
483    while n < nbSeqs {
484        let seqLen = ZSTD_getSequenceLength(seqStore, sequences.add(n));
485        litLengthSum = litLengthSum.wrapping_add(seqLen.litLength as size_t);
486        matchLengthSum = matchLengthSum.wrapping_add(seqLen.matchLength as size_t);
487        n = n.wrapping_add(1);
488    }
489    lastSubBlock == 0;
490    matchLengthSum.wrapping_add(litSize)
491}
492unsafe fn ZSTD_compressSubBlock_sequences(
493    fseTables: *const ZSTD_fseCTables_t,
494    fseMetadata: *const ZSTD_fseCTablesMetadata_t,
495    sequences: *const SeqDef,
496    nbSeq: size_t,
497    llCode: *const u8,
498    mlCode: *const u8,
499    ofCode: *const u8,
500    cctxParams: *const ZSTD_CCtx_params,
501    dst: *mut core::ffi::c_void,
502    dstCapacity: size_t,
503    bmi2: core::ffi::c_int,
504    writeEntropy: core::ffi::c_int,
505    entropyWritten: *mut core::ffi::c_int,
506) -> size_t {
507    let longOffsets = ((*cctxParams).cParams.windowLog
508        > (if MEM_32bits() != 0 {
509            STREAM_ACCUMULATOR_MIN_32
510        } else {
511            STREAM_ACCUMULATOR_MIN_64
512        }) as u32) as core::ffi::c_int;
513    let ostart = dst as *mut u8;
514    let oend = ostart.add(dstCapacity);
515    let mut op = ostart;
516    let mut seqHead = core::ptr::null_mut::<u8>();
517    *entropyWritten = 0;
518    if (oend.offset_from(op) as core::ffi::c_long) < (3 + 1) as core::ffi::c_long {
519        return Error::dstSize_tooSmall.to_error_code();
520    }
521    if nbSeq < 128 {
522        let fresh0 = op;
523        op = op.offset(1);
524        *fresh0 = nbSeq as u8;
525    } else if nbSeq < LONGNBSEQ as size_t {
526        *op.offset(0) = (nbSeq >> 8).wrapping_add(0x80 as core::ffi::c_int as size_t) as u8;
527        *op.offset(1) = nbSeq as u8;
528        op = op.offset(2);
529    } else {
530        *op.offset(0) = 0xff as core::ffi::c_int as u8;
531        MEM_writeLE16(
532            op.offset(1) as *mut core::ffi::c_void,
533            nbSeq.wrapping_sub(LONGNBSEQ as size_t) as u16,
534        );
535        op = op.offset(3);
536    }
537    if nbSeq == 0 {
538        return op.offset_from(ostart) as size_t;
539    }
540    let fresh1 = op;
541    op = op.offset(1);
542    seqHead = fresh1;
543    if writeEntropy != 0 {
544        let LLtype = (*fseMetadata).llType;
545        let Offtype = (*fseMetadata).ofType;
546        let MLtype = (*fseMetadata).mlType;
547        *seqHead = (LLtype << 6)
548            .wrapping_add(Offtype << 4)
549            .wrapping_add(MLtype << 2) as u8;
550        libc::memcpy(
551            op as *mut core::ffi::c_void,
552            ((*fseMetadata).fseTablesBuffer).as_ptr() as *const core::ffi::c_void,
553            (*fseMetadata).fseTablesSize as libc::size_t,
554        );
555        op = op.add((*fseMetadata).fseTablesSize);
556    } else {
557        let repeat = set_repeat as core::ffi::c_int as u32;
558        *seqHead = (repeat << 6)
559            .wrapping_add(repeat << 4)
560            .wrapping_add(repeat << 2) as u8;
561    }
562    let bitstreamSize = ZSTD_encodeSequences(
563        op as *mut core::ffi::c_void,
564        oend.offset_from(op) as size_t,
565        ((*fseTables).matchlengthCTable).as_ptr(),
566        mlCode,
567        ((*fseTables).offcodeCTable).as_ptr(),
568        ofCode,
569        ((*fseTables).litlengthCTable).as_ptr(),
570        llCode,
571        sequences,
572        nbSeq,
573        longOffsets,
574        bmi2,
575    );
576    let err_code = bitstreamSize;
577    if ERR_isError(err_code) {
578        return err_code;
579    }
580    op = op.add(bitstreamSize);
581    if writeEntropy != 0
582        && (*fseMetadata).lastCountSize != 0
583        && ((*fseMetadata).lastCountSize).wrapping_add(bitstreamSize) < 4
584    {
585        return 0;
586    }
587    if (op.offset_from(seqHead) as core::ffi::c_long) < 4 {
588        return 0;
589    }
590    *entropyWritten = 1;
591    op.offset_from(ostart) as size_t
592}
593unsafe fn ZSTD_compressSubBlock(
594    entropy: *const ZSTD_entropyCTables_t,
595    entropyMetadata: *const ZSTD_entropyCTablesMetadata_t,
596    sequences: *const SeqDef,
597    nbSeq: size_t,
598    literals: *const u8,
599    litSize: size_t,
600    llCode: *const u8,
601    mlCode: *const u8,
602    ofCode: *const u8,
603    cctxParams: *const ZSTD_CCtx_params,
604    dst: *mut core::ffi::c_void,
605    dstCapacity: size_t,
606    bmi2: core::ffi::c_int,
607    writeLitEntropy: core::ffi::c_int,
608    writeSeqEntropy: core::ffi::c_int,
609    litEntropyWritten: *mut core::ffi::c_int,
610    seqEntropyWritten: *mut core::ffi::c_int,
611    lastBlock: u32,
612) -> size_t {
613    let ostart = dst as *mut u8;
614    let oend = ostart.add(dstCapacity);
615    let mut op = ostart.add(ZSTD_blockHeaderSize);
616    let cLitSize = ZSTD_compressSubBlock_literal(
617        ((*entropy).huf.CTable).as_ptr(),
618        &(*entropyMetadata).hufMetadata,
619        literals,
620        litSize,
621        op as *mut core::ffi::c_void,
622        oend.offset_from(op) as size_t,
623        bmi2,
624        writeLitEntropy,
625        litEntropyWritten,
626    );
627    let err_code = cLitSize;
628    if ERR_isError(err_code) {
629        return err_code;
630    }
631    if cLitSize == 0 {
632        return 0;
633    }
634    op = op.add(cLitSize);
635    let cSeqSize = ZSTD_compressSubBlock_sequences(
636        &(*entropy).fse,
637        &(*entropyMetadata).fseMetadata,
638        sequences,
639        nbSeq,
640        llCode,
641        mlCode,
642        ofCode,
643        cctxParams,
644        op as *mut core::ffi::c_void,
645        oend.offset_from(op) as size_t,
646        bmi2,
647        writeSeqEntropy,
648        seqEntropyWritten,
649    );
650    let err_code_0 = cSeqSize;
651    if ERR_isError(err_code_0) {
652        return err_code_0;
653    }
654    if cSeqSize == 0 {
655        return 0;
656    }
657    op = op.add(cSeqSize);
658    let cSize = (op.offset_from(ostart) as size_t).wrapping_sub(ZSTD_blockHeaderSize);
659    let cBlockHeader24 = lastBlock
660        .wrapping_add((bt_compressed as core::ffi::c_int as u32) << 1)
661        .wrapping_add((cSize << 3) as u32);
662    MEM_writeLE24(ostart as *mut core::ffi::c_void, cBlockHeader24);
663    op.offset_from(ostart) as size_t
664}
665unsafe fn ZSTD_estimateSubBlockSize_literal(
666    literals: *const u8,
667    litSize: size_t,
668    huf: *const ZSTD_hufCTables_t,
669    hufMetadata: *const ZSTD_hufCTablesMetadata_t,
670    workspace: *mut core::ffi::c_void,
671    wkspSize: size_t,
672    writeEntropy: core::ffi::c_int,
673) -> size_t {
674    let countWksp = workspace as *mut core::ffi::c_uint;
675    let mut maxSymbolValue = 255;
676    let literalSectionHeaderSize = 3;
677    if (*hufMetadata).hType as core::ffi::c_uint
678        == set_basic as core::ffi::c_int as core::ffi::c_uint
679    {
680        return litSize;
681    } else if (*hufMetadata).hType as core::ffi::c_uint
682        == set_rle as core::ffi::c_int as core::ffi::c_uint
683    {
684        return 1;
685    } else if (*hufMetadata).hType as core::ffi::c_uint
686        == set_compressed as core::ffi::c_int as core::ffi::c_uint
687        || (*hufMetadata).hType as core::ffi::c_uint
688            == set_repeat as core::ffi::c_int as core::ffi::c_uint
689    {
690        let largest = HIST_count_wksp(
691            countWksp,
692            &mut maxSymbolValue,
693            literals as *const core::ffi::c_void,
694            litSize,
695            workspace,
696            wkspSize,
697        );
698        if ERR_isError(largest) {
699            return litSize;
700        }
701        let mut cLitSizeEstimate =
702            HUF_estimateCompressedSize(((*huf).CTable).as_ptr(), countWksp, maxSymbolValue);
703        if writeEntropy != 0 {
704            cLitSizeEstimate = cLitSizeEstimate.wrapping_add((*hufMetadata).hufDesSize);
705        }
706        return cLitSizeEstimate.wrapping_add(literalSectionHeaderSize);
707    }
708    0
709}
710unsafe fn ZSTD_estimateSubBlockSize_symbolType(
711    type_0: SymbolEncodingType_e,
712    codeTable: *const u8,
713    maxCode: core::ffi::c_uint,
714    nbSeq: size_t,
715    fseCTable: *const FSE_CTable,
716    additionalBits: *const u8,
717    defaultNorm: *const core::ffi::c_short,
718    defaultNormLog: u32,
719    defaultMax: u32,
720    workspace: *mut core::ffi::c_void,
721    wkspSize: size_t,
722) -> size_t {
723    let countWksp = workspace as *mut core::ffi::c_uint;
724    let mut ctp = codeTable;
725    let ctStart = ctp;
726    let ctEnd = ctStart.add(nbSeq);
727    let mut cSymbolTypeSizeEstimateInBits = 0;
728    let mut max = maxCode;
729    HIST_countFast_wksp(
730        countWksp,
731        &mut max,
732        codeTable as *const core::ffi::c_void,
733        nbSeq,
734        workspace,
735        wkspSize,
736    );
737    if type_0 as core::ffi::c_uint == set_basic as core::ffi::c_int as core::ffi::c_uint {
738        cSymbolTypeSizeEstimateInBits = if max <= defaultMax {
739            ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, countWksp, max)
740        } else {
741            Error::GENERIC.to_error_code()
742        };
743    } else if type_0 as core::ffi::c_uint == set_rle as core::ffi::c_int as core::ffi::c_uint {
744        cSymbolTypeSizeEstimateInBits = 0;
745    } else if type_0 as core::ffi::c_uint == set_compressed as core::ffi::c_int as core::ffi::c_uint
746        || type_0 as core::ffi::c_uint == set_repeat as core::ffi::c_int as core::ffi::c_uint
747    {
748        cSymbolTypeSizeEstimateInBits = ZSTD_fseBitCost(fseCTable, countWksp, max);
749    }
750    if ERR_isError(cSymbolTypeSizeEstimateInBits) {
751        return nbSeq * 10;
752    }
753    while ctp < ctEnd {
754        if !additionalBits.is_null() {
755            cSymbolTypeSizeEstimateInBits = cSymbolTypeSizeEstimateInBits
756                .wrapping_add(*additionalBits.offset(*ctp as isize) as size_t);
757        } else {
758            cSymbolTypeSizeEstimateInBits =
759                cSymbolTypeSizeEstimateInBits.wrapping_add(*ctp as size_t);
760        }
761        ctp = ctp.offset(1);
762    }
763    cSymbolTypeSizeEstimateInBits / 8
764}
765unsafe fn ZSTD_estimateSubBlockSize_sequences(
766    ofCodeTable: *const u8,
767    llCodeTable: *const u8,
768    mlCodeTable: *const u8,
769    nbSeq: size_t,
770    fseTables: *const ZSTD_fseCTables_t,
771    fseMetadata: *const ZSTD_fseCTablesMetadata_t,
772    workspace: *mut core::ffi::c_void,
773    wkspSize: size_t,
774    writeEntropy: core::ffi::c_int,
775) -> size_t {
776    let sequencesSectionHeaderSize = 3;
777    let mut cSeqSizeEstimate = 0 as size_t;
778    if nbSeq == 0 {
779        return sequencesSectionHeaderSize;
780    }
781    cSeqSizeEstimate = cSeqSizeEstimate.wrapping_add(ZSTD_estimateSubBlockSize_symbolType(
782        (*fseMetadata).ofType,
783        ofCodeTable,
784        MaxOff as core::ffi::c_uint,
785        nbSeq,
786        ((*fseTables).offcodeCTable).as_ptr(),
787        core::ptr::null(),
788        OF_defaultNorm.as_ptr(),
789        OF_defaultNormLog,
790        DefaultMaxOff as u32,
791        workspace,
792        wkspSize,
793    ));
794    cSeqSizeEstimate = cSeqSizeEstimate.wrapping_add(ZSTD_estimateSubBlockSize_symbolType(
795        (*fseMetadata).llType,
796        llCodeTable,
797        MaxLL as core::ffi::c_uint,
798        nbSeq,
799        ((*fseTables).litlengthCTable).as_ptr(),
800        LL_bits.as_ptr(),
801        LL_defaultNorm.as_ptr(),
802        LL_defaultNormLog,
803        MaxLL as u32,
804        workspace,
805        wkspSize,
806    ));
807    cSeqSizeEstimate = cSeqSizeEstimate.wrapping_add(ZSTD_estimateSubBlockSize_symbolType(
808        (*fseMetadata).mlType,
809        mlCodeTable,
810        MaxML as core::ffi::c_uint,
811        nbSeq,
812        ((*fseTables).matchlengthCTable).as_ptr(),
813        ML_bits.as_ptr(),
814        ML_defaultNorm.as_ptr(),
815        ML_defaultNormLog,
816        MaxML as u32,
817        workspace,
818        wkspSize,
819    ));
820    if writeEntropy != 0 {
821        cSeqSizeEstimate = cSeqSizeEstimate.wrapping_add((*fseMetadata).fseTablesSize);
822    }
823    cSeqSizeEstimate.wrapping_add(sequencesSectionHeaderSize)
824}
825unsafe fn ZSTD_estimateSubBlockSize(
826    literals: *const u8,
827    litSize: size_t,
828    ofCodeTable: *const u8,
829    llCodeTable: *const u8,
830    mlCodeTable: *const u8,
831    nbSeq: size_t,
832    entropy: *const ZSTD_entropyCTables_t,
833    entropyMetadata: *const ZSTD_entropyCTablesMetadata_t,
834    workspace: *mut core::ffi::c_void,
835    wkspSize: size_t,
836    writeLitEntropy: core::ffi::c_int,
837    writeSeqEntropy: core::ffi::c_int,
838) -> EstimatedBlockSize {
839    let mut ebs = EstimatedBlockSize {
840        estLitSize: 0,
841        estBlockSize: 0,
842    };
843    ebs.estLitSize = ZSTD_estimateSubBlockSize_literal(
844        literals,
845        litSize,
846        &(*entropy).huf,
847        &(*entropyMetadata).hufMetadata,
848        workspace,
849        wkspSize,
850        writeLitEntropy,
851    );
852    ebs.estBlockSize = ZSTD_estimateSubBlockSize_sequences(
853        ofCodeTable,
854        llCodeTable,
855        mlCodeTable,
856        nbSeq,
857        &(*entropy).fse,
858        &(*entropyMetadata).fseMetadata,
859        workspace,
860        wkspSize,
861        writeSeqEntropy,
862    );
863    ebs.estBlockSize =
864        (ebs.estBlockSize).wrapping_add((ebs.estLitSize).wrapping_add(ZSTD_blockHeaderSize));
865    ebs
866}
867unsafe fn ZSTD_needSequenceEntropyTables(
868    fseMetadata: *const ZSTD_fseCTablesMetadata_t,
869) -> core::ffi::c_int {
870    if (*fseMetadata).llType as core::ffi::c_uint
871        == set_compressed as core::ffi::c_int as core::ffi::c_uint
872        || (*fseMetadata).llType as core::ffi::c_uint
873            == set_rle as core::ffi::c_int as core::ffi::c_uint
874    {
875        return 1;
876    }
877    if (*fseMetadata).mlType as core::ffi::c_uint
878        == set_compressed as core::ffi::c_int as core::ffi::c_uint
879        || (*fseMetadata).mlType as core::ffi::c_uint
880            == set_rle as core::ffi::c_int as core::ffi::c_uint
881    {
882        return 1;
883    }
884    if (*fseMetadata).ofType as core::ffi::c_uint
885        == set_compressed as core::ffi::c_int as core::ffi::c_uint
886        || (*fseMetadata).ofType as core::ffi::c_uint
887            == set_rle as core::ffi::c_int as core::ffi::c_uint
888    {
889        return 1;
890    }
891    0
892}
893unsafe fn countLiterals(
894    seqStore: *const SeqStore_t,
895    sp: *const SeqDef,
896    seqCount: size_t,
897) -> size_t {
898    let mut n: size_t = 0;
899    let mut total = 0 as size_t;
900    n = 0;
901    while n < seqCount {
902        total =
903            total.wrapping_add((ZSTD_getSequenceLength(seqStore, sp.add(n))).litLength as size_t);
904        n = n.wrapping_add(1);
905    }
906    total
907}
908pub const BYTESCALE: core::ffi::c_int = 256;
909unsafe fn sizeBlockSequences(
910    sp: *const SeqDef,
911    nbSeqs: size_t,
912    targetBudget: size_t,
913    avgLitCost: size_t,
914    avgSeqCost: size_t,
915    firstSubBlock: core::ffi::c_int,
916) -> size_t {
917    let mut n: size_t = 0;
918    let mut budget = 0 as size_t;
919    let mut inSize = 0;
920    let headerSize = firstSubBlock as size_t * 120 * BYTESCALE as size_t;
921    budget = budget.wrapping_add(headerSize);
922    budget = budget
923        .wrapping_add(((*sp.offset(0)).litLength as size_t * avgLitCost).wrapping_add(avgSeqCost));
924    if budget > targetBudget {
925        return 1;
926    }
927    inSize = ((*sp.offset(0)).litLength as core::ffi::c_int
928        + ((*sp.offset(0)).mlBase as core::ffi::c_int + MINMATCH)) as size_t;
929    n = 1;
930    while n < nbSeqs {
931        let currentCost = ((*sp.add(n)).litLength as size_t * avgLitCost).wrapping_add(avgSeqCost);
932        budget = budget.wrapping_add(currentCost);
933        inSize = inSize.wrapping_add(
934            ((*sp.add(n)).litLength as core::ffi::c_int
935                + ((*sp.add(n)).mlBase as core::ffi::c_int + MINMATCH)) as size_t,
936        );
937        if budget > targetBudget && budget < inSize * BYTESCALE as size_t {
938            break;
939        }
940        n = n.wrapping_add(1);
941    }
942    n
943}
944unsafe fn ZSTD_compressSubBlock_multi(
945    seqStorePtr: *const SeqStore_t,
946    prevCBlock: *const ZSTD_compressedBlockState_t,
947    nextCBlock: *mut ZSTD_compressedBlockState_t,
948    entropyMetadata: *const ZSTD_entropyCTablesMetadata_t,
949    cctxParams: *const ZSTD_CCtx_params,
950    dst: *mut core::ffi::c_void,
951    dstCapacity: size_t,
952    src: *const core::ffi::c_void,
953    srcSize: size_t,
954    bmi2: core::ffi::c_int,
955    lastBlock: u32,
956    workspace: *mut core::ffi::c_void,
957    wkspSize: size_t,
958) -> size_t {
959    let sstart: *const SeqDef = (*seqStorePtr).sequencesStart;
960    let send: *const SeqDef = (*seqStorePtr).sequences;
961    let mut sp = sstart;
962    let nbSeqs = send.offset_from(sstart) as size_t;
963    let lstart: *const u8 = (*seqStorePtr).litStart;
964    let lend: *const u8 = (*seqStorePtr).lit;
965    let mut lp = lstart;
966    let nbLiterals = lend.offset_from(lstart) as size_t;
967    let mut ip = src as *const u8;
968    let iend = ip.add(srcSize);
969    let ostart = dst as *mut u8;
970    let oend = ostart.add(dstCapacity);
971    let mut op = ostart;
972    let mut llCodePtr: *const u8 = (*seqStorePtr).llCode;
973    let mut mlCodePtr: *const u8 = (*seqStorePtr).mlCode;
974    let mut ofCodePtr: *const u8 = (*seqStorePtr).ofCode;
975    let minTarget = ZSTD_TARGETCBLOCKSIZE_MIN as size_t;
976    let targetCBlockSize = if minTarget > (*cctxParams).targetCBlockSize {
977        minTarget
978    } else {
979        (*cctxParams).targetCBlockSize
980    };
981    let mut writeLitEntropy = ((*entropyMetadata).hufMetadata.hType as core::ffi::c_uint
982        == set_compressed as core::ffi::c_int as core::ffi::c_uint)
983        as core::ffi::c_int;
984    let mut writeSeqEntropy = 1;
985    if nbSeqs > 0 {
986        let ebs = ZSTD_estimateSubBlockSize(
987            lp,
988            nbLiterals,
989            ofCodePtr,
990            llCodePtr,
991            mlCodePtr,
992            nbSeqs,
993            &(*nextCBlock).entropy,
994            entropyMetadata,
995            workspace,
996            wkspSize,
997            writeLitEntropy,
998            writeSeqEntropy,
999        );
1000        let avgLitCost = if nbLiterals != 0 {
1001            ebs.estLitSize * BYTESCALE as size_t / nbLiterals
1002        } else {
1003            BYTESCALE as size_t
1004        };
1005        let avgSeqCost =
1006            (ebs.estBlockSize).wrapping_sub(ebs.estLitSize) * BYTESCALE as size_t / nbSeqs;
1007        let nbSubBlocks =
1008            if (ebs.estBlockSize).wrapping_add(targetCBlockSize / 2) / targetCBlockSize > 1 {
1009                (ebs.estBlockSize).wrapping_add(targetCBlockSize / 2) / targetCBlockSize
1010            } else {
1011                1
1012            };
1013        let mut n: size_t = 0;
1014        let mut avgBlockBudget: size_t = 0;
1015        let mut blockBudgetSupp = 0;
1016        avgBlockBudget = ebs.estBlockSize * BYTESCALE as size_t / nbSubBlocks;
1017        if ebs.estBlockSize > srcSize {
1018            return 0;
1019        }
1020        n = 0;
1021        while n < nbSubBlocks.wrapping_sub(1) {
1022            let seqCount = sizeBlockSequences(
1023                sp,
1024                send.offset_from(sp) as size_t,
1025                avgBlockBudget.wrapping_add(blockBudgetSupp),
1026                avgLitCost,
1027                avgSeqCost,
1028                (n == 0) as core::ffi::c_int,
1029            );
1030            if sp.add(seqCount) == send {
1031                break;
1032            }
1033            let mut litEntropyWritten = 0;
1034            let mut seqEntropyWritten = 0;
1035            let litSize = countLiterals(seqStorePtr, sp, seqCount);
1036            let decompressedSize = ZSTD_seqDecompressedSize(seqStorePtr, sp, seqCount, litSize, 0);
1037            let cSize = ZSTD_compressSubBlock(
1038                &(*nextCBlock).entropy,
1039                entropyMetadata,
1040                sp,
1041                seqCount,
1042                lp,
1043                litSize,
1044                llCodePtr,
1045                mlCodePtr,
1046                ofCodePtr,
1047                cctxParams,
1048                op as *mut core::ffi::c_void,
1049                oend.offset_from(op) as size_t,
1050                bmi2,
1051                writeLitEntropy,
1052                writeSeqEntropy,
1053                &mut litEntropyWritten,
1054                &mut seqEntropyWritten,
1055                0,
1056            );
1057            let err_code = cSize;
1058            if ERR_isError(err_code) {
1059                return err_code;
1060            }
1061            if cSize > 0 && cSize < decompressedSize {
1062                ip = ip.add(decompressedSize);
1063                lp = lp.add(litSize);
1064                op = op.add(cSize);
1065                llCodePtr = llCodePtr.add(seqCount);
1066                mlCodePtr = mlCodePtr.add(seqCount);
1067                ofCodePtr = ofCodePtr.add(seqCount);
1068                if litEntropyWritten != 0 {
1069                    writeLitEntropy = 0;
1070                }
1071                if seqEntropyWritten != 0 {
1072                    writeSeqEntropy = 0;
1073                }
1074                sp = sp.add(seqCount);
1075                blockBudgetSupp = 0;
1076            }
1077            n = n.wrapping_add(1);
1078        }
1079    }
1080    let mut litEntropyWritten_0 = 0;
1081    let mut seqEntropyWritten_0 = 0;
1082    let litSize_0 = lend.offset_from(lp) as size_t;
1083    let seqCount_0 = send.offset_from(sp) as size_t;
1084    let decompressedSize_0 = ZSTD_seqDecompressedSize(seqStorePtr, sp, seqCount_0, litSize_0, 1);
1085    let cSize_0 = ZSTD_compressSubBlock(
1086        &(*nextCBlock).entropy,
1087        entropyMetadata,
1088        sp,
1089        seqCount_0,
1090        lp,
1091        litSize_0,
1092        llCodePtr,
1093        mlCodePtr,
1094        ofCodePtr,
1095        cctxParams,
1096        op as *mut core::ffi::c_void,
1097        oend.offset_from(op) as size_t,
1098        bmi2,
1099        writeLitEntropy,
1100        writeSeqEntropy,
1101        &mut litEntropyWritten_0,
1102        &mut seqEntropyWritten_0,
1103        lastBlock,
1104    );
1105    let err_code_0 = cSize_0;
1106    if ERR_isError(err_code_0) {
1107        return err_code_0;
1108    }
1109    if cSize_0 > 0 && cSize_0 < decompressedSize_0 {
1110        ip = ip.add(decompressedSize_0);
1111        lp = lp.add(litSize_0);
1112        op = op.add(cSize_0);
1113        llCodePtr = llCodePtr.add(seqCount_0);
1114        mlCodePtr = mlCodePtr.add(seqCount_0);
1115        ofCodePtr = ofCodePtr.add(seqCount_0);
1116        if litEntropyWritten_0 != 0 {
1117            writeLitEntropy = 0;
1118        }
1119        if seqEntropyWritten_0 != 0 {
1120            writeSeqEntropy = 0;
1121        }
1122        sp = sp.add(seqCount_0);
1123    }
1124    if writeLitEntropy != 0 {
1125        libc::memcpy(
1126            &mut (*nextCBlock).entropy.huf as *mut ZSTD_hufCTables_t as *mut core::ffi::c_void,
1127            &(*prevCBlock).entropy.huf as *const ZSTD_hufCTables_t as *const core::ffi::c_void,
1128            ::core::mem::size_of::<ZSTD_hufCTables_t>() as core::ffi::c_ulong as libc::size_t,
1129        );
1130    }
1131    if writeSeqEntropy != 0 && ZSTD_needSequenceEntropyTables(&(*entropyMetadata).fseMetadata) != 0
1132    {
1133        return 0;
1134    }
1135    if ip < iend {
1136        let rSize = iend.offset_from(ip) as size_t;
1137        let cSize_1 = ZSTD_noCompressBlock(
1138            op as *mut core::ffi::c_void,
1139            oend.offset_from(op) as size_t,
1140            ip as *const core::ffi::c_void,
1141            rSize,
1142            lastBlock,
1143        );
1144        let err_code_1 = cSize_1;
1145        if ERR_isError(err_code_1) {
1146            return err_code_1;
1147        }
1148        op = op.add(cSize_1);
1149        if sp < send {
1150            let mut seq = core::ptr::null::<SeqDef>();
1151            let mut rep = repcodes_s { rep: [0; 3] };
1152            libc::memcpy(
1153                &mut rep as *mut Repcodes_t as *mut core::ffi::c_void,
1154                ((*prevCBlock).rep).as_ptr() as *const core::ffi::c_void,
1155                ::core::mem::size_of::<Repcodes_t>() as core::ffi::c_ulong as libc::size_t,
1156            );
1157            seq = sstart;
1158            while seq < sp {
1159                ZSTD_updateRep(
1160                    (rep.rep).as_mut_ptr(),
1161                    (*seq).offBase,
1162                    ((ZSTD_getSequenceLength(seqStorePtr, seq)).litLength == 0) as core::ffi::c_int
1163                        as u32,
1164                );
1165                seq = seq.offset(1);
1166            }
1167            libc::memcpy(
1168                ((*nextCBlock).rep).as_mut_ptr() as *mut core::ffi::c_void,
1169                &mut rep as *mut Repcodes_t as *const core::ffi::c_void,
1170                ::core::mem::size_of::<Repcodes_t>() as core::ffi::c_ulong as libc::size_t,
1171            );
1172        }
1173    }
1174    op.offset_from(ostart) as size_t
1175}
1176pub unsafe fn ZSTD_compressSuperBlock(
1177    zc: *mut ZSTD_CCtx,
1178    dst: *mut core::ffi::c_void,
1179    dstCapacity: size_t,
1180    src: *const core::ffi::c_void,
1181    srcSize: size_t,
1182    lastBlock: core::ffi::c_uint,
1183) -> size_t {
1184    let mut entropyMetadata = ZSTD_entropyCTablesMetadata_t {
1185        hufMetadata: ZSTD_hufCTablesMetadata_t {
1186            hType: set_basic,
1187            hufDesBuffer: [0; 128],
1188            hufDesSize: 0,
1189        },
1190        fseMetadata: ZSTD_fseCTablesMetadata_t {
1191            llType: set_basic,
1192            ofType: set_basic,
1193            mlType: set_basic,
1194            fseTablesBuffer: [0; 133],
1195            fseTablesSize: 0,
1196            lastCountSize: 0,
1197        },
1198    };
1199    let err_code = ZSTD_buildBlockEntropyStats(
1200        &(*zc).seqStore,
1201        &(*(*zc).blockState.prevCBlock).entropy,
1202        &mut (*(*zc).blockState.nextCBlock).entropy,
1203        &(*zc).appliedParams,
1204        &mut entropyMetadata,
1205        (*zc).tmpWorkspace,
1206        (*zc).tmpWkspSize,
1207    );
1208    if ERR_isError(err_code) {
1209        return err_code;
1210    }
1211    ZSTD_compressSubBlock_multi(
1212        &(*zc).seqStore,
1213        (*zc).blockState.prevCBlock,
1214        (*zc).blockState.nextCBlock,
1215        &entropyMetadata,
1216        &(*zc).appliedParams,
1217        dst,
1218        dstCapacity,
1219        src,
1220        srcSize,
1221        (*zc).bmi2,
1222        lastBlock,
1223        (*zc).tmpWorkspace,
1224        (*zc).tmpWkspSize,
1225    )
1226}