zstd_ldm.h 4.3 KB

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