Zstandard - Fast real-time compression algorithm
version-number
version-string
min-c-level
default-c-level
max-c-level
exn:  fail:  zstd
get-error-name
1 About FFI
2 Simple API
compress
decompress
3 Explicit Contexts
cctx?
make-compression-context
dctx?
make-decompression-context
compress-context
decompress-context
4 Advanced API
valid-compression-parameter?
valid-compression-strategy?
compression-parameter-get-bounds
cctx-set-parameter
cctx-set-pledged-src-size
cctx-reset
compress-advanced
valid-decompression-parameter?
decompression-parameter-get-bounds
dctx-set-parameter
dctx-reset
5 Streaming API
init-compress-stream
compress-stream
init-decompress-stream
decompress-stream
6 Simple Dictionary API
compress-using-dict
decompress-using-dict
7 Bulk Dictionary API
cdict?
ddict?
create-cdict
create-ddict
compress-using-cdict
decompress-using-ddict
8 Dictionary Helpers
dict-id
cdict-dict-id
ddict-dict-id
dict-id-from-frame
9 Advanced Dictionary API
cctx-load-dictionary
dctx-load-dictionary
10 Dictionary Training
train-dict
11 Extra Helpers
zstd-through-ports
unzstd-through-ports
12 License and Contributing
8.12

Zstandard - Fast real-time compression algorithm🔗ℹ

 (require zstd) package: zstd

This collection provides Racket bindings to the reference C implementation of the RFC8878 Zstandard compression format, libzstd.

Binding names are provided by this module without prefixes. To prevent name clashes, it is recommended you require the library using prefix-in as follows:

