xref: /netbsd-src/external/bsd/zstd/dist/lib/compress/zstd_ldm.h (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /*
2*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*3117ece4Schristos  * All rights reserved.
4*3117ece4Schristos  *
5*3117ece4Schristos  * This source code is licensed under both the BSD-style license (found in the
6*3117ece4Schristos  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*3117ece4Schristos  * in the COPYING file in the root directory of this source tree).
8*3117ece4Schristos  * You may select, at your option, one of the above-listed licenses.
9*3117ece4Schristos  */
10*3117ece4Schristos 
11*3117ece4Schristos #ifndef ZSTD_LDM_H
12*3117ece4Schristos #define ZSTD_LDM_H
13*3117ece4Schristos 
14*3117ece4Schristos #if defined (__cplusplus)
15*3117ece4Schristos extern "C" {
16*3117ece4Schristos #endif
17*3117ece4Schristos 
18*3117ece4Schristos #include "zstd_compress_internal.h"   /* ldmParams_t, U32 */
19*3117ece4Schristos #include "../zstd.h"   /* ZSTD_CCtx, size_t */
20*3117ece4Schristos 
21*3117ece4Schristos /*-*************************************
22*3117ece4Schristos *  Long distance matching
23*3117ece4Schristos ***************************************/
24*3117ece4Schristos 
25*3117ece4Schristos #define ZSTD_LDM_DEFAULT_WINDOW_LOG ZSTD_WINDOWLOG_LIMIT_DEFAULT
26*3117ece4Schristos 
27*3117ece4Schristos void ZSTD_ldm_fillHashTable(
28*3117ece4Schristos             ldmState_t* state, const BYTE* ip,
29*3117ece4Schristos             const BYTE* iend, ldmParams_t const* params);
30*3117ece4Schristos 
31*3117ece4Schristos /**
32*3117ece4Schristos  * ZSTD_ldm_generateSequences():
33*3117ece4Schristos  *
34*3117ece4Schristos  * Generates the sequences using the long distance match finder.
35*3117ece4Schristos  * Generates long range matching sequences in `sequences`, which parse a prefix
36*3117ece4Schristos  * of the source. `sequences` must be large enough to store every sequence,
37*3117ece4Schristos  * which can be checked with `ZSTD_ldm_getMaxNbSeq()`.
38*3117ece4Schristos  * @returns 0 or an error code.
39*3117ece4Schristos  *
40*3117ece4Schristos  * NOTE: The user must have called ZSTD_window_update() for all of the input
41*3117ece4Schristos  * they have, even if they pass it to ZSTD_ldm_generateSequences() in chunks.
42*3117ece4Schristos  * NOTE: This function returns an error if it runs out of space to store
43*3117ece4Schristos  *       sequences.
44*3117ece4Schristos  */
45*3117ece4Schristos size_t ZSTD_ldm_generateSequences(
46*3117ece4Schristos             ldmState_t* ldms, rawSeqStore_t* sequences,
47*3117ece4Schristos             ldmParams_t const* params, void const* src, size_t srcSize);
48*3117ece4Schristos 
49*3117ece4Schristos /**
50*3117ece4Schristos  * ZSTD_ldm_blockCompress():
51*3117ece4Schristos  *
52*3117ece4Schristos  * Compresses a block using the predefined sequences, along with a secondary
53*3117ece4Schristos  * block compressor. The literals section of every sequence is passed to the
54*3117ece4Schristos  * secondary block compressor, and those sequences are interspersed with the
55*3117ece4Schristos  * predefined sequences. Returns the length of the last literals.
56*3117ece4Schristos  * Updates `rawSeqStore.pos` to indicate how many sequences have been consumed.
57*3117ece4Schristos  * `rawSeqStore.seq` may also be updated to split the last sequence between two
58*3117ece4Schristos  * blocks.
59*3117ece4Schristos  * @return The length of the last literals.
60*3117ece4Schristos  *
61*3117ece4Schristos  * NOTE: The source must be at most the maximum block size, but the predefined
62*3117ece4Schristos  * sequences can be any size, and may be longer than the block. In the case that
63*3117ece4Schristos  * they are longer than the block, the last sequences may need to be split into
64*3117ece4Schristos  * two. We handle that case correctly, and update `rawSeqStore` appropriately.
65*3117ece4Schristos  * NOTE: This function does not return any errors.
66*3117ece4Schristos  */
67*3117ece4Schristos size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore,
68*3117ece4Schristos             ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
69*3117ece4Schristos             ZSTD_paramSwitch_e useRowMatchFinder,
70*3117ece4Schristos             void const* src, size_t srcSize);
71*3117ece4Schristos 
72*3117ece4Schristos /**
73*3117ece4Schristos  * ZSTD_ldm_skipSequences():
74*3117ece4Schristos  *
75*3117ece4Schristos  * Skip past `srcSize` bytes worth of sequences in `rawSeqStore`.
76*3117ece4Schristos  * Avoids emitting matches less than `minMatch` bytes.
77*3117ece4Schristos  * Must be called for data that is not passed to ZSTD_ldm_blockCompress().
78*3117ece4Schristos  */
79*3117ece4Schristos void ZSTD_ldm_skipSequences(rawSeqStore_t* rawSeqStore, size_t srcSize,
80*3117ece4Schristos     U32 const minMatch);
81*3117ece4Schristos 
82*3117ece4Schristos /* ZSTD_ldm_skipRawSeqStoreBytes():
83*3117ece4Schristos  * Moves forward in rawSeqStore by nbBytes, updating fields 'pos' and 'posInSequence'.
84*3117ece4Schristos  * Not to be used in conjunction with ZSTD_ldm_skipSequences().
85*3117ece4Schristos  * Must be called for data with is not passed to ZSTD_ldm_blockCompress().
86*3117ece4Schristos  */
87*3117ece4Schristos void ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes);
88*3117ece4Schristos 
89*3117ece4Schristos /** ZSTD_ldm_getTableSize() :
90*3117ece4Schristos  *  Estimate the space needed for long distance matching tables or 0 if LDM is
91*3117ece4Schristos  *  disabled.
92*3117ece4Schristos  */
93*3117ece4Schristos size_t ZSTD_ldm_getTableSize(ldmParams_t params);
94*3117ece4Schristos 
95*3117ece4Schristos /** ZSTD_ldm_getSeqSpace() :
96*3117ece4Schristos  *  Return an upper bound on the number of sequences that can be produced by
97*3117ece4Schristos  *  the long distance matcher, or 0 if LDM is disabled.
98*3117ece4Schristos  */
99*3117ece4Schristos size_t ZSTD_ldm_getMaxNbSeq(ldmParams_t params, size_t maxChunkSize);
100*3117ece4Schristos 
101*3117ece4Schristos /** ZSTD_ldm_adjustParameters() :
102*3117ece4Schristos  *  If the params->hashRateLog is not set, set it to its default value based on
103*3117ece4Schristos  *  windowLog and params->hashLog.
104*3117ece4Schristos  *
105*3117ece4Schristos  *  Ensures that params->bucketSizeLog is <= params->hashLog (setting it to
106*3117ece4Schristos  *  params->hashLog if it is not).
107*3117ece4Schristos  *
108*3117ece4Schristos  *  Ensures that the minMatchLength >= targetLength during optimal parsing.
109*3117ece4Schristos  */
110*3117ece4Schristos void ZSTD_ldm_adjustParameters(ldmParams_t* params,
111*3117ece4Schristos                                ZSTD_compressionParameters const* cParams);
112*3117ece4Schristos 
113*3117ece4Schristos #if defined (__cplusplus)
114*3117ece4Schristos }
115*3117ece4Schristos #endif
116*3117ece4Schristos 
117*3117ece4Schristos #endif /* ZSTD_FAST_H */
118