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