contrib/python-zstandard/zstd/zstd.h
changeset 42937 69de49c4e39c
parent 42070 675775c33ab6
child 43994 de7838053207
equal deleted inserted replaced
42936:2da754532dd3 42937:69de49c4e39c
    68   the future. Only static linking is allowed.
    68   the future. Only static linking is allowed.
    69 *******************************************************************************/
    69 *******************************************************************************/
    70 
    70 
    71 /*------   Version   ------*/
    71 /*------   Version   ------*/
    72 #define ZSTD_VERSION_MAJOR    1
    72 #define ZSTD_VERSION_MAJOR    1
    73 #define ZSTD_VERSION_MINOR    3
    73 #define ZSTD_VERSION_MINOR    4
    74 #define ZSTD_VERSION_RELEASE  8
    74 #define ZSTD_VERSION_RELEASE  3
    75 
    75 
    76 #define ZSTD_VERSION_NUMBER  (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
    76 #define ZSTD_VERSION_NUMBER  (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
    77 ZSTDLIB_API unsigned ZSTD_versionNumber(void);   /**< to check runtime library version */
    77 ZSTDLIB_API unsigned ZSTD_versionNumber(void);   /**< to check runtime library version */
    78 
    78 
    79 #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
    79 #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
    80 #define ZSTD_QUOTE(str) #str
    80 #define ZSTD_QUOTE(str) #str
    81 #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
    81 #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
    82 #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
    82 #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
    83 ZSTDLIB_API const char* ZSTD_versionString(void);   /* requires v1.3.0+ */
    83 ZSTDLIB_API const char* ZSTD_versionString(void);   /* requires v1.3.0+ */
    84 
    84 
    85 /***************************************
    85 /* *************************************
    86 *  Default constant
    86  *  Default constant
    87 ***************************************/
    87  ***************************************/
    88 #ifndef ZSTD_CLEVEL_DEFAULT
    88 #ifndef ZSTD_CLEVEL_DEFAULT
    89 #  define ZSTD_CLEVEL_DEFAULT 3
    89 #  define ZSTD_CLEVEL_DEFAULT 3
    90 #endif
    90 #endif
       
    91 
       
    92 /* *************************************
       
    93  *  Constants
       
    94  ***************************************/
       
    95 
       
    96 /* All magic numbers are supposed read/written to/from files/memory using little-endian convention */
       
    97 #define ZSTD_MAGICNUMBER            0xFD2FB528    /* valid since v0.8.0 */
       
    98 #define ZSTD_MAGIC_DICTIONARY       0xEC30A437    /* valid since v0.7.0 */
       
    99 #define ZSTD_MAGIC_SKIPPABLE_START  0x184D2A50    /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */
       
   100 #define ZSTD_MAGIC_SKIPPABLE_MASK   0xFFFFFFF0
       
   101 
       
   102 #define ZSTD_BLOCKSIZELOG_MAX  17
       
   103 #define ZSTD_BLOCKSIZE_MAX     (1<<ZSTD_BLOCKSIZELOG_MAX)
       
   104 
       
   105 
    91 
   106 
    92 /***************************************
   107 /***************************************
    93 *  Simple API
   108 *  Simple API
    94 ***************************************/
   109 ***************************************/
    95 /*! ZSTD_compress() :
   110 /*! ZSTD_compress() :
   143  *  "empty", "unknown" and "error" results to the same return value (0),
   158  *  "empty", "unknown" and "error" results to the same return value (0),
   144  *  while ZSTD_getFrameContentSize() gives them separate return values.
   159  *  while ZSTD_getFrameContentSize() gives them separate return values.
   145  * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
   160  * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
   146 ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
   161 ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
   147 
   162 
       
   163 /*! ZSTD_findFrameCompressedSize() :
       
   164  * `src` should point to the start of a ZSTD frame or skippable frame.
       
   165  * `srcSize` must be >= first frame size
       
   166  * @return : the compressed size of the first frame starting at `src`,
       
   167  *           suitable to pass as `srcSize` to `ZSTD_decompress` or similar,
       
   168  *        or an error code if input is invalid */
       
   169 ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
       
   170 
   148 
   171 
   149 /*======  Helper functions  ======*/
   172 /*======  Helper functions  ======*/
   150 #define ZSTD_COMPRESSBOUND(srcSize)   ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0))  /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */
   173 #define ZSTD_COMPRESSBOUND(srcSize)   ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0))  /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */
   151 ZSTDLIB_API size_t      ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */
   174 ZSTDLIB_API size_t      ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */
   152 ZSTDLIB_API unsigned    ZSTD_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */
   175 ZSTDLIB_API unsigned    ZSTD_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */
   153 ZSTDLIB_API const char* ZSTD_getErrorName(size_t code);     /*!< provides readable string from an error code */
   176 ZSTDLIB_API const char* ZSTD_getErrorName(size_t code);     /*!< provides readable string from an error code */
       
   177 ZSTDLIB_API int         ZSTD_minCLevel(void);               /*!< minimum negative compression level allowed */
   154 ZSTDLIB_API int         ZSTD_maxCLevel(void);               /*!< maximum compression level available */
   178 ZSTDLIB_API int         ZSTD_maxCLevel(void);               /*!< maximum compression level available */
   155 
   179 
   156 
   180 
   157 /***************************************
   181 /***************************************
   158 *  Explicit context
   182 *  Explicit context
   159 ***************************************/
   183 ***************************************/
   160 /*= Compression context
   184 /*= Compression context
   161  *  When compressing many times,
   185  *  When compressing many times,
   162  *  it is recommended to allocate a context just once, and re-use it for each successive compression operation.
   186  *  it is recommended to allocate a context just once,
       
   187  *  and re-use it for each successive compression operation.
   163  *  This will make workload friendlier for system's memory.
   188  *  This will make workload friendlier for system's memory.
   164  *  Use one context per thread for parallel execution in multi-threaded environments. */
   189  *  Note : re-using context is just a speed / resource optimization.
       
   190  *         It doesn't change the compression ratio, which remains identical.
       
   191  *  Note 2 : In multi-threaded environments,
       
   192  *         use one different context per thread for parallel execution.
       
   193  */
   165 typedef struct ZSTD_CCtx_s ZSTD_CCtx;
   194 typedef struct ZSTD_CCtx_s ZSTD_CCtx;
   166 ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
   195 ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
   167 ZSTDLIB_API size_t     ZSTD_freeCCtx(ZSTD_CCtx* cctx);
   196 ZSTDLIB_API size_t     ZSTD_freeCCtx(ZSTD_CCtx* cctx);
   168 
   197 
   169 /*! ZSTD_compressCCtx() :
   198 /*! ZSTD_compressCCtx() :
   191  *  Compatible with sticky parameters.
   220  *  Compatible with sticky parameters.
   192  */
   221  */
   193 ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
   222 ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
   194                                        void* dst, size_t dstCapacity,
   223                                        void* dst, size_t dstCapacity,
   195                                  const void* src, size_t srcSize);
   224                                  const void* src, size_t srcSize);
   196 
       
   197 
       
   198 /**************************
       
   199 *  Simple dictionary API
       
   200 ***************************/
       
   201 /*! ZSTD_compress_usingDict() :
       
   202  *  Compression at an explicit compression level using a Dictionary.
       
   203  *  A dictionary can be any arbitrary data segment (also called a prefix),
       
   204  *  or a buffer with specified information (see dictBuilder/zdict.h).
       
   205  *  Note : This function loads the dictionary, resulting in significant startup delay.
       
   206  *         It's intended for a dictionary used only once.
       
   207  *  Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */
       
   208 ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
       
   209                                            void* dst, size_t dstCapacity,
       
   210                                      const void* src, size_t srcSize,
       
   211                                      const void* dict,size_t dictSize,
       
   212                                            int compressionLevel);
       
   213 
       
   214 /*! ZSTD_decompress_usingDict() :
       
   215  *  Decompression using a known Dictionary.
       
   216  *  Dictionary must be identical to the one used during compression.
       
   217  *  Note : This function loads the dictionary, resulting in significant startup delay.
       
   218  *         It's intended for a dictionary used only once.
       
   219  *  Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
       
   220 ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
       
   221                                              void* dst, size_t dstCapacity,
       
   222                                        const void* src, size_t srcSize,
       
   223                                        const void* dict,size_t dictSize);
       
   224 
       
   225 
       
   226 /***********************************
       
   227  *  Bulk processing dictionary API
       
   228  **********************************/
       
   229 typedef struct ZSTD_CDict_s ZSTD_CDict;
       
   230 
       
   231 /*! ZSTD_createCDict() :
       
   232  *  When compressing multiple messages / blocks using the same dictionary, it's recommended to load it only once.
       
   233  *  ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup cost.
       
   234  *  ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
       
   235  * `dictBuffer` can be released after ZSTD_CDict creation, because its content is copied within CDict.
       
   236  *  Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate `dictBuffer` content.
       
   237  *  Note : A ZSTD_CDict can be created from an empty dictBuffer, but it is inefficient when used to compress small data. */
       
   238 ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
       
   239                                          int compressionLevel);
       
   240 
       
   241 /*! ZSTD_freeCDict() :
       
   242  *  Function frees memory allocated by ZSTD_createCDict(). */
       
   243 ZSTDLIB_API size_t      ZSTD_freeCDict(ZSTD_CDict* CDict);
       
   244 
       
   245 /*! ZSTD_compress_usingCDict() :
       
   246  *  Compression using a digested Dictionary.
       
   247  *  Recommended when same dictionary is used multiple times.
       
   248  *  Note : compression level is _decided at dictionary creation time_,
       
   249  *     and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
       
   250 ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
       
   251                                             void* dst, size_t dstCapacity,
       
   252                                       const void* src, size_t srcSize,
       
   253                                       const ZSTD_CDict* cdict);
       
   254 
       
   255 
       
   256 typedef struct ZSTD_DDict_s ZSTD_DDict;
       
   257 
       
   258 /*! ZSTD_createDDict() :
       
   259  *  Create a digested dictionary, ready to start decompression operation without startup delay.
       
   260  *  dictBuffer can be released after DDict creation, as its content is copied inside DDict. */
       
   261 ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
       
   262 
       
   263 /*! ZSTD_freeDDict() :
       
   264  *  Function frees memory allocated with ZSTD_createDDict() */
       
   265 ZSTDLIB_API size_t      ZSTD_freeDDict(ZSTD_DDict* ddict);
       
   266 
       
   267 /*! ZSTD_decompress_usingDDict() :
       
   268  *  Decompression using a digested Dictionary.
       
   269  *  Recommended when same dictionary is used multiple times. */
       
   270 ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
       
   271                                               void* dst, size_t dstCapacity,
       
   272                                         const void* src, size_t srcSize,
       
   273                                         const ZSTD_DDict* ddict);
       
   274 
       
   275 
       
   276 /****************************
       
   277 *  Streaming
       
   278 ****************************/
       
   279 
       
   280 typedef struct ZSTD_inBuffer_s {
       
   281   const void* src;    /**< start of input buffer */
       
   282   size_t size;        /**< size of input buffer */
       
   283   size_t pos;         /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
       
   284 } ZSTD_inBuffer;
       
   285 
       
   286 typedef struct ZSTD_outBuffer_s {
       
   287   void*  dst;         /**< start of output buffer */
       
   288   size_t size;        /**< size of output buffer */
       
   289   size_t pos;         /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
       
   290 } ZSTD_outBuffer;
       
   291 
       
   292 
       
   293 
       
   294 /*-***********************************************************************
       
   295 *  Streaming compression - HowTo
       
   296 *
       
   297 *  A ZSTD_CStream object is required to track streaming operation.
       
   298 *  Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
       
   299 *  ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
       
   300 *  It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory.
       
   301 *
       
   302 *  For parallel execution, use one separate ZSTD_CStream per thread.
       
   303 *
       
   304 *  note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing.
       
   305 *
       
   306 *  Parameters are sticky : when starting a new compression on the same context,
       
   307 *  it will re-use the same sticky parameters as previous compression session.
       
   308 *  When in doubt, it's recommended to fully initialize the context before usage.
       
   309 *  Use ZSTD_initCStream() to set the parameter to a selected compression level.
       
   310 *  Use advanced API (ZSTD_CCtx_setParameter(), etc.) to set more specific parameters.
       
   311 *
       
   312 *  Use ZSTD_compressStream() as many times as necessary to consume input stream.
       
   313 *  The function will automatically update both `pos` fields within `input` and `output`.
       
   314 *  Note that the function may not consume the entire input,
       
   315 *  for example, because the output buffer is already full,
       
   316 *  in which case `input.pos < input.size`.
       
   317 *  The caller must check if input has been entirely consumed.
       
   318 *  If not, the caller must make some room to receive more compressed data,
       
   319 *  and then present again remaining input data.
       
   320 * @return : a size hint, preferred nb of bytes to use as input for next function call
       
   321 *           or an error code, which can be tested using ZSTD_isError().
       
   322 *           Note 1 : it's just a hint, to help latency a little, any value will work fine.
       
   323 *           Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
       
   324 *
       
   325 *  At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
       
   326 *  using ZSTD_flushStream(). `output->pos` will be updated.
       
   327 *  Note that, if `output->size` is too small, a single invocation of ZSTD_flushStream() might not be enough (return code > 0).
       
   328 *  In which case, make some room to receive more compressed data, and call again ZSTD_flushStream().
       
   329 *  @return : 0 if internal buffers are entirely flushed,
       
   330 *            >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
       
   331 *            or an error code, which can be tested using ZSTD_isError().
       
   332 *
       
   333 *  ZSTD_endStream() instructs to finish a frame.
       
   334 *  It will perform a flush and write frame epilogue.
       
   335 *  The epilogue is required for decoders to consider a frame completed.
       
   336 *  flush() operation is the same, and follows same rules as ZSTD_flushStream().
       
   337 *  @return : 0 if frame fully completed and fully flushed,
       
   338 *            >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
       
   339 *            or an error code, which can be tested using ZSTD_isError().
       
   340 *
       
   341 * *******************************************************************/
       
   342 
       
   343 typedef ZSTD_CCtx ZSTD_CStream;  /**< CCtx and CStream are now effectively same object (>= v1.3.0) */
       
   344                                  /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */
       
   345 /*===== ZSTD_CStream management functions =====*/
       
   346 ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
       
   347 ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
       
   348 
       
   349 /*===== Streaming compression functions =====*/
       
   350 ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
       
   351 ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
       
   352 ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
       
   353 ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
       
   354 
       
   355 ZSTDLIB_API size_t ZSTD_CStreamInSize(void);    /**< recommended size for input buffer */
       
   356 ZSTDLIB_API size_t ZSTD_CStreamOutSize(void);   /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */
       
   357 
       
   358 
       
   359 
       
   360 /*-***************************************************************************
       
   361 *  Streaming decompression - HowTo
       
   362 *
       
   363 *  A ZSTD_DStream object is required to track streaming operations.
       
   364 *  Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
       
   365 *  ZSTD_DStream objects can be re-used multiple times.
       
   366 *
       
   367 *  Use ZSTD_initDStream() to start a new decompression operation.
       
   368 * @return : recommended first input size
       
   369 *  Alternatively, use advanced API to set specific properties.
       
   370 *
       
   371 *  Use ZSTD_decompressStream() repetitively to consume your input.
       
   372 *  The function will update both `pos` fields.
       
   373 *  If `input.pos < input.size`, some input has not been consumed.
       
   374 *  It's up to the caller to present again remaining data.
       
   375 *  The function tries to flush all data decoded immediately, respecting output buffer size.
       
   376 *  If `output.pos < output.size`, decoder has flushed everything it could.
       
   377 *  But if `output.pos == output.size`, there might be some data left within internal buffers.,
       
   378 *  In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.
       
   379 *  Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.
       
   380 * @return : 0 when a frame is completely decoded and fully flushed,
       
   381 *        or an error code, which can be tested using ZSTD_isError(),
       
   382 *        or any other value > 0, which means there is still some decoding or flushing to do to complete current frame :
       
   383 *                                the return value is a suggested next input size (just a hint for better latency)
       
   384 *                                that will never request more than the remaining frame size.
       
   385 * *******************************************************************************/
       
   386 
       
   387 typedef ZSTD_DCtx ZSTD_DStream;  /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
       
   388                                  /* For compatibility with versions <= v1.2.0, prefer differentiating them. */
       
   389 /*===== ZSTD_DStream management functions =====*/
       
   390 ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
       
   391 ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
       
   392 
       
   393 /*===== Streaming decompression functions =====*/
       
   394 ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
       
   395 ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
       
   396 
       
   397 ZSTDLIB_API size_t ZSTD_DStreamInSize(void);    /*!< recommended size for input buffer */
       
   398 ZSTDLIB_API size_t ZSTD_DStreamOutSize(void);   /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
       
   399 
       
   400 #endif  /* ZSTD_H_235446 */
       
   401 
       
   402 
       
   403 
       
   404 
       
   405 /****************************************************************************************
       
   406  *   ADVANCED AND EXPERIMENTAL FUNCTIONS
       
   407  ****************************************************************************************
       
   408  * The definitions in the following section are considered experimental.
       
   409  * They are provided for advanced scenarios.
       
   410  * They should never be used with a dynamic library, as prototypes may change in the future.
       
   411  * Use them only in association with static linking.
       
   412  * ***************************************************************************************/
       
   413 
       
   414 #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
       
   415 #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
       
   416 
       
   417 
       
   418 /****************************************************************************************
       
   419  *   Candidate API for promotion to stable status
       
   420  ****************************************************************************************
       
   421  * The following symbols and constants form the "staging area" :
       
   422  * they are considered to join "stable API" by v1.4.0.
       
   423  * The proposal is written so that it can be made stable "as is",
       
   424  * though it's still possible to suggest improvements.
       
   425  * Staging is in fact last chance for changes,
       
   426  * the API is locked once reaching "stable" status.
       
   427  * ***************************************************************************************/
       
   428 
       
   429 
       
   430 /* ===  Constants   === */
       
   431 
       
   432 /* all magic numbers are supposed read/written to/from files/memory using little-endian convention */
       
   433 #define ZSTD_MAGICNUMBER            0xFD2FB528    /* valid since v0.8.0 */
       
   434 #define ZSTD_MAGIC_DICTIONARY       0xEC30A437    /* valid since v0.7.0 */
       
   435 #define ZSTD_MAGIC_SKIPPABLE_START  0x184D2A50    /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */
       
   436 #define ZSTD_MAGIC_SKIPPABLE_MASK   0xFFFFFFF0
       
   437 
       
   438 #define ZSTD_BLOCKSIZELOG_MAX  17
       
   439 #define ZSTD_BLOCKSIZE_MAX     (1<<ZSTD_BLOCKSIZELOG_MAX)
       
   440 
       
   441 
       
   442 /* ===   query limits   === */
       
   443 
       
   444 ZSTDLIB_API int ZSTD_minCLevel(void);  /*!< minimum negative compression level allowed */
       
   445 
       
   446 
       
   447 /* ===   frame size   === */
       
   448 
       
   449 /*! ZSTD_findFrameCompressedSize() :
       
   450  * `src` should point to the start of a ZSTD frame or skippable frame.
       
   451  * `srcSize` must be >= first frame size
       
   452  * @return : the compressed size of the first frame starting at `src`,
       
   453  *           suitable to pass as `srcSize` to `ZSTD_decompress` or similar,
       
   454  *        or an error code if input is invalid */
       
   455 ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
       
   456 
       
   457 
       
   458 /* ===   Memory management   === */
       
   459 
       
   460 /*! ZSTD_sizeof_*() :
       
   461  *  These functions give the _current_ memory usage of selected object.
       
   462  *  Note that object memory usage can evolve (increase or decrease) over time. */
       
   463 ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
       
   464 ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
       
   465 ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
       
   466 ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
       
   467 ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
       
   468 ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
       
   469 
   225 
   470 
   226 
   471 /***************************************
   227 /***************************************
   472 *  Advanced compression API
   228 *  Advanced compression API
   473 ***************************************/
   229 ***************************************/
   501 } ZSTD_strategy;
   257 } ZSTD_strategy;
   502 
   258 
   503 
   259 
   504 typedef enum {
   260 typedef enum {
   505 
   261 
   506     /* compression parameters */
   262     /* compression parameters
       
   263      * Note: When compressing with a ZSTD_CDict these parameters are superseded
       
   264      * by the parameters used to construct the ZSTD_CDict. See ZSTD_CCtx_refCDict()
       
   265      * for more info (superseded-by-cdict). */
   507     ZSTD_c_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
   266     ZSTD_c_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
   508                               * Default level is ZSTD_CLEVEL_DEFAULT==3.
   267                               * Default level is ZSTD_CLEVEL_DEFAULT==3.
   509                               * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
   268                               * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
   510                               * Note 1 : it's possible to pass a negative compression level.
   269                               * Note 1 : it's possible to pass a negative compression level.
   511                               * Note 2 : setting a level sets all default values of other compression parameters */
   270                               * Note 2 : setting a level sets all default values of other compression parameters */
   623      * At the time of this writing, they include :
   382      * At the time of this writing, they include :
   624      * ZSTD_c_rsyncable
   383      * ZSTD_c_rsyncable
   625      * ZSTD_c_format
   384      * ZSTD_c_format
   626      * ZSTD_c_forceMaxWindow
   385      * ZSTD_c_forceMaxWindow
   627      * ZSTD_c_forceAttachDict
   386      * ZSTD_c_forceAttachDict
       
   387      * ZSTD_c_literalCompressionMode
       
   388      * ZSTD_c_targetCBlockSize
   628      * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
   389      * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
   629      * note : never ever use experimentalParam? names directly;
   390      * note : never ever use experimentalParam? names directly;
   630      *        also, the enums values themselves are unstable and can still change.
   391      *        also, the enums values themselves are unstable and can still change.
   631      */
   392      */
   632      ZSTD_c_experimentalParam1=500,
   393      ZSTD_c_experimentalParam1=500,
   633      ZSTD_c_experimentalParam2=10,
   394      ZSTD_c_experimentalParam2=10,
   634      ZSTD_c_experimentalParam3=1000,
   395      ZSTD_c_experimentalParam3=1000,
   635      ZSTD_c_experimentalParam4=1001
   396      ZSTD_c_experimentalParam4=1001,
       
   397      ZSTD_c_experimentalParam5=1002,
       
   398      ZSTD_c_experimentalParam6=1003,
   636 } ZSTD_cParameter;
   399 } ZSTD_cParameter;
   637 
       
   638 
   400 
   639 typedef struct {
   401 typedef struct {
   640     size_t error;
   402     size_t error;
   641     int lowerBound;
   403     int lowerBound;
   642     int upperBound;
   404     int upperBound;
   675  *  Note 2 : pledgedSrcSize is only valid once, for the next frame.
   437  *  Note 2 : pledgedSrcSize is only valid once, for the next frame.
   676  *           It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN.
   438  *           It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN.
   677  *  Note 3 : Whenever all input data is provided and consumed in a single round,
   439  *  Note 3 : Whenever all input data is provided and consumed in a single round,
   678  *           for example with ZSTD_compress2(),
   440  *           for example with ZSTD_compress2(),
   679  *           or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end),
   441  *           or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end),
   680  *           this value is automatically overriden by srcSize instead.
   442  *           this value is automatically overridden by srcSize instead.
   681  */
   443  */
   682 ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
   444 ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
       
   445 
       
   446 typedef enum {
       
   447     ZSTD_reset_session_only = 1,
       
   448     ZSTD_reset_parameters = 2,
       
   449     ZSTD_reset_session_and_parameters = 3
       
   450 } ZSTD_ResetDirective;
       
   451 
       
   452 /*! ZSTD_CCtx_reset() :
       
   453  *  There are 2 different things that can be reset, independently or jointly :
       
   454  *  - The session : will stop compressing current frame, and make CCtx ready to start a new one.
       
   455  *                  Useful after an error, or to interrupt any ongoing compression.
       
   456  *                  Any internal data not yet flushed is cancelled.
       
   457  *                  Compression parameters and dictionary remain unchanged.
       
   458  *                  They will be used to compress next frame.
       
   459  *                  Resetting session never fails.
       
   460  *  - The parameters : changes all parameters back to "default".
       
   461  *                  This removes any reference to any dictionary too.
       
   462  *                  Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing)
       
   463  *                  otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())
       
   464  *  - Both : similar to resetting the session, followed by resetting parameters.
       
   465  */
       
   466 ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
       
   467 
       
   468 /*! ZSTD_compress2() :
       
   469  *  Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
       
   470  *  ZSTD_compress2() always starts a new frame.
       
   471  *  Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
       
   472  *  - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
       
   473  *  - The function is always blocking, returns when compression is completed.
       
   474  *  Hint : compression runs faster if `dstCapacity` >=  `ZSTD_compressBound(srcSize)`.
       
   475  * @return : compressed size written into `dst` (<= `dstCapacity),
       
   476  *           or an error code if it fails (which can be tested using ZSTD_isError()).
       
   477  */
       
   478 ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx,
       
   479                                    void* dst, size_t dstCapacity,
       
   480                              const void* src, size_t srcSize);
       
   481 
       
   482 
       
   483 /***************************************
       
   484 *  Advanced decompression API
       
   485 ***************************************/
       
   486 
       
   487 /* The advanced API pushes parameters one by one into an existing DCtx context.
       
   488  * Parameters are sticky, and remain valid for all following frames
       
   489  * using the same DCtx context.
       
   490  * It's possible to reset parameters to default values using ZSTD_DCtx_reset().
       
   491  * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream().
       
   492  *        Therefore, no new decompression function is necessary.
       
   493  */
       
   494 
       
   495 typedef enum {
       
   496 
       
   497     ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which
       
   498                               * the streaming API will refuse to allocate memory buffer
       
   499                               * in order to protect the host from unreasonable memory requirements.
       
   500                               * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
       
   501                               * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT).
       
   502                               * Special: value 0 means "use default maximum windowLog". */
       
   503 
       
   504     /* note : additional experimental parameters are also available
       
   505      * within the experimental section of the API.
       
   506      * At the time of this writing, they include :
       
   507      * ZSTD_c_format
       
   508      * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
       
   509      * note : never ever use experimentalParam? names directly
       
   510      */
       
   511      ZSTD_d_experimentalParam1=1000
       
   512 
       
   513 } ZSTD_dParameter;
       
   514 
       
   515 /*! ZSTD_dParam_getBounds() :
       
   516  *  All parameters must belong to an interval with lower and upper bounds,
       
   517  *  otherwise they will either trigger an error or be automatically clamped.
       
   518  * @return : a structure, ZSTD_bounds, which contains
       
   519  *         - an error status field, which must be tested using ZSTD_isError()
       
   520  *         - both lower and upper bounds, inclusive
       
   521  */
       
   522 ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);
       
   523 
       
   524 /*! ZSTD_DCtx_setParameter() :
       
   525  *  Set one compression parameter, selected by enum ZSTD_dParameter.
       
   526  *  All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds().
       
   527  *  Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
       
   528  *  Setting a parameter is only possible during frame initialization (before starting decompression).
       
   529  * @return : 0, or an error code (which can be tested using ZSTD_isError()).
       
   530  */
       
   531 ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value);
       
   532 
       
   533 /*! ZSTD_DCtx_reset() :
       
   534  *  Return a DCtx to clean state.
       
   535  *  Session and parameters can be reset jointly or separately.
       
   536  *  Parameters can only be reset when no active frame is being decompressed.
       
   537  * @return : 0, or an error code, which can be tested with ZSTD_isError()
       
   538  */
       
   539 ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
       
   540 
       
   541 
       
   542 /****************************
       
   543 *  Streaming
       
   544 ****************************/
       
   545 
       
   546 typedef struct ZSTD_inBuffer_s {
       
   547   const void* src;    /**< start of input buffer */
       
   548   size_t size;        /**< size of input buffer */
       
   549   size_t pos;         /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
       
   550 } ZSTD_inBuffer;
       
   551 
       
   552 typedef struct ZSTD_outBuffer_s {
       
   553   void*  dst;         /**< start of output buffer */
       
   554   size_t size;        /**< size of output buffer */
       
   555   size_t pos;         /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
       
   556 } ZSTD_outBuffer;
       
   557 
       
   558 
       
   559 
       
   560 /*-***********************************************************************
       
   561 *  Streaming compression - HowTo
       
   562 *
       
   563 *  A ZSTD_CStream object is required to track streaming operation.
       
   564 *  Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
       
   565 *  ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
       
   566 *  It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory.
       
   567 *
       
   568 *  For parallel execution, use one separate ZSTD_CStream per thread.
       
   569 *
       
   570 *  note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing.
       
   571 *
       
   572 *  Parameters are sticky : when starting a new compression on the same context,
       
   573 *  it will re-use the same sticky parameters as previous compression session.
       
   574 *  When in doubt, it's recommended to fully initialize the context before usage.
       
   575 *  Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(),
       
   576 *  ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to
       
   577 *  set more specific parameters, the pledged source size, or load a dictionary.
       
   578 *
       
   579 *  Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to
       
   580 *  consume input stream. The function will automatically update both `pos`
       
   581 *  fields within `input` and `output`.
       
   582 *  Note that the function may not consume the entire input, for example, because
       
   583 *  the output buffer is already full, in which case `input.pos < input.size`.
       
   584 *  The caller must check if input has been entirely consumed.
       
   585 *  If not, the caller must make some room to receive more compressed data,
       
   586 *  and then present again remaining input data.
       
   587 *  note: ZSTD_e_continue is guaranteed to make some forward progress when called,
       
   588 *        but doesn't guarantee maximal forward progress. This is especially relevant
       
   589 *        when compressing with multiple threads. The call won't block if it can
       
   590 *        consume some input, but if it can't it will wait for some, but not all,
       
   591 *        output to be flushed.
       
   592 * @return : provides a minimum amount of data remaining to be flushed from internal buffers
       
   593 *           or an error code, which can be tested using ZSTD_isError().
       
   594 *
       
   595 *  At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
       
   596 *  using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated.
       
   597 *  Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0).
       
   598 *  In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush.
       
   599 *  You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the
       
   600 *  operation.
       
   601 *  note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will
       
   602 *        block until the flush is complete or the output buffer is full.
       
   603 *  @return : 0 if internal buffers are entirely flushed,
       
   604 *            >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
       
   605 *            or an error code, which can be tested using ZSTD_isError().
       
   606 *
       
   607 *  Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame.
       
   608 *  It will perform a flush and write frame epilogue.
       
   609 *  The epilogue is required for decoders to consider a frame completed.
       
   610 *  flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush.
       
   611 *  You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to
       
   612 *  start a new frame.
       
   613 *  note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will
       
   614 *        block until the flush is complete or the output buffer is full.
       
   615 *  @return : 0 if frame fully completed and fully flushed,
       
   616 *            >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
       
   617 *            or an error code, which can be tested using ZSTD_isError().
       
   618 *
       
   619 * *******************************************************************/
       
   620 
       
   621 typedef ZSTD_CCtx ZSTD_CStream;  /**< CCtx and CStream are now effectively same object (>= v1.3.0) */
       
   622                                  /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */
       
   623 /*===== ZSTD_CStream management functions =====*/
       
   624 ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
       
   625 ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
       
   626 
       
   627 /*===== Streaming compression functions =====*/
       
   628 typedef enum {
       
   629     ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
       
   630     ZSTD_e_flush=1,    /* flush any data provided so far,
       
   631                         * it creates (at least) one new block, that can be decoded immediately on reception;
       
   632                         * frame will continue: any future data can still reference previously compressed data, improving compression.
       
   633                         * note : multithreaded compression will block to flush as much output as possible. */
       
   634     ZSTD_e_end=2       /* flush any remaining data _and_ close current frame.
       
   635                         * note that frame is only closed after compressed data is fully flushed (return value == 0).
       
   636                         * After that point, any additional data starts a new frame.
       
   637                         * note : each frame is independent (does not reference any content from previous frame).
       
   638                         : note : multithreaded compression will block to flush as much output as possible. */
       
   639 } ZSTD_EndDirective;
       
   640 
       
   641 /*! ZSTD_compressStream2() :
       
   642  *  Behaves about the same as ZSTD_compressStream, with additional control on end directive.
       
   643  *  - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
       
   644  *  - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode)
       
   645  *  - output->pos must be <= dstCapacity, input->pos must be <= srcSize
       
   646  *  - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
       
   647  *  - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller.
       
   648  *  - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available,
       
   649  *                                                  and then immediately returns, just indicating that there is some data remaining to be flushed.
       
   650  *                                                  The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
       
   651  *  - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking.
       
   652  *  - @return provides a minimum amount of data remaining to be flushed from internal buffers
       
   653  *            or an error code, which can be tested using ZSTD_isError().
       
   654  *            if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
       
   655  *            This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
       
   656  *            For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
       
   657  *  - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
       
   658  *            only ZSTD_e_end or ZSTD_e_flush operations are allowed.
       
   659  *            Before starting a new compression job, or changing compression parameters,
       
   660  *            it is required to fully flush internal buffers.
       
   661  */
       
   662 ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
       
   663                                          ZSTD_outBuffer* output,
       
   664                                          ZSTD_inBuffer* input,
       
   665                                          ZSTD_EndDirective endOp);
       
   666 
       
   667 
       
   668 /* These buffer sizes are softly recommended.
       
   669  * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output.
       
   670  * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(),
       
   671  * reducing the amount of memory shuffling and buffering, resulting in minor performance savings.
       
   672  *
       
   673  * However, note that these recommendations are from the perspective of a C caller program.
       
   674  * If the streaming interface is invoked from some other language,
       
   675  * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo,
       
   676  * a major performance rule is to reduce crossing such interface to an absolute minimum.
       
   677  * It's not rare that performance ends being spent more into the interface, rather than compression itself.
       
   678  * In which cases, prefer using large buffers, as large as practical,
       
   679  * for both input and output, to reduce the nb of roundtrips.
       
   680  */
       
   681 ZSTDLIB_API size_t ZSTD_CStreamInSize(void);    /**< recommended size for input buffer */
       
   682 ZSTDLIB_API size_t ZSTD_CStreamOutSize(void);   /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */
       
   683 
       
   684 
       
   685 /* *****************************************************************************
       
   686  * This following is a legacy streaming API.
       
   687  * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2().
       
   688  * It is redundant, but remains fully supported.
       
   689  * Advanced parameters and dictionary compression can only be used through the
       
   690  * new API.
       
   691  ******************************************************************************/
       
   692 
       
   693 /*!
       
   694  * Equivalent to:
       
   695  *
       
   696  *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
       
   697  *     ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
       
   698  *     ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
       
   699  */
       
   700 ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
       
   701 /*!
       
   702  * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue).
       
   703  * NOTE: The return value is different. ZSTD_compressStream() returns a hint for
       
   704  * the next read size (if non-zero and not an error). ZSTD_compressStream2()
       
   705  * returns the minimum nb of bytes left to flush (if non-zero and not an error).
       
   706  */
       
   707 ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
       
   708 /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */
       
   709 ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
       
   710 /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */
       
   711 ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
       
   712 
       
   713 
       
   714 /*-***************************************************************************
       
   715 *  Streaming decompression - HowTo
       
   716 *
       
   717 *  A ZSTD_DStream object is required to track streaming operations.
       
   718 *  Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
       
   719 *  ZSTD_DStream objects can be re-used multiple times.
       
   720 *
       
   721 *  Use ZSTD_initDStream() to start a new decompression operation.
       
   722 * @return : recommended first input size
       
   723 *  Alternatively, use advanced API to set specific properties.
       
   724 *
       
   725 *  Use ZSTD_decompressStream() repetitively to consume your input.
       
   726 *  The function will update both `pos` fields.
       
   727 *  If `input.pos < input.size`, some input has not been consumed.
       
   728 *  It's up to the caller to present again remaining data.
       
   729 *  The function tries to flush all data decoded immediately, respecting output buffer size.
       
   730 *  If `output.pos < output.size`, decoder has flushed everything it could.
       
   731 *  But if `output.pos == output.size`, there might be some data left within internal buffers.,
       
   732 *  In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.
       
   733 *  Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.
       
   734 * @return : 0 when a frame is completely decoded and fully flushed,
       
   735 *        or an error code, which can be tested using ZSTD_isError(),
       
   736 *        or any other value > 0, which means there is still some decoding or flushing to do to complete current frame :
       
   737 *                                the return value is a suggested next input size (just a hint for better latency)
       
   738 *                                that will never request more than the remaining frame size.
       
   739 * *******************************************************************************/
       
   740 
       
   741 typedef ZSTD_DCtx ZSTD_DStream;  /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
       
   742                                  /* For compatibility with versions <= v1.2.0, prefer differentiating them. */
       
   743 /*===== ZSTD_DStream management functions =====*/
       
   744 ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
       
   745 ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
       
   746 
       
   747 /*===== Streaming decompression functions =====*/
       
   748 
       
   749 /* This function is redundant with the advanced API and equivalent to:
       
   750  *
       
   751  *     ZSTD_DCtx_reset(zds);
       
   752  *     ZSTD_DCtx_refDDict(zds, NULL);
       
   753  */
       
   754 ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
       
   755 
       
   756 ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
       
   757 
       
   758 ZSTDLIB_API size_t ZSTD_DStreamInSize(void);    /*!< recommended size for input buffer */
       
   759 ZSTDLIB_API size_t ZSTD_DStreamOutSize(void);   /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
       
   760 
       
   761 
       
   762 /**************************
       
   763 *  Simple dictionary API
       
   764 ***************************/
       
   765 /*! ZSTD_compress_usingDict() :
       
   766  *  Compression at an explicit compression level using a Dictionary.
       
   767  *  A dictionary can be any arbitrary data segment (also called a prefix),
       
   768  *  or a buffer with specified information (see dictBuilder/zdict.h).
       
   769  *  Note : This function loads the dictionary, resulting in significant startup delay.
       
   770  *         It's intended for a dictionary used only once.
       
   771  *  Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */
       
   772 ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
       
   773                                            void* dst, size_t dstCapacity,
       
   774                                      const void* src, size_t srcSize,
       
   775                                      const void* dict,size_t dictSize,
       
   776                                            int compressionLevel);
       
   777 
       
   778 /*! ZSTD_decompress_usingDict() :
       
   779  *  Decompression using a known Dictionary.
       
   780  *  Dictionary must be identical to the one used during compression.
       
   781  *  Note : This function loads the dictionary, resulting in significant startup delay.
       
   782  *         It's intended for a dictionary used only once.
       
   783  *  Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
       
   784 ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
       
   785                                              void* dst, size_t dstCapacity,
       
   786                                        const void* src, size_t srcSize,
       
   787                                        const void* dict,size_t dictSize);
       
   788 
       
   789 
       
   790 /***********************************
       
   791  *  Bulk processing dictionary API
       
   792  **********************************/
       
   793 typedef struct ZSTD_CDict_s ZSTD_CDict;
       
   794 
       
   795 /*! ZSTD_createCDict() :
       
   796  *  When compressing multiple messages / blocks using the same dictionary, it's recommended to load it only once.
       
   797  *  ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup cost.
       
   798  *  ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
       
   799  * `dictBuffer` can be released after ZSTD_CDict creation, because its content is copied within CDict.
       
   800  *  Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate `dictBuffer` content.
       
   801  *  Note : A ZSTD_CDict can be created from an empty dictBuffer, but it is inefficient when used to compress small data. */
       
   802 ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
       
   803                                          int compressionLevel);
       
   804 
       
   805 /*! ZSTD_freeCDict() :
       
   806  *  Function frees memory allocated by ZSTD_createCDict(). */
       
   807 ZSTDLIB_API size_t      ZSTD_freeCDict(ZSTD_CDict* CDict);
       
   808 
       
   809 /*! ZSTD_compress_usingCDict() :
       
   810  *  Compression using a digested Dictionary.
       
   811  *  Recommended when same dictionary is used multiple times.
       
   812  *  Note : compression level is _decided at dictionary creation time_,
       
   813  *     and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
       
   814 ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
       
   815                                             void* dst, size_t dstCapacity,
       
   816                                       const void* src, size_t srcSize,
       
   817                                       const ZSTD_CDict* cdict);
       
   818 
       
   819 
       
   820 typedef struct ZSTD_DDict_s ZSTD_DDict;
       
   821 
       
   822 /*! ZSTD_createDDict() :
       
   823  *  Create a digested dictionary, ready to start decompression operation without startup delay.
       
   824  *  dictBuffer can be released after DDict creation, as its content is copied inside DDict. */
       
   825 ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
       
   826 
       
   827 /*! ZSTD_freeDDict() :
       
   828  *  Function frees memory allocated with ZSTD_createDDict() */
       
   829 ZSTDLIB_API size_t      ZSTD_freeDDict(ZSTD_DDict* ddict);
       
   830 
       
   831 /*! ZSTD_decompress_usingDDict() :
       
   832  *  Decompression using a digested Dictionary.
       
   833  *  Recommended when same dictionary is used multiple times. */
       
   834 ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
       
   835                                               void* dst, size_t dstCapacity,
       
   836                                         const void* src, size_t srcSize,
       
   837                                         const ZSTD_DDict* ddict);
       
   838 
       
   839 
       
   840 /********************************
       
   841  *  Dictionary helper functions
       
   842  *******************************/
       
   843 
       
   844 /*! ZSTD_getDictID_fromDict() :
       
   845  *  Provides the dictID stored within dictionary.
       
   846  *  if @return == 0, the dictionary is not conformant with Zstandard specification.
       
   847  *  It can still be loaded, but as a content-only dictionary. */
       
   848 ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
       
   849 
       
   850 /*! ZSTD_getDictID_fromDDict() :
       
   851  *  Provides the dictID of the dictionary loaded into `ddict`.
       
   852  *  If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
       
   853  *  Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
       
   854 ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
       
   855 
       
   856 /*! ZSTD_getDictID_fromFrame() :
       
   857  *  Provides the dictID required to decompressed the frame stored within `src`.
       
   858  *  If @return == 0, the dictID could not be decoded.
       
   859  *  This could for one of the following reasons :
       
   860  *  - The frame does not require a dictionary to be decoded (most common case).
       
   861  *  - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
       
   862  *    Note : this use case also happens when using a non-conformant dictionary.
       
   863  *  - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
       
   864  *  - This is not a Zstandard frame.
       
   865  *  When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
       
   866 ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
       
   867 
       
   868 
       
   869 /*******************************************************************************
       
   870  * Advanced dictionary and prefix API
       
   871  *
       
   872  * This API allows dictionaries to be used with ZSTD_compress2(),
       
   873  * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and
       
   874  * only reset with the context is reset with ZSTD_reset_parameters or
       
   875  * ZSTD_reset_session_and_parameters. Prefixes are single-use.
       
   876  ******************************************************************************/
       
   877 
   683 
   878 
   684 /*! ZSTD_CCtx_loadDictionary() :
   879 /*! ZSTD_CCtx_loadDictionary() :
   685  *  Create an internal CDict from `dict` buffer.
   880  *  Create an internal CDict from `dict` buffer.
   686  *  Decompression will have to use same dictionary.
   881  *  Decompression will have to use same dictionary.
   687  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
   882  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
   701 ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
   896 ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
   702 
   897 
   703 /*! ZSTD_CCtx_refCDict() :
   898 /*! ZSTD_CCtx_refCDict() :
   704  *  Reference a prepared dictionary, to be used for all next compressed frames.
   899  *  Reference a prepared dictionary, to be used for all next compressed frames.
   705  *  Note that compression parameters are enforced from within CDict,
   900  *  Note that compression parameters are enforced from within CDict,
   706  *  and supercede any compression parameter previously set within CCtx.
   901  *  and supersede any compression parameter previously set within CCtx.
       
   902  *  The parameters ignored are labled as "superseded-by-cdict" in the ZSTD_cParameter enum docs.
       
   903  *  The ignored parameters will be used again if the CCtx is returned to no-dictionary mode.
   707  *  The dictionary will remain valid for future compressed frames using same CCtx.
   904  *  The dictionary will remain valid for future compressed frames using same CCtx.
   708  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
   905  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
   709  *  Special : Referencing a NULL CDict means "return to no-dictionary mode".
   906  *  Special : Referencing a NULL CDict means "return to no-dictionary mode".
   710  *  Note 1 : Currently, only one dictionary can be managed.
   907  *  Note 1 : Currently, only one dictionary can be managed.
   711  *           Referencing a new dictionary effectively "discards" any previous one.
   908  *           Referencing a new dictionary effectively "discards" any previous one.
   731  *  Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dm_rawContent).
   928  *  Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dm_rawContent).
   732  *           Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */
   929  *           Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */
   733 ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
   930 ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
   734                                  const void* prefix, size_t prefixSize);
   931                                  const void* prefix, size_t prefixSize);
   735 
   932 
   736 
       
   737 typedef enum {
       
   738     ZSTD_reset_session_only = 1,
       
   739     ZSTD_reset_parameters = 2,
       
   740     ZSTD_reset_session_and_parameters = 3
       
   741 } ZSTD_ResetDirective;
       
   742 
       
   743 /*! ZSTD_CCtx_reset() :
       
   744  *  There are 2 different things that can be reset, independently or jointly :
       
   745  *  - The session : will stop compressing current frame, and make CCtx ready to start a new one.
       
   746  *                  Useful after an error, or to interrupt any ongoing compression.
       
   747  *                  Any internal data not yet flushed is cancelled.
       
   748  *                  Compression parameters and dictionary remain unchanged.
       
   749  *                  They will be used to compress next frame.
       
   750  *                  Resetting session never fails.
       
   751  *  - The parameters : changes all parameters back to "default".
       
   752  *                  This removes any reference to any dictionary too.
       
   753  *                  Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing)
       
   754  *                  otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())
       
   755  *  - Both : similar to resetting the session, followed by resetting parameters.
       
   756  */
       
   757 ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
       
   758 
       
   759 
       
   760 
       
   761 /*! ZSTD_compress2() :
       
   762  *  Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
       
   763  *  ZSTD_compress2() always starts a new frame.
       
   764  *  Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
       
   765  *  - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
       
   766  *  - The function is always blocking, returns when compression is completed.
       
   767  *  Hint : compression runs faster if `dstCapacity` >=  `ZSTD_compressBound(srcSize)`.
       
   768  * @return : compressed size written into `dst` (<= `dstCapacity),
       
   769  *           or an error code if it fails (which can be tested using ZSTD_isError()).
       
   770  */
       
   771 ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx,
       
   772                                    void* dst, size_t dstCapacity,
       
   773                              const void* src, size_t srcSize);
       
   774 
       
   775 typedef enum {
       
   776     ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
       
   777     ZSTD_e_flush=1,    /* flush any data provided so far,
       
   778                         * it creates (at least) one new block, that can be decoded immediately on reception;
       
   779                         * frame will continue: any future data can still reference previously compressed data, improving compression. */
       
   780     ZSTD_e_end=2       /* flush any remaining data _and_ close current frame.
       
   781                         * note that frame is only closed after compressed data is fully flushed (return value == 0).
       
   782                         * After that point, any additional data starts a new frame.
       
   783                         * note : each frame is independent (does not reference any content from previous frame). */
       
   784 } ZSTD_EndDirective;
       
   785 
       
   786 /*! ZSTD_compressStream2() :
       
   787  *  Behaves about the same as ZSTD_compressStream, with additional control on end directive.
       
   788  *  - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
       
   789  *  - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode)
       
   790  *  - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize
       
   791  *  - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
       
   792  *  - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller.
       
   793  *  - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available,
       
   794  *                                                  and then immediately returns, just indicating that there is some data remaining to be flushed.
       
   795  *                                                  The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
       
   796  *  - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking.
       
   797  *  - @return provides a minimum amount of data remaining to be flushed from internal buffers
       
   798  *            or an error code, which can be tested using ZSTD_isError().
       
   799  *            if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
       
   800  *            This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
       
   801  *            For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
       
   802  *  - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
       
   803  *            only ZSTD_e_end or ZSTD_e_flush operations are allowed.
       
   804  *            Before starting a new compression job, or changing compression parameters,
       
   805  *            it is required to fully flush internal buffers.
       
   806  */
       
   807 ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
       
   808                                          ZSTD_outBuffer* output,
       
   809                                          ZSTD_inBuffer* input,
       
   810                                          ZSTD_EndDirective endOp);
       
   811 
       
   812 
       
   813 
       
   814 /* ============================== */
       
   815 /*   Advanced decompression API   */
       
   816 /* ============================== */
       
   817 
       
   818 /* The advanced API pushes parameters one by one into an existing DCtx context.
       
   819  * Parameters are sticky, and remain valid for all following frames
       
   820  * using the same DCtx context.
       
   821  * It's possible to reset parameters to default values using ZSTD_DCtx_reset().
       
   822  * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream().
       
   823  *        Therefore, no new decompression function is necessary.
       
   824  */
       
   825 
       
   826 
       
   827 typedef enum {
       
   828 
       
   829     ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which
       
   830                               * the streaming API will refuse to allocate memory buffer
       
   831                               * in order to protect the host from unreasonable memory requirements.
       
   832                               * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
       
   833                               * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) */
       
   834 
       
   835     /* note : additional experimental parameters are also available
       
   836      * within the experimental section of the API.
       
   837      * At the time of this writing, they include :
       
   838      * ZSTD_c_format
       
   839      * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
       
   840      * note : never ever use experimentalParam? names directly
       
   841      */
       
   842      ZSTD_d_experimentalParam1=1000
       
   843 
       
   844 } ZSTD_dParameter;
       
   845 
       
   846 
       
   847 /*! ZSTD_dParam_getBounds() :
       
   848  *  All parameters must belong to an interval with lower and upper bounds,
       
   849  *  otherwise they will either trigger an error or be automatically clamped.
       
   850  * @return : a structure, ZSTD_bounds, which contains
       
   851  *         - an error status field, which must be tested using ZSTD_isError()
       
   852  *         - both lower and upper bounds, inclusive
       
   853  */
       
   854 ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);
       
   855 
       
   856 /*! ZSTD_DCtx_setParameter() :
       
   857  *  Set one compression parameter, selected by enum ZSTD_dParameter.
       
   858  *  All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds().
       
   859  *  Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
       
   860  *  Setting a parameter is only possible during frame initialization (before starting decompression).
       
   861  * @return : 0, or an error code (which can be tested using ZSTD_isError()).
       
   862  */
       
   863 ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value);
       
   864 
       
   865 
       
   866 /*! ZSTD_DCtx_loadDictionary() :
   933 /*! ZSTD_DCtx_loadDictionary() :
   867  *  Create an internal DDict from dict buffer,
   934  *  Create an internal DDict from dict buffer,
   868  *  to be used to decompress next frames.
   935  *  to be used to decompress next frames.
   869  *  The dictionary remains valid for all future frames, until explicitly invalidated.
   936  *  The dictionary remains valid for all future frames, until explicitly invalidated.
   870  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
   937  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
   908  *           A full dictionary is more costly, as it requires building tables.
   975  *           A full dictionary is more costly, as it requires building tables.
   909  */
   976  */
   910 ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
   977 ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
   911                                  const void* prefix, size_t prefixSize);
   978                                  const void* prefix, size_t prefixSize);
   912 
   979 
   913 /*! ZSTD_DCtx_reset() :
   980 /* ===   Memory management   === */
   914  *  Return a DCtx to clean state.
   981 
   915  *  Session and parameters can be reset jointly or separately.
   982 /*! ZSTD_sizeof_*() :
   916  *  Parameters can only be reset when no active frame is being decompressed.
   983  *  These functions give the _current_ memory usage of selected object.
   917  * @return : 0, or an error code, which can be tested with ZSTD_isError()
   984  *  Note that object memory usage can evolve (increase or decrease) over time. */
   918  */
   985 ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
   919 ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
   986 ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
   920 
   987 ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
   921 
   988 ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
       
   989 ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
       
   990 ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
       
   991 
       
   992 #endif  /* ZSTD_H_235446 */
       
   993 
       
   994 
       
   995 /* **************************************************************************************
       
   996  *   ADVANCED AND EXPERIMENTAL FUNCTIONS
       
   997  ****************************************************************************************
       
   998  * The definitions in the following section are considered experimental.
       
   999  * They are provided for advanced scenarios.
       
  1000  * They should never be used with a dynamic library, as prototypes may change in the future.
       
  1001  * Use them only in association with static linking.
       
  1002  * ***************************************************************************************/
       
  1003 
       
  1004 #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
       
  1005 #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
   922 
  1006 
   923 /****************************************************************************************
  1007 /****************************************************************************************
   924  *   experimental API (static linking only)
  1008  *   experimental API (static linking only)
   925  ****************************************************************************************
  1009  ****************************************************************************************
   926  * The following symbols and constants
  1010  * The following symbols and constants
   960 #define ZSTD_OVERLAPLOG_MAX       9
  1044 #define ZSTD_OVERLAPLOG_MAX       9
   961 
  1045 
   962 #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27   /* by default, the streaming decoder will refuse any frame
  1046 #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27   /* by default, the streaming decoder will refuse any frame
   963                                            * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size,
  1047                                            * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size,
   964                                            * to preserve host's memory from unreasonable requirements.
  1048                                            * to preserve host's memory from unreasonable requirements.
   965                                            * This limit can be overriden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).
  1049                                            * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).
   966                                            * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */
  1050                                            * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */
   967 
  1051 
   968 
  1052 
   969 /* LDM parameter bounds */
  1053 /* LDM parameter bounds */
   970 #define ZSTD_LDM_HASHLOG_MIN      ZSTD_HASHLOG_MIN
  1054 #define ZSTD_LDM_HASHLOG_MIN      ZSTD_HASHLOG_MIN
   973 #define ZSTD_LDM_MINMATCH_MAX     4096
  1057 #define ZSTD_LDM_MINMATCH_MAX     4096
   974 #define ZSTD_LDM_BUCKETSIZELOG_MIN   1
  1058 #define ZSTD_LDM_BUCKETSIZELOG_MIN   1
   975 #define ZSTD_LDM_BUCKETSIZELOG_MAX   8
  1059 #define ZSTD_LDM_BUCKETSIZELOG_MAX   8
   976 #define ZSTD_LDM_HASHRATELOG_MIN     0
  1060 #define ZSTD_LDM_HASHRATELOG_MIN     0
   977 #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)
  1061 #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)
       
  1062 
       
  1063 /* Advanced parameter bounds */
       
  1064 #define ZSTD_TARGETCBLOCKSIZE_MIN   64
       
  1065 #define ZSTD_TARGETCBLOCKSIZE_MAX   ZSTD_BLOCKSIZE_MAX
   978 
  1066 
   979 /* internal */
  1067 /* internal */
   980 #define ZSTD_HASHLOG3_MAX           17
  1068 #define ZSTD_HASHLOG3_MAX           17
   981 
  1069 
   982 
  1070 
  1062     ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */
  1150     ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */
  1063     ZSTD_dictForceAttach   = 1, /* Never copy the dictionary. */
  1151     ZSTD_dictForceAttach   = 1, /* Never copy the dictionary. */
  1064     ZSTD_dictForceCopy     = 2, /* Always copy the dictionary. */
  1152     ZSTD_dictForceCopy     = 2, /* Always copy the dictionary. */
  1065 } ZSTD_dictAttachPref_e;
  1153 } ZSTD_dictAttachPref_e;
  1066 
  1154 
       
  1155 typedef enum {
       
  1156   ZSTD_lcm_auto = 0,          /**< Automatically determine the compression mode based on the compression level.
       
  1157                                *   Negative compression levels will be uncompressed, and positive compression
       
  1158                                *   levels will be compressed. */
       
  1159   ZSTD_lcm_huffman = 1,       /**< Always attempt Huffman compression. Uncompressed literals will still be
       
  1160                                *   emitted if Huffman compression is not profitable. */
       
  1161   ZSTD_lcm_uncompressed = 2,  /**< Always emit uncompressed literals. */
       
  1162 } ZSTD_literalCompressionMode_e;
       
  1163 
  1067 
  1164 
  1068 /***************************************
  1165 /***************************************
  1069 *  Frame size functions
  1166 *  Frame size functions
  1070 ***************************************/
  1167 ***************************************/
  1071 
  1168 
  1072 /*! ZSTD_findDecompressedSize() :
  1169 /*! ZSTD_findDecompressedSize() :
  1073  *  `src` should point the start of a series of ZSTD encoded and/or skippable frames
  1170  *  `src` should point to the start of a series of ZSTD encoded and/or skippable frames
  1074  *  `srcSize` must be the _exact_ size of this series
  1171  *  `srcSize` must be the _exact_ size of this series
  1075  *       (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`)
  1172  *       (i.e. there should be a frame boundary at `src + srcSize`)
  1076  *  @return : - decompressed size of all data in all successive frames
  1173  *  @return : - decompressed size of all data in all successive frames
  1077  *            - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
  1174  *            - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
  1078  *            - if an error occurred: ZSTD_CONTENTSIZE_ERROR
  1175  *            - if an error occurred: ZSTD_CONTENTSIZE_ERROR
  1079  *
  1176  *
  1080  *   note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
  1177  *   note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
  1090  *   note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
  1187  *   note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
  1091  *            read each contained frame header.  This is fast as most of the data is skipped,
  1188  *            read each contained frame header.  This is fast as most of the data is skipped,
  1092  *            however it does mean that all frame data must be present and valid. */
  1189  *            however it does mean that all frame data must be present and valid. */
  1093 ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
  1190 ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
  1094 
  1191 
       
  1192 /*! ZSTD_decompressBound() :
       
  1193  *  `src` should point to the start of a series of ZSTD encoded and/or skippable frames
       
  1194  *  `srcSize` must be the _exact_ size of this series
       
  1195  *       (i.e. there should be a frame boundary at `src + srcSize`)
       
  1196  *  @return : - upper-bound for the decompressed size of all data in all successive frames
       
  1197  *            - if an error occured: ZSTD_CONTENTSIZE_ERROR
       
  1198  *
       
  1199  *  note 1  : an error can occur if `src` contains an invalid or incorrectly formatted frame.
       
  1200  *  note 2  : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`.
       
  1201  *            in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value.
       
  1202  *  note 3  : when the decompressed size field isn't available, the upper-bound for that frame is calculated by:
       
  1203  *              upper-bound = # blocks * min(128 KB, Window_Size)
       
  1204  */
       
  1205 ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize);
       
  1206 
  1095 /*! ZSTD_frameHeaderSize() :
  1207 /*! ZSTD_frameHeaderSize() :
  1096  *  srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.
  1208  *  srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.
  1097  * @return : size of the Frame Header,
  1209  * @return : size of the Frame Header,
  1098  *           or an error code (if srcSize is too small) */
  1210  *           or an error code (if srcSize is too small) */
  1099 ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
  1211 ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
  1108  *  of a future {D,C}Ctx, before its creation.
  1220  *  of a future {D,C}Ctx, before its creation.
  1109  *  ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one.
  1221  *  ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one.
  1110  *  It will also consider src size to be arbitrarily "large", which is worst case.
  1222  *  It will also consider src size to be arbitrarily "large", which is worst case.
  1111  *  If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation.
  1223  *  If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation.
  1112  *  ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
  1224  *  ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
  1113  *  ZSTD_estimateCCtxSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
  1225  *  ZSTD_estimateCCtxSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
  1114  *  Note : CCtx size estimation is only correct for single-threaded compression. */
  1226  *  Note : CCtx size estimation is only correct for single-threaded compression. */
  1115 ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
  1227 ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
  1116 ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
  1228 ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
  1117 ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
  1229 ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
  1118 ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
  1230 ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
  1120 /*! ZSTD_estimateCStreamSize() :
  1232 /*! ZSTD_estimateCStreamSize() :
  1121  *  ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
  1233  *  ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
  1122  *  It will also consider src size to be arbitrarily "large", which is worst case.
  1234  *  It will also consider src size to be arbitrarily "large", which is worst case.
  1123  *  If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
  1235  *  If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
  1124  *  ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
  1236  *  ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
  1125  *  ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
  1237  *  ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
  1126  *  Note : CStream size estimation is only correct for single-threaded compression.
  1238  *  Note : CStream size estimation is only correct for single-threaded compression.
  1127  *  ZSTD_DStream memory budget depends on window Size.
  1239  *  ZSTD_DStream memory budget depends on window Size.
  1128  *  This information can be passed manually, using ZSTD_estimateDStreamSize,
  1240  *  This information can be passed manually, using ZSTD_estimateDStreamSize,
  1129  *  or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
  1241  *  or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
  1130  *  Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
  1242  *  Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
  1224  *  As a consequence, `dictBuffer` **must** outlive CDict,
  1336  *  As a consequence, `dictBuffer` **must** outlive CDict,
  1225  *  and its content must remain unmodified throughout the lifetime of CDict. */
  1337  *  and its content must remain unmodified throughout the lifetime of CDict. */
  1226 ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
  1338 ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
  1227 
  1339 
  1228 /*! ZSTD_getCParams() :
  1340 /*! ZSTD_getCParams() :
  1229 *   @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
  1341  * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
  1230 *   `estimatedSrcSize` value is optional, select 0 if not known */
  1342  * `estimatedSrcSize` value is optional, select 0 if not known */
  1231 ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  1343 ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  1232 
  1344 
  1233 /*! ZSTD_getParams() :
  1345 /*! ZSTD_getParams() :
  1234 *   same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
  1346  *  same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
  1235 *   All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
  1347  *  All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
  1236 ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  1348 ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  1237 
  1349 
  1238 /*! ZSTD_checkCParams() :
  1350 /*! ZSTD_checkCParams() :
  1239 *   Ensure param values remain within authorized range */
  1351  *  Ensure param values remain within authorized range.
       
  1352  * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */
  1240 ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
  1353 ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
  1241 
  1354 
  1242 /*! ZSTD_adjustCParams() :
  1355 /*! ZSTD_adjustCParams() :
  1243  *  optimize params for a given `srcSize` and `dictSize`.
  1356  *  optimize params for a given `srcSize` and `dictSize`.
  1244  *  both values are optional, select `0` if unknown. */
  1357  * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN.
       
  1358  * `dictSize` must be `0` when there is no dictionary.
       
  1359  *  cPar can be invalid : all parameters will be clamped within valid range in the @return struct.
       
  1360  *  This function never fails (wide contract) */
  1245 ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
  1361 ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
  1246 
  1362 
  1247 /*! ZSTD_compress_advanced() :
  1363 /*! ZSTD_compress_advanced() :
  1248  *  Same as ZSTD_compress_usingDict(), with fine-tune control over compression parameters (by structure) */
  1364  *  Same as ZSTD_compress_usingDict(), with fine-tune control over compression parameters (by structure) */
  1249 ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,
  1365 ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,
  1312  * are used in place, or copied into the working context.
  1428  * are used in place, or copied into the working context.
  1313  * Accepts values from the ZSTD_dictAttachPref_e enum.
  1429  * Accepts values from the ZSTD_dictAttachPref_e enum.
  1314  * See the comments on that enum for an explanation of the feature. */
  1430  * See the comments on that enum for an explanation of the feature. */
  1315 #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4
  1431 #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4
  1316 
  1432 
       
  1433 /* Controls how the literals are compressed (default is auto).
       
  1434  * The value must be of type ZSTD_literalCompressionMode_e.
       
  1435  * See ZSTD_literalCompressionMode_t enum definition for details.
       
  1436  */
       
  1437 #define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5
       
  1438 
       
  1439 /* Tries to fit compressed block size to be around targetCBlockSize.
       
  1440  * No target when targetCBlockSize == 0.
       
  1441  * There is no guarantee on compressed block size (default:0) */
       
  1442 #define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6
       
  1443 
  1317 /*! ZSTD_CCtx_getParameter() :
  1444 /*! ZSTD_CCtx_getParameter() :
  1318  *  Get the requested compression parameter value, selected by enum ZSTD_cParameter,
  1445  *  Get the requested compression parameter value, selected by enum ZSTD_cParameter,
  1319  *  and store it into int* value.
  1446  *  and store it into int* value.
  1320  * @return : 0, or an error code (which can be tested with ZSTD_isError()).
  1447  * @return : 0, or an error code (which can be tested with ZSTD_isError()).
  1321  */
  1448  */
  1323 
  1450 
  1324 
  1451 
  1325 /*! ZSTD_CCtx_params :
  1452 /*! ZSTD_CCtx_params :
  1326  *  Quick howto :
  1453  *  Quick howto :
  1327  *  - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
  1454  *  - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
  1328  *  - ZSTD_CCtxParam_setParameter() : Push parameters one by one into
  1455  *  - ZSTD_CCtxParams_setParameter() : Push parameters one by one into
  1329  *                                    an existing ZSTD_CCtx_params structure.
  1456  *                                     an existing ZSTD_CCtx_params structure.
  1330  *                                    This is similar to
  1457  *                                     This is similar to
  1331  *                                    ZSTD_CCtx_setParameter().
  1458  *                                     ZSTD_CCtx_setParameter().
  1332  *  - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
  1459  *  - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
  1333  *                                    an existing CCtx.
  1460  *                                    an existing CCtx.
  1334  *                                    These parameters will be applied to
  1461  *                                    These parameters will be applied to
  1335  *                                    all subsequent frames.
  1462  *                                    all subsequent frames.
  1336  *  - ZSTD_compressStream2() : Do compression using the CCtx.
  1463  *  - ZSTD_compressStream2() : Do compression using the CCtx.
  1357  *  Initializes the compression and frame parameters of cctxParams according to
  1484  *  Initializes the compression and frame parameters of cctxParams according to
  1358  *  params. All other parameters are reset to their default values.
  1485  *  params. All other parameters are reset to their default values.
  1359  */
  1486  */
  1360 ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
  1487 ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
  1361 
  1488 
  1362 /*! ZSTD_CCtxParam_setParameter() :
  1489 /*! ZSTD_CCtxParams_setParameter() :
  1363  *  Similar to ZSTD_CCtx_setParameter.
  1490  *  Similar to ZSTD_CCtx_setParameter.
  1364  *  Set one compression parameter, selected by enum ZSTD_cParameter.
  1491  *  Set one compression parameter, selected by enum ZSTD_cParameter.
  1365  *  Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
  1492  *  Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
  1366  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1493  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1367  */
  1494  */
  1368 ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);
  1495 ZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);
  1369 
  1496 
  1370 /*! ZSTD_CCtxParam_getParameter() :
  1497 /*! ZSTD_CCtxParams_getParameter() :
  1371  * Similar to ZSTD_CCtx_getParameter.
  1498  * Similar to ZSTD_CCtx_getParameter.
  1372  * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
  1499  * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
  1373  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1500  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1374  */
  1501  */
  1375 ZSTDLIB_API size_t ZSTD_CCtxParam_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);
  1502 ZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);
  1376 
  1503 
  1377 /*! ZSTD_CCtx_setParametersUsingCCtxParams() :
  1504 /*! ZSTD_CCtx_setParametersUsingCCtxParams() :
  1378  *  Apply a set of ZSTD_CCtx_params to the compression context.
  1505  *  Apply a set of ZSTD_CCtx_params to the compression context.
  1379  *  This can be done even after compression is started,
  1506  *  This can be done even after compression is started,
  1380  *    if nbWorkers==0, this will have no impact until a new compression is started.
  1507  *    if nbWorkers==0, this will have no impact until a new compression is started.
  1413  *  Dictionary content is referenced, and therefore stays in dictBuffer.
  1540  *  Dictionary content is referenced, and therefore stays in dictBuffer.
  1414  *  It is important that dictBuffer outlives DDict,
  1541  *  It is important that dictBuffer outlives DDict,
  1415  *  it must remain read accessible throughout the lifetime of DDict */
  1542  *  it must remain read accessible throughout the lifetime of DDict */
  1416 ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
  1543 ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
  1417 
  1544 
  1418 
       
  1419 /*! ZSTD_getDictID_fromDict() :
       
  1420  *  Provides the dictID stored within dictionary.
       
  1421  *  if @return == 0, the dictionary is not conformant with Zstandard specification.
       
  1422  *  It can still be loaded, but as a content-only dictionary. */
       
  1423 ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
       
  1424 
       
  1425 /*! ZSTD_getDictID_fromDDict() :
       
  1426  *  Provides the dictID of the dictionary loaded into `ddict`.
       
  1427  *  If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
       
  1428  *  Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
       
  1429 ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
       
  1430 
       
  1431 /*! ZSTD_getDictID_fromFrame() :
       
  1432  *  Provides the dictID required to decompressed the frame stored within `src`.
       
  1433  *  If @return == 0, the dictID could not be decoded.
       
  1434  *  This could for one of the following reasons :
       
  1435  *  - The frame does not require a dictionary to be decoded (most common case).
       
  1436  *  - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
       
  1437  *    Note : this use case also happens when using a non-conformant dictionary.
       
  1438  *  - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
       
  1439  *  - This is not a Zstandard frame.
       
  1440  *  When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
       
  1441 ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
       
  1442 
       
  1443 /*! ZSTD_DCtx_loadDictionary_byReference() :
  1545 /*! ZSTD_DCtx_loadDictionary_byReference() :
  1444  *  Same as ZSTD_DCtx_loadDictionary(),
  1546  *  Same as ZSTD_DCtx_loadDictionary(),
  1445  *  but references `dict` content instead of copying it into `dctx`.
  1547  *  but references `dict` content instead of copying it into `dctx`.
  1446  *  This saves memory if `dict` remains around.,
  1548  *  This saves memory if `dict` remains around.,
  1447  *  However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */
  1549  *  However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */
  1499 *  Once Advanced API reaches "stable" status,
  1601 *  Once Advanced API reaches "stable" status,
  1500 *  redundant functions will be deprecated, and then at some point removed.
  1602 *  redundant functions will be deprecated, and then at some point removed.
  1501 ********************************************************************/
  1603 ********************************************************************/
  1502 
  1604 
  1503 /*=====   Advanced Streaming compression functions  =====*/
  1605 /*=====   Advanced Streaming compression functions  =====*/
  1504 ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize);   /**< pledgedSrcSize must be correct. If it is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, "0" also disables frame content size field. It may be enabled in the future. */
  1606 /**! ZSTD_initCStream_srcSize() :
  1505 ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.*/
  1607  * This function is deprecated, and equivalent to:
       
  1608  *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
       
  1609  *     ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
       
  1610  *     ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
       
  1611  *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
       
  1612  *
       
  1613  * pledgedSrcSize must be correct. If it is not known at init time, use
       
  1614  * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs,
       
  1615  * "0" also disables frame content size field. It may be enabled in the future.
       
  1616  */
       
  1617 ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize);
       
  1618 /**! ZSTD_initCStream_usingDict() :
       
  1619  * This function is deprecated, and is equivalent to:
       
  1620  *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
       
  1621  *     ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
       
  1622  *     ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
       
  1623  *
       
  1624  * Creates of an internal CDict (incompatible with static CCtx), except if
       
  1625  * dict == NULL or dictSize < 8, in which case no dict is used.
       
  1626  * Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if
       
  1627  * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.
       
  1628  */
       
  1629 ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
       
  1630 /**! ZSTD_initCStream_advanced() :
       
  1631  * This function is deprecated, and is approximately equivalent to:
       
  1632  *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
       
  1633  *     ZSTD_CCtx_setZstdParams(zcs, params); // Set the zstd params and leave the rest as-is
       
  1634  *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
       
  1635  *     ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
       
  1636  *
       
  1637  * pledgedSrcSize must be correct. If srcSize is not known at init time, use
       
  1638  * value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy.
       
  1639  */
  1506 ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
  1640 ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
  1507                                              ZSTD_parameters params, unsigned long long pledgedSrcSize);  /**< pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. */
  1641                                              ZSTD_parameters params, unsigned long long pledgedSrcSize);
  1508 ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict);  /**< note : cdict will just be referenced, and must outlive compression session */
  1642 /**! ZSTD_initCStream_usingCDict() :
  1509 ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize);  /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters. pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. */
  1643  * This function is deprecated, and equivalent to:
       
  1644  *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
       
  1645  *     ZSTD_CCtx_refCDict(zcs, cdict);
       
  1646  *
       
  1647  * note : cdict will just be referenced, and must outlive compression session
       
  1648  */
       
  1649 ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict);
       
  1650 /**! ZSTD_initCStream_usingCDict_advanced() :
       
  1651  * This function is deprecated, and is approximately equivalent to:
       
  1652  *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
       
  1653  *     ZSTD_CCtx_setZstdFrameParams(zcs, fParams); // Set the zstd frame params and leave the rest as-is
       
  1654  *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
       
  1655  *     ZSTD_CCtx_refCDict(zcs, cdict);
       
  1656  *
       
  1657  * same as ZSTD_initCStream_usingCDict(), with control over frame parameters.
       
  1658  * pledgedSrcSize must be correct. If srcSize is not known at init time, use
       
  1659  * value ZSTD_CONTENTSIZE_UNKNOWN.
       
  1660  */
       
  1661 ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize);
  1510 
  1662 
  1511 /*! ZSTD_resetCStream() :
  1663 /*! ZSTD_resetCStream() :
       
  1664  * This function is deprecated, and is equivalent to:
       
  1665  *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
       
  1666  *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
       
  1667  *
  1512  *  start a new frame, using same parameters from previous frame.
  1668  *  start a new frame, using same parameters from previous frame.
  1513  *  This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
  1669  *  This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
  1514  *  Note that zcs must be init at least once before using ZSTD_resetCStream().
  1670  *  Note that zcs must be init at least once before using ZSTD_resetCStream().
  1515  *  If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
  1671  *  If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
  1516  *  If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
  1672  *  If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
  1553  */
  1709  */
  1554 ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
  1710 ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
  1555 
  1711 
  1556 
  1712 
  1557 /*=====   Advanced Streaming decompression functions  =====*/
  1713 /*=====   Advanced Streaming decompression functions  =====*/
  1558 ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: no dictionary will be used if dict == NULL or dictSize < 8 */
  1714 /**
  1559 ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict);  /**< note : ddict is referenced, it must outlive decompression session */
  1715  * This function is deprecated, and is equivalent to:
  1560 ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);  /**< re-use decompression parameters from previous init; saves dictionary loading */
  1716  *
       
  1717  *     ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
       
  1718  *     ZSTD_DCtx_loadDictionary(zds, dict, dictSize);
       
  1719  *
       
  1720  * note: no dictionary will be used if dict == NULL or dictSize < 8
       
  1721  */
       
  1722 ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
       
  1723 /**
       
  1724  * This function is deprecated, and is equivalent to:
       
  1725  *
       
  1726  *     ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
       
  1727  *     ZSTD_DCtx_refDDict(zds, ddict);
       
  1728  *
       
  1729  * note : ddict is referenced, it must outlive decompression session
       
  1730  */
       
  1731 ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict);
       
  1732 /**
       
  1733  * This function is deprecated, and is equivalent to:
       
  1734  *
       
  1735  *     ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
       
  1736  *
       
  1737  * re-use decompression parameters from previous init; saves dictionary loading
       
  1738  */
       
  1739 ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);
  1561 
  1740 
  1562 
  1741 
  1563 /*********************************************************************
  1742 /*********************************************************************
  1564 *  Buffer-less and synchronous inner streaming functions
  1743 *  Buffer-less and synchronous inner streaming functions
  1565 *
  1744 *
  1694     unsigned headerSize;
  1873     unsigned headerSize;
  1695     unsigned dictID;
  1874     unsigned dictID;
  1696     unsigned checksumFlag;
  1875     unsigned checksumFlag;
  1697 } ZSTD_frameHeader;
  1876 } ZSTD_frameHeader;
  1698 
  1877 
  1699 /** ZSTD_getFrameHeader() :
  1878 /*! ZSTD_getFrameHeader() :
  1700  *  decode Frame Header, or requires larger `srcSize`.
  1879  *  decode Frame Header, or requires larger `srcSize`.
  1701  * @return : 0, `zfhPtr` is correctly filled,
  1880  * @return : 0, `zfhPtr` is correctly filled,
  1702  *          >0, `srcSize` is too small, value is wanted `srcSize` amount,
  1881  *          >0, `srcSize` is too small, value is wanted `srcSize` amount,
  1703  *           or an error code, which can be tested using ZSTD_isError() */
  1882  *           or an error code, which can be tested using ZSTD_isError() */
  1704 ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize);   /**< doesn't consume input */
  1883 ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize);   /**< doesn't consume input */
  1728 /* ============================ */
  1907 /* ============================ */
  1729 
  1908 
  1730 /*!
  1909 /*!
  1731     Block functions produce and decode raw zstd blocks, without frame metadata.
  1910     Block functions produce and decode raw zstd blocks, without frame metadata.
  1732     Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
  1911     Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
  1733     User will have to take in charge required information to regenerate data, such as compressed and content sizes.
  1912     But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes.
  1734 
  1913 
  1735     A few rules to respect :
  1914     A few rules to respect :
  1736     - Compressing and decompressing require a context structure
  1915     - Compressing and decompressing require a context structure
  1737       + Use ZSTD_createCCtx() and ZSTD_createDCtx()
  1916       + Use ZSTD_createCCtx() and ZSTD_createDCtx()
  1738     - It is necessary to init context before starting
  1917     - It is necessary to init context before starting
  1739       + compression : any ZSTD_compressBegin*() variant, including with dictionary
  1918       + compression : any ZSTD_compressBegin*() variant, including with dictionary
  1740       + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
  1919       + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
  1741       + copyCCtx() and copyDCtx() can be used too
  1920       + copyCCtx() and copyDCtx() can be used too
  1742     - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
  1921     - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
  1743       + If input is larger than a block size, it's necessary to split input data into multiple blocks
  1922       + If input is larger than a block size, it's necessary to split input data into multiple blocks
  1744       + For inputs larger than a single block, really consider using regular ZSTD_compress() instead.
  1923       + For inputs larger than a single block, consider using regular ZSTD_compress() instead.
  1745         Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
  1924         Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block.
  1746     - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
  1925     - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) !
  1747       In which case, nothing is produced into `dst` !
  1926       ===> In which case, nothing is produced into `dst` !
  1748       + User must test for such outcome and deal directly with uncompressed data
  1927       + User __must__ test for such outcome and deal directly with uncompressed data
  1749       + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
  1928       + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0.
       
  1929         Doing so would mess up with statistics history, leading to potential data corruption.
       
  1930       + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !!
  1750       + In case of multiple successive blocks, should some of them be uncompressed,
  1931       + In case of multiple successive blocks, should some of them be uncompressed,
  1751         decoder must be informed of their existence in order to follow proper history.
  1932         decoder must be informed of their existence in order to follow proper history.
  1752         Use ZSTD_insertBlock() for such a case.
  1933         Use ZSTD_insertBlock() for such a case.
  1753 */
  1934 */
  1754 
  1935