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