162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) Yann Collet, Facebook, Inc.
362306a36Sopenharmony_ci * All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This source code is licensed under both the BSD-style license (found in the
662306a36Sopenharmony_ci * LICENSE file in the root directory of this source tree) and the GPLv2 (found
762306a36Sopenharmony_ci * in the COPYING file in the root directory of this source tree).
862306a36Sopenharmony_ci * You may select, at your option, one of the above-listed licenses.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef ZSTD_LDM_H
1262306a36Sopenharmony_ci#define ZSTD_LDM_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include "zstd_compress_internal.h"   /* ldmParams_t, U32 */
1662306a36Sopenharmony_ci#include <linux/zstd.h>   /* ZSTD_CCtx, size_t */
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/*-*************************************
1962306a36Sopenharmony_ci*  Long distance matching
2062306a36Sopenharmony_ci***************************************/
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define ZSTD_LDM_DEFAULT_WINDOW_LOG ZSTD_WINDOWLOG_LIMIT_DEFAULT
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_civoid ZSTD_ldm_fillHashTable(
2562306a36Sopenharmony_ci            ldmState_t* state, const BYTE* ip,
2662306a36Sopenharmony_ci            const BYTE* iend, ldmParams_t const* params);
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * ZSTD_ldm_generateSequences():
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * Generates the sequences using the long distance match finder.
3262306a36Sopenharmony_ci * Generates long range matching sequences in `sequences`, which parse a prefix
3362306a36Sopenharmony_ci * of the source. `sequences` must be large enough to store every sequence,
3462306a36Sopenharmony_ci * which can be checked with `ZSTD_ldm_getMaxNbSeq()`.
3562306a36Sopenharmony_ci * @returns 0 or an error code.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * NOTE: The user must have called ZSTD_window_update() for all of the input
3862306a36Sopenharmony_ci * they have, even if they pass it to ZSTD_ldm_generateSequences() in chunks.
3962306a36Sopenharmony_ci * NOTE: This function returns an error if it runs out of space to store
4062306a36Sopenharmony_ci *       sequences.
4162306a36Sopenharmony_ci */
4262306a36Sopenharmony_cisize_t ZSTD_ldm_generateSequences(
4362306a36Sopenharmony_ci            ldmState_t* ldms, rawSeqStore_t* sequences,
4462306a36Sopenharmony_ci            ldmParams_t const* params, void const* src, size_t srcSize);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/*
4762306a36Sopenharmony_ci * ZSTD_ldm_blockCompress():
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * Compresses a block using the predefined sequences, along with a secondary
5062306a36Sopenharmony_ci * block compressor. The literals section of every sequence is passed to the
5162306a36Sopenharmony_ci * secondary block compressor, and those sequences are interspersed with the
5262306a36Sopenharmony_ci * predefined sequences. Returns the length of the last literals.
5362306a36Sopenharmony_ci * Updates `rawSeqStore.pos` to indicate how many sequences have been consumed.
5462306a36Sopenharmony_ci * `rawSeqStore.seq` may also be updated to split the last sequence between two
5562306a36Sopenharmony_ci * blocks.
5662306a36Sopenharmony_ci * @return The length of the last literals.
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * NOTE: The source must be at most the maximum block size, but the predefined
5962306a36Sopenharmony_ci * sequences can be any size, and may be longer than the block. In the case that
6062306a36Sopenharmony_ci * they are longer than the block, the last sequences may need to be split into
6162306a36Sopenharmony_ci * two. We handle that case correctly, and update `rawSeqStore` appropriately.
6262306a36Sopenharmony_ci * NOTE: This function does not return any errors.
6362306a36Sopenharmony_ci */
6462306a36Sopenharmony_cisize_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore,
6562306a36Sopenharmony_ci            ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
6662306a36Sopenharmony_ci            ZSTD_paramSwitch_e useRowMatchFinder,
6762306a36Sopenharmony_ci            void const* src, size_t srcSize);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci/*
7062306a36Sopenharmony_ci * ZSTD_ldm_skipSequences():
7162306a36Sopenharmony_ci *
7262306a36Sopenharmony_ci * Skip past `srcSize` bytes worth of sequences in `rawSeqStore`.
7362306a36Sopenharmony_ci * Avoids emitting matches less than `minMatch` bytes.
7462306a36Sopenharmony_ci * Must be called for data that is not passed to ZSTD_ldm_blockCompress().
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_civoid ZSTD_ldm_skipSequences(rawSeqStore_t* rawSeqStore, size_t srcSize,
7762306a36Sopenharmony_ci    U32 const minMatch);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/* ZSTD_ldm_skipRawSeqStoreBytes():
8062306a36Sopenharmony_ci * Moves forward in rawSeqStore by nbBytes, updating fields 'pos' and 'posInSequence'.
8162306a36Sopenharmony_ci * Not to be used in conjunction with ZSTD_ldm_skipSequences().
8262306a36Sopenharmony_ci * Must be called for data with is not passed to ZSTD_ldm_blockCompress().
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_civoid ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/* ZSTD_ldm_getTableSize() :
8762306a36Sopenharmony_ci *  Estimate the space needed for long distance matching tables or 0 if LDM is
8862306a36Sopenharmony_ci *  disabled.
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_cisize_t ZSTD_ldm_getTableSize(ldmParams_t params);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/* ZSTD_ldm_getSeqSpace() :
9362306a36Sopenharmony_ci *  Return an upper bound on the number of sequences that can be produced by
9462306a36Sopenharmony_ci *  the long distance matcher, or 0 if LDM is disabled.
9562306a36Sopenharmony_ci */
9662306a36Sopenharmony_cisize_t ZSTD_ldm_getMaxNbSeq(ldmParams_t params, size_t maxChunkSize);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/* ZSTD_ldm_adjustParameters() :
9962306a36Sopenharmony_ci *  If the params->hashRateLog is not set, set it to its default value based on
10062306a36Sopenharmony_ci *  windowLog and params->hashLog.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci *  Ensures that params->bucketSizeLog is <= params->hashLog (setting it to
10362306a36Sopenharmony_ci *  params->hashLog if it is not).
10462306a36Sopenharmony_ci *
10562306a36Sopenharmony_ci *  Ensures that the minMatchLength >= targetLength during optimal parsing.
10662306a36Sopenharmony_ci */
10762306a36Sopenharmony_civoid ZSTD_ldm_adjustParameters(ldmParams_t* params,
10862306a36Sopenharmony_ci                               ZSTD_compressionParameters const* cParams);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci#endif /* ZSTD_FAST_H */
112