diff options
author | Andrew Clayton <a.clayton@nginx.com> | 2024-06-11 00:24:28 +0100 |
---|---|---|
committer | Andrew Clayton <a.clayton@nginx.com> | 2024-06-19 16:15:04 +0100 |
commit | 4071de5797da0a104b4983fc1ad393d3744b6128 (patch) | |
tree | 003803a861f2609cfae58db412e1c3d7ee6dc8f9 /src/nxt_zstd.c | |
parent | ea5c41b8056997ed40138020272b5159271f1b87 (diff) | |
download | unit-4071de5797da0a104b4983fc1ad393d3744b6128.tar.gz unit-4071de5797da0a104b4983fc1ad393d3744b6128.tar.bz2 |
[WIP] HTTP Compression Supportcompr
Diffstat (limited to 'src/nxt_zstd.c')
-rw-r--r-- | src/nxt_zstd.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/nxt_zstd.c b/src/nxt_zstd.c new file mode 100644 index 00000000..07da9ef6 --- /dev/null +++ b/src/nxt_zstd.c @@ -0,0 +1,73 @@ +/* + * + */ + +#include <stddef.h> +#include <stdint.h> +#include <stdbool.h> + +#include <zstd.h> + +#include <nxt_http_compression.h> + +static void nxt_zstd_free(const nxt_http_comp_compressor_ctx_t *ctx) +{ + ZSTD_CStream *zstd = ctx->zstd_ctx; + + ZSTD_freeCStream(zstd); +} + +static void nxt_zstd_init(nxt_http_comp_compressor_ctx_t *ctx) +{ + ZSTD_CStream **zstd = &ctx->zstd_ctx; + + *zstd = ZSTD_createCStream(); + ZSTD_initCStream(*zstd, ctx->level); + + printf("%s: zstd compression level [%d]\n", __func__, ctx->level); +} + +static size_t nxt_zstd_compressed_size(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 }; + + printf("%s: in_len [%lu] out_len [%lu]\n", __func__, in_len, out_len); + + ret = ZSTD_compressStream(zstd, &zoutb, &zinb); + + if (zinb.pos < zinb.size) { + printf("%s: short by [%d]\n", __func__, zinb.pos < zinb.size); + ret = ZSTD_flushStream(zstd, &zoutb); + } + + if (last) { + ret = ZSTD_endStream(zstd, &zoutb); + nxt_zstd_free(ctx); + } + + printf("%s: ret [%lu]\n", __func__, ret); + if (ZSTD_isError(ret)) { + printf("%s: [%s]\n", __func__, ZSTD_getErrorName(ret)); + return -1; + } + + return zoutb.pos; +} + +const nxt_http_comp_operations_t nxt_comp_zstd_ops = { + .init = nxt_zstd_init, + .compressed_size = nxt_zstd_compressed_size, + .deflate = nxt_zstd_compress, + .free_ctx = nxt_zstd_free, +}; |