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