Skip to main content

libzstd_rs_sys/lib/compress/
zstd_opt.rs

1use core::ptr;
2pub type ZSTD_longLengthType_e = core::ffi::c_uint;
3pub const ZSTD_llt_matchLength: ZSTD_longLengthType_e = 2;
4pub const ZSTD_llt_literalLength: ZSTD_longLengthType_e = 1;
5pub const ZSTD_llt_none: ZSTD_longLengthType_e = 0;
6pub type ZSTD_ParamSwitch_e = core::ffi::c_uint;
7pub const ZSTD_ps_disable: ZSTD_ParamSwitch_e = 2;
8pub const ZSTD_ps_enable: ZSTD_ParamSwitch_e = 1;
9pub const ZSTD_ps_auto: ZSTD_ParamSwitch_e = 0;
10#[repr(C)]
11pub struct ZSTD_entropyCTables_t {
12    pub huf: ZSTD_hufCTables_t,
13    pub fse: ZSTD_fseCTables_t,
14}
15#[repr(C)]
16pub struct ZSTD_fseCTables_t {
17    pub offcodeCTable: [FSE_CTable; 193],
18    pub matchlengthCTable: [FSE_CTable; 363],
19    pub litlengthCTable: [FSE_CTable; 329],
20    pub offcode_repeatMode: FSE_repeat,
21    pub matchlength_repeatMode: FSE_repeat,
22    pub litlength_repeatMode: FSE_repeat,
23}
24#[repr(C)]
25pub struct ZSTD_hufCTables_t {
26    pub CTable: [HUF_CElt; 257],
27    pub repeatMode: HUF_repeat,
28}
29pub type ZSTD_OptPrice_e = core::ffi::c_uint;
30pub const zop_predef: ZSTD_OptPrice_e = 1;
31pub const zop_dynamic: ZSTD_OptPrice_e = 0;
32#[repr(C)]
33pub struct ZSTD_window_t {
34    pub nextSrc: *const u8,
35    pub base: *const u8,
36    pub dictBase: *const u8,
37    pub dictLimit: u32,
38    pub lowLimit: u32,
39    pub nbOverflowCorrections: u32,
40}
41pub type ZSTD_dictMode_e = core::ffi::c_uint;
42pub const ZSTD_dedicatedDictSearch: ZSTD_dictMode_e = 3;
43pub const ZSTD_dictMatchState: ZSTD_dictMode_e = 2;
44pub const ZSTD_extDict: ZSTD_dictMode_e = 1;
45pub const ZSTD_noDict: ZSTD_dictMode_e = 0;
46#[repr(C)]
47pub struct repcodes_s {
48    pub rep: [u32; 3],
49}
50pub type Repcodes_t = repcodes_s;
51pub type ZSTD_getAllMatchesFn = Option<
52    unsafe fn(
53        *mut ZSTD_match_t,
54        *mut ZSTD_MatchState_t,
55        *mut u32,
56        *const u8,
57        *const u8,
58        *const u32,
59        u32,
60        u32,
61    ) -> u32,
62>;
63#[repr(C)]
64pub struct ZSTD_optLdm_t {
65    pub seqStore: RawSeqStore_t,
66    pub startPosInBlock: u32,
67    pub endPosInBlock: u32,
68    pub offset: u32,
69}
70pub type base_directive_e = core::ffi::c_uint;
71pub const base_1guaranteed: base_directive_e = 1;
72pub const base_0possible: base_directive_e = 0;
73
74use libc::size_t;
75
76use crate::lib::common::fse::{
77    FSE_CState_t, FSE_CTable, FSE_getMaxNbBits, FSE_initCState, FSE_repeat,
78};
79use crate::lib::common::huf::{HUF_CElt, HUF_repeat, HUF_repeat_valid};
80use crate::lib::common::mem::{
81    MEM_64bits, MEM_isLittleEndian, MEM_read16, MEM_read32, MEM_readLE32, MEM_readLE64, MEM_readST,
82};
83use crate::lib::common::zstd_internal::{
84    LL_bits, ML_bits, MaxLL, MaxLit, MaxML, MaxOff, Overlap, ZSTD_copy16, ZSTD_wildcopy, MINMATCH,
85    WILDCOPY_OVERLENGTH, ZSTD_OPT_NUM, ZSTD_REP_NUM,
86};
87use crate::lib::compress::hist::HIST_count_simple;
88use crate::lib::compress::huf_compress::HUF_getNbBitsFromCTable;
89use crate::lib::compress::zstd_compress::{
90    optState_t, rawSeq, RawSeqStore_t, SeqStore_t, ZSTD_MatchState_t, ZSTD_match_t, ZSTD_optimal_t,
91    ZSTD_resetSeqStore,
92};
93use crate::lib::zstd::*;
94pub const ZSTD_BLOCKSIZELOG_MAX: core::ffi::c_int = 17;
95pub const ZSTD_BLOCKSIZE_MAX: core::ffi::c_int = (1) << ZSTD_BLOCKSIZELOG_MAX;
96static mut kNullRawSeqStore: RawSeqStore_t = RawSeqStore_t {
97    seq: core::ptr::null_mut(),
98    pos: 0,
99    posInSequence: 0,
100    size: 0,
101    capacity: 0,
102};
103#[inline]
104unsafe fn ZSTD_LLcode(litLength: u32) -> u32 {
105    static LL_Code: [u8; 64] = [
106        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20,
107        20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
108        24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
109    ];
110    static LL_deltaCode: u32 = 19;
111    if litLength > 63 {
112        (ZSTD_highbit32(litLength)).wrapping_add(LL_deltaCode)
113    } else {
114        *LL_Code.as_ptr().offset(litLength as isize) as core::ffi::c_uint
115    }
116}
117#[inline]
118unsafe fn ZSTD_MLcode(mlBase: u32) -> u32 {
119    static ML_Code: [u8; 128] = [
120        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
121        25, 26, 27, 28, 29, 30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
122        38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40,
123        40, 40, 40, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
124        41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
125        42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
126    ];
127    static ML_deltaCode: u32 = 36;
128    if mlBase > 127 {
129        (ZSTD_highbit32(mlBase)).wrapping_add(ML_deltaCode)
130    } else {
131        *ML_Code.as_ptr().offset(mlBase as isize) as core::ffi::c_uint
132    }
133}
134unsafe fn ZSTD_safecopyLiterals(
135    mut op: *mut u8,
136    mut ip: *const u8,
137    iend: *const u8,
138    ilimit_w: *const u8,
139) {
140    if ip <= ilimit_w {
141        ZSTD_wildcopy(
142            op as *mut core::ffi::c_void,
143            ip as *const core::ffi::c_void,
144            ilimit_w.offset_from(ip) as size_t,
145            Overlap::NoOverlap,
146        );
147        op = op.offset(ilimit_w.offset_from(ip) as core::ffi::c_long as isize);
148        ip = ilimit_w;
149    }
150    while ip < iend {
151        let fresh0 = ip;
152        ip = ip.offset(1);
153        let fresh1 = op;
154        op = op.offset(1);
155        *fresh1 = *fresh0;
156    }
157}
158#[inline(always)]
159unsafe fn ZSTD_storeSeqOnly(
160    seqStorePtr: *mut SeqStore_t,
161    litLength: size_t,
162    offBase: u32,
163    matchLength: size_t,
164) {
165    if (litLength > 0xffff as core::ffi::c_int as size_t) as core::ffi::c_int as core::ffi::c_long
166        != 0
167    {
168        (*seqStorePtr).longLengthType = ZSTD_llt_literalLength;
169        (*seqStorePtr).longLengthPos = ((*seqStorePtr).sequences)
170            .offset_from((*seqStorePtr).sequencesStart)
171            as core::ffi::c_long as u32;
172    }
173    (*((*seqStorePtr).sequences).offset(0)).litLength = litLength as u16;
174    (*((*seqStorePtr).sequences).offset(0)).offBase = offBase;
175    let mlBase = matchLength.wrapping_sub(MINMATCH as size_t);
176    if (mlBase > 0xffff as core::ffi::c_int as size_t) as core::ffi::c_int as core::ffi::c_long != 0
177    {
178        (*seqStorePtr).longLengthType = ZSTD_llt_matchLength;
179        (*seqStorePtr).longLengthPos = ((*seqStorePtr).sequences)
180            .offset_from((*seqStorePtr).sequencesStart)
181            as core::ffi::c_long as u32;
182    }
183    (*((*seqStorePtr).sequences).offset(0)).mlBase = mlBase as u16;
184    (*seqStorePtr).sequences = ((*seqStorePtr).sequences).offset(1);
185    (*seqStorePtr).sequences;
186}
187#[inline(always)]
188unsafe fn ZSTD_storeSeq(
189    seqStorePtr: *mut SeqStore_t,
190    litLength: size_t,
191    literals: *const u8,
192    litLimit: *const u8,
193    offBase: u32,
194    matchLength: size_t,
195) {
196    let litLimit_w = litLimit.sub(WILDCOPY_OVERLENGTH);
197    let litEnd = literals.add(litLength);
198    if litEnd <= litLimit_w {
199        ZSTD_copy16(
200            (*seqStorePtr).lit as *mut core::ffi::c_void,
201            literals as *const core::ffi::c_void,
202        );
203        if litLength > 16 {
204            ZSTD_wildcopy(
205                ((*seqStorePtr).lit).offset(16) as *mut core::ffi::c_void,
206                literals.offset(16) as *const core::ffi::c_void,
207                litLength.wrapping_sub(16),
208                Overlap::NoOverlap,
209            );
210        }
211    } else {
212        ZSTD_safecopyLiterals((*seqStorePtr).lit, literals, litEnd, litLimit_w);
213    }
214    (*seqStorePtr).lit = ((*seqStorePtr).lit).add(litLength);
215    ZSTD_storeSeqOnly(seqStorePtr, litLength, offBase, matchLength);
216}
217#[inline]
218unsafe fn ZSTD_updateRep(rep: *mut u32, offBase: u32, ll0: u32) {
219    if offBase > ZSTD_REP_NUM as u32 {
220        *rep.offset(2) = *rep.offset(1);
221        *rep.offset(1) = *rep.offset(0);
222        *rep.offset(0) = offBase.wrapping_sub(ZSTD_REP_NUM as u32);
223    } else {
224        let repCode = offBase.wrapping_sub(1).wrapping_add(ll0);
225        if repCode > 0 {
226            let currentOffset = if repCode == ZSTD_REP_NUM as u32 {
227                (*rep.offset(0)).wrapping_sub(1)
228            } else {
229                *rep.offset(repCode as isize)
230            };
231            *rep.offset(2) = if repCode >= 2 {
232                *rep.offset(1)
233            } else {
234                *rep.offset(2)
235            };
236            *rep.offset(1) = *rep.offset(0);
237            *rep.offset(0) = currentOffset;
238        }
239    };
240}
241#[inline]
242unsafe fn ZSTD_newRep(rep: *const u32, offBase: u32, ll0: u32) -> Repcodes_t {
243    let mut newReps = repcodes_s { rep: [0; 3] };
244    libc::memcpy(
245        &mut newReps as *mut Repcodes_t as *mut core::ffi::c_void,
246        rep as *const core::ffi::c_void,
247        ::core::mem::size_of::<Repcodes_t>() as core::ffi::c_ulong as libc::size_t,
248    );
249    ZSTD_updateRep((newReps.rep).as_mut_ptr(), offBase, ll0);
250    newReps
251}
252#[inline]
253unsafe fn ZSTD_count(mut pIn: *const u8, mut pMatch: *const u8, pInLimit: *const u8) -> size_t {
254    let pStart = pIn;
255    let pInLoopLimit = pInLimit.offset(
256        -((::core::mem::size_of::<size_t>() as core::ffi::c_ulong).wrapping_sub(1) as isize),
257    );
258    if pIn < pInLoopLimit {
259        let diff = MEM_readST(pMatch as *const core::ffi::c_void)
260            ^ MEM_readST(pIn as *const core::ffi::c_void);
261        if diff != 0 {
262            return ZSTD_NbCommonBytes(diff) as size_t;
263        }
264        pIn = pIn.offset(::core::mem::size_of::<size_t>() as core::ffi::c_ulong as isize);
265        pMatch = pMatch.offset(::core::mem::size_of::<size_t>() as core::ffi::c_ulong as isize);
266        while pIn < pInLoopLimit {
267            let diff_0 = MEM_readST(pMatch as *const core::ffi::c_void)
268                ^ MEM_readST(pIn as *const core::ffi::c_void);
269            if diff_0 == 0 {
270                pIn = pIn.offset(::core::mem::size_of::<size_t>() as core::ffi::c_ulong as isize);
271                pMatch =
272                    pMatch.offset(::core::mem::size_of::<size_t>() as core::ffi::c_ulong as isize);
273            } else {
274                pIn = pIn.offset(ZSTD_NbCommonBytes(diff_0) as isize);
275                return pIn.offset_from(pStart) as size_t;
276            }
277        }
278    }
279    if MEM_64bits() != 0
280        && pIn < pInLimit.offset(-(3))
281        && MEM_read32(pMatch as *const core::ffi::c_void)
282            == MEM_read32(pIn as *const core::ffi::c_void)
283    {
284        pIn = pIn.offset(4);
285        pMatch = pMatch.offset(4);
286    }
287    if pIn < pInLimit.offset(-(1))
288        && MEM_read16(pMatch as *const core::ffi::c_void) as core::ffi::c_int
289            == MEM_read16(pIn as *const core::ffi::c_void) as core::ffi::c_int
290    {
291        pIn = pIn.offset(2);
292        pMatch = pMatch.offset(2);
293    }
294    if pIn < pInLimit && *pMatch as core::ffi::c_int == *pIn as core::ffi::c_int {
295        pIn = pIn.offset(1);
296    }
297    pIn.offset_from(pStart) as size_t
298}
299#[inline]
300unsafe fn ZSTD_count_2segments(
301    ip: *const u8,
302    match_0: *const u8,
303    iEnd: *const u8,
304    mEnd: *const u8,
305    iStart: *const u8,
306) -> size_t {
307    let vEnd = if ip.offset(mEnd.offset_from(match_0) as core::ffi::c_long as isize) < iEnd {
308        ip.offset(mEnd.offset_from(match_0) as core::ffi::c_long as isize)
309    } else {
310        iEnd
311    };
312    let matchLength = ZSTD_count(ip, match_0, vEnd);
313    if match_0.add(matchLength) != mEnd {
314        return matchLength;
315    }
316    matchLength.wrapping_add(ZSTD_count(ip.add(matchLength), iStart, iEnd))
317}
318static prime3bytes: u32 = 506832829;
319unsafe fn ZSTD_hash3(u: u32, h: u32, s: u32) -> u32 {
320    (((u << (32 as core::ffi::c_int - 24 as core::ffi::c_int)) * prime3bytes) ^ s)
321        >> 32u32.wrapping_sub(h)
322}
323#[inline]
324unsafe fn ZSTD_hash3Ptr(ptr: *const core::ffi::c_void, h: u32) -> size_t {
325    ZSTD_hash3(MEM_readLE32(ptr), h, 0) as size_t
326}
327static prime4bytes: u32 = 2654435761;
328unsafe fn ZSTD_hash4(u: u32, h: u32, s: u32) -> u32 {
329    ((u * prime4bytes) ^ s) >> (32 as core::ffi::c_int as u32).wrapping_sub(h)
330}
331unsafe fn ZSTD_hash4Ptr(ptr: *const core::ffi::c_void, h: u32) -> size_t {
332    ZSTD_hash4(MEM_readLE32(ptr), h, 0) as size_t
333}
334static prime5bytes: u64 = 889523592379;
335unsafe fn ZSTD_hash5(u: u64, h: u32, s: u64) -> size_t {
336    ((((u << (64 - 40)) * prime5bytes) ^ s) >> 64u32.wrapping_sub(h)) as size_t
337}
338unsafe fn ZSTD_hash5Ptr(p: *const core::ffi::c_void, h: u32) -> size_t {
339    ZSTD_hash5(MEM_readLE64(p), h, 0)
340}
341static prime6bytes: u64 = 227718039650203;
342unsafe fn ZSTD_hash6(u: u64, h: u32, s: u64) -> size_t {
343    ((((u << (64 - 48)) * prime6bytes) ^ s) >> 64u32.wrapping_sub(h)) as size_t
344}
345unsafe fn ZSTD_hash6Ptr(p: *const core::ffi::c_void, h: u32) -> size_t {
346    ZSTD_hash6(MEM_readLE64(p), h, 0)
347}
348static prime7bytes: u64 = 58295818150454627;
349unsafe fn ZSTD_hash7(u: u64, h: u32, s: u64) -> size_t {
350    ((((u << (64 - 56)) * prime7bytes) ^ s) >> 64u32.wrapping_sub(h)) as size_t
351}
352unsafe fn ZSTD_hash7Ptr(p: *const core::ffi::c_void, h: u32) -> size_t {
353    ZSTD_hash7(MEM_readLE64(p), h, 0)
354}
355static prime8bytes: u64 = 0xcf1bbcdcb7a56463 as core::ffi::c_ulonglong;
356unsafe fn ZSTD_hash8(u: u64, h: u32, s: u64) -> size_t {
357    (((u * prime8bytes) ^ s) >> 64u32.wrapping_sub(h)) as size_t
358}
359unsafe fn ZSTD_hash8Ptr(p: *const core::ffi::c_void, h: u32) -> size_t {
360    ZSTD_hash8(MEM_readLE64(p), h, 0)
361}
362#[inline(always)]
363unsafe fn ZSTD_hashPtr(p: *const core::ffi::c_void, hBits: u32, mls: u32) -> size_t {
364    match mls {
365        5 => ZSTD_hash5Ptr(p, hBits),
366        6 => ZSTD_hash6Ptr(p, hBits),
367        7 => ZSTD_hash7Ptr(p, hBits),
368        8 => ZSTD_hash8Ptr(p, hBits),
369        4 | _ => ZSTD_hash4Ptr(p, hBits),
370    }
371}
372#[inline]
373unsafe fn ZSTD_getLowestMatchIndex(
374    ms: *const ZSTD_MatchState_t,
375    curr: u32,
376    windowLog: core::ffi::c_uint,
377) -> u32 {
378    let maxDistance = (1) << windowLog;
379    let lowestValid = (*ms).window.lowLimit;
380    let withinWindow = if curr.wrapping_sub(lowestValid) > maxDistance {
381        curr.wrapping_sub(maxDistance)
382    } else {
383        lowestValid
384    };
385    let isDictionary = ((*ms).loadedDictEnd != 0) as core::ffi::c_int as u32;
386
387    if isDictionary != 0 {
388        lowestValid
389    } else {
390        withinWindow
391    }
392}
393#[inline]
394unsafe fn ZSTD_index_overlap_check(prefixLowestIndex: u32, repIndex: u32) -> core::ffi::c_int {
395    (prefixLowestIndex.wrapping_sub(1).wrapping_sub(repIndex) >= 3) as core::ffi::c_int
396}
397#[inline]
398unsafe fn ZSTD_countTrailingZeros32(val: u32) -> core::ffi::c_uint {
399    val.trailing_zeros() as i32 as core::ffi::c_uint
400}
401#[inline]
402unsafe fn ZSTD_countLeadingZeros32(val: u32) -> core::ffi::c_uint {
403    val.leading_zeros() as i32 as core::ffi::c_uint
404}
405#[inline]
406unsafe fn ZSTD_countTrailingZeros64(val: u64) -> core::ffi::c_uint {
407    (val as core::ffi::c_ulonglong).trailing_zeros() as i32 as core::ffi::c_uint
408}
409#[inline]
410unsafe fn ZSTD_countLeadingZeros64(val: u64) -> core::ffi::c_uint {
411    (val as core::ffi::c_ulonglong).leading_zeros() as i32 as core::ffi::c_uint
412}
413#[inline]
414unsafe fn ZSTD_NbCommonBytes(val: size_t) -> core::ffi::c_uint {
415    if MEM_isLittleEndian() != 0 {
416        if MEM_64bits() != 0 {
417            ZSTD_countTrailingZeros64(val as u64) >> 3
418        } else {
419            ZSTD_countTrailingZeros32(val as u32) >> 3
420        }
421    } else if MEM_64bits() != 0 {
422        ZSTD_countLeadingZeros64(val as u64) >> 3
423    } else {
424        ZSTD_countLeadingZeros32(val as u32) >> 3
425    }
426}
427#[inline]
428unsafe fn ZSTD_highbit32(val: u32) -> core::ffi::c_uint {
429    (31 as core::ffi::c_uint).wrapping_sub(ZSTD_countLeadingZeros32(val))
430}
431pub const UINT_MAX: core::ffi::c_uint = (__INT_MAX__ as core::ffi::c_uint)
432    .wrapping_mul(2)
433    .wrapping_add(1);
434pub const ZSTD_LITFREQ_ADD: core::ffi::c_int = 2;
435pub const ZSTD_MAX_PRICE: core::ffi::c_int = (1) << 30;
436pub const ZSTD_PREDEF_THRESHOLD: core::ffi::c_int = 8;
437pub const BITCOST_ACCURACY: core::ffi::c_int = 8;
438pub const BITCOST_MULTIPLIER: core::ffi::c_int = (1) << BITCOST_ACCURACY;
439#[inline]
440unsafe fn ZSTD_bitWeight(stat: u32) -> u32 {
441    (ZSTD_highbit32(stat.wrapping_add(1))).wrapping_mul(BITCOST_MULTIPLIER as core::ffi::c_uint)
442}
443#[inline]
444unsafe fn ZSTD_fracWeight(rawStat: u32) -> u32 {
445    let stat = rawStat.wrapping_add(1);
446    let hb = ZSTD_highbit32(stat);
447    let BWeight = hb * BITCOST_MULTIPLIER as u32;
448    let FWeight = stat << BITCOST_ACCURACY >> hb;
449
450    BWeight.wrapping_add(FWeight)
451}
452unsafe fn ZSTD_compressedLiterals(optPtr: *const optState_t) -> core::ffi::c_int {
453    ((*optPtr).literalCompressionMode as core::ffi::c_uint
454        != ZSTD_ps_disable as core::ffi::c_int as core::ffi::c_uint) as core::ffi::c_int
455}
456unsafe fn ZSTD_setBasePrices(optPtr: *mut optState_t, optLevel: core::ffi::c_int) {
457    if ZSTD_compressedLiterals(optPtr) != 0 {
458        (*optPtr).litSumBasePrice = if optLevel != 0 {
459            ZSTD_fracWeight((*optPtr).litSum)
460        } else {
461            ZSTD_bitWeight((*optPtr).litSum)
462        };
463    }
464    (*optPtr).litLengthSumBasePrice = if optLevel != 0 {
465        ZSTD_fracWeight((*optPtr).litLengthSum)
466    } else {
467        ZSTD_bitWeight((*optPtr).litLengthSum)
468    };
469    (*optPtr).matchLengthSumBasePrice = if optLevel != 0 {
470        ZSTD_fracWeight((*optPtr).matchLengthSum)
471    } else {
472        ZSTD_bitWeight((*optPtr).matchLengthSum)
473    };
474    (*optPtr).offCodeSumBasePrice = if optLevel != 0 {
475        ZSTD_fracWeight((*optPtr).offCodeSum)
476    } else {
477        ZSTD_bitWeight((*optPtr).offCodeSum)
478    };
479}
480unsafe fn sum_u32(table: *const core::ffi::c_uint, nbElts: size_t) -> u32 {
481    let mut n: size_t = 0;
482    let mut total = 0;
483    n = 0;
484    while n < nbElts {
485        total = (total as core::ffi::c_uint).wrapping_add(*table.add(n)) as u32 as u32;
486        n = n.wrapping_add(1);
487    }
488    total
489}
490unsafe fn ZSTD_downscaleStats(
491    table: *mut core::ffi::c_uint,
492    lastEltIndex: u32,
493    shift: u32,
494    base1: base_directive_e,
495) -> u32 {
496    let mut s: u32 = 0;
497    let mut sum = 0;
498    s = 0;
499    while s < lastEltIndex.wrapping_add(1) {
500        let base = (if base1 as core::ffi::c_uint != 0 {
501            1
502        } else {
503            (*table.offset(s as isize) > 0) as core::ffi::c_int
504        }) as core::ffi::c_uint;
505        let newStat = base.wrapping_add(*table.offset(s as isize) >> shift);
506        sum = (sum as core::ffi::c_uint).wrapping_add(newStat);
507        *table.offset(s as isize) = newStat;
508        s = s.wrapping_add(1);
509    }
510    sum
511}
512unsafe fn ZSTD_scaleStats(table: *mut core::ffi::c_uint, lastEltIndex: u32, logTarget: u32) -> u32 {
513    let prevsum = sum_u32(
514        table as *const core::ffi::c_uint,
515        lastEltIndex.wrapping_add(1) as size_t,
516    );
517    let factor = prevsum >> logTarget;
518    if factor <= 1 {
519        return prevsum;
520    }
521    ZSTD_downscaleStats(
522        table,
523        lastEltIndex,
524        ZSTD_highbit32(factor),
525        base_1guaranteed,
526    )
527}
528unsafe fn ZSTD_rescaleFreqs(
529    optPtr: *mut optState_t,
530    src: *const u8,
531    srcSize: size_t,
532    optLevel: core::ffi::c_int,
533) {
534    let compressedLiterals = ZSTD_compressedLiterals(optPtr);
535    (*optPtr).priceType = zop_dynamic;
536    if (*optPtr).litLengthSum == 0 {
537        if srcSize <= ZSTD_PREDEF_THRESHOLD as size_t {
538            (*optPtr).priceType = zop_predef;
539        }
540        if (*(*optPtr).symbolCosts).huf.repeatMode as core::ffi::c_uint
541            == HUF_repeat_valid as core::ffi::c_int as core::ffi::c_uint
542        {
543            (*optPtr).priceType = zop_dynamic;
544            if compressedLiterals != 0 {
545                let mut lit: core::ffi::c_uint = 0;
546                (*optPtr).litSum = 0;
547                lit = 0;
548                while lit <= MaxLit as core::ffi::c_uint {
549                    let scaleLog = 11u32;
550                    let bitCost = HUF_getNbBitsFromCTable(
551                        ((*(*optPtr).symbolCosts).huf.CTable).as_ptr(),
552                        lit,
553                    );
554                    *((*optPtr).litFreq).offset(lit as isize) = (if bitCost != 0 {
555                        (1) << scaleLog.wrapping_sub(bitCost)
556                    } else {
557                        1
558                    })
559                        as core::ffi::c_uint;
560                    (*optPtr).litSum = ((*optPtr).litSum as core::ffi::c_uint)
561                        .wrapping_add(*((*optPtr).litFreq).offset(lit as isize))
562                        as u32 as u32;
563                    lit = lit.wrapping_add(1);
564                }
565            }
566            let mut ll: core::ffi::c_uint = 0;
567            let mut llstate = FSE_CState_t {
568                value: 0,
569                stateTable: core::ptr::null::<core::ffi::c_void>(),
570                symbolTT: core::ptr::null::<core::ffi::c_void>(),
571                stateLog: 0,
572            };
573            FSE_initCState(
574                &mut llstate,
575                ((*(*optPtr).symbolCosts).fse.litlengthCTable).as_ptr(),
576            );
577            (*optPtr).litLengthSum = 0;
578            ll = 0;
579            while ll <= MaxLL as core::ffi::c_uint {
580                let scaleLog_0 = 10u32;
581                let bitCost_0 = FSE_getMaxNbBits(llstate.symbolTT, ll);
582                *((*optPtr).litLengthFreq).offset(ll as isize) = (if bitCost_0 != 0 {
583                    (1) << scaleLog_0.wrapping_sub(bitCost_0)
584                } else {
585                    1
586                })
587                    as core::ffi::c_uint;
588                (*optPtr).litLengthSum = ((*optPtr).litLengthSum as core::ffi::c_uint)
589                    .wrapping_add(*((*optPtr).litLengthFreq).offset(ll as isize))
590                    as u32 as u32;
591                ll = ll.wrapping_add(1);
592            }
593            let mut ml: core::ffi::c_uint = 0;
594            let mut mlstate = FSE_CState_t {
595                value: 0,
596                stateTable: core::ptr::null::<core::ffi::c_void>(),
597                symbolTT: core::ptr::null::<core::ffi::c_void>(),
598                stateLog: 0,
599            };
600            FSE_initCState(
601                &mut mlstate,
602                ((*(*optPtr).symbolCosts).fse.matchlengthCTable).as_ptr(),
603            );
604            (*optPtr).matchLengthSum = 0;
605            ml = 0;
606            while ml <= MaxML as core::ffi::c_uint {
607                let scaleLog_1 = 10u32;
608                let bitCost_1 = FSE_getMaxNbBits(mlstate.symbolTT, ml);
609                *((*optPtr).matchLengthFreq).offset(ml as isize) = (if bitCost_1 != 0 {
610                    (1) << scaleLog_1.wrapping_sub(bitCost_1)
611                } else {
612                    1
613                })
614                    as core::ffi::c_uint;
615                (*optPtr).matchLengthSum = ((*optPtr).matchLengthSum as core::ffi::c_uint)
616                    .wrapping_add(*((*optPtr).matchLengthFreq).offset(ml as isize))
617                    as u32 as u32;
618                ml = ml.wrapping_add(1);
619            }
620            let mut of: core::ffi::c_uint = 0;
621            let mut ofstate = FSE_CState_t {
622                value: 0,
623                stateTable: core::ptr::null::<core::ffi::c_void>(),
624                symbolTT: core::ptr::null::<core::ffi::c_void>(),
625                stateLog: 0,
626            };
627            FSE_initCState(
628                &mut ofstate,
629                ((*(*optPtr).symbolCosts).fse.offcodeCTable).as_ptr(),
630            );
631            (*optPtr).offCodeSum = 0;
632            of = 0;
633            while of <= MaxOff as core::ffi::c_uint {
634                let scaleLog_2 = 10u32;
635                let bitCost_2 = FSE_getMaxNbBits(ofstate.symbolTT, of);
636                *((*optPtr).offCodeFreq).offset(of as isize) = (if bitCost_2 != 0 {
637                    (1) << scaleLog_2.wrapping_sub(bitCost_2)
638                } else {
639                    1
640                })
641                    as core::ffi::c_uint;
642                (*optPtr).offCodeSum = ((*optPtr).offCodeSum as core::ffi::c_uint)
643                    .wrapping_add(*((*optPtr).offCodeFreq).offset(of as isize))
644                    as u32 as u32;
645                of = of.wrapping_add(1);
646            }
647        } else {
648            if compressedLiterals != 0 {
649                let mut lit_0 = MaxLit as core::ffi::c_uint;
650                HIST_count_simple(
651                    (*optPtr).litFreq,
652                    &mut lit_0,
653                    src as *const core::ffi::c_void,
654                    srcSize,
655                );
656                (*optPtr).litSum =
657                    ZSTD_downscaleStats((*optPtr).litFreq, MaxLit as u32, 8, base_0possible);
658            }
659            let baseLLfreqs: [core::ffi::c_uint; 36] = [
660                4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
661                1, 1, 1, 1, 1, 1, 1, 1,
662            ];
663            libc::memcpy(
664                (*optPtr).litLengthFreq as *mut core::ffi::c_void,
665                baseLLfreqs.as_ptr() as *const core::ffi::c_void,
666                ::core::mem::size_of::<[core::ffi::c_uint; 36]>() as core::ffi::c_ulong
667                    as libc::size_t,
668            );
669            (*optPtr).litLengthSum = sum_u32(baseLLfreqs.as_ptr(), (MaxLL + 1) as size_t);
670            let mut ml_0: core::ffi::c_uint = 0;
671            ml_0 = 0;
672            while ml_0 <= MaxML as core::ffi::c_uint {
673                *((*optPtr).matchLengthFreq).offset(ml_0 as isize) = 1;
674                ml_0 = ml_0.wrapping_add(1);
675            }
676            (*optPtr).matchLengthSum = (MaxML + 1) as u32;
677            let baseOFCfreqs: [core::ffi::c_uint; 32] = [
678                6, 2, 1, 1, 2, 3, 4, 4, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
679                1, 1, 1, 1,
680            ];
681            libc::memcpy(
682                (*optPtr).offCodeFreq as *mut core::ffi::c_void,
683                baseOFCfreqs.as_ptr() as *const core::ffi::c_void,
684                ::core::mem::size_of::<[core::ffi::c_uint; 32]>() as core::ffi::c_ulong
685                    as libc::size_t,
686            );
687            (*optPtr).offCodeSum = sum_u32(baseOFCfreqs.as_ptr(), (MaxOff + 1) as size_t);
688        }
689    } else {
690        if compressedLiterals != 0 {
691            (*optPtr).litSum = ZSTD_scaleStats((*optPtr).litFreq, MaxLit as u32, 12);
692        }
693        (*optPtr).litLengthSum = ZSTD_scaleStats((*optPtr).litLengthFreq, MaxLL as u32, 11);
694        (*optPtr).matchLengthSum = ZSTD_scaleStats((*optPtr).matchLengthFreq, MaxML as u32, 11);
695        (*optPtr).offCodeSum = ZSTD_scaleStats((*optPtr).offCodeFreq, MaxOff as u32, 11);
696    }
697    ZSTD_setBasePrices(optPtr, optLevel);
698}
699unsafe fn ZSTD_rawLiteralsCost(
700    literals: *const u8,
701    litLength: u32,
702    optPtr: *const optState_t,
703    optLevel: core::ffi::c_int,
704) -> u32 {
705    if litLength == 0 {
706        return 0;
707    }
708    if ZSTD_compressedLiterals(optPtr) == 0 {
709        return (litLength << 3) * BITCOST_MULTIPLIER as u32;
710    }
711    if (*optPtr).priceType as core::ffi::c_uint
712        == zop_predef as core::ffi::c_int as core::ffi::c_uint
713    {
714        return litLength * 6 * BITCOST_MULTIPLIER as u32;
715    }
716    let mut price = (*optPtr).litSumBasePrice * litLength;
717    let litPriceMax = ((*optPtr).litSumBasePrice).wrapping_sub(BITCOST_MULTIPLIER as u32);
718    let mut u: u32 = 0;
719    u = 0;
720    while u < litLength {
721        let mut litPrice = if optLevel != 0 {
722            ZSTD_fracWeight(*((*optPtr).litFreq).offset(*literals.offset(u as isize) as isize))
723        } else {
724            ZSTD_bitWeight(*((*optPtr).litFreq).offset(*literals.offset(u as isize) as isize))
725        };
726        if (litPrice > litPriceMax) as core::ffi::c_int as core::ffi::c_long != 0 {
727            litPrice = litPriceMax;
728        }
729        price = price.wrapping_sub(litPrice);
730        u = u.wrapping_add(1);
731    }
732    price
733}
734unsafe fn ZSTD_litLengthPrice(
735    litLength: u32,
736    optPtr: *const optState_t,
737    optLevel: core::ffi::c_int,
738) -> u32 {
739    if (*optPtr).priceType as core::ffi::c_uint
740        == zop_predef as core::ffi::c_int as core::ffi::c_uint
741    {
742        return if optLevel != 0 {
743            ZSTD_fracWeight(litLength)
744        } else {
745            ZSTD_bitWeight(litLength)
746        };
747    }
748    if litLength == ZSTD_BLOCKSIZE_MAX as u32 {
749        return (BITCOST_MULTIPLIER as u32).wrapping_add(ZSTD_litLengthPrice(
750            (ZSTD_BLOCKSIZE_MAX - 1) as u32,
751            optPtr,
752            optLevel,
753        ));
754    }
755    let llCode = ZSTD_LLcode(litLength);
756    ((*LL_bits.as_ptr().offset(llCode as isize) as core::ffi::c_int * BITCOST_MULTIPLIER) as u32)
757        .wrapping_add((*optPtr).litLengthSumBasePrice)
758        .wrapping_sub(if optLevel != 0 {
759            ZSTD_fracWeight(*((*optPtr).litLengthFreq).offset(llCode as isize))
760        } else {
761            ZSTD_bitWeight(*((*optPtr).litLengthFreq).offset(llCode as isize))
762        })
763}
764#[inline(always)]
765unsafe fn ZSTD_getMatchPrice(
766    offBase: u32,
767    matchLength: u32,
768    optPtr: *const optState_t,
769    optLevel: core::ffi::c_int,
770) -> u32 {
771    let mut price: u32 = 0;
772    let offCode = ZSTD_highbit32(offBase);
773    let mlBase = matchLength.wrapping_sub(MINMATCH as u32);
774    if (*optPtr).priceType as core::ffi::c_uint
775        == zop_predef as core::ffi::c_int as core::ffi::c_uint
776    {
777        return (if optLevel != 0 {
778            ZSTD_fracWeight(mlBase)
779        } else {
780            ZSTD_bitWeight(mlBase)
781        })
782        .wrapping_add(16u32.wrapping_add(offCode) * BITCOST_MULTIPLIER as u32);
783    }
784    price = (offCode * BITCOST_MULTIPLIER as u32).wrapping_add(
785        ((*optPtr).offCodeSumBasePrice).wrapping_sub(if optLevel != 0 {
786            ZSTD_fracWeight(*((*optPtr).offCodeFreq).offset(offCode as isize))
787        } else {
788            ZSTD_bitWeight(*((*optPtr).offCodeFreq).offset(offCode as isize))
789        }),
790    );
791    if optLevel < 2 && offCode >= 20 {
792        price = price.wrapping_add(offCode.wrapping_sub(19) * 2 * BITCOST_MULTIPLIER as u32);
793    }
794    let mlCode = ZSTD_MLcode(mlBase);
795    price = price.wrapping_add(
796        ((*ML_bits.as_ptr().offset(mlCode as isize) as core::ffi::c_int * BITCOST_MULTIPLIER)
797            as u32)
798            .wrapping_add(
799                ((*optPtr).matchLengthSumBasePrice).wrapping_sub(if optLevel != 0 {
800                    ZSTD_fracWeight(*((*optPtr).matchLengthFreq).offset(mlCode as isize))
801                } else {
802                    ZSTD_bitWeight(*((*optPtr).matchLengthFreq).offset(mlCode as isize))
803                }),
804            ),
805    );
806    price = price.wrapping_add((BITCOST_MULTIPLIER / 5) as u32);
807    price
808}
809unsafe fn ZSTD_updateStats(
810    optPtr: *mut optState_t,
811    litLength: u32,
812    literals: *const u8,
813    offBase: u32,
814    matchLength: u32,
815) {
816    if ZSTD_compressedLiterals(optPtr) != 0 {
817        let mut u: u32 = 0;
818        u = 0;
819        while u < litLength {
820            let fresh2 = &mut (*((*optPtr).litFreq).offset(*literals.offset(u as isize) as isize));
821            *fresh2 = (*fresh2).wrapping_add(ZSTD_LITFREQ_ADD as core::ffi::c_uint);
822            u = u.wrapping_add(1);
823        }
824        (*optPtr).litSum = ((*optPtr).litSum).wrapping_add(litLength * ZSTD_LITFREQ_ADD as u32);
825    }
826    let llCode = ZSTD_LLcode(litLength);
827    let fresh3 = &mut (*((*optPtr).litLengthFreq).offset(llCode as isize));
828    *fresh3 = (*fresh3).wrapping_add(1);
829    (*optPtr).litLengthSum = ((*optPtr).litLengthSum).wrapping_add(1);
830    (*optPtr).litLengthSum;
831    let offCode = ZSTD_highbit32(offBase);
832    let fresh4 = &mut (*((*optPtr).offCodeFreq).offset(offCode as isize));
833    *fresh4 = (*fresh4).wrapping_add(1);
834    (*optPtr).offCodeSum = ((*optPtr).offCodeSum).wrapping_add(1);
835    (*optPtr).offCodeSum;
836    let mlBase = matchLength.wrapping_sub(MINMATCH as u32);
837    let mlCode = ZSTD_MLcode(mlBase);
838    let fresh5 = &mut (*((*optPtr).matchLengthFreq).offset(mlCode as isize));
839    *fresh5 = (*fresh5).wrapping_add(1);
840    (*optPtr).matchLengthSum = ((*optPtr).matchLengthSum).wrapping_add(1);
841    (*optPtr).matchLengthSum;
842}
843#[inline]
844unsafe fn ZSTD_readMINMATCH(memPtr: *const core::ffi::c_void, length: u32) -> u32 {
845    match length {
846        3 => {
847            if MEM_isLittleEndian() != 0 {
848                MEM_read32(memPtr) << 8
849            } else {
850                MEM_read32(memPtr) >> 8
851            }
852        }
853        4 | _ => MEM_read32(memPtr),
854    }
855}
856unsafe fn ZSTD_insertAndFindFirstIndexHash3(
857    ms: *const ZSTD_MatchState_t,
858    nextToUpdate3: *mut u32,
859    ip: *const u8,
860) -> u32 {
861    let hashTable3 = (*ms).hashTable3;
862    let hashLog3 = (*ms).hashLog3;
863    let base = (*ms).window.base;
864    let mut idx = *nextToUpdate3;
865    let target = ip.offset_from(base) as core::ffi::c_long as u32;
866    let hash3 = ZSTD_hash3Ptr(ip as *const core::ffi::c_void, hashLog3);
867    while idx < target {
868        *hashTable3.add(ZSTD_hash3Ptr(
869            base.offset(idx as isize) as *const core::ffi::c_void,
870            hashLog3,
871        )) = idx;
872        idx = idx.wrapping_add(1);
873    }
874    *nextToUpdate3 = target;
875    *hashTable3.add(hash3)
876}
877unsafe fn ZSTD_insertBt1(
878    ms: *const ZSTD_MatchState_t,
879    ip: *const u8,
880    iend: *const u8,
881    target: u32,
882    mls: u32,
883    extDict: core::ffi::c_int,
884) -> u32 {
885    let cParams: *const ZSTD_compressionParameters = &(*ms).cParams;
886    let hashTable = (*ms).hashTable;
887    let hashLog = (*cParams).hashLog;
888    let h = ZSTD_hashPtr(ip as *const core::ffi::c_void, hashLog, mls);
889    let bt = (*ms).chainTable;
890    let btLog = ((*cParams).chainLog).wrapping_sub(1);
891    let btMask = (((1) << btLog) - 1) as u32;
892    let mut matchIndex = *hashTable.add(h);
893    let mut commonLengthSmaller = 0;
894    let mut commonLengthLarger = 0;
895    let base = (*ms).window.base;
896    let dictBase = (*ms).window.dictBase;
897    let dictLimit = (*ms).window.dictLimit;
898    let dictEnd = dictBase.offset(dictLimit as isize);
899    let prefixStart = base.offset(dictLimit as isize);
900    let mut match_0 = core::ptr::null::<u8>();
901    let curr = ip.offset_from(base) as core::ffi::c_long as u32;
902    let btLow = if btMask >= curr {
903        0
904    } else {
905        curr.wrapping_sub(btMask)
906    };
907    let mut smallerPtr = bt.offset((2 * (curr & btMask)) as isize);
908    let mut largerPtr = smallerPtr.offset(1);
909    let mut dummy32: u32 = 0;
910    let windowLow = ZSTD_getLowestMatchIndex(ms, target, (*cParams).windowLog);
911    let mut matchEndIdx = curr.wrapping_add(8).wrapping_add(1);
912    let mut bestLength = 8;
913    let mut nbCompares = (1 as core::ffi::c_uint) << (*cParams).searchLog;
914    *hashTable.add(h) = curr;
915    while nbCompares != 0 && matchIndex >= windowLow {
916        let nextPtr = bt.offset((2 * (matchIndex & btMask)) as isize);
917        let mut matchLength = if commonLengthSmaller < commonLengthLarger {
918            commonLengthSmaller
919        } else {
920            commonLengthLarger
921        };
922        if extDict == 0 || (matchIndex as size_t).wrapping_add(matchLength) >= dictLimit as size_t {
923            match_0 = base.offset(matchIndex as isize);
924            matchLength = matchLength.wrapping_add(ZSTD_count(
925                ip.add(matchLength),
926                match_0.add(matchLength),
927                iend,
928            ));
929        } else {
930            match_0 = dictBase.offset(matchIndex as isize);
931            matchLength = matchLength.wrapping_add(ZSTD_count_2segments(
932                ip.add(matchLength),
933                match_0.add(matchLength),
934                iend,
935                dictEnd,
936                prefixStart,
937            ));
938            if (matchIndex as size_t).wrapping_add(matchLength) >= dictLimit as size_t {
939                match_0 = base.offset(matchIndex as isize);
940            }
941        }
942        if matchLength > bestLength {
943            bestLength = matchLength;
944            if matchLength > matchEndIdx.wrapping_sub(matchIndex) as size_t {
945                matchEndIdx = matchIndex.wrapping_add(matchLength as u32);
946            }
947        }
948        if ip.add(matchLength) == iend {
949            break;
950        } else {
951            if (*match_0.add(matchLength) as core::ffi::c_int)
952                < *ip.add(matchLength) as core::ffi::c_int
953            {
954                *smallerPtr = matchIndex;
955                commonLengthSmaller = matchLength;
956                if matchIndex <= btLow {
957                    smallerPtr = &mut dummy32;
958                    break;
959                } else {
960                    smallerPtr = nextPtr.offset(1);
961                    matchIndex = *nextPtr.offset(1);
962                }
963            } else {
964                *largerPtr = matchIndex;
965                commonLengthLarger = matchLength;
966                if matchIndex <= btLow {
967                    largerPtr = &mut dummy32;
968                    break;
969                } else {
970                    largerPtr = nextPtr;
971                    matchIndex = *nextPtr.offset(0);
972                }
973            }
974            nbCompares = nbCompares.wrapping_sub(1);
975        }
976    }
977    *largerPtr = 0;
978    *smallerPtr = *largerPtr;
979    let mut positions = 0;
980    if bestLength > 384 {
981        positions = if (192) < bestLength.wrapping_sub(384) as u32 {
982            192
983        } else {
984            bestLength.wrapping_sub(384) as u32
985        };
986    }
987    if positions > matchEndIdx.wrapping_sub(curr.wrapping_add(8)) {
988        positions
989    } else {
990        matchEndIdx.wrapping_sub(curr.wrapping_add(8))
991    }
992}
993#[inline(always)]
994unsafe fn ZSTD_updateTree_internal(
995    ms: *mut ZSTD_MatchState_t,
996    ip: *const u8,
997    iend: *const u8,
998    mls: u32,
999    dictMode: ZSTD_dictMode_e,
1000) {
1001    let base = (*ms).window.base;
1002    let target = ip.offset_from(base) as core::ffi::c_long as u32;
1003    let mut idx = (*ms).nextToUpdate;
1004    while idx < target {
1005        let forward = ZSTD_insertBt1(
1006            ms,
1007            base.offset(idx as isize),
1008            iend,
1009            target,
1010            mls,
1011            (dictMode as core::ffi::c_uint == ZSTD_extDict as core::ffi::c_int as core::ffi::c_uint)
1012                as core::ffi::c_int,
1013        );
1014        idx = idx.wrapping_add(forward);
1015    }
1016    (*ms).nextToUpdate = target;
1017}
1018pub unsafe fn ZSTD_updateTree(ms: *mut ZSTD_MatchState_t, ip: *const u8, iend: *const u8) {
1019    ZSTD_updateTree_internal(ms, ip, iend, (*ms).cParams.minMatch, ZSTD_noDict);
1020}
1021#[inline(always)]
1022unsafe fn ZSTD_insertBtAndGetAllMatches(
1023    matches: *mut ZSTD_match_t,
1024    ms: *mut ZSTD_MatchState_t,
1025    nextToUpdate3: *mut u32,
1026    ip: *const u8,
1027    iLimit: *const u8,
1028    dictMode: ZSTD_dictMode_e,
1029    rep: *const u32,
1030    ll0: u32,
1031    lengthToBeat: u32,
1032    mls: u32,
1033) -> u32 {
1034    let cParams: *const ZSTD_compressionParameters = &mut (*ms).cParams;
1035    let sufficient_len = if (*cParams).targetLength < (((1) << 12) - 1) as core::ffi::c_uint {
1036        (*cParams).targetLength
1037    } else {
1038        (((1) << 12) - 1) as core::ffi::c_uint
1039    };
1040    let base = (*ms).window.base;
1041    let curr = ip.offset_from(base) as core::ffi::c_long as u32;
1042    let hashLog = (*cParams).hashLog;
1043    let minMatch = (if mls == 3 { 3 } else { 4 }) as u32;
1044    let hashTable = (*ms).hashTable;
1045    let h = ZSTD_hashPtr(ip as *const core::ffi::c_void, hashLog, mls);
1046    let mut matchIndex = *hashTable.add(h);
1047    let bt = (*ms).chainTable;
1048    let btLog = ((*cParams).chainLog).wrapping_sub(1 as core::ffi::c_uint);
1049    let btMask = ((1 as core::ffi::c_uint) << btLog).wrapping_sub(1);
1050    let mut commonLengthSmaller = 0;
1051    let mut commonLengthLarger = 0;
1052    let dictBase = (*ms).window.dictBase;
1053    let dictLimit = (*ms).window.dictLimit;
1054    let dictEnd = dictBase.offset(dictLimit as isize);
1055    let prefixStart = base.offset(dictLimit as isize);
1056    let btLow = if btMask >= curr {
1057        0
1058    } else {
1059        curr.wrapping_sub(btMask)
1060    };
1061    let windowLow = ZSTD_getLowestMatchIndex(ms, curr, (*cParams).windowLog);
1062    let matchLow = if windowLow != 0 { windowLow } else { 1 };
1063    let mut smallerPtr = bt.offset((2 * (curr & btMask)) as isize);
1064    let mut largerPtr = bt.offset((2 * (curr & btMask)) as isize).offset(1);
1065    let mut matchEndIdx = curr.wrapping_add(8).wrapping_add(1);
1066    let mut dummy32: u32 = 0;
1067    let mut mnum = 0u32;
1068    let mut nbCompares = (1 as core::ffi::c_uint) << (*cParams).searchLog;
1069    let dms = if dictMode as core::ffi::c_uint
1070        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1071    {
1072        (*ms).dictMatchState
1073    } else {
1074        core::ptr::null()
1075    };
1076    let dmsCParams = if dictMode as core::ffi::c_uint
1077        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1078    {
1079        &(*dms).cParams
1080    } else {
1081        core::ptr::null()
1082    };
1083    let dmsBase = if dictMode as core::ffi::c_uint
1084        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1085    {
1086        (*dms).window.base
1087    } else {
1088        core::ptr::null()
1089    };
1090    let dmsEnd = if dictMode as core::ffi::c_uint
1091        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1092    {
1093        (*dms).window.nextSrc
1094    } else {
1095        core::ptr::null()
1096    };
1097    let dmsHighLimit = if dictMode as core::ffi::c_uint
1098        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1099    {
1100        dmsEnd.offset_from(dmsBase) as core::ffi::c_long as u32
1101    } else {
1102        0
1103    };
1104    let dmsLowLimit = if dictMode as core::ffi::c_uint
1105        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1106    {
1107        (*dms).window.lowLimit
1108    } else {
1109        0
1110    };
1111    let dmsIndexDelta = if dictMode as core::ffi::c_uint
1112        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1113    {
1114        windowLow.wrapping_sub(dmsHighLimit)
1115    } else {
1116        0
1117    };
1118    let dmsHashLog = if dictMode as core::ffi::c_uint
1119        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1120    {
1121        (*dmsCParams).hashLog
1122    } else {
1123        hashLog
1124    };
1125    let dmsBtLog = if dictMode as core::ffi::c_uint
1126        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1127    {
1128        ((*dmsCParams).chainLog).wrapping_sub(1)
1129    } else {
1130        btLog
1131    };
1132    let dmsBtMask = if dictMode as core::ffi::c_uint
1133        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1134    {
1135        ((1 as core::ffi::c_uint) << dmsBtLog).wrapping_sub(1)
1136    } else {
1137        0
1138    };
1139    let dmsBtLow = if dictMode as core::ffi::c_uint
1140        == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1141        && dmsBtMask < dmsHighLimit.wrapping_sub(dmsLowLimit)
1142    {
1143        dmsHighLimit.wrapping_sub(dmsBtMask)
1144    } else {
1145        dmsLowLimit
1146    };
1147    let mut bestLength = lengthToBeat.wrapping_sub(1) as size_t;
1148    let lastR = (ZSTD_REP_NUM as u32).wrapping_add(ll0);
1149    let mut repCode: u32 = 0;
1150    repCode = ll0;
1151    while repCode < lastR {
1152        let repOffset = if repCode == ZSTD_REP_NUM as u32 {
1153            (*rep.offset(0)).wrapping_sub(1)
1154        } else {
1155            *rep.offset(repCode as isize)
1156        };
1157        let repIndex = curr.wrapping_sub(repOffset);
1158        let mut repLen = 0;
1159        if repOffset.wrapping_sub(1) < curr.wrapping_sub(dictLimit) {
1160            if (repIndex >= windowLow) as core::ffi::c_int
1161                & (ZSTD_readMINMATCH(ip as *const core::ffi::c_void, minMatch)
1162                    == ZSTD_readMINMATCH(
1163                        ip.offset(-(repOffset as isize)) as *const core::ffi::c_void,
1164                        minMatch,
1165                    )) as core::ffi::c_int
1166                != 0
1167            {
1168                repLen = (ZSTD_count(
1169                    ip.offset(minMatch as isize),
1170                    ip.offset(minMatch as isize).offset(-(repOffset as isize)),
1171                    iLimit,
1172                ) as u32)
1173                    .wrapping_add(minMatch);
1174            }
1175        } else {
1176            let repMatch = if dictMode as core::ffi::c_uint
1177                == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1178            {
1179                dmsBase
1180                    .offset(repIndex as isize)
1181                    .offset(-(dmsIndexDelta as isize))
1182            } else {
1183                dictBase.offset(repIndex as isize)
1184            };
1185            if dictMode as core::ffi::c_uint
1186                == ZSTD_extDict as core::ffi::c_int as core::ffi::c_uint
1187                && (repOffset.wrapping_sub(1) < curr.wrapping_sub(windowLow)) as core::ffi::c_int
1188                    & ZSTD_index_overlap_check(dictLimit, repIndex)
1189                    != 0
1190                && ZSTD_readMINMATCH(ip as *const core::ffi::c_void, minMatch)
1191                    == ZSTD_readMINMATCH(repMatch as *const core::ffi::c_void, minMatch)
1192            {
1193                repLen = (ZSTD_count_2segments(
1194                    ip.offset(minMatch as isize),
1195                    repMatch.offset(minMatch as isize),
1196                    iLimit,
1197                    dictEnd,
1198                    prefixStart,
1199                ) as u32)
1200                    .wrapping_add(minMatch);
1201            }
1202            if dictMode as core::ffi::c_uint
1203                == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1204                && (repOffset.wrapping_sub(1)
1205                    < curr.wrapping_sub(dmsLowLimit.wrapping_add(dmsIndexDelta)))
1206                    as core::ffi::c_int
1207                    & ZSTD_index_overlap_check(dictLimit, repIndex)
1208                    != 0
1209                && ZSTD_readMINMATCH(ip as *const core::ffi::c_void, minMatch)
1210                    == ZSTD_readMINMATCH(repMatch as *const core::ffi::c_void, minMatch)
1211            {
1212                repLen = (ZSTD_count_2segments(
1213                    ip.offset(minMatch as isize),
1214                    repMatch.offset(minMatch as isize),
1215                    iLimit,
1216                    dmsEnd,
1217                    prefixStart,
1218                ) as u32)
1219                    .wrapping_add(minMatch);
1220            }
1221        }
1222        if repLen as size_t > bestLength {
1223            bestLength = repLen as size_t;
1224            (*matches.offset(mnum as isize)).off = repCode.wrapping_sub(ll0).wrapping_add(1);
1225            (*matches.offset(mnum as isize)).len = repLen;
1226            mnum = mnum.wrapping_add(1);
1227            if (repLen > sufficient_len) as core::ffi::c_int
1228                | (ip.offset(repLen as isize) == iLimit) as core::ffi::c_int
1229                != 0
1230            {
1231                return mnum;
1232            }
1233        }
1234        repCode = repCode.wrapping_add(1);
1235    }
1236    if mls == 3 && bestLength < mls as size_t {
1237        let matchIndex3 = ZSTD_insertAndFindFirstIndexHash3(ms, nextToUpdate3, ip);
1238        if (matchIndex3 >= matchLow) as core::ffi::c_int
1239            & (curr.wrapping_sub(matchIndex3) < ((1) << 18) as u32) as core::ffi::c_int
1240            != 0
1241        {
1242            let mut mlen: size_t = 0;
1243            if dictMode as core::ffi::c_uint == ZSTD_noDict as core::ffi::c_int as core::ffi::c_uint
1244                || dictMode as core::ffi::c_uint
1245                    == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1246                || matchIndex3 >= dictLimit
1247            {
1248                let match_0 = base.offset(matchIndex3 as isize);
1249                mlen = ZSTD_count(ip, match_0, iLimit);
1250            } else {
1251                let match_1 = dictBase.offset(matchIndex3 as isize);
1252                mlen = ZSTD_count_2segments(ip, match_1, iLimit, dictEnd, prefixStart);
1253            }
1254            if mlen >= mls as size_t {
1255                bestLength = mlen;
1256                (*matches.offset(0)).off = curr
1257                    .wrapping_sub(matchIndex3)
1258                    .wrapping_add(ZSTD_REP_NUM as u32);
1259                (*matches.offset(0)).len = mlen as u32;
1260                mnum = 1;
1261                if (mlen > sufficient_len as size_t) as core::ffi::c_int
1262                    | (ip.add(mlen) == iLimit) as core::ffi::c_int
1263                    != 0
1264                {
1265                    (*ms).nextToUpdate = curr.wrapping_add(1);
1266                    return 1;
1267                }
1268            }
1269        }
1270    }
1271    *hashTable.add(h) = curr;
1272    while nbCompares != 0 && matchIndex >= matchLow {
1273        let nextPtr = bt.offset((2 * (matchIndex & btMask)) as isize);
1274        let mut match_2 = core::ptr::null::<u8>();
1275        let mut matchLength = if commonLengthSmaller < commonLengthLarger {
1276            commonLengthSmaller
1277        } else {
1278            commonLengthLarger
1279        };
1280        if dictMode as core::ffi::c_uint == ZSTD_noDict as core::ffi::c_int as core::ffi::c_uint
1281            || dictMode as core::ffi::c_uint
1282                == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1283            || (matchIndex as size_t).wrapping_add(matchLength) >= dictLimit as size_t
1284        {
1285            match_2 = base.offset(matchIndex as isize);
1286            matchIndex >= dictLimit;
1287            matchLength = matchLength.wrapping_add(ZSTD_count(
1288                ip.add(matchLength),
1289                match_2.add(matchLength),
1290                iLimit,
1291            ));
1292        } else {
1293            match_2 = dictBase.offset(matchIndex as isize);
1294            matchLength = matchLength.wrapping_add(ZSTD_count_2segments(
1295                ip.add(matchLength),
1296                match_2.add(matchLength),
1297                iLimit,
1298                dictEnd,
1299                prefixStart,
1300            ));
1301            if (matchIndex as size_t).wrapping_add(matchLength) >= dictLimit as size_t {
1302                match_2 = base.offset(matchIndex as isize);
1303            }
1304        }
1305        if matchLength > bestLength {
1306            if matchLength > matchEndIdx.wrapping_sub(matchIndex) as size_t {
1307                matchEndIdx = matchIndex.wrapping_add(matchLength as u32);
1308            }
1309            bestLength = matchLength;
1310            (*matches.offset(mnum as isize)).off = curr
1311                .wrapping_sub(matchIndex)
1312                .wrapping_add(ZSTD_REP_NUM as u32);
1313            (*matches.offset(mnum as isize)).len = matchLength as u32;
1314            mnum = mnum.wrapping_add(1);
1315            if (matchLength > ZSTD_OPT_NUM as size_t) as core::ffi::c_int
1316                | (ip.add(matchLength) == iLimit) as core::ffi::c_int
1317                != 0
1318            {
1319                if dictMode as core::ffi::c_uint
1320                    == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1321                {
1322                    nbCompares = 0;
1323                }
1324                break;
1325            }
1326        }
1327        if (*match_2.add(matchLength) as core::ffi::c_int)
1328            < *ip.add(matchLength) as core::ffi::c_int
1329        {
1330            *smallerPtr = matchIndex;
1331            commonLengthSmaller = matchLength;
1332            if matchIndex <= btLow {
1333                smallerPtr = &mut dummy32;
1334                break;
1335            } else {
1336                smallerPtr = nextPtr.offset(1);
1337                matchIndex = *nextPtr.offset(1);
1338            }
1339        } else {
1340            *largerPtr = matchIndex;
1341            commonLengthLarger = matchLength;
1342            if matchIndex <= btLow {
1343                largerPtr = &mut dummy32;
1344                break;
1345            } else {
1346                largerPtr = nextPtr;
1347                matchIndex = *nextPtr.offset(0);
1348            }
1349        }
1350        nbCompares = nbCompares.wrapping_sub(1);
1351    }
1352    *largerPtr = 0;
1353    *smallerPtr = *largerPtr;
1354    if dictMode as core::ffi::c_uint == ZSTD_dictMatchState as core::ffi::c_int as core::ffi::c_uint
1355        && nbCompares != 0
1356    {
1357        let dmsH = ZSTD_hashPtr(ip as *const core::ffi::c_void, dmsHashLog, mls);
1358        let mut dictMatchIndex = *((*dms).hashTable).add(dmsH);
1359        let dmsBt: *const u32 = (*dms).chainTable;
1360        commonLengthLarger = 0;
1361        commonLengthSmaller = commonLengthLarger;
1362        while nbCompares != 0 && dictMatchIndex > dmsLowLimit {
1363            let nextPtr_0 = dmsBt.offset((2 * (dictMatchIndex & dmsBtMask)) as isize);
1364            let mut matchLength_0 = if commonLengthSmaller < commonLengthLarger {
1365                commonLengthSmaller
1366            } else {
1367                commonLengthLarger
1368            };
1369            let mut match_3 = dmsBase.offset(dictMatchIndex as isize);
1370            matchLength_0 = matchLength_0.wrapping_add(ZSTD_count_2segments(
1371                ip.add(matchLength_0),
1372                match_3.add(matchLength_0),
1373                iLimit,
1374                dmsEnd,
1375                prefixStart,
1376            ));
1377            if (dictMatchIndex as size_t).wrapping_add(matchLength_0) >= dmsHighLimit as size_t {
1378                match_3 = base
1379                    .offset(dictMatchIndex as isize)
1380                    .offset(dmsIndexDelta as isize);
1381            }
1382            if matchLength_0 > bestLength {
1383                matchIndex = dictMatchIndex.wrapping_add(dmsIndexDelta);
1384                if matchLength_0 > matchEndIdx.wrapping_sub(matchIndex) as size_t {
1385                    matchEndIdx = matchIndex.wrapping_add(matchLength_0 as u32);
1386                }
1387                bestLength = matchLength_0;
1388                (*matches.offset(mnum as isize)).off = curr
1389                    .wrapping_sub(matchIndex)
1390                    .wrapping_add(ZSTD_REP_NUM as u32);
1391                (*matches.offset(mnum as isize)).len = matchLength_0 as u32;
1392                mnum = mnum.wrapping_add(1);
1393                if (matchLength_0 > ZSTD_OPT_NUM as size_t) as core::ffi::c_int
1394                    | (ip.add(matchLength_0) == iLimit) as core::ffi::c_int
1395                    != 0
1396                {
1397                    break;
1398                }
1399            }
1400            if dictMatchIndex <= dmsBtLow {
1401                break;
1402            }
1403            if (*match_3.add(matchLength_0) as core::ffi::c_int)
1404                < *ip.add(matchLength_0) as core::ffi::c_int
1405            {
1406                commonLengthSmaller = matchLength_0;
1407                dictMatchIndex = *nextPtr_0.offset(1);
1408            } else {
1409                commonLengthLarger = matchLength_0;
1410                dictMatchIndex = *nextPtr_0.offset(0);
1411            }
1412            nbCompares = nbCompares.wrapping_sub(1);
1413        }
1414    }
1415    (*ms).nextToUpdate = matchEndIdx.wrapping_sub(8);
1416    mnum
1417}
1418#[inline(always)]
1419unsafe fn ZSTD_btGetAllMatches_internal(
1420    matches: *mut ZSTD_match_t,
1421    ms: *mut ZSTD_MatchState_t,
1422    nextToUpdate3: *mut u32,
1423    ip: *const u8,
1424    iHighLimit: *const u8,
1425    rep: *const u32,
1426    ll0: u32,
1427    lengthToBeat: u32,
1428    dictMode: ZSTD_dictMode_e,
1429    mls: u32,
1430) -> u32 {
1431    if ip < ((*ms).window.base).offset((*ms).nextToUpdate as isize) {
1432        return 0;
1433    }
1434    ZSTD_updateTree_internal(ms, ip, iHighLimit, mls, dictMode);
1435    ZSTD_insertBtAndGetAllMatches(
1436        matches,
1437        ms,
1438        nextToUpdate3,
1439        ip,
1440        iHighLimit,
1441        dictMode,
1442        rep,
1443        ll0,
1444        lengthToBeat,
1445        mls,
1446    )
1447}
1448unsafe fn ZSTD_btGetAllMatches_noDict_5(
1449    matches: *mut ZSTD_match_t,
1450    ms: *mut ZSTD_MatchState_t,
1451    nextToUpdate3: *mut u32,
1452    ip: *const u8,
1453    iHighLimit: *const u8,
1454    rep: *const u32,
1455    ll0: u32,
1456    lengthToBeat: u32,
1457) -> u32 {
1458    ZSTD_btGetAllMatches_internal(
1459        matches,
1460        ms,
1461        nextToUpdate3,
1462        ip,
1463        iHighLimit,
1464        rep,
1465        ll0,
1466        lengthToBeat,
1467        ZSTD_noDict,
1468        5,
1469    )
1470}
1471unsafe fn ZSTD_btGetAllMatches_noDict_6(
1472    matches: *mut ZSTD_match_t,
1473    ms: *mut ZSTD_MatchState_t,
1474    nextToUpdate3: *mut u32,
1475    ip: *const u8,
1476    iHighLimit: *const u8,
1477    rep: *const u32,
1478    ll0: u32,
1479    lengthToBeat: u32,
1480) -> u32 {
1481    ZSTD_btGetAllMatches_internal(
1482        matches,
1483        ms,
1484        nextToUpdate3,
1485        ip,
1486        iHighLimit,
1487        rep,
1488        ll0,
1489        lengthToBeat,
1490        ZSTD_noDict,
1491        6,
1492    )
1493}
1494unsafe fn ZSTD_btGetAllMatches_noDict_4(
1495    matches: *mut ZSTD_match_t,
1496    ms: *mut ZSTD_MatchState_t,
1497    nextToUpdate3: *mut u32,
1498    ip: *const u8,
1499    iHighLimit: *const u8,
1500    rep: *const u32,
1501    ll0: u32,
1502    lengthToBeat: u32,
1503) -> u32 {
1504    ZSTD_btGetAllMatches_internal(
1505        matches,
1506        ms,
1507        nextToUpdate3,
1508        ip,
1509        iHighLimit,
1510        rep,
1511        ll0,
1512        lengthToBeat,
1513        ZSTD_noDict,
1514        4,
1515    )
1516}
1517unsafe fn ZSTD_btGetAllMatches_noDict_3(
1518    matches: *mut ZSTD_match_t,
1519    ms: *mut ZSTD_MatchState_t,
1520    nextToUpdate3: *mut u32,
1521    ip: *const u8,
1522    iHighLimit: *const u8,
1523    rep: *const u32,
1524    ll0: u32,
1525    lengthToBeat: u32,
1526) -> u32 {
1527    ZSTD_btGetAllMatches_internal(
1528        matches,
1529        ms,
1530        nextToUpdate3,
1531        ip,
1532        iHighLimit,
1533        rep,
1534        ll0,
1535        lengthToBeat,
1536        ZSTD_noDict,
1537        3,
1538    )
1539}
1540unsafe fn ZSTD_btGetAllMatches_extDict_5(
1541    matches: *mut ZSTD_match_t,
1542    ms: *mut ZSTD_MatchState_t,
1543    nextToUpdate3: *mut u32,
1544    ip: *const u8,
1545    iHighLimit: *const u8,
1546    rep: *const u32,
1547    ll0: u32,
1548    lengthToBeat: u32,
1549) -> u32 {
1550    ZSTD_btGetAllMatches_internal(
1551        matches,
1552        ms,
1553        nextToUpdate3,
1554        ip,
1555        iHighLimit,
1556        rep,
1557        ll0,
1558        lengthToBeat,
1559        ZSTD_extDict,
1560        5,
1561    )
1562}
1563unsafe fn ZSTD_btGetAllMatches_extDict_6(
1564    matches: *mut ZSTD_match_t,
1565    ms: *mut ZSTD_MatchState_t,
1566    nextToUpdate3: *mut u32,
1567    ip: *const u8,
1568    iHighLimit: *const u8,
1569    rep: *const u32,
1570    ll0: u32,
1571    lengthToBeat: u32,
1572) -> u32 {
1573    ZSTD_btGetAllMatches_internal(
1574        matches,
1575        ms,
1576        nextToUpdate3,
1577        ip,
1578        iHighLimit,
1579        rep,
1580        ll0,
1581        lengthToBeat,
1582        ZSTD_extDict,
1583        6,
1584    )
1585}
1586unsafe fn ZSTD_btGetAllMatches_extDict_4(
1587    matches: *mut ZSTD_match_t,
1588    ms: *mut ZSTD_MatchState_t,
1589    nextToUpdate3: *mut u32,
1590    ip: *const u8,
1591    iHighLimit: *const u8,
1592    rep: *const u32,
1593    ll0: u32,
1594    lengthToBeat: u32,
1595) -> u32 {
1596    ZSTD_btGetAllMatches_internal(
1597        matches,
1598        ms,
1599        nextToUpdate3,
1600        ip,
1601        iHighLimit,
1602        rep,
1603        ll0,
1604        lengthToBeat,
1605        ZSTD_extDict,
1606        4,
1607    )
1608}
1609unsafe fn ZSTD_btGetAllMatches_extDict_3(
1610    matches: *mut ZSTD_match_t,
1611    ms: *mut ZSTD_MatchState_t,
1612    nextToUpdate3: *mut u32,
1613    ip: *const u8,
1614    iHighLimit: *const u8,
1615    rep: *const u32,
1616    ll0: u32,
1617    lengthToBeat: u32,
1618) -> u32 {
1619    ZSTD_btGetAllMatches_internal(
1620        matches,
1621        ms,
1622        nextToUpdate3,
1623        ip,
1624        iHighLimit,
1625        rep,
1626        ll0,
1627        lengthToBeat,
1628        ZSTD_extDict,
1629        3,
1630    )
1631}
1632unsafe fn ZSTD_btGetAllMatches_dictMatchState_5(
1633    matches: *mut ZSTD_match_t,
1634    ms: *mut ZSTD_MatchState_t,
1635    nextToUpdate3: *mut u32,
1636    ip: *const u8,
1637    iHighLimit: *const u8,
1638    rep: *const u32,
1639    ll0: u32,
1640    lengthToBeat: u32,
1641) -> u32 {
1642    ZSTD_btGetAllMatches_internal(
1643        matches,
1644        ms,
1645        nextToUpdate3,
1646        ip,
1647        iHighLimit,
1648        rep,
1649        ll0,
1650        lengthToBeat,
1651        ZSTD_dictMatchState,
1652        5,
1653    )
1654}
1655unsafe fn ZSTD_btGetAllMatches_dictMatchState_6(
1656    matches: *mut ZSTD_match_t,
1657    ms: *mut ZSTD_MatchState_t,
1658    nextToUpdate3: *mut u32,
1659    ip: *const u8,
1660    iHighLimit: *const u8,
1661    rep: *const u32,
1662    ll0: u32,
1663    lengthToBeat: u32,
1664) -> u32 {
1665    ZSTD_btGetAllMatches_internal(
1666        matches,
1667        ms,
1668        nextToUpdate3,
1669        ip,
1670        iHighLimit,
1671        rep,
1672        ll0,
1673        lengthToBeat,
1674        ZSTD_dictMatchState,
1675        6,
1676    )
1677}
1678unsafe fn ZSTD_btGetAllMatches_dictMatchState_3(
1679    matches: *mut ZSTD_match_t,
1680    ms: *mut ZSTD_MatchState_t,
1681    nextToUpdate3: *mut u32,
1682    ip: *const u8,
1683    iHighLimit: *const u8,
1684    rep: *const u32,
1685    ll0: u32,
1686    lengthToBeat: u32,
1687) -> u32 {
1688    ZSTD_btGetAllMatches_internal(
1689        matches,
1690        ms,
1691        nextToUpdate3,
1692        ip,
1693        iHighLimit,
1694        rep,
1695        ll0,
1696        lengthToBeat,
1697        ZSTD_dictMatchState,
1698        3,
1699    )
1700}
1701unsafe fn ZSTD_btGetAllMatches_dictMatchState_4(
1702    matches: *mut ZSTD_match_t,
1703    ms: *mut ZSTD_MatchState_t,
1704    nextToUpdate3: *mut u32,
1705    ip: *const u8,
1706    iHighLimit: *const u8,
1707    rep: *const u32,
1708    ll0: u32,
1709    lengthToBeat: u32,
1710) -> u32 {
1711    ZSTD_btGetAllMatches_internal(
1712        matches,
1713        ms,
1714        nextToUpdate3,
1715        ip,
1716        iHighLimit,
1717        rep,
1718        ll0,
1719        lengthToBeat,
1720        ZSTD_dictMatchState,
1721        4,
1722    )
1723}
1724unsafe fn ZSTD_selectBtGetAllMatches(
1725    ms: *const ZSTD_MatchState_t,
1726    dictMode: ZSTD_dictMode_e,
1727) -> ZSTD_getAllMatchesFn {
1728    let getAllMatchesFns: [[ZSTD_getAllMatchesFn; 4]; 3] = [
1729        [
1730            Some(
1731                ZSTD_btGetAllMatches_noDict_3
1732                    as unsafe fn(
1733                        *mut ZSTD_match_t,
1734                        *mut ZSTD_MatchState_t,
1735                        *mut u32,
1736                        *const u8,
1737                        *const u8,
1738                        *const u32,
1739                        u32,
1740                        u32,
1741                    ) -> u32,
1742            ),
1743            Some(
1744                ZSTD_btGetAllMatches_noDict_4
1745                    as unsafe fn(
1746                        *mut ZSTD_match_t,
1747                        *mut ZSTD_MatchState_t,
1748                        *mut u32,
1749                        *const u8,
1750                        *const u8,
1751                        *const u32,
1752                        u32,
1753                        u32,
1754                    ) -> u32,
1755            ),
1756            Some(
1757                ZSTD_btGetAllMatches_noDict_5
1758                    as unsafe fn(
1759                        *mut ZSTD_match_t,
1760                        *mut ZSTD_MatchState_t,
1761                        *mut u32,
1762                        *const u8,
1763                        *const u8,
1764                        *const u32,
1765                        u32,
1766                        u32,
1767                    ) -> u32,
1768            ),
1769            Some(
1770                ZSTD_btGetAllMatches_noDict_6
1771                    as unsafe fn(
1772                        *mut ZSTD_match_t,
1773                        *mut ZSTD_MatchState_t,
1774                        *mut u32,
1775                        *const u8,
1776                        *const u8,
1777                        *const u32,
1778                        u32,
1779                        u32,
1780                    ) -> u32,
1781            ),
1782        ],
1783        [
1784            Some(
1785                ZSTD_btGetAllMatches_extDict_3
1786                    as unsafe fn(
1787                        *mut ZSTD_match_t,
1788                        *mut ZSTD_MatchState_t,
1789                        *mut u32,
1790                        *const u8,
1791                        *const u8,
1792                        *const u32,
1793                        u32,
1794                        u32,
1795                    ) -> u32,
1796            ),
1797            Some(
1798                ZSTD_btGetAllMatches_extDict_4
1799                    as unsafe fn(
1800                        *mut ZSTD_match_t,
1801                        *mut ZSTD_MatchState_t,
1802                        *mut u32,
1803                        *const u8,
1804                        *const u8,
1805                        *const u32,
1806                        u32,
1807                        u32,
1808                    ) -> u32,
1809            ),
1810            Some(
1811                ZSTD_btGetAllMatches_extDict_5
1812                    as unsafe fn(
1813                        *mut ZSTD_match_t,
1814                        *mut ZSTD_MatchState_t,
1815                        *mut u32,
1816                        *const u8,
1817                        *const u8,
1818                        *const u32,
1819                        u32,
1820                        u32,
1821                    ) -> u32,
1822            ),
1823            Some(
1824                ZSTD_btGetAllMatches_extDict_6
1825                    as unsafe fn(
1826                        *mut ZSTD_match_t,
1827                        *mut ZSTD_MatchState_t,
1828                        *mut u32,
1829                        *const u8,
1830                        *const u8,
1831                        *const u32,
1832                        u32,
1833                        u32,
1834                    ) -> u32,
1835            ),
1836        ],
1837        [
1838            Some(
1839                ZSTD_btGetAllMatches_dictMatchState_3
1840                    as unsafe fn(
1841                        *mut ZSTD_match_t,
1842                        *mut ZSTD_MatchState_t,
1843                        *mut u32,
1844                        *const u8,
1845                        *const u8,
1846                        *const u32,
1847                        u32,
1848                        u32,
1849                    ) -> u32,
1850            ),
1851            Some(
1852                ZSTD_btGetAllMatches_dictMatchState_4
1853                    as unsafe fn(
1854                        *mut ZSTD_match_t,
1855                        *mut ZSTD_MatchState_t,
1856                        *mut u32,
1857                        *const u8,
1858                        *const u8,
1859                        *const u32,
1860                        u32,
1861                        u32,
1862                    ) -> u32,
1863            ),
1864            Some(
1865                ZSTD_btGetAllMatches_dictMatchState_5
1866                    as unsafe fn(
1867                        *mut ZSTD_match_t,
1868                        *mut ZSTD_MatchState_t,
1869                        *mut u32,
1870                        *const u8,
1871                        *const u8,
1872                        *const u32,
1873                        u32,
1874                        u32,
1875                    ) -> u32,
1876            ),
1877            Some(
1878                ZSTD_btGetAllMatches_dictMatchState_6
1879                    as unsafe fn(
1880                        *mut ZSTD_match_t,
1881                        *mut ZSTD_MatchState_t,
1882                        *mut u32,
1883                        *const u8,
1884                        *const u8,
1885                        *const u32,
1886                        u32,
1887                        u32,
1888                    ) -> u32,
1889            ),
1890        ],
1891    ];
1892    let mls = if 3
1893        > (if (*ms).cParams.minMatch < 6 {
1894            (*ms).cParams.minMatch
1895        } else {
1896            6
1897        }) {
1898        3
1899    } else if (*ms).cParams.minMatch < 6 {
1900        (*ms).cParams.minMatch
1901    } else {
1902        6
1903    };
1904    *(*getAllMatchesFns
1905        .as_ptr()
1906        .offset(dictMode as core::ffi::c_int as isize))
1907    .as_ptr()
1908    .offset(mls.wrapping_sub(3) as isize)
1909}
1910unsafe fn ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore: *mut RawSeqStore_t, nbBytes: size_t) {
1911    let mut currPos = ((*rawSeqStore).posInSequence).wrapping_add(nbBytes) as u32;
1912    while currPos != 0 && (*rawSeqStore).pos < (*rawSeqStore).size {
1913        let currSeq = *((*rawSeqStore).seq).add((*rawSeqStore).pos);
1914        if currPos >= (currSeq.litLength).wrapping_add(currSeq.matchLength) {
1915            currPos = currPos.wrapping_sub((currSeq.litLength).wrapping_add(currSeq.matchLength));
1916            (*rawSeqStore).pos = ((*rawSeqStore).pos).wrapping_add(1);
1917            (*rawSeqStore).pos;
1918        } else {
1919            (*rawSeqStore).posInSequence = currPos as size_t;
1920            break;
1921        }
1922    }
1923    if currPos == 0 || (*rawSeqStore).pos == (*rawSeqStore).size {
1924        (*rawSeqStore).posInSequence = 0;
1925    }
1926}
1927unsafe fn ZSTD_opt_getNextMatchAndUpdateSeqStore(
1928    optLdm: *mut ZSTD_optLdm_t,
1929    currPosInBlock: u32,
1930    blockBytesRemaining: u32,
1931) {
1932    let mut currSeq = rawSeq {
1933        offset: 0,
1934        litLength: 0,
1935        matchLength: 0,
1936    };
1937    let mut currBlockEndPos: u32 = 0;
1938    let mut literalsBytesRemaining: u32 = 0;
1939    let mut matchBytesRemaining: u32 = 0;
1940    if (*optLdm).seqStore.size == 0 || (*optLdm).seqStore.pos >= (*optLdm).seqStore.size {
1941        (*optLdm).startPosInBlock = UINT_MAX;
1942        (*optLdm).endPosInBlock = UINT_MAX;
1943        return;
1944    }
1945    currSeq = *((*optLdm).seqStore.seq).add((*optLdm).seqStore.pos);
1946    currBlockEndPos = currPosInBlock.wrapping_add(blockBytesRemaining);
1947    literalsBytesRemaining = if (*optLdm).seqStore.posInSequence < currSeq.litLength as size_t {
1948        (currSeq.litLength).wrapping_sub((*optLdm).seqStore.posInSequence as u32)
1949    } else {
1950        0
1951    };
1952    matchBytesRemaining = if literalsBytesRemaining == 0 {
1953        (currSeq.matchLength)
1954            .wrapping_sub(((*optLdm).seqStore.posInSequence as u32).wrapping_sub(currSeq.litLength))
1955    } else {
1956        currSeq.matchLength
1957    };
1958    if literalsBytesRemaining >= blockBytesRemaining {
1959        (*optLdm).startPosInBlock = UINT_MAX;
1960        (*optLdm).endPosInBlock = UINT_MAX;
1961        ZSTD_optLdm_skipRawSeqStoreBytes(&mut (*optLdm).seqStore, blockBytesRemaining as size_t);
1962        return;
1963    }
1964    (*optLdm).startPosInBlock = currPosInBlock.wrapping_add(literalsBytesRemaining);
1965    (*optLdm).endPosInBlock = ((*optLdm).startPosInBlock).wrapping_add(matchBytesRemaining);
1966    (*optLdm).offset = currSeq.offset;
1967    if (*optLdm).endPosInBlock > currBlockEndPos {
1968        (*optLdm).endPosInBlock = currBlockEndPos;
1969        ZSTD_optLdm_skipRawSeqStoreBytes(
1970            &mut (*optLdm).seqStore,
1971            currBlockEndPos.wrapping_sub(currPosInBlock) as size_t,
1972        );
1973    } else {
1974        ZSTD_optLdm_skipRawSeqStoreBytes(
1975            &mut (*optLdm).seqStore,
1976            literalsBytesRemaining.wrapping_add(matchBytesRemaining) as size_t,
1977        );
1978    };
1979}
1980unsafe fn ZSTD_optLdm_maybeAddMatch(
1981    matches: *mut ZSTD_match_t,
1982    nbMatches: *mut u32,
1983    optLdm: *const ZSTD_optLdm_t,
1984    currPosInBlock: u32,
1985    minMatch: u32,
1986) {
1987    let posDiff = currPosInBlock.wrapping_sub((*optLdm).startPosInBlock);
1988    let candidateMatchLength = ((*optLdm).endPosInBlock)
1989        .wrapping_sub((*optLdm).startPosInBlock)
1990        .wrapping_sub(posDiff);
1991    if currPosInBlock < (*optLdm).startPosInBlock
1992        || currPosInBlock >= (*optLdm).endPosInBlock
1993        || candidateMatchLength < minMatch
1994    {
1995        return;
1996    }
1997    if *nbMatches == 0
1998        || candidateMatchLength > (*matches.offset((*nbMatches).wrapping_sub(1) as isize)).len
1999            && *nbMatches < ZSTD_OPT_NUM as u32
2000    {
2001        let candidateOffBase = ((*optLdm).offset).wrapping_add(ZSTD_REP_NUM as u32);
2002        (*matches.offset(*nbMatches as isize)).len = candidateMatchLength;
2003        (*matches.offset(*nbMatches as isize)).off = candidateOffBase;
2004        *nbMatches = (*nbMatches).wrapping_add(1);
2005    }
2006}
2007unsafe fn ZSTD_optLdm_processMatchCandidate(
2008    optLdm: *mut ZSTD_optLdm_t,
2009    matches: *mut ZSTD_match_t,
2010    nbMatches: *mut u32,
2011    currPosInBlock: u32,
2012    remainingBytes: u32,
2013    minMatch: u32,
2014) {
2015    if (*optLdm).seqStore.size == 0 || (*optLdm).seqStore.pos >= (*optLdm).seqStore.size {
2016        return;
2017    }
2018    if currPosInBlock >= (*optLdm).endPosInBlock {
2019        if currPosInBlock > (*optLdm).endPosInBlock {
2020            let posOvershoot = currPosInBlock.wrapping_sub((*optLdm).endPosInBlock);
2021            ZSTD_optLdm_skipRawSeqStoreBytes(&mut (*optLdm).seqStore, posOvershoot as size_t);
2022        }
2023        ZSTD_opt_getNextMatchAndUpdateSeqStore(optLdm, currPosInBlock, remainingBytes);
2024    }
2025    ZSTD_optLdm_maybeAddMatch(matches, nbMatches, optLdm, currPosInBlock, minMatch);
2026}
2027#[inline(always)]
2028unsafe fn ZSTD_compressBlock_opt_generic(
2029    ms: *mut ZSTD_MatchState_t,
2030    seqStore: *mut SeqStore_t,
2031    rep: *mut u32,
2032    src: *const core::ffi::c_void,
2033    srcSize: size_t,
2034    optLevel: core::ffi::c_int,
2035    dictMode: ZSTD_dictMode_e,
2036) -> size_t {
2037    let mut current_block: u64;
2038    let optStatePtr: *mut optState_t = &mut (*ms).opt;
2039    let istart = src as *const u8;
2040    let mut ip = istart;
2041    let mut anchor = istart;
2042    let iend = istart.add(srcSize);
2043    let ilimit = iend.offset(-(8));
2044    let base = (*ms).window.base;
2045    let prefixStart = base.offset((*ms).window.dictLimit as isize);
2046    let cParams: *const ZSTD_compressionParameters = &mut (*ms).cParams;
2047    let getAllMatches = ZSTD_selectBtGetAllMatches(ms, dictMode);
2048    let sufficient_len = if (*cParams).targetLength < (((1) << 12) - 1) as core::ffi::c_uint {
2049        (*cParams).targetLength
2050    } else {
2051        (((1) << 12) - 1) as core::ffi::c_uint
2052    };
2053    let minMatch = (if (*cParams).minMatch == 3 { 3 } else { 4 }) as u32;
2054    let mut nextToUpdate3 = (*ms).nextToUpdate;
2055    let opt = (*optStatePtr).priceTable;
2056    let matches = (*optStatePtr).matchTable;
2057    let mut lastStretch = ZSTD_optimal_t {
2058        price: 0,
2059        off: 0,
2060        mlen: 0,
2061        litlen: 0,
2062        rep: [0; 3],
2063    };
2064    let mut optLdm = ZSTD_optLdm_t {
2065        seqStore: RawSeqStore_t {
2066            seq: core::ptr::null_mut::<rawSeq>(),
2067            pos: 0,
2068            posInSequence: 0,
2069            size: 0,
2070            capacity: 0,
2071        },
2072        startPosInBlock: 0,
2073        endPosInBlock: 0,
2074        offset: 0,
2075    };
2076    ptr::write_bytes(
2077        &mut lastStretch as *mut ZSTD_optimal_t as *mut u8,
2078        0,
2079        ::core::mem::size_of::<ZSTD_optimal_t>(),
2080    );
2081    optLdm.seqStore = if !((*ms).ldmSeqStore).is_null() {
2082        *(*ms).ldmSeqStore
2083    } else {
2084        kNullRawSeqStore
2085    };
2086    optLdm.offset = 0;
2087    optLdm.startPosInBlock = optLdm.offset;
2088    optLdm.endPosInBlock = optLdm.startPosInBlock;
2089    ZSTD_opt_getNextMatchAndUpdateSeqStore(
2090        &mut optLdm,
2091        ip.offset_from(istart) as core::ffi::c_long as u32,
2092        iend.offset_from(ip) as core::ffi::c_long as u32,
2093    );
2094    ZSTD_rescaleFreqs(optStatePtr, src as *const u8, srcSize, optLevel);
2095    ip = ip.offset((ip == prefixStart) as core::ffi::c_int as isize);
2096    while ip < ilimit {
2097        let mut cur: u32 = 0;
2098        let mut last_pos = 0;
2099        let litlen = ip.offset_from(anchor) as core::ffi::c_long as u32;
2100        let ll0 = (litlen == 0) as core::ffi::c_int as u32;
2101        let mut nbMatches = getAllMatches.unwrap_unchecked()(
2102            matches,
2103            ms,
2104            &mut nextToUpdate3,
2105            ip,
2106            iend,
2107            rep as *const u32,
2108            ll0,
2109            minMatch,
2110        );
2111        ZSTD_optLdm_processMatchCandidate(
2112            &mut optLdm,
2113            matches,
2114            &mut nbMatches,
2115            ip.offset_from(istart) as core::ffi::c_long as u32,
2116            iend.offset_from(ip) as core::ffi::c_long as u32,
2117            minMatch,
2118        );
2119        if nbMatches == 0 {
2120            ip = ip.offset(1);
2121        } else {
2122            (*opt.offset(0)).mlen = 0;
2123            (*opt.offset(0)).litlen = litlen;
2124            (*opt.offset(0)).price =
2125                ZSTD_litLengthPrice(litlen, optStatePtr, optLevel) as core::ffi::c_int;
2126            libc::memcpy(
2127                &mut (*opt.offset(0)).rep as *mut [u32; 3] as *mut core::ffi::c_void,
2128                rep as *const core::ffi::c_void,
2129                ::core::mem::size_of::<[u32; 3]>() as core::ffi::c_ulong as libc::size_t,
2130            );
2131            let maxML = (*matches.offset(nbMatches.wrapping_sub(1) as isize)).len;
2132            let maxOffBase = (*matches.offset(nbMatches.wrapping_sub(1) as isize)).off;
2133            if maxML > sufficient_len {
2134                lastStretch.litlen = 0;
2135                lastStretch.mlen = maxML;
2136                lastStretch.off = maxOffBase;
2137                cur = 0;
2138                last_pos = maxML;
2139            } else {
2140                let mut pos: u32 = 0;
2141                let mut matchNb: u32 = 0;
2142                pos = 1;
2143                while pos < minMatch {
2144                    (*opt.offset(pos as isize)).price = ZSTD_MAX_PRICE;
2145                    (*opt.offset(pos as isize)).mlen = 0;
2146                    (*opt.offset(pos as isize)).litlen = litlen.wrapping_add(pos);
2147                    pos = pos.wrapping_add(1);
2148                }
2149                matchNb = 0;
2150                while matchNb < nbMatches {
2151                    let offBase = (*matches.offset(matchNb as isize)).off;
2152                    let end = (*matches.offset(matchNb as isize)).len;
2153                    while pos <= end {
2154                        let matchPrice = ZSTD_getMatchPrice(offBase, pos, optStatePtr, optLevel)
2155                            as core::ffi::c_int;
2156                        let sequencePrice = (*opt.offset(0)).price + matchPrice;
2157                        (*opt.offset(pos as isize)).mlen = pos;
2158                        (*opt.offset(pos as isize)).off = offBase;
2159                        (*opt.offset(pos as isize)).litlen = 0;
2160                        (*opt.offset(pos as isize)).price = sequencePrice
2161                            + ZSTD_litLengthPrice(0, optStatePtr, optLevel) as core::ffi::c_int;
2162                        pos = pos.wrapping_add(1);
2163                    }
2164                    matchNb = matchNb.wrapping_add(1);
2165                }
2166                last_pos = pos.wrapping_sub(1);
2167                (*opt.offset(pos as isize)).price = ZSTD_MAX_PRICE;
2168                cur = 1;
2169                loop {
2170                    if cur > last_pos {
2171                        current_block = 10357520176418200368;
2172                        break;
2173                    }
2174                    let inr = ip.offset(cur as isize);
2175                    let litlen_0 =
2176                        ((*opt.offset(cur.wrapping_sub(1) as isize)).litlen).wrapping_add(1);
2177                    let price = (*opt.offset(cur.wrapping_sub(1) as isize)).price
2178                        + ZSTD_rawLiteralsCost(
2179                            ip.offset(cur as isize).offset(-(1)),
2180                            1,
2181                            optStatePtr,
2182                            optLevel,
2183                        ) as core::ffi::c_int
2184                        + (ZSTD_litLengthPrice(litlen_0, optStatePtr, optLevel)
2185                            as core::ffi::c_int
2186                            - ZSTD_litLengthPrice(litlen_0.wrapping_sub(1), optStatePtr, optLevel)
2187                                as core::ffi::c_int);
2188                    if price <= (*opt.offset(cur as isize)).price {
2189                        let prevMatch = *opt.offset(cur as isize);
2190                        *opt.offset(cur as isize) = *opt.offset(cur.wrapping_sub(1) as isize);
2191                        (*opt.offset(cur as isize)).litlen = litlen_0;
2192                        (*opt.offset(cur as isize)).price = price;
2193                        if optLevel >= 1
2194                            && prevMatch.litlen == 0
2195                            && (ZSTD_litLengthPrice(1, optStatePtr, optLevel) as core::ffi::c_int
2196                                - ZSTD_litLengthPrice((1 - 1) as u32, optStatePtr, optLevel)
2197                                    as core::ffi::c_int)
2198                                < 0
2199                            && (ip.offset(cur as isize) < iend) as core::ffi::c_int
2200                                as core::ffi::c_long
2201                                != 0
2202                        {
2203                            let with1literal = prevMatch.price
2204                                + ZSTD_rawLiteralsCost(
2205                                    ip.offset(cur as isize),
2206                                    1,
2207                                    optStatePtr,
2208                                    optLevel,
2209                                ) as core::ffi::c_int
2210                                + (ZSTD_litLengthPrice(1, optStatePtr, optLevel)
2211                                    as core::ffi::c_int
2212                                    - ZSTD_litLengthPrice((1 - 1) as u32, optStatePtr, optLevel)
2213                                        as core::ffi::c_int);
2214                            let withMoreLiterals = price
2215                                + ZSTD_rawLiteralsCost(
2216                                    ip.offset(cur as isize),
2217                                    1,
2218                                    optStatePtr,
2219                                    optLevel,
2220                                ) as core::ffi::c_int
2221                                + (ZSTD_litLengthPrice(
2222                                    litlen_0.wrapping_add(1),
2223                                    optStatePtr,
2224                                    optLevel,
2225                                ) as core::ffi::c_int
2226                                    - ZSTD_litLengthPrice(
2227                                        litlen_0.wrapping_add(1).wrapping_sub(1),
2228                                        optStatePtr,
2229                                        optLevel,
2230                                    ) as core::ffi::c_int);
2231                            if with1literal < withMoreLiterals
2232                                && with1literal < (*opt.offset(cur.wrapping_add(1) as isize)).price
2233                            {
2234                                let prev = cur.wrapping_sub(prevMatch.mlen);
2235                                let newReps = ZSTD_newRep(
2236                                    ((*opt.offset(prev as isize)).rep).as_mut_ptr() as *const u32,
2237                                    prevMatch.off,
2238                                    ((*opt.offset(prev as isize)).litlen == 0) as core::ffi::c_int
2239                                        as u32,
2240                                );
2241                                *opt.offset(cur.wrapping_add(1) as isize) = prevMatch;
2242                                libc::memcpy(
2243                                    ((*opt.offset(cur.wrapping_add(1) as isize)).rep).as_mut_ptr()
2244                                        as *mut core::ffi::c_void,
2245                                    &newReps as *const Repcodes_t as *const core::ffi::c_void,
2246                                    ::core::mem::size_of::<Repcodes_t>() as core::ffi::c_ulong
2247                                        as libc::size_t,
2248                                );
2249                                (*opt.offset(cur.wrapping_add(1) as isize)).litlen = 1;
2250                                (*opt.offset(cur.wrapping_add(1) as isize)).price = with1literal;
2251                                if last_pos < cur.wrapping_add(1) {
2252                                    last_pos = cur.wrapping_add(1);
2253                                }
2254                            }
2255                        }
2256                    }
2257                    if (*opt.offset(cur as isize)).litlen == 0 {
2258                        let prev_0 = cur.wrapping_sub((*opt.offset(cur as isize)).mlen);
2259                        let newReps_0 = ZSTD_newRep(
2260                            ((*opt.offset(prev_0 as isize)).rep).as_mut_ptr() as *const u32,
2261                            (*opt.offset(cur as isize)).off,
2262                            ((*opt.offset(prev_0 as isize)).litlen == 0) as core::ffi::c_int as u32,
2263                        );
2264                        libc::memcpy(
2265                            ((*opt.offset(cur as isize)).rep).as_mut_ptr()
2266                                as *mut core::ffi::c_void,
2267                            &newReps_0 as *const Repcodes_t as *const core::ffi::c_void,
2268                            ::core::mem::size_of::<Repcodes_t>() as core::ffi::c_ulong
2269                                as libc::size_t,
2270                        );
2271                    }
2272                    if inr <= ilimit {
2273                        if cur == last_pos {
2274                            current_block = 10357520176418200368;
2275                            break;
2276                        }
2277                        if !(optLevel == 0
2278                            && (*opt.offset(cur.wrapping_add(1) as isize)).price
2279                                <= (*opt.offset(cur as isize)).price + BITCOST_MULTIPLIER / 2)
2280                        {
2281                            let ll0_0 = ((*opt.offset(cur as isize)).litlen == 0)
2282                                as core::ffi::c_int as u32;
2283                            let previousPrice = (*opt.offset(cur as isize)).price;
2284                            let basePrice = previousPrice
2285                                + ZSTD_litLengthPrice(0, optStatePtr, optLevel) as core::ffi::c_int;
2286                            let mut nbMatches_0 = getAllMatches.unwrap_unchecked()(
2287                                matches,
2288                                ms,
2289                                &mut nextToUpdate3,
2290                                inr,
2291                                iend,
2292                                ((*opt.offset(cur as isize)).rep).as_mut_ptr() as *const u32,
2293                                ll0_0,
2294                                minMatch,
2295                            );
2296                            let mut matchNb_0: u32 = 0;
2297                            ZSTD_optLdm_processMatchCandidate(
2298                                &mut optLdm,
2299                                matches,
2300                                &mut nbMatches_0,
2301                                inr.offset_from(istart) as core::ffi::c_long as u32,
2302                                iend.offset_from(inr) as core::ffi::c_long as u32,
2303                                minMatch,
2304                            );
2305                            if nbMatches_0 != 0 {
2306                                let longestML =
2307                                    (*matches.offset(nbMatches_0.wrapping_sub(1) as isize)).len;
2308                                if longestML > sufficient_len
2309                                    || cur.wrapping_add(longestML) >= ZSTD_OPT_NUM as u32
2310                                    || ip.offset(cur as isize).offset(longestML as isize) >= iend
2311                                {
2312                                    lastStretch.mlen = longestML;
2313                                    lastStretch.off =
2314                                        (*matches.offset(nbMatches_0.wrapping_sub(1) as isize)).off;
2315                                    lastStretch.litlen = 0;
2316                                    last_pos = cur.wrapping_add(longestML);
2317                                    current_block = 12608488225262500095;
2318                                    break;
2319                                } else {
2320                                    matchNb_0 = 0;
2321                                    while matchNb_0 < nbMatches_0 {
2322                                        let offset = (*matches.offset(matchNb_0 as isize)).off;
2323                                        let lastML = (*matches.offset(matchNb_0 as isize)).len;
2324                                        let startML = if matchNb_0 > 0 {
2325                                            ((*matches.offset(matchNb_0.wrapping_sub(1) as isize))
2326                                                .len)
2327                                                .wrapping_add(1)
2328                                        } else {
2329                                            minMatch
2330                                        };
2331                                        let mut mlen: u32 = 0;
2332                                        mlen = lastML;
2333                                        while mlen >= startML {
2334                                            let pos_0 = cur.wrapping_add(mlen);
2335                                            let price_0 = basePrice
2336                                                + ZSTD_getMatchPrice(
2337                                                    offset,
2338                                                    mlen,
2339                                                    optStatePtr,
2340                                                    optLevel,
2341                                                )
2342                                                    as core::ffi::c_int;
2343                                            if pos_0 > last_pos
2344                                                || price_0 < (*opt.offset(pos_0 as isize)).price
2345                                            {
2346                                                while last_pos < pos_0 {
2347                                                    last_pos = last_pos.wrapping_add(1);
2348                                                    (*opt.offset(last_pos as isize)).price =
2349                                                        ZSTD_MAX_PRICE;
2350                                                    (*opt.offset(last_pos as isize)).litlen =
2351                                                        (0 == 0) as core::ffi::c_int as u32;
2352                                                }
2353                                                (*opt.offset(pos_0 as isize)).mlen = mlen;
2354                                                (*opt.offset(pos_0 as isize)).off = offset;
2355                                                (*opt.offset(pos_0 as isize)).litlen = 0;
2356                                                (*opt.offset(pos_0 as isize)).price = price_0;
2357                                            } else if optLevel == 0 {
2358                                                break;
2359                                            }
2360                                            mlen = mlen.wrapping_sub(1);
2361                                        }
2362                                        matchNb_0 = matchNb_0.wrapping_add(1);
2363                                    }
2364                                    (*opt.offset(last_pos.wrapping_add(1) as isize)).price =
2365                                        ZSTD_MAX_PRICE;
2366                                }
2367                            }
2368                        }
2369                    }
2370                    cur = cur.wrapping_add(1);
2371                }
2372                match current_block {
2373                    12608488225262500095 => {}
2374                    _ => {
2375                        lastStretch = *opt.offset(last_pos as isize);
2376                        cur = last_pos.wrapping_sub(lastStretch.mlen);
2377                    }
2378                }
2379            }
2380            if lastStretch.mlen == 0 {
2381                ip = ip.offset(last_pos as isize);
2382            } else {
2383                if lastStretch.litlen == 0 {
2384                    let reps = ZSTD_newRep(
2385                        ((*opt.offset(cur as isize)).rep).as_mut_ptr() as *const u32,
2386                        lastStretch.off,
2387                        ((*opt.offset(cur as isize)).litlen == 0) as core::ffi::c_int as u32,
2388                    );
2389                    libc::memcpy(
2390                        rep as *mut core::ffi::c_void,
2391                        &reps as *const Repcodes_t as *const core::ffi::c_void,
2392                        ::core::mem::size_of::<Repcodes_t>() as core::ffi::c_ulong as libc::size_t,
2393                    );
2394                } else {
2395                    libc::memcpy(
2396                        rep as *mut core::ffi::c_void,
2397                        (lastStretch.rep).as_mut_ptr() as *const core::ffi::c_void,
2398                        ::core::mem::size_of::<Repcodes_t>() as core::ffi::c_ulong as libc::size_t,
2399                    );
2400                    cur = cur.wrapping_sub(lastStretch.litlen);
2401                }
2402                let storeEnd = cur.wrapping_add(2);
2403                let mut storeStart = storeEnd;
2404                let mut stretchPos = cur;
2405                if lastStretch.litlen > 0 {
2406                    (*opt.offset(storeEnd as isize)).litlen = lastStretch.litlen;
2407                    (*opt.offset(storeEnd as isize)).mlen = 0;
2408                    storeStart = storeEnd.wrapping_sub(1);
2409                    *opt.offset(storeStart as isize) = lastStretch;
2410                }
2411                *opt.offset(storeEnd as isize) = lastStretch;
2412                storeStart = storeEnd;
2413                loop {
2414                    let nextStretch = *opt.offset(stretchPos as isize);
2415                    (*opt.offset(storeStart as isize)).litlen = nextStretch.litlen;
2416                    if nextStretch.mlen == 0 {
2417                        break;
2418                    }
2419                    storeStart = storeStart.wrapping_sub(1);
2420                    *opt.offset(storeStart as isize) = nextStretch;
2421                    stretchPos = stretchPos
2422                        .wrapping_sub((nextStretch.litlen).wrapping_add(nextStretch.mlen));
2423                }
2424                let mut storePos: u32 = 0;
2425                storePos = storeStart;
2426                while storePos <= storeEnd {
2427                    let llen = (*opt.offset(storePos as isize)).litlen;
2428                    let mlen_0 = (*opt.offset(storePos as isize)).mlen;
2429                    let offBase_0 = (*opt.offset(storePos as isize)).off;
2430                    let advance = llen.wrapping_add(mlen_0);
2431                    if mlen_0 == 0 {
2432                        ip = anchor.offset(llen as isize);
2433                    } else {
2434                        ZSTD_updateStats(optStatePtr, llen, anchor, offBase_0, mlen_0);
2435                        ZSTD_storeSeq(
2436                            seqStore,
2437                            llen as size_t,
2438                            anchor,
2439                            iend,
2440                            offBase_0,
2441                            mlen_0 as size_t,
2442                        );
2443                        anchor = anchor.offset(advance as isize);
2444                        ip = anchor;
2445                    }
2446                    storePos = storePos.wrapping_add(1);
2447                }
2448                ZSTD_setBasePrices(optStatePtr, optLevel);
2449            }
2450        }
2451    }
2452    iend.offset_from(anchor) as size_t
2453}
2454unsafe fn ZSTD_compressBlock_opt0(
2455    ms: *mut ZSTD_MatchState_t,
2456    seqStore: *mut SeqStore_t,
2457    rep: *mut u32,
2458    src: *const core::ffi::c_void,
2459    srcSize: size_t,
2460    dictMode: ZSTD_dictMode_e,
2461) -> size_t {
2462    ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0, dictMode)
2463}
2464unsafe fn ZSTD_compressBlock_opt2(
2465    ms: *mut ZSTD_MatchState_t,
2466    seqStore: *mut SeqStore_t,
2467    rep: *mut u32,
2468    src: *const core::ffi::c_void,
2469    srcSize: size_t,
2470    dictMode: ZSTD_dictMode_e,
2471) -> size_t {
2472    ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2, dictMode)
2473}
2474pub unsafe fn ZSTD_compressBlock_btopt(
2475    ms: *mut ZSTD_MatchState_t,
2476    seqStore: *mut SeqStore_t,
2477    rep: *mut u32,
2478    src: *const core::ffi::c_void,
2479    srcSize: size_t,
2480) -> size_t {
2481    ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_noDict)
2482}
2483unsafe fn ZSTD_initStats_ultra(
2484    ms: *mut ZSTD_MatchState_t,
2485    seqStore: *mut SeqStore_t,
2486    rep: *mut u32,
2487    src: *const core::ffi::c_void,
2488    srcSize: size_t,
2489) {
2490    let mut tmpRep: [u32; 3] = [0; 3];
2491    libc::memcpy(
2492        tmpRep.as_mut_ptr() as *mut core::ffi::c_void,
2493        rep as *const core::ffi::c_void,
2494        ::core::mem::size_of::<[u32; 3]>() as core::ffi::c_ulong as libc::size_t,
2495    );
2496    ZSTD_compressBlock_opt2(ms, seqStore, tmpRep.as_mut_ptr(), src, srcSize, ZSTD_noDict);
2497    ZSTD_resetSeqStore(seqStore);
2498    (*ms).window.base = ((*ms).window.base).offset(-(srcSize as isize));
2499    (*ms).window.dictLimit = ((*ms).window.dictLimit).wrapping_add(srcSize as u32);
2500    (*ms).window.lowLimit = (*ms).window.dictLimit;
2501    (*ms).nextToUpdate = (*ms).window.dictLimit;
2502}
2503pub unsafe fn ZSTD_compressBlock_btultra(
2504    ms: *mut ZSTD_MatchState_t,
2505    seqStore: *mut SeqStore_t,
2506    rep: *mut u32,
2507    src: *const core::ffi::c_void,
2508    srcSize: size_t,
2509) -> size_t {
2510    ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_noDict)
2511}
2512pub unsafe fn ZSTD_compressBlock_btultra2(
2513    ms: *mut ZSTD_MatchState_t,
2514    seqStore: *mut SeqStore_t,
2515    rep: *mut u32,
2516    src: *const core::ffi::c_void,
2517    srcSize: size_t,
2518) -> size_t {
2519    let curr = (src as *const u8).offset_from((*ms).window.base) as core::ffi::c_long as u32;
2520    if (*ms).opt.litLengthSum == 0
2521        && (*seqStore).sequences == (*seqStore).sequencesStart
2522        && (*ms).window.dictLimit == (*ms).window.lowLimit
2523        && curr == (*ms).window.dictLimit
2524        && srcSize > ZSTD_PREDEF_THRESHOLD as size_t
2525    {
2526        ZSTD_initStats_ultra(ms, seqStore, rep, src, srcSize);
2527    }
2528    ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_noDict)
2529}
2530pub unsafe fn ZSTD_compressBlock_btopt_dictMatchState(
2531    ms: *mut ZSTD_MatchState_t,
2532    seqStore: *mut SeqStore_t,
2533    rep: *mut u32,
2534    src: *const core::ffi::c_void,
2535    srcSize: size_t,
2536) -> size_t {
2537    ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_dictMatchState)
2538}
2539pub unsafe fn ZSTD_compressBlock_btopt_extDict(
2540    ms: *mut ZSTD_MatchState_t,
2541    seqStore: *mut SeqStore_t,
2542    rep: *mut u32,
2543    src: *const core::ffi::c_void,
2544    srcSize: size_t,
2545) -> size_t {
2546    ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_extDict)
2547}
2548pub unsafe fn ZSTD_compressBlock_btultra_dictMatchState(
2549    ms: *mut ZSTD_MatchState_t,
2550    seqStore: *mut SeqStore_t,
2551    rep: *mut u32,
2552    src: *const core::ffi::c_void,
2553    srcSize: size_t,
2554) -> size_t {
2555    ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_dictMatchState)
2556}
2557pub unsafe fn ZSTD_compressBlock_btultra_extDict(
2558    ms: *mut ZSTD_MatchState_t,
2559    seqStore: *mut SeqStore_t,
2560    rep: *mut u32,
2561    src: *const core::ffi::c_void,
2562    srcSize: size_t,
2563) -> size_t {
2564    ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_extDict)
2565}
2566pub const __INT_MAX__: core::ffi::c_int = 2147483647;