diff options
| author | Andrew Clayton <a.clayton@nginx.com> | 2024-11-20 16:19:29 +0000 |
|---|---|---|
| committer | Andrew Clayton <a.clayton@nginx.com> | 2025-04-14 18:11:53 +0100 |
| commit | 511b2177a6414e745bf9daa65f77bda932117780 (patch) | |
| tree | 9deccaef93b55495992860fee551ef25d8ceb958 /src/nxt_zstd.c | |
| parent | 47cbbbbff9b1d3a166355b8a242b623f796db6df (diff) | |
| download | unit-511b2177a6414e745bf9daa65f77bda932117780.tar.gz unit-511b2177a6414e745bf9daa65f77bda932117780.tar.bz2 | |
http: Add support for zstd compression
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'src/nxt_zstd.c')
| -rw-r--r-- | src/nxt_zstd.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/nxt_zstd.c b/src/nxt_zstd.c new file mode 100644 index 00000000..8fb37944 --- /dev/null +++ b/src/nxt_zstd.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) Andrew Clayton + * Copyright (C) F5, Inc. + */ + +#include <stddef.h> +#include <stdint.h> +#include <stdbool.h> + +#include <zstd.h> + +#include "nxt_http_compression.h" + + +static int +nxt_zstd_init(nxt_http_comp_compressor_ctx_t *ctx) +{ + ZSTD_CStream **zstd = &ctx->zstd_ctx; + + *zstd = ZSTD_createCStream(); + if (*zstd == NULL) { + return -1; + } + ZSTD_initCStream(*zstd, ctx->level); + + return 0; +} + + +static size_t +nxt_zstd_bound(const nxt_http_comp_compressor_ctx_t *ctx, size_t in_len) +{ + return ZSTD_compressBound(in_len); +} + + +static ssize_t +nxt_zstd_compress(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf, + size_t in_len, uint8_t *out_buf, size_t out_len, bool last) +{ + size_t ret; + ZSTD_CStream *zstd = ctx->zstd_ctx; + ZSTD_inBuffer zinb = { .src = in_buf, .size = in_len }; + ZSTD_outBuffer zoutb = { .dst = out_buf, .size = out_len }; + + ret = ZSTD_compressStream(zstd, &zoutb, &zinb); + + if (zinb.pos < zinb.size) { + ret = ZSTD_flushStream(zstd, &zoutb); + } + + if (last) { + ret = ZSTD_endStream(zstd, &zoutb); + ZSTD_freeCStream(zstd); + } + + if (ZSTD_isError(ret)) { + return -1; + } + + return zoutb.pos; +} + + +const nxt_http_comp_operations_t nxt_http_comp_zstd_ops = { + .init = nxt_zstd_init, + .bound = nxt_zstd_bound, + .deflate = nxt_zstd_compress, +}; |
