zstd_v07.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 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_getFrameSrcSize() : get the source length of a ZSTD frame
  45. compressedSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  46. return : the number of bytes that would be read to decompress this frame
  47. or an errorCode if it fails (which can be tested using ZSTDv07_isError())
  48. */
  49. size_t ZSTDv07_findFrameCompressedSize(const void* src, size_t compressedSize);
  50. /*====== Helper functions ======*/
  51. ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  52. ZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code); /*!< provides readable string from an error code */
  53. /*-*************************************
  54. * Explicit memory management
  55. ***************************************/
  56. /** Decompression context */
  57. typedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx;
  58. ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void);
  59. ZSTDLIBv07_API size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx); /*!< @return : errorCode */
  60. /** ZSTDv07_decompressDCtx() :
  61. * Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */
  62. ZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  63. /*-************************
  64. * Simple dictionary API
  65. ***************************/
  66. /*! ZSTDv07_decompress_usingDict() :
  67. * Decompression using a pre-defined Dictionary content (see dictBuilder).
  68. * Dictionary must be identical to the one used during compression.
  69. * Note : This function load the dictionary, resulting in a significant startup time */
  70. ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx,
  71. void* dst, size_t dstCapacity,
  72. const void* src, size_t srcSize,
  73. const void* dict,size_t dictSize);
  74. /*-**************************
  75. * Advanced Dictionary API
  76. ****************************/
  77. /*! ZSTDv07_createDDict() :
  78. * Create a digested dictionary, ready to start decompression operation without startup delay.
  79. * `dict` can be released after creation */
  80. typedef struct ZSTDv07_DDict_s ZSTDv07_DDict;
  81. ZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize);
  82. ZSTDLIBv07_API size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict);
  83. /*! ZSTDv07_decompress_usingDDict() :
  84. * Decompression using a pre-digested Dictionary
  85. * Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */
  86. ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx,
  87. void* dst, size_t dstCapacity,
  88. const void* src, size_t srcSize,
  89. const ZSTDv07_DDict* ddict);
  90. typedef struct {
  91. unsigned long long frameContentSize;
  92. unsigned windowSize;
  93. unsigned dictID;
  94. unsigned checksumFlag;
  95. } ZSTDv07_frameParams;
  96. ZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
  97. /* *************************************
  98. * Streaming functions
  99. ***************************************/
  100. typedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx;
  101. ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void);
  102. ZSTDLIBv07_API size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx);
  103. ZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx);
  104. ZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize);
  105. ZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx,
  106. void* dst, size_t* dstCapacityPtr,
  107. const void* src, size_t* srcSizePtr);
  108. /*-***************************************************************************
  109. * Streaming decompression howto
  110. *
  111. * A ZBUFFv07_DCtx object is required to track streaming operations.
  112. * Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources.
  113. * Use ZBUFFv07_decompressInit() to start a new decompression operation,
  114. * or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary.
  115. * Note that ZBUFFv07_DCtx objects can be re-init multiple times.
  116. *
  117. * Use ZBUFFv07_decompressContinue() repetitively to consume your input.
  118. * *srcSizePtr and *dstCapacityPtr can be any size.
  119. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  120. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  121. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
  122. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
  123. * or 0 when a frame is completely decoded,
  124. * or an error code, which can be tested using ZBUFFv07_isError().
  125. *
  126. * Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize()
  127. * output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  128. * input : ZBUFFv07_recommendedDInSize == 128KB + 3;
  129. * just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  130. * *******************************************************************************/
  131. /* *************************************
  132. * Tool functions
  133. ***************************************/
  134. ZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode);
  135. ZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode);
  136. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  137. * These sizes are just hints, they tend to offer better latency */
  138. ZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void);
  139. ZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void);
  140. /*-*************************************
  141. * Constants
  142. ***************************************/
  143. #define ZSTDv07_MAGICNUMBER 0xFD2FB527 /* v0.7 */
  144. #if defined (__cplusplus)
  145. }
  146. #endif
  147. #endif /* ZSTDv07_H_235446 */