(require (prefix-in zstd: zstd))
(zstd:compress #"data to compress" 1)

For the rest of this manual, names will be referred to without prefix.

The version number of libzstd, as returned by ZSTD_versionNumber.

The version string of libzstd, as returned by ZSTD_versionString.

The minimum supported compression level.

The default compression level. Use this if you don’t know what else to use.

The maximum supported compression level.

struct

(struct exn:fail:zstd exn:fail (code)
    #:transparent)
  code : number?
Exception type raised whenever an underlying libzstd ffi call returns an error.

procedure

(get-error-name code)  string?

  code : exact-integer?
Returns the string error message corresponding to libzstd error code code.

1 About FFI🔗ℹ

Please note that this library is a binding to a native library via FFI. Native calls to FFI functions suspend all Racket threads and cannot be preempted.

You should use caution when using these functions on codepaths which are both latency- sensitive and accept very large inputs. Alternative solutions are to have a dedicated place responsible for running compression and decompression, or using the streaming API’s, which operate in chunks and thus allow for the Racket runtime to run between chunks of work.

2 Simple API🔗ℹ

procedure

(compress src compression-level)  bytes?

  src : bytes?
  compression-level : exact-integer?
Corresponds to ZSTD_compress.

Compresses src at level compression-level and returns the compressed result in a fresh byte string.

procedure

(decompress src [dest-capacity])  bytes?

  src : bytes?
  dest-capacity : (or/c exact-nonnegative-integer? #f) = #f
Correponds to ZSTD_decompress.

Decompresses src and returns the result in a fresh byte string.

If dest-capacity is not #f, it is used directly as an upper bound on the length of the result. Providing a value that is too small for the decompressed data will raise exn:fail:zstd.

If dest-capacity is #f, the decompressed size is estimated using ZSTD_getFrameContentSize. If the size still cannot be determined, for example if the ZStandard metadata does not include the decompressed size or src is malformed, exn:fail:zstd is raised.

Note that the size estimated by ZSTD_getFrameContentSize can be very large. If your application handles inputs of unknown size, you should provide an explicit dest-capacity limiting the output size, or use the streaming API, to avoid denial-of-service attacks.

3 Explicit Contexts🔗ℹ

An explicit context object allows the library to reuse some state, providing a speed boost. Context objects also hold parameters used to fine-tune the compression and decompression.

procedure

(cctx? v)  boolean?

  v : any/c
Returns #t if v is a compression context.

Creates a compression context. Underlying native resources are automatically released when the returned value is garbage-collected.

procedure

(dctx? v)  boolean?

  v : any/c
Returns #t if v is a decompression context.

Creates a decompression context. Underlying native resources are automatically released when the returned value is garbage-collected.

procedure

(compress-context cctx    
  src    
  compression-level)  bytes?
  cctx : cctx?
  src : bytes?
  compression-level : exact-integer?
Corresponds to ZSTD_compressCCtx.

Compresses src at level compression-level and returns the compressed result in a fresh byte string.

procedure

(decompress-context dctx src [dest-capacity])  bytes?

  dctx : dctx?
  src : bytes?
  dest-capacity : (or/c exact-nonnegative-integer? #f) = #f
Corresponds to ZSTD_decompressDCtx.

Decompresses src and returns the result in a fresh byte string.

dest-capacity is handled as in decompress.

4 Advanced API🔗ℹ

The advaned API allows parameters to be set on compression and decompression contexts.

procedure

(valid-compression-parameter? v)  boolean?

  v : any/c
Returns #t if v is the valid name of a compression parameter.

The list of valid parameter names is given by the C enum ZSTD_cParameter.

Only non-experimental parameters are supported, and the names are mapped from the C names as follows:

So, for example, the C enum constant ZSTD_c_ldmHashRateLog is expressed as 'zstd-c-ldm-hash-rate-log.

procedure

(valid-compression-strategy? v)  boolean?

  v : any/c
Returns #t if v is the valid name of a compression strategy.

The list of valid strategies is given by the C enum ZSTD_strategy.

The C names are mapped to Racket names in the same manner as valid-compression-parameter?.

Additionally, 'zstd-default is not defined in the C enum but is supported to mean "the default strategy".

Corresponds to ZSTD_cParam_getBounds.

Returns the minimum and maximum values for the given compression parameter param.

procedure

(cctx-set-parameter cctx param value)  void?

  cctx : cctx?
  param : symbol?
  value : (or/c valid-compression-strategy? exact-integer?)
Corresponds to ZSTD_CCtx_setParameter.

Set the parameter param to the value value. Values must be an exact integer, unless if param is 'zstd-c-strategy, in which case it must be one of the symbols accepted by valid-compression-strategy?.

procedure

(cctx-set-pledged-src-size cctx value)  void?

  cctx : cctx?
  value : exact-nonnegative-integer?
Corresponds to ZSTD_CCtx_setPledgedSrcSize.

procedure

(cctx-reset cctx directive)  void?

  cctx : cctx?
  directive : 
(or/c 'zstd-reset-session-only
      'zstd-reset-parameters
      'zstd-reset-session-and-parameters)
Corresponds to ZSTD_CCtx_reset.

Resets cctx to default values according to directive.

procedure

(compress-advanced cctx src)  bytes?

  cctx : cctx?
  src : bytes?
Corresponds to ZSTD_compress2.

Compresses src according to the parameters set in cctx and returns the result in a fresh byte string.

procedure

(valid-decompression-parameter? v)  boolean?

  v : any/c
Returns #t if v is the valid name of a decompression parameter.

The list of valid parameter names is given by the C enum ZSTD_dParameter.

Only non-experimental parameters are supported.

The C names are mapped to Racket names in the same manner as valid-compression-parameter?.

Corresponds to ZSTD_dParam_getBounds.

Returns the minimum and maximum values for the given decompression parameter param.

procedure

(dctx-set-parameter dctx param value)  void?

  dctx : dctx?
  param : symbol?
  value : exact-integer?
Corresponds to ZSTD_DCtx_setParameter.

Set the parameter param to the value value.

procedure

(dctx-reset dctx directive)  void?

  dctx : dctx?
  directive : 
(or/c 'zstd-reset-session-only
      'zstd-reset-parameters
      'zstd-reset-session-and-parameters)
Corresponds to ZSTD_DCtx_reset.

Resets dctx to default values according to directive.

5 Streaming API🔗ℹ

procedure

(init-compress-stream cctx    
  compression-level)  void?
  cctx : cctx?
  compression-level : exact-integer?
Corresponds to ZSTD_initCStream.

Prepares cctx for streaming compression at compression-level.

procedure

(compress-stream cctx f [in])  void?

  cctx : cctx?
  f : (-> bytes? any/c)
  in : input-port? = (current-input-port)
Higher-level wrapper over ZSTD_compressStream2.

Reads chunks of bytes from in and compresses them, calling f with each chunk of compressed data as a fresh mutable byte string. Stops when eof is first returned by in.

The return value of f is ignored.

procedure

(init-decompress-stream dctx)  void?

  dctx : dctx?
Corresponds to ZSTD_initDStream.

Prepares dctx for streaming decompression.

procedure

(decompress-stream dctx f [in])  void?

  dctx : dctx?
  f : (-> bytes? any/c)
  in : input-port? = (current-input-port)
Higher-level wrapper over ZSTD_decompressStream.

Reads chunks of bytes from in and decompresses them, calling f with each chunk of decompressed data as a fresh mutable byte string. Stops when eof is first returned by in.

The return value of f is ignored.

6 Simple Dictionary API🔗ℹ

procedure

(compress-using-dict cctx    
  src    
  dict    
  compression-level)  bytes?
  cctx : cctx?
  src : bytes?
  dict : bytes?
  compression-level : exact-integer?
Corresponds to ZSTD_compress_usingDict.

Compresses src using dict at compression-level, and returns the compressed result in a fresh byte string.

procedure

(decompress-using-dict dctx    
  src    
  dict    
  [dest-capacity])  bytes?
  dctx : dctx?
  src : bytes?
  dict : bytes?
  dest-capacity : (or/c exact-nonnegative-integer? #f) = #f
Corresponds to ZSTD_decompress_usingDict.

Decompresses src and returns the result in a fresh byte string.

dest-capacity is handled as in decompress.

7 Bulk Dictionary API🔗ℹ

procedure

(cdict? v)  boolean?

  v : any/c
Returns #t if v is a compression dictionary object.

procedure

(ddict? v)  boolean?

  v : any/c
Returns #t if v is a decompression dictionary object.

procedure

(create-cdict dict compression-level)  cdict?

  dict : bytes?
  compression-level : exact-integer?
Corresponds to ZSTD_createCDict.

Loads the dictionary represented by dict for compression at compression-level and returns it. Underlying native resources are automatically released when the returned value is garbage-collected.

procedure

(create-ddict dict)  ddict?

  dict : bytes?
Corresponds to ZSTD_createDDict.

Loads the dictionary represented by dict for decompression and returns it. Underlying native resources are automatically released when the returned value is garbage-collected.

procedure

(compress-using-cdict cctx src cdict)  bytes?

  cctx : cctx?
  src : bytes?
  cdict : cdict?
Corresponds to ZSTD_compress_usingCDict.

Compresses src using cdict and returns the compressed result in a fresh byte string.

procedure

(decompress-using-ddict dctx    
  src    
  ddict    
  dest-capacity)  bytes?
  dctx : dctx?
  src : bytes?
  ddict : ddict?
  dest-capacity : (or/c exact-nonnegative-integer? #f)
Corresponds to ZSTD_decompress_usingDDict.

Decompresses src and returns the result in a fresh byte string.

dest-capacity is handled as in decompress.

8 Dictionary Helpers🔗ℹ

procedure

(dict-id dict)  exact-nonnegative-integer?

  dict : bytes?
Corresponds to ZSTD_getDictID_fromDict.

Returns the dictionary ID of the dictionary represented by dict, or 0 if it does not have a valid ZStandard dictionary header.

procedure

(cdict-dict-id cdict)  exact-nonnegative-integer?

  cdict : cdict?
Corresponds to ZSTD_getDictID_fromCDict.

Returns the dictionary ID of cdict, or 0 if it was loaded from a buffer that did not have a valid ZStandard dictionary header.

procedure

(ddict-dict-id ddict)  exact-nonnegative-integer?

  ddict : ddict?
Corresponds to ZSTD_getDictID_fromDDict.

Returns the dictionary ID of ddict, or 0 if it was loaded from a buffer that did not have a valid ZStandard dictionary header.

procedure

(dict-id-from-frame src)  exact-nonnegative-integer?

  src : bytes?
Corresponds to ZSTD_getDictID_fromFrame.

Returns the dictionary ID needed to decompress the ZStandard frame in src, or 0 in the following cases:

9 Advanced Dictionary API🔗ℹ

The ref* family of functions are not exposed, because they hold onto user-provided objects in native code, which is dangerous in the face of garbage collection.

procedure

(cctx-load-dictionary cctx dict)  void?

  cctx : cctx?
  dict : bytes?
Corresponds to ZSTD_CCtx_loadDictionary.

Loads the dictionary dict into cctx.

procedure

(dctx-load-dictionary dctx dict)  void?

  dctx : dctx?
  dict : bytes?
Corresponds to ZSTD_DCtx_loadDictionary.

Loads the dictionary dict into dctx.

10 Dictionary Training🔗ℹ

procedure

(train-dict dict-size samples)  bytes?

  dict-size : exact-integer?
  samples : (listof bytes?)
Corresponds to ZDICT_trainFromBuffer.

Trains a dictionary on the samples, returning a byte string of length at most dict-size. This dictionary can be saved and later reloaded for use in the various dictionary compression and decompression API’s.

11 Extra Helpers🔗ℹ

This section documents items not directly corresponding to an underlying libzstd API, but are provided for convenience to Racket programmers.

procedure

(zstd-through-ports in out)  void?

  in : input-port?
  out : output-port?
Convenience wrapper over compress-stream.

Streams bytes from in, compresses them at the default compression level, and writes them to out. Stops when eof is first returned by in.

procedure

(unzstd-through-ports in out)  void?

  in : input-port?
  out : output-port?
Convenience wrapper over decompress-stream.

Streams bytes from in, decompresses them, and writes them to out. Stops when eof is first returned by in.

12 License and Contributing🔗ℹ

This binding is licensed under the 3-Clause BSD license, same as libzstd.

Please see this page for information about contributing to my libraries.