Compression Helpers
ZSTD_compressBound()
Calculates the maximum compressed size in worst-case single-pass scenario.Parameters
Size of the source data in bytes.
Return Value
Returns the maximum compressed size. If
srcSize >= ZSTD_MAX_INPUT_SIZE, returns an error code which can be tested using ZSTD_isError().Important Notes
- When invoking
ZSTD_compress()or any other one-pass compression function, it’s recommended to providedstCapacity >= ZSTD_compressBound(srcSize) - This eliminates one potential failure scenario: not enough room in destination buffer
- This function itself can fail if
srcSize >= ZSTD_MAX_INPUT_SIZE
Example
Decompression Helpers
ZSTD_getFrameContentSize()
Returns the decompressed content size stored in a ZSTD frame header.Parameters
Pointer to the beginning of a ZSTD encoded frame.
Size of the buffer pointed to by
src. It must be at least as large as the frame header. Any value greater than or equal to ZSTD_FRAMEHEADERSIZE_MAX (18 bytes) is sufficient.Return Value
- The decompressed size in bytes when the value is available in the frame header
ZSTD_CONTENTSIZE_UNKNOWN(0ULL - 1) - The frame does not encode a decompressed size (typical for streaming)ZSTD_CONTENTSIZE_ERROR(0ULL - 2) - An error occurred (e.g., invalid magic number, srcSize too small)
Important Notes
- The return value is not compatible with
ZSTD_isError() - A return value of 0 denotes a valid but empty frame. Skippable frames also report 0
- The decompressed size field is optional. When absent, the caller must rely on streaming decompression or an application-specific upper bound
- The decompressed size is guaranteed to be present when compression was performed with single-pass APIs such as
ZSTD_compress(),ZSTD_compressCCtx(),ZSTD_compress_usingDict(), orZSTD_compress_usingCDict() - When processing untrusted input, validate the returned size against the application’s limits
Example
ZSTD_findFrameCompressedSize()
Finds the compressed size of the first frame starting at the source pointer.Parameters
Should point to the start of a ZSTD frame or skippable frame.
Must be >= first frame size.
Return Value
The compressed size of the first frame starting at
src, suitable to pass as srcSize to ZSTD_decompress or similar, or an error code if input is invalid.Important Notes
- This method is called
find*()because it’s not enough to read the header; it may have to scan through the frame’s content to reach its end - This method also works with Skippable Frames, returning the size of the complete skippable frame (content size + 8 bytes for headers)
- Requires v1.4.0+
Error Handling
ZSTD_isError()
Tests if a function result is an error code.Parameters
The return value from a zstd function.
Return Value
Returns 1 if error, 0 otherwise.
Important Notes
- Most
ZSTD_*functions returning asize_tvalue can be tested for error using this function - Some functions like
ZSTD_getFrameContentSize()use special return values and are not compatible withZSTD_isError()
ZSTD_getErrorName()
Provides a readable string from a function result.Parameters
The return value from a zstd function.
Return Value
Returns a human-readable error string. If the result is not an error, returns a success message.
Example
ZSTD_getErrorCode()
Converts a function result into an error code enum.Parameters
The return value from a zstd function.
Return Value
Returns an error code that can be compared to the error enum list defined in
zstd_errors.h.Compression Level Helpers
ZSTD_minCLevel()
Returns the minimum negative compression level allowed.Return Value
Minimum negative compression level (typically a negative value for fastest compression).
Important Notes
- Requires v1.4.0+
- Negative compression levels extend the range of speed vs. ratio preferences
- The lower the level, the faster the speed (at the cost of compression ratio)
ZSTD_maxCLevel()
Returns the maximum compression level available.Return Value
Maximum compression level available (currently 22).
Important Notes
- Levels >= 20, labeled
--ultra, should be used with caution as they require more memory
ZSTD_defaultCLevel()
Returns the default compression level.Return Value
Default compression level, specified by
ZSTD_CLEVEL_DEFAULT (typically 3).Important Notes
- Requires v1.5.0+
Context Management
ZSTD_createCCtx()
Creates a compression context.Return Value
Returns a pointer to the newly created compression context, or NULL if allocation failed.
Important Notes
- When compressing many times, it is recommended to allocate a compression context just once and reuse it
- This will make the workload easier for system’s memory
- Re-using context is just a speed/resource optimization; it doesn’t change the compression ratio
- For parallel execution in multi-threaded environments, use one different context per thread
ZSTD_freeCCtx()
Frees a compression context.Parameters
Pointer to the compression context to free. Compatible with NULL pointer.
Return Value
Returns 0 (this function never fails).
ZSTD_createDCtx()
Creates a decompression context.Return Value
Returns a pointer to the newly created decompression context, or NULL if allocation failed.
Important Notes
- When decompressing many times, it is recommended to allocate a context only once and reuse it
- This will make workload friendlier for system’s memory
- Use one context per thread for parallel execution
ZSTD_freeDCtx()
Frees a decompression context.Parameters
Pointer to the decompression context to free. Accepts NULL pointer.
Return Value
Returns 0 (this function never fails).
Version Information
ZSTD_versionNumber()
Returns the runtime library version number.Return Value
Returns the version number formatted as:
MAJOR*100*100 + MINOR*100 + RELEASE.Example
ZSTD_versionString()
Returns the runtime library version as a string.Return Value
Returns a pointer to a string containing the version (e.g., “1.6.0”).
Important Notes
- Requires v1.3.0+
- The returned string is statically allocated and should not be freed
Example
Dictionary Helpers
ZSTD_getDictID_fromFrame()
Provides the dictionary ID required to decompress the frame stored within source data.Parameters
Pointer to the beginning of a compressed frame.
Size of the source buffer.
Return Value
Returns the dictionary ID, or 0 if:
- The frame does not require a dictionary (most common case)
- The frame was built with dictID intentionally removed
srcSizeis too small and the frame header could not be decoded- This is not a Zstandard frame
Important Notes
- Requires v1.4.0+
- When identifying the exact failure cause, use
ZSTD_getFrameHeader()for a more precise error code