zstd_v07.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) Yann Collet, 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 ZSTDv07_H_235446
  11. #define ZSTDv07_H_235446
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. /*====== Dependency ======*/
  16. #include <stddef.h> /* size_t */
  17. /*====== Export for Windows ======*/
  18. /*!
  19. * ZSTDv07_DLL_EXPORT :
  20. * Enable exporting of functions when building a Windows DLL
  21. */
  22. #if defined(_WIN32) && defined(ZSTDv07_DLL_EXPORT) && (ZSTDv07_DLL_EXPORT==1)
  23. # define ZSTDLIBv07_API __declspec(dllexport)
  24. #else
  25. # define ZSTDLIBv07_API
  26. #endif
  27. /* *************************************
  28. * Simple API
  29. ***************************************/
  30. /*! ZSTDv07_getDecompressedSize() :
  31. * @return : decompressed size if known, 0 otherwise.
  32. note 1 : if `0`, follow up with ZSTDv07_getFrameParams() to know precise failure cause.
  33. note 2 : decompressed size could be wrong or intentionally modified !
  34. always ensure results fit within application's authorized limits */
  35. unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize);
  36. /*! ZSTDv07_decompress() :
  37. `compressedSize` : must be _exact_ size of compressed input, otherwise decompression will fail.
  38. `dstCapacity` must be equal or larger than originalSize.
  39. @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
  40. or an errorCode if it fails (which can be tested using ZSTDv07_isError()) */
  41. ZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity,
  42. const void* src, size_t compressedSize);
  43. /**
  44. ZSTDv07_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.7.x format
  45. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  46. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  47. or an error code if it fails (which can be tested using ZSTDv01_isError())
  48. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  49. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  50. note : assumes `cSize` and `dBound` are _not_ NULL.
  51. */
  52. void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  53. size_t* cSize, unsigned long long* dBound);
  54. /*====== Helper functions ======*/
  55. ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  56. ZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code); /*!< provides readable string from an error code */
  57. /*-*************************************
  58. * Explicit memory management
  59. ***************************************/
  60. /** Decompression context */
  61. typedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx;
  62. ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void);
  63. ZSTDLIBv07_API size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx); /*!< @return : errorCode */
  64. /** ZSTDv07_decompressDCtx() :
  65. * Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */
  66. ZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  67. /*-************************
  68. * Simple dictionary API
  69. ***************************/
  70. /*! ZSTDv07_decompress_usingDict() :
  71. * Decompression using a pre-defined Dictionary content (see dictBuilder).
  72. * Dictionary must be identical to the one used during compression.
  73. * Note : This function load the dictionary, resulting in a significant startup time */
  74. ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx,
  75. void* dst, size_t dstCapacity,
  76. const void* src, size_t srcSize,
  77. const void* dict,size_t dictSize);
  78. /*-**************************
  79. * Advanced Dictionary API
  80. ****************************/
  81. /*! ZSTDv07_createDDict() :
  82. * Create a digested dictionary, ready to start decompression operation without startup delay.
  83. * `dict` can be released after creation */
  84. typedef struct ZSTDv07_DDict_s ZSTDv07_DDict;
  85. ZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize);
  86. ZSTDLIBv07_API size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict);
  87. /*! ZSTDv07_decompress_usingDDict() :
  88. * Decompression using a pre-digested Dictionary
  89. * Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */
  90. ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx,
  91. void* dst, size_t dstCapacity,
  92. const void* src, size_t srcSize,
  93. const ZSTDv07_DDict* ddict);
  94. typedef struct {
  95. unsigned long long frameContentSize;
  96. unsigned windowSize;
  97. unsigned dictID;
  98. unsigned checksumFlag;
  99. } ZSTDv07_frameParams;
  100. ZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
  101. /* *************************************
  102. * Streaming functions
  103. ***************************************/
  104. typedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx;
  105. ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void);
  106. ZSTDLIBv07_API size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx);
  107. ZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx);
  108. ZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize);
  109. ZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx,
  110. void* dst, size_t* dstCapacityPtr,
  111. const void* src, size_t* srcSizePtr);
  112. /*-***************************************************************************
  113. * Streaming decompression howto
  114. *
  115. * A ZBUFFv07_DCtx object is required to track streaming operations.
  116. * Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources.
  117. * Use ZBUFFv07_decompressInit() to start a new decompression operation,
  118. * or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary.
  119. * Note that ZBUFFv07_DCtx objects can be re-init multiple times.
  120. *
  121. * Use ZBUFFv07_decompressContinue() repetitively to consume your input.
  122. * *srcSizePtr and *dstCapacityPtr can be any size.
  123. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  124. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  125. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
  126. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
  127. * or 0 when a frame is completely decoded,
  128. * or an error code, which can be tested using ZBUFFv07_isError().
  129. *
  130. * Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize()
  131. * output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  132. * input : ZBUFFv07_recommendedDInSize == 128KB + 3;
  133. * just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  134. * *******************************************************************************/
  135. /* *************************************
  136. * Tool functions
  137. ***************************************/
  138. ZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode);
  139. ZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode);
  140. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  141. * These sizes are just hints, they tend to offer better latency */
  142. ZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void);
  143. ZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void);
  144. /*-*************************************
  145. * Constants
  146. ***************************************/
  147. #define ZSTDv07_MAGICNUMBER 0xFD2FB527 /* v0.7 */
  148. #if defined (__cplusplus)
  149. }
  150. #endif
  151. #endif /* ZSTDv07_H_235446 */