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.
value
value
value
value
value
struct
(struct exn:fail:zstd exn:fail (code) #:transparent) code : number?
procedure
(get-error-name code) → string?
code : exact-integer?
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
src : bytes? compression-level : exact-integer?
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
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
procedure
procedure
(compress-context cctx src compression-level) → bytes? cctx : cctx? src : bytes? compression-level : exact-integer?
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
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
v : any/c
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:
Lower-case the name
Replace all underscores with dashes
Separate words with dashes
Convert to symbol
So, for example, the C enum constant ZSTD_c_ldmHashRateLog is expressed as 'zstd-c-ldm-hash-rate-log.
procedure
v : any/c
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".
procedure
(compression-parameter-get-bounds param)
→
exact-integer? exact-integer? param : valid-compression-parameter?
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?)
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?
procedure
(cctx-reset cctx directive) → void?
cctx : cctx?
directive :
(or/c 'zstd-reset-session-only 'zstd-reset-parameters 'zstd-reset-session-and-parameters)
Resets cctx to default values according to directive.
procedure
(compress-advanced cctx src) → bytes?
cctx : cctx? src : bytes?
Compresses src according to the parameters set in cctx and returns the result in a fresh byte string.
procedure
v : any/c
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?.
procedure
→
exact-integer? exact-integer? param : valid-decompression-parameter?
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?
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)
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?
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)
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?
Prepares dctx for streaming decompression.
procedure
(decompress-stream dctx f [in]) → void?
dctx : dctx? f : (-> bytes? any/c) in : input-port? = (current-input-port)
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?
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
Decompresses src and returns the result in a fresh byte string.
dest-capacity is handled as in decompress.
7 Bulk Dictionary API
procedure
(create-cdict dict compression-level) → cdict?
dict : bytes? compression-level : exact-integer?
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?
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?
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)
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?
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?
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?
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
src : bytes?
Returns the dictionary ID needed to decompress the ZStandard frame in src, or 0 in the following cases:
The frame does not require a dictionary to be decoded (most common case).
The frame was built with dictID intentionally removed. Whatever dictionary is necessary is hidden information.
src is too small, and as a result, the frame header could not be decoded.
src is not a Zstandard frame.
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?
Loads the dictionary dict into cctx.
procedure
(dctx-load-dictionary dctx dict) → void?
dctx : dctx? dict : bytes?
Loads the dictionary dict into dctx.
10 Dictionary Training
procedure
(train-dict dict-size samples) → bytes?
dict-size : exact-integer? samples : (listof bytes?)
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?
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?
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.