Skip to content
pedrobtzPublic

About

Portable Byte Compression with a Pluggable Codec Registry

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

zukomp

CRAN status Lifecycle: experimental R-CMD-check coverage

zukomp compresses and decompresses raw vectors through a single codec-neutral interface backed by a runtime registry, so the set of available codecs is discovered rather than fixed at compile time. The DEFLATE family (gzip, zlib and headerless deflate-raw), zstd, LZ4 (lz4-frame and the bare lz4-block) and Snappy (snappy-frame and the bare snappy-raw) are built in from vendored miniz, zstd, lz4 and carquet's Snappy sources, so no system compression library is required.

Installation

install.packages("zukomp")

Or the development version from GitHub:

# install.packages("pak")
pak::pak("pedrobtz/zukomp")

Usage

Bytes in, bytes out. komp_compress() names a codec; komp_decompress() detects it by default.

library(zukomp)

x <- charToRaw(strrep("the quick brown fox ", 500))
z <- komp_compress(x, "gzip")

length(x)                        # 10000
length(z)                        # 81
identical(komp_decompress(z), x) # TRUE

komp_codecs() is the registry. Codecs this build knows the name of but does not implement still get a row, so "not implemented here" is a possible answer:

komp_codecs()[, c("id", "available", "content_encoding", "source")]
#>              id available content_encoding source
#> 1      identity      TRUE         identity zukomp
#> 2   deflate-raw      TRUE             <NA> zukomp
#> 3          zlib      TRUE          deflate zukomp
#> 4          gzip      TRUE             gzip zukomp
#> 5        brotli     FALSE               br   <NA>
#> 6          zstd      TRUE             zstd zukomp
#> 7     lz4-frame      TRUE             <NA> zukomp
#> 8     lz4-block      TRUE             <NA> zukomp
#> ...

Decompression is safe to point at untrusted input: output is capped at 1 GiB by default, and the cap is enforced by the core stream driver, so no codec — including a third-party one — can bypass it. The getting started article covers the limits, detection, the structured error conditions, and the C ABI that lets another package drive these codecs or register one of its own.

Using zukomp from C

There are two ways for another package to reach the codecs, and they suit different consumers.

The registered function table is the one to prefer. Declare

Imports:    zukomp
LinkingTo:  zukomp

and include <zukomp-r.h>, which resolves zukomp_api() through R_GetCCallable(). The consumer drives whatever codecs the installed zukomp has, including ones registered after it was built, and links nothing.

A container format that records each body's decoded length (an Arrow IPC buffer's 8-byte prefix, a Parquet page header) decompresses one shot into a buffer of that length, and passes the length so zukomp holds the codec to it:

const zukomp_api_v1 *api = zukomp_api();       /* NULL: zukomp missing or too old */
zu_decoder_opts o;
memset(&o, 0, sizeof o);
o.struct_size   = sizeof o;
o.codec         = api->codec_lookup("zstd");   /* or "lz4-frame", "lz4-block" */
o.max_output    = limit;                        /* 0 = unlimited */
o.flags         = ZU_DEC_REJECT_TRAILING | ZU_DEC_EXPECTED_SIZE;
o.expected_size = n;
size_t written;
zu_status st = api->decompress_one(&o, src, src_len, dst, n, &written);
/* ZU_OK, or ZU_ERR_INVALID_DATA if the body is not exactly n bytes */

The static archive is for a consumer that cannot depend on zukomp at run time: one that needs the ZIP container layer (reading or writing the members of a .zip, an .xlsx or any other ZIP-shaped format, which is trimmed out of zukomp.so on purpose and is not reachable through the table), or one whose decoding runs with no R in the process. The archive holds miniz's ZIP layer and every built-in codec. zukomp_static_api(), declared in zukomp.h, returns the same zukomp_api_v1 table zukomp_api() does, with nothing to look up and nothing to initialise:

