komp_decompress: Decompress a raw vector

View source: R/decompress.R

komp_decompressR Documentation

Decompress a raw vector

Description

Decompress a raw vector

Usage

komp_decompress(
  x,
  codec = "auto",
  max_output = getOption("zukomp.max_output", 1024^3),
  max_ratio = getOption("zukomp.max_ratio", NULL)
)

Arguments

x

A raw vector holding a complete compressed stream.

codec

Codec name, as listed in komp_codecs().

max_output

Refuse to produce more than this many bytes. Defaults to 1 GiB, overridable with options(zukomp.max_output = ). Use 0 for unlimited, deliberately. Compressed input from an untrusted source can expand enormously, and the cap is enforced by the core stream driver, so no codec can bypass it.

max_ratio

Refuse to expand by more than this factor. NULL (the default) means no ratio limit: legitimately compressible data routinely exceeds any safe-looking threshold, so this is opt-in. The ratio is checked as decoding proceeds, on the bytes decoded so far against the compressed bytes read so far, so a stream whose start is far more compressible than the rest can be refused although its overall ratio is under the limit.

Details

A stream may hold several gzip members, or several zstd or LZ4 frames, back to back; they decode as one, as the reference tools do. Anything else after the end of the stream is an error of class zukomp_trailing_bytes.

An lz4-block stream is one bare LZ4 block, which records neither its decoded size nor where it ends. Decoding one therefore searches for the size, bounded by max_output, and a block cut short can be a valid, shorter block: nothing in the format can tell. Prefer lz4-frame wherever the format is yours to choose.

Value

A raw vector.

See Also

komp_compress(), komp_codecs()

Examples

z <- komp_compress(charToRaw(strrep("data ", 200)), "gzip")
rawToChar(komp_decompress(z, "gzip"))

# a small input that would expand a long way is stopped, not allocated
bomb <- komp_compress(raw(1e6), "gzip")
length(bomb)
try(komp_decompress(bomb, "gzip", max_output = 1024))

# zstd and lz4-frame streams are detected by their magic bytes
komp_decompress(komp_compress(charToRaw("hello"), "zstd"))

zukomp documentation built on Nov. 24, 2026, 1:06 a.m.