zstd_ldm.h 4.3 KB

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