#include <zukomp.h>
const zukomp_api_v1 *api = zukomp_static_api();
/* api->decompress_one(...), as above */

That table is complete and constant: register_codec returns ZU_ERR_UNSUPPORTED, and the codecs are those of the zukomp the consumer was built against. Its vendored zstd, LZ4 and Snappy symbols are renamed zukompv_*, so the archive links beside a package's own copy of any of them; miniz keeps its names, since miniz.h is the interface. An installed zukomp carries

zukomp/include/miniz.h
zukomp/lib/libzukomp.a          # lib/x64/ on Windows; see below
zukomp/licenses/miniz-LICENSE

where the archive is a second compilation of zukomp's sources, with miniz's ZIP reader and writer, stdio and timestamps left in. LinkingTo: zukomp puts the header on the include path; the archive's location comes from system.file("lib", .Platform$r_arch, package = "zukomp"), which a configure script can resolve into src/Makevars without adding an Imports: dependency:

ZUKOMP_LIB=$("${R_HOME}/bin/Rscript" -e \
  'cat(system.file("lib", .Platform$r_arch, package = "zukomp"))')
sed "s|@ZUKOMP_LIB@|${ZUKOMP_LIB}|" src/Makevars.in > src/Makevars
PKG_CPPFLAGS = -DMINIZ_NO_ZLIB_COMPATIBLE_NAMES
PKG_LIBS = "@ZUKOMP_LIB@/libzukomp.a"

.Platform$r_arch is "" on every single-arch platform and "x64" on Windows, where the archive installs under that sub-directory beside the shared object's. Quote the expansion in PKG_LIBS: an R library path containing a space (C:/Program Files/R/...) otherwise arrives at the linker as two arguments. Windows needs the same two lines in configure.win, since R runs only that file there.

Four things to know before taking this route:

  • -DMINIZ_NO_ZLIB_COMPATIBLE_NAMES is required, not optional. Without it miniz.h #defines compress, crc32, adler32 and friends over every translation unit that includes it, colliding with the zlib the R process already links. The archive is built with it set, so the names are not there to link against either way.
  • Define nothing else. The archive was compiled from a fixed set of MINIZ_NO_* flags, and two of them change what miniz.h declares rather than only what it exports. MINIZ_NO_TIME swaps MZ_TIME_T from time_t to a two-word struct, which changes the layout of mz_zip_archive_file_stat between your translation unit and the archive -- a silent mismatch, not a link error. MINIZ_NO_STDIO removes the declaration of mz_zip_reader_init_file() entirely, and MINIZ_NO_ARCHIVE_WRITING_APIS every mz_zip_writer_* one. zukomp's own src/Makevars sets all three, because zukomp.so has no ZIP layer to describe; do not copy them from there.
  • The writer is there too, and reads no clock. mz_zip_writer_* links as well (to memory, through a write callback, or to a file). The archive is built with zukomp's MINIZ_NO_CLOCK patch: a member added without a time gets zero date and time fields instead of the current time, and a given time_t is stored as its UTC date and time, clamped to the range the fields hold (1980 to 2107), instead of going through localtime(). The same members and times therefore give the same bytes on every machine. Reading is unchanged: m_time still comes from mktime(), in local time. You do not define MINIZ_NO_CLOCK yourself; it changes no declaration.
  • You get your own copy of miniz, linked into your shared object, sharing no state with the one inside zukomp.so. A zukomp update does not reach it until you rebuild. It carries zukomp's local patches, the RFC 1951 match-distance check among them, and was compiled by whichever toolchain installed zukomp -- so reinstall zukomp after changing compilers, as you would for any other LinkingTo dependency shipping object code.

miniz is MIT-licensed third-party code. An installed zukomp carries the notice alongside it, at zukomp/licenses/miniz-LICENSE; inst/COPYRIGHTS records the provenance.

About

Portable Byte Compression with a Pluggable Codec Registry

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages