Home /Modules/compression/zstd.c

zstd.c

compression/compressors/zstd/zstd.c · 53 line(s) · c

/*
 * Zstandard compressor -- Backend handler.
 *
 * Zstandard: the frame magic 0x28 0xB5 0x2F 0xFD (little-endian 0xFD2FB528). Compress/decompress require an approved Zstandard backend.
 *
 * This handler identifies the format (probe) and advertises its honest state
 * (SLEELA_COMPRESSION_BACKEND); the C++ signature probe is in zstd_detail.cpp. The
 * manager loads it via sleela_compressor_plugin_zstd(). With no backend
 * registered, compress/decompress return SLEELA_COMPRESSION_ERR_UNSUPPORTED --
 * never a false success. Register one via
 * sleela_compression_backend_register(SLEELA_COMPRESSION_ZSTD, ...).
 */

#include "sleela_compressor_plugin.h"
#include "zstd_detail.h"

static const sleela_compressor_handler k_meta = {
    SLEELA_COMPRESSION_ZSTD, "Zstandard", "application/zstd", ".zst",
    SLEELA_COMPRESSION_BACKEND, 1, 1
};

static const sleela_compressor_handler *zstd_describe(void) { return &k_meta; }

static sleela_compression_result zstd_probe(sleela_compressor_instance *inst,
                                              const uint8_t *data, size_t len) {
    (void)inst;
    return sleela_zstd_probe_bytes(data, len);
}

static sleela_compression_result zstd_compress(sleela_compressor_instance *inst,
                                                 const uint8_t *input, size_t input_len,
                                                 sleela_byte_buffer *out) {
    (void)inst;
    const sleela_compression_backend *b = sleela_compression_backend_get(SLEELA_COMPRESSION_ZSTD);
    if (b && b->compress) return b->compress(input, input_len, out);
    return SLEELA_COMPRESSION_ERR_UNSUPPORTED;
}

static sleela_compression_result zstd_decompress(sleela_compressor_instance *inst,
                                                   const uint8_t *input, size_t input_len,
                                                   sleela_byte_buffer *out) {
    (void)inst;
    const sleela_compression_backend *b = sleela_compression_backend_get(SLEELA_COMPRESSION_ZSTD);
    if (b && b->decompress) return b->decompress(input, input_len, out);
    return SLEELA_COMPRESSION_ERR_UNSUPPORTED;
}

static const sleela_compressor_plugin k_plugin = {
    zstd_describe, /*load*/ 0, /*unload*/ 0, zstd_probe, zstd_compress, zstd_decompress
};

const sleela_compressor_plugin *sleela_compressor_plugin_zstd(void) { return &k_plugin; }