zstd_v06.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 ZSTDv06_H
  11. #define ZSTDv06_H
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. /*====== Dependency ======*/
  16. #include <stddef.h> /* size_t */
  17. /*====== Export for Windows ======*/
  18. /*!
  19. * ZSTDv06_DLL_EXPORT :
  20. * Enable exporting of functions when building a Windows DLL
  21. */
  22. #if defined(_WIN32) && defined(ZSTDv06_DLL_EXPORT) && (ZSTDv06_DLL_EXPORT==1)
  23. # define ZSTDLIBv06_API __declspec(dllexport)
  24. #else
  25. # define ZSTDLIBv06_API
  26. #endif
  27. /* *************************************
  28. * Simple functions
  29. ***************************************/
  30. /*! ZSTDv06_decompress() :
  31. `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.
  32. `dstCapacity` must be large enough, equal or larger than originalSize.
  33. @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
  34. or an errorCode if it fails (which can be tested using ZSTDv06_isError()) */
  35. ZSTDLIBv06_API size_t ZSTDv06_decompress( void* dst, size_t dstCapacity,
  36. const void* src, size_t compressedSize);
  37. /**
  38. ZSTDv06_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.6.x format
  39. srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
  40. cSize (output parameter) : the number of bytes that would be read to decompress this frame
  41. or an error code if it fails (which can be tested using ZSTDv01_isError())
  42. dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
  43. or ZSTD_CONTENTSIZE_ERROR if an error occurs
  44. note : assumes `cSize` and `dBound` are _not_ NULL.
  45. */
  46. void ZSTDv06_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
  47. size_t* cSize, unsigned long long* dBound);
  48. /* *************************************
  49. * Helper functions
  50. ***************************************/
  51. ZSTDLIBv06_API size_t ZSTDv06_compressBound(size_t srcSize); /*!< maximum compressed size (worst case scenario) */
  52. /* Error Management */
  53. ZSTDLIBv06_API unsigned ZSTDv06_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  54. ZSTDLIBv06_API const char* ZSTDv06_getErrorName(size_t code); /*!< provides readable string for an error code */
  55. /* *************************************
  56. * Explicit memory management
  57. ***************************************/
  58. /** Decompression context */
  59. typedef struct ZSTDv06_DCtx_s ZSTDv06_DCtx;
  60. ZSTDLIBv06_API ZSTDv06_DCtx* ZSTDv06_createDCtx(void);
  61. ZSTDLIBv06_API size_t ZSTDv06_freeDCtx(ZSTDv06_DCtx* dctx); /*!< @return : errorCode */
  62. /** ZSTDv06_decompressDCtx() :
  63. * Same as ZSTDv06_decompress(), but requires an already allocated ZSTDv06_DCtx (see ZSTDv06_createDCtx()) */
  64. ZSTDLIBv06_API size_t ZSTDv06_decompressDCtx(ZSTDv06_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  65. /*-***********************
  66. * Dictionary API
  67. *************************/
  68. /*! ZSTDv06_decompress_usingDict() :
  69. * Decompression using a pre-defined Dictionary content (see dictBuilder).
  70. * Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.
  71. * Note : dict can be NULL, in which case, it's equivalent to ZSTDv06_decompressDCtx() */
  72. ZSTDLIBv06_API size_t ZSTDv06_decompress_usingDict(ZSTDv06_DCtx* dctx,
  73. void* dst, size_t dstCapacity,
  74. const void* src, size_t srcSize,
  75. const void* dict,size_t dictSize);
  76. /*-************************
  77. * Advanced Streaming API
  78. ***************************/
  79. struct ZSTDv06_frameParams_s { unsigned long long frameContentSize; unsigned windowLog; };
  80. typedef struct ZSTDv06_frameParams_s ZSTDv06_frameParams;
  81. ZSTDLIBv06_API size_t ZSTDv06_getFrameParams(ZSTDv06_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
  82. ZSTDLIBv06_API size_t ZSTDv06_decompressBegin_usingDict(ZSTDv06_DCtx* dctx, const void* dict, size_t dictSize);
  83. ZSTDLIBv06_API void ZSTDv06_copyDCtx(ZSTDv06_DCtx* dctx, const ZSTDv06_DCtx* preparedDCtx);
  84. ZSTDLIBv06_API size_t ZSTDv06_nextSrcSizeToDecompress(ZSTDv06_DCtx* dctx);
  85. ZSTDLIBv06_API size_t ZSTDv06_decompressContinue(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  86. /* *************************************
  87. * ZBUFF API
  88. ***************************************/
  89. typedef struct ZBUFFv06_DCtx_s ZBUFFv06_DCtx;
  90. ZSTDLIBv06_API ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void);
  91. ZSTDLIBv06_API size_t ZBUFFv06_freeDCtx(ZBUFFv06_DCtx* dctx);
  92. ZSTDLIBv06_API size_t ZBUFFv06_decompressInit(ZBUFFv06_DCtx* dctx);
  93. ZSTDLIBv06_API size_t ZBUFFv06_decompressInitDictionary(ZBUFFv06_DCtx* dctx, const void* dict, size_t dictSize);
  94. ZSTDLIBv06_API size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* dctx,
  95. void* dst, size_t* dstCapacityPtr,
  96. const void* src, size_t* srcSizePtr);
  97. /*-***************************************************************************
  98. * Streaming decompression howto
  99. *
  100. * A ZBUFFv06_DCtx object is required to track streaming operations.
  101. * Use ZBUFFv06_createDCtx() and ZBUFFv06_freeDCtx() to create/release resources.
  102. * Use ZBUFFv06_decompressInit() to start a new decompression operation,
  103. * or ZBUFFv06_decompressInitDictionary() if decompression requires a dictionary.
  104. * Note that ZBUFFv06_DCtx objects can be re-init multiple times.
  105. *
  106. * Use ZBUFFv06_decompressContinue() repetitively to consume your input.
  107. * *srcSizePtr and *dstCapacityPtr can be any size.
  108. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  109. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  110. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
  111. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
  112. * or 0 when a frame is completely decoded,
  113. * or an error code, which can be tested using ZBUFFv06_isError().
  114. *
  115. * Hint : recommended buffer sizes (not compulsory) : ZBUFFv06_recommendedDInSize() and ZBUFFv06_recommendedDOutSize()
  116. * output : ZBUFFv06_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  117. * input : ZBUFFv06_recommendedDInSize == 128KB + 3;
  118. * just follow indications from ZBUFFv06_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  119. * *******************************************************************************/
  120. /* *************************************
  121. * Tool functions
  122. ***************************************/
  123. ZSTDLIBv06_API unsigned ZBUFFv06_isError(size_t errorCode);
  124. ZSTDLIBv06_API const char* ZBUFFv06_getErrorName(size_t errorCode);
  125. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  126. * These sizes are just hints, they tend to offer better latency */
  127. ZSTDLIBv06_API size_t ZBUFFv06_recommendedDInSize(void);
  128. ZSTDLIBv06_API size_t ZBUFFv06_recommendedDOutSize(void);
  129. /*-*************************************
  130. * Constants
  131. ***************************************/
  132. #define ZSTDv06_MAGICNUMBER 0xFD2FB526 /* v0.6 */
  133. #if defined (__cplusplus)
  134. }
  135. #endif
  136. #endif /* ZSTDv06_BUFFERED_H */