You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zdict.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  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 DICTBUILDER_H_001
  11. #define DICTBUILDER_H_001
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. /*====== Dependencies ======*/
  16. #include <stddef.h> /* size_t */
  17. /* ===== ZDICTLIB_API : control library symbols visibility ===== */
  18. #ifndef ZDICTLIB_VISIBILITY
  19. # if defined(__GNUC__) && (__GNUC__ >= 4)
  20. # define ZDICTLIB_VISIBILITY __attribute__ ((visibility ("default")))
  21. # else
  22. # define ZDICTLIB_VISIBILITY
  23. # endif
  24. #endif
  25. #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
  26. # define ZDICTLIB_API __declspec(dllexport) ZDICTLIB_VISIBILITY
  27. #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
  28. # define ZDICTLIB_API __declspec(dllimport) ZDICTLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
  29. #else
  30. # define ZDICTLIB_API ZDICTLIB_VISIBILITY
  31. #endif
  32. /*! ZDICT_trainFromBuffer():
  33. * Train a dictionary from an array of samples.
  34. * Uses ZDICT_optimizeTrainFromBuffer_cover() single-threaded, with d=8 and steps=4.
  35. * Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
  36. * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
  37. * The resulting dictionary will be saved into `dictBuffer`.
  38. * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
  39. * or an error code, which can be tested with ZDICT_isError().
  40. * Note: ZDICT_trainFromBuffer() requires about 9 bytes of memory for each input byte.
  41. * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
  42. * It's obviously possible to target smaller or larger ones, just by specifying different `dictBufferCapacity`.
  43. * In general, it's recommended to provide a few thousands samples, but this can vary a lot.
  44. * It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
  45. */
  46. ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity,
  47. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
  48. /*====== Helper functions ======*/
  49. ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize); /**< extracts dictID; @return zero if error (not a valid dictionary) */
  50. ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode);
  51. ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode);
  52. #ifdef ZDICT_STATIC_LINKING_ONLY
  53. /* ====================================================================================
  54. * The definitions in this section are considered experimental.
  55. * They should never be used with a dynamic library, as they may change in the future.
  56. * They are provided for advanced usages.
  57. * Use them only in association with static linking.
  58. * ==================================================================================== */
  59. typedef struct {
  60. int compressionLevel; /* 0 means default; target a specific zstd compression level */
  61. unsigned notificationLevel; /* Write to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */
  62. unsigned dictID; /* 0 means auto mode (32-bits random value); other : force dictID value */
  63. } ZDICT_params_t;
  64. /*! ZDICT_cover_params_t:
  65. * For all values 0 means default.
  66. * k and d are the only required parameters.
  67. */
  68. typedef struct {
  69. unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */
  70. unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */
  71. unsigned steps; /* Number of steps : Only used for optimization : 0 means default (32) : Higher means more parameters checked */
  72. unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
  73. ZDICT_params_t zParams;
  74. } ZDICT_cover_params_t;
  75. /*! ZDICT_trainFromBuffer_cover():
  76. * Train a dictionary from an array of samples using the COVER algorithm.
  77. * Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
  78. * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
  79. * The resulting dictionary will be saved into `dictBuffer`.
  80. * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
  81. * or an error code, which can be tested with ZDICT_isError().
  82. * Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte.
  83. * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
  84. * It's obviously possible to target smaller or larger ones, just by specifying different `dictBufferCapacity`.
  85. * In general, it's recommended to provide a few thousands samples, but this can vary a lot.
  86. * It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
  87. */
  88. ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover(
  89. void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer,
  90. const size_t *samplesSizes, unsigned nbSamples,
  91. ZDICT_cover_params_t parameters);
  92. /*! ZDICT_optimizeTrainFromBuffer_cover():
  93. * The same requirements as above hold for all the parameters except `parameters`.
  94. * This function tries many parameter combinations and picks the best parameters.
  95. * `*parameters` is filled with the best parameters found, and the dictionary
  96. * constructed with those parameters is stored in `dictBuffer`.
  97. *
  98. * All of the parameters d, k, steps are optional.
  99. * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8, 10, 12, 14, 16}.
  100. * if steps is zero it defaults to its default value.
  101. * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [16, 2048].
  102. *
  103. * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
  104. * or an error code, which can be tested with ZDICT_isError().
  105. * On success `*parameters` contains the parameters selected.
  106. * Note: ZDICT_optimizeTrainFromBuffer_cover() requires about 8 bytes of memory for each input byte and additionally another 5 bytes of memory for each byte of memory for each thread.
  107. */
  108. ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover(
  109. void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer,
  110. const size_t *samplesSizes, unsigned nbSamples,
  111. ZDICT_cover_params_t *parameters);
  112. /*! ZDICT_finalizeDictionary():
  113. * Given a custom content as a basis for dictionary, and a set of samples,
  114. * finalize dictionary by adding headers and statistics.
  115. *
  116. * Samples must be stored concatenated in a flat buffer `samplesBuffer`,
  117. * supplied with an array of sizes `samplesSizes`, providing the size of each sample in order.
  118. *
  119. * dictContentSize must be >= ZDICT_CONTENTSIZE_MIN bytes.
  120. * maxDictSize must be >= dictContentSize, and must be >= ZDICT_DICTSIZE_MIN bytes.
  121. *
  122. * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`),
  123. * or an error code, which can be tested by ZDICT_isError().
  124. * Note: ZDICT_finalizeDictionary() will push notifications into stderr if instructed to, using notificationLevel>0.
  125. * Note 2: dictBuffer and dictContent can overlap
  126. */
  127. #define ZDICT_CONTENTSIZE_MIN 128
  128. #define ZDICT_DICTSIZE_MIN 256
  129. ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dictBuffer, size_t dictBufferCapacity,
  130. const void* dictContent, size_t dictContentSize,
  131. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
  132. ZDICT_params_t parameters);
  133. typedef struct {
  134. unsigned selectivityLevel; /* 0 means default; larger => select more => larger dictionary */
  135. ZDICT_params_t zParams;
  136. } ZDICT_legacy_params_t;
  137. /*! ZDICT_trainFromBuffer_legacy():
  138. * Train a dictionary from an array of samples.
  139. * Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
  140. * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
  141. * The resulting dictionary will be saved into `dictBuffer`.
  142. * `parameters` is optional and can be provided with values set to 0 to mean "default".
  143. * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
  144. * or an error code, which can be tested with ZDICT_isError().
  145. * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
  146. * It's obviously possible to target smaller or larger ones, just by specifying different `dictBufferCapacity`.
  147. * In general, it's recommended to provide a few thousands samples, but this can vary a lot.
  148. * It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
  149. * Note: ZDICT_trainFromBuffer_legacy() will send notifications into stderr if instructed to, using notificationLevel>0.
  150. */
  151. ZDICTLIB_API size_t ZDICT_trainFromBuffer_legacy(
  152. void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer,
  153. const size_t *samplesSizes, unsigned nbSamples, ZDICT_legacy_params_t parameters);
  154. /* Deprecation warnings */
  155. /* It is generally possible to disable deprecation warnings from compiler,
  156. for example with -Wno-deprecated-declarations for gcc
  157. or _CRT_SECURE_NO_WARNINGS in Visual.
  158. Otherwise, it's also possible to manually define ZDICT_DISABLE_DEPRECATE_WARNINGS */
  159. #ifdef ZDICT_DISABLE_DEPRECATE_WARNINGS
  160. # define ZDICT_DEPRECATED(message) ZDICTLIB_API /* disable deprecation warnings */
  161. #else
  162. # define ZDICT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  163. # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
  164. # define ZDICT_DEPRECATED(message) [[deprecated(message)]] ZDICTLIB_API
  165. # elif (ZDICT_GCC_VERSION >= 405) || defined(__clang__)
  166. # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated(message)))
  167. # elif (ZDICT_GCC_VERSION >= 301)
  168. # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated))
  169. # elif defined(_MSC_VER)
  170. # define ZDICT_DEPRECATED(message) ZDICTLIB_API __declspec(deprecated(message))
  171. # else
  172. # pragma message("WARNING: You need to implement ZDICT_DEPRECATED for this compiler")
  173. # define ZDICT_DEPRECATED(message) ZDICTLIB_API
  174. # endif
  175. #endif /* ZDICT_DISABLE_DEPRECATE_WARNINGS */
  176. ZDICT_DEPRECATED("use ZDICT_finalizeDictionary() instead")
  177. size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
  178. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
  179. #endif /* ZDICT_STATIC_LINKING_ONLY */
  180. #if defined (__cplusplus)
  181. }
  182. #endif
  183. #endif /* DICTBUILDER_H_001 */