2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
9 * Uses zstd compression library from https://github.com/facebook/zstd
10 * Requires version 1.4.x (latest as of this writing is 1.4.5)
11 * Using custom free functions require static linking, so that is disabled when
12 * using the shared library.
18 #include <openssl/objects.h>
19 #include "internal/comp.h"
20 #include <openssl/err.h>
21 #include "crypto/cryptlib.h"
22 #include "internal/bio.h"
23 #include "internal/thread_once.h"
24 #include "comp_local.h"
26 COMP_METHOD *COMP_zstd(void);
28 static COMP_METHOD zstd_method_nozstd = {
37 #ifdef OPENSSL_NO_ZSTD
42 # define ZSTD_STATIC_LINKING_ONLY
46 /* Note: There is also a linux zstd.h file in the kernel source */
47 # ifndef ZSTD_H_235446
48 # error Wrong (i.e. linux) zstd.h included.
51 # if ZSTD_VERSION_MAJOR != 1 && ZSTD_VERSION_MINOR < 4
52 # error Expecting version 1.4 or greater of ZSTD
56 /* memory allocations functions for zstd initialisation */
57 static void *zstd_alloc(void *opaque, size_t size)
59 return OPENSSL_zalloc(size);
62 static void zstd_free(void *opaque, void *address)
64 OPENSSL_free(address);
67 static ZSTD_customMem zstd_mem_funcs = {
75 * When OpenSSL is built on Windows, we do not want to require that
76 * the LIBZSTD.DLL be available in order for the OpenSSL DLLs to
77 * work. Therefore, all ZSTD routines are loaded at run time
78 * and we do not link to a .LIB file when ZSTD_SHARED is set.
80 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
85 # include "internal/dso.h"
87 /* Function pointers */
88 typedef ZSTD_CStream* (*createCStream_ft)(void);
89 typedef size_t (*initCStream_ft)(ZSTD_CStream*, int);
90 typedef size_t (*freeCStream_ft)(ZSTD_CStream*);
91 typedef size_t (*compressStream2_ft)(ZSTD_CCtx*, ZSTD_outBuffer*, ZSTD_inBuffer*, ZSTD_EndDirective);
92 typedef size_t (*flushStream_ft)(ZSTD_CStream*, ZSTD_outBuffer*);
93 typedef size_t (*endStream_ft)(ZSTD_CStream*, ZSTD_outBuffer*);
94 typedef size_t (*compress_ft)(void*, size_t, const void*, size_t, int);
95 typedef ZSTD_DStream* (*createDStream_ft)(void);
96 typedef size_t (*initDStream_ft)(ZSTD_DStream*);
97 typedef size_t (*freeDStream_ft)(ZSTD_DStream*);
98 typedef size_t (*decompressStream_ft)(ZSTD_DStream*, ZSTD_outBuffer*, ZSTD_inBuffer*);
99 typedef size_t (*decompress_ft)(void*, size_t, const void*, size_t);
100 typedef unsigned (*isError_ft)(size_t);
101 typedef const char* (*getErrorName_ft)(size_t);
102 typedef size_t (*DStreamInSize_ft)(void);
103 typedef size_t (*CStreamInSize_ft)(void);
105 static createCStream_ft p_createCStream = NULL;
106 static initCStream_ft p_initCStream = NULL;
107 static freeCStream_ft p_freeCStream = NULL;
108 static compressStream2_ft p_compressStream2 = NULL;
109 static flushStream_ft p_flushStream = NULL;
110 static endStream_ft p_endStream = NULL;
111 static compress_ft p_compress = NULL;
112 static createDStream_ft p_createDStream = NULL;
113 static initDStream_ft p_initDStream = NULL;
114 static freeDStream_ft p_freeDStream = NULL;
115 static decompressStream_ft p_decompressStream = NULL;
116 static decompress_ft p_decompress = NULL;
117 static isError_ft p_isError = NULL;
118 static getErrorName_ft p_getErrorName = NULL;
119 static DStreamInSize_ft p_DStreamInSize = NULL;
120 static CStreamInSize_ft p_CStreamInSize = NULL;
122 static DSO *zstd_dso = NULL;
124 # define ZSTD_createCStream p_createCStream
125 # define ZSTD_initCStream p_initCStream
126 # define ZSTD_freeCStream p_freeCStream
127 # define ZSTD_compressStream2 p_compressStream2
128 # define ZSTD_flushStream p_flushStream
129 # define ZSTD_endStream p_endStream
130 # define ZSTD_compress p_compress
131 # define ZSTD_createDStream p_createDStream
132 # define ZSTD_initDStream p_initDStream
133 # define ZSTD_freeDStream p_freeDStream
134 # define ZSTD_decompressStream p_decompressStream
135 # define ZSTD_decompress p_decompress
136 # define ZSTD_isError p_isError
137 # define ZSTD_getErrorName p_getErrorName
138 # define ZSTD_DStreamInSize p_DStreamInSize
139 # define ZSTD_CStreamInSize p_CStreamInSize
141 # endif /* ifdef ZSTD_SHARED */
144 ZSTD_CStream *compressor;
145 ZSTD_DStream *decompressor;
148 static int zstd_stateful_init(COMP_CTX *ctx)
150 struct zstd_state *state = OPENSSL_zalloc(sizeof(*state));
156 state->compressor = ZSTD_createCStream();
158 state->compressor = ZSTD_createCStream_advanced(zstd_mem_funcs);
160 if (state->compressor == NULL)
162 ZSTD_initCStream(state->compressor, ZSTD_CLEVEL_DEFAULT);
165 state->decompressor = ZSTD_createDStream();
167 state->decompressor = ZSTD_createDStream_advanced(zstd_mem_funcs);
169 if (state->decompressor == NULL)
171 ZSTD_initDStream(state->decompressor);
176 ZSTD_freeCStream(state->compressor);
177 ZSTD_freeDStream(state->decompressor);
182 static void zstd_stateful_finish(COMP_CTX *ctx)
184 struct zstd_state *state = ctx->data;
187 ZSTD_freeCStream(state->compressor);
188 ZSTD_freeDStream(state->decompressor);
194 static int zstd_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
195 unsigned int olen, unsigned char *in,
199 ZSTD_outBuffer outbuf;
201 struct zstd_state *state = ctx->data;
213 /* If input length is zero, end the stream/frame ? */
215 ret = ZSTD_endStream(state->compressor, &outbuf);
216 if (ZSTD_isError(ret))
222 * The finish API does not provide a final output buffer,
223 * so each compress operation has to be ended, if all
224 * the input data can't be accepted, or there is more output,
225 * this has to be considered an error, since there is no more
226 * output buffer space.
229 ret = ZSTD_compressStream2(state->compressor, &outbuf, &inbuf, ZSTD_e_continue);
230 if (ZSTD_isError(ret))
232 /* do I need to check for ret == 0 ? */
233 } while (inbuf.pos < inbuf.size);
235 /* Did not consume all the data */
236 if (inbuf.pos < inbuf.size)
239 ret = ZSTD_flushStream(state->compressor, &outbuf);
240 if (ZSTD_isError(ret))
246 static int zstd_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
247 unsigned int olen, unsigned char *in,
251 ZSTD_outBuffer outbuf;
253 struct zstd_state *state = ctx->data;
269 ret = ZSTD_decompressStream(state->decompressor, &outbuf, &inbuf);
270 if (ZSTD_isError(ret))
272 /* If we completed a frame, and there's more data, try again */
273 } while (ret == 0 && inbuf.pos < inbuf.size);
275 /* Did not consume all the data */
276 if (inbuf.pos < inbuf.size)
283 static COMP_METHOD zstd_stateful_method = {
287 zstd_stateful_finish,
288 zstd_stateful_compress_block,
289 zstd_stateful_expand_block
292 static int zstd_oneshot_init(COMP_CTX *ctx)
297 static void zstd_oneshot_finish(COMP_CTX *ctx)
301 static int zstd_oneshot_compress_block(COMP_CTX *ctx, unsigned char *out,
302 unsigned int olen, unsigned char *in,
310 /* Note: uses STDLIB memory allocators */
311 out_size = ZSTD_compress(out, olen, in, ilen, ZSTD_CLEVEL_DEFAULT);
312 if (ZSTD_isError(out_size))
318 static int zstd_oneshot_expand_block(COMP_CTX *ctx, unsigned char *out,
319 unsigned int olen, unsigned char *in,
327 /* Note: uses STDLIB memory allocators */
328 out_size = ZSTD_decompress(out, olen, in, ilen);
329 if (ZSTD_isError(out_size))
335 static COMP_METHOD zstd_oneshot_method = {
340 zstd_oneshot_compress_block,
341 zstd_oneshot_expand_block
344 static CRYPTO_ONCE zstd_once = CRYPTO_ONCE_STATIC_INIT;
345 DEFINE_RUN_ONCE_STATIC(ossl_comp_zstd_init)
348 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
349 # define LIBZSTD "LIBZSTD"
351 # define LIBZSTD "zstd"
354 zstd_dso = DSO_load(NULL, LIBZSTD, NULL, 0);
355 if (zstd_dso != NULL) {
356 p_createCStream = (createCStream_ft)DSO_bind_func(zstd_dso, "ZSTD_createCStream");
357 p_initCStream = (initCStream_ft)DSO_bind_func(zstd_dso, "ZSTD_initCStream");
358 p_freeCStream = (freeCStream_ft)DSO_bind_func(zstd_dso, "ZSTD_freeCStream");
359 p_compressStream2 = (compressStream2_ft)DSO_bind_func(zstd_dso, "ZSTD_compressStream2");
360 p_flushStream = (flushStream_ft)DSO_bind_func(zstd_dso, "ZSTD_flushStream");
361 p_endStream = (endStream_ft)DSO_bind_func(zstd_dso, "ZSTD_endStream");
362 p_compress = (compress_ft)DSO_bind_func(zstd_dso, "ZSTD_compress");
363 p_createDStream = (createDStream_ft)DSO_bind_func(zstd_dso, "ZSTD_createDStream");
364 p_initDStream = (initDStream_ft)DSO_bind_func(zstd_dso, "ZSTD_initDStream");
365 p_freeDStream = (freeDStream_ft)DSO_bind_func(zstd_dso, "ZSTD_freeDStream");
366 p_decompressStream = (decompressStream_ft)DSO_bind_func(zstd_dso, "ZSTD_decompressStream");
367 p_decompress = (decompress_ft)DSO_bind_func(zstd_dso, "ZSTD_decompress");
368 p_isError = (isError_ft)DSO_bind_func(zstd_dso, "ZSTD_isError");
369 p_getErrorName = (getErrorName_ft)DSO_bind_func(zstd_dso, "ZSTD_getErrorName");
370 p_DStreamInSize = (DStreamInSize_ft)DSO_bind_func(zstd_dso, "ZSTD_DStreamInSize");
371 p_CStreamInSize = (CStreamInSize_ft)DSO_bind_func(zstd_dso, "ZSTD_CStreamInSize");
374 if (p_createCStream == NULL || p_initCStream == NULL || p_freeCStream == NULL
375 || p_compressStream2 == NULL || p_flushStream == NULL || p_endStream == NULL
376 || p_compress == NULL || p_createDStream == NULL || p_initDStream == NULL
377 || p_freeDStream == NULL || p_decompressStream == NULL || p_decompress == NULL
378 || p_isError == NULL || p_getErrorName == NULL || p_DStreamInSize == NULL
379 || p_CStreamInSize == NULL) {
380 ossl_comp_zstd_cleanup();
386 #endif /* ifndef ZSTD / else */
388 COMP_METHOD *COMP_zstd(void)
390 COMP_METHOD *meth = &zstd_method_nozstd;
392 #ifndef OPENSSL_NO_ZSTD
393 if (RUN_ONCE(&zstd_once, ossl_comp_zstd_init))
394 meth = &zstd_stateful_method;
399 COMP_METHOD *COMP_zstd_oneshot(void)
401 COMP_METHOD *meth = &zstd_method_nozstd;
403 #ifndef OPENSSL_NO_ZSTD
404 if (RUN_ONCE(&zstd_once, ossl_comp_zstd_init))
405 meth = &zstd_oneshot_method;
410 /* Also called from OPENSSL_cleanup() */
411 void ossl_comp_zstd_cleanup(void)
416 p_createCStream = NULL;
417 p_initCStream = NULL;
418 p_freeCStream = NULL;
419 p_compressStream2 = NULL;
420 p_flushStream = NULL;
423 p_createDStream = NULL;
424 p_initDStream = NULL;
425 p_freeDStream = NULL;
426 p_decompressStream = NULL;
429 p_getErrorName = NULL;
430 p_DStreamInSize = NULL;
431 p_CStreamInSize = NULL;
435 #ifndef OPENSSL_NO_ZSTD
437 /* Zstd-based compression/decompression filter BIO */
440 struct { /* input structure */
442 ZSTD_inBuffer inbuf; /* has const src */
446 struct { /* output structure */
448 ZSTD_outBuffer outbuf;
454 # define ZSTD_DEFAULT_BUFSIZE 1024
456 static int bio_zstd_new(BIO *bi);
457 static int bio_zstd_free(BIO *bi);
458 static int bio_zstd_read(BIO *b, char *out, int outl);
459 static int bio_zstd_write(BIO *b, const char *in, int inl);
460 static long bio_zstd_ctrl(BIO *b, int cmd, long num, void *ptr);
461 static long bio_zstd_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
463 static const BIO_METHOD bio_meth_zstd = {
466 /* TODO: Convert to new style write function */
469 /* TODO: Convert to new style read function */
472 NULL, /* bio_zstd_puts, */
473 NULL, /* bio_zstd_gets, */
477 bio_zstd_callback_ctrl
481 const BIO_METHOD *BIO_f_zstd(void)
483 #ifndef OPENSSL_NO_ZSTD
484 return &bio_meth_zstd;
490 #ifndef OPENSSL_NO_ZSTD
491 static int bio_zstd_new(BIO *bi)
497 if (zstd_dso == NULL) {
498 ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_NOT_SUPPORTED);
502 ctx = OPENSSL_zalloc(sizeof(*ctx));
504 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
509 ctx->decompress.state = ZSTD_createDStream();
511 ctx->decompress.state = ZSTD_createDStream_advanced(zstd_mem_funcs);
513 if (ctx->decompress.state == NULL)
515 ZSTD_initDStream(ctx->decompress.state);
516 ctx->decompress.bufsize = ZSTD_DStreamInSize();
519 ctx->compress.state = ZSTD_createCStream();
521 ctx->compress.state = ZSTD_createCStream_advanced(zstd_mem_funcs);
523 if (ctx->compress.state == NULL)
525 ZSTD_initCStream(ctx->compress.state, ZSTD_CLEVEL_DEFAULT);
526 ctx->compress.bufsize = ZSTD_CStreamInSize();
529 BIO_set_data(bi, ctx);
533 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
534 ZSTD_freeDStream(ctx->decompress.state);
535 ZSTD_freeCStream(ctx->compress.state);
540 static int bio_zstd_free(BIO *bi)
547 ctx = BIO_get_data(bi);
549 ZSTD_freeDStream(ctx->decompress.state);
550 OPENSSL_free(ctx->decompress.buffer);
551 ZSTD_freeCStream(ctx->compress.state);
552 OPENSSL_free(ctx->compress.outbuf.dst);
555 BIO_set_data(bi, NULL);
561 static int bio_zstd_read(BIO *b, char *out, int outl)
566 ZSTD_outBuffer outBuf;
567 BIO *next = BIO_next(b);
569 if (out == NULL || outl <= 0)
572 ctx = BIO_get_data(b);
573 BIO_clear_retry_flags(b);
574 if (ctx->decompress.buffer == NULL) {
575 ctx->decompress.buffer = OPENSSL_malloc(ctx->decompress.bufsize);
576 if (ctx->decompress.buffer == NULL) {
577 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
580 ctx->decompress.inbuf.src = ctx->decompress.buffer;
581 ctx->decompress.inbuf.size = 0;
582 ctx->decompress.inbuf.pos = 0;
585 /* Copy output data directly to supplied buffer */
587 outBuf.size = (size_t)outl;
590 /* Decompress while data available */
592 zret = ZSTD_decompressStream(ctx->decompress.state, &outBuf, &ctx->decompress.inbuf);
593 if (ZSTD_isError(zret)) {
594 ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_DECOMPRESS_ERROR);
595 ERR_add_error_data(1, ZSTD_getErrorName(zret));
598 /* No more output space */
599 if (outBuf.pos == outBuf.size)
601 } while (ctx->decompress.inbuf.pos < ctx->decompress.inbuf.size);
604 * No data in input buffer try to read some in, if an error then
605 * return the total data read.
607 ret = BIO_read(next, ctx->decompress.buffer, ctx->decompress.bufsize);
609 BIO_copy_next_retry(b);
610 if (ret < 0 && outBuf.pos == 0)
614 ctx->decompress.inbuf.size = ret;
615 ctx->decompress.inbuf.pos = 0;
619 static int bio_zstd_write(BIO *b, const char *in, int inl)
626 BIO *next = BIO_next(b);
628 if (in == NULL || inl <= 0)
631 ctx = BIO_get_data(b);
633 BIO_clear_retry_flags(b);
634 if (ctx->compress.outbuf.dst == NULL) {
635 ctx->compress.outbuf.dst = OPENSSL_malloc(ctx->compress.bufsize);
636 if (ctx->compress.outbuf.dst == NULL) {
637 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
640 ctx->compress.outbuf.size = ctx->compress.bufsize;
641 ctx->compress.outbuf.pos = 0;
642 ctx->compress.write_pos = 0;
644 /* Obtain input data directly from supplied buffer */
649 /* If data in output buffer write it first */
650 while (ctx->compress.write_pos < ctx->compress.outbuf.pos) {
651 ret = BIO_write(next, (unsigned char*)ctx->compress.outbuf.dst + ctx->compress.write_pos,
652 ctx->compress.outbuf.pos - ctx->compress.write_pos);
654 BIO_copy_next_retry(b);
655 if (ret < 0 && inBuf.pos == 0)
659 ctx->compress.write_pos += ret;
662 /* Have we consumed all supplied data? */
667 ctx->compress.outbuf.pos = 0;
668 ctx->compress.outbuf.size = ctx->compress.bufsize;
669 ctx->compress.write_pos = 0;
670 /* Compress some more */
671 zret = ZSTD_compressStream2(ctx->compress.state, &ctx->compress.outbuf, &inBuf, ZSTD_e_end);
672 if (ZSTD_isError(zret)) {
673 ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_COMPRESS_ERROR);
674 ERR_add_error_data(1, ZSTD_getErrorName(zret));
676 } else if (zret == 0) {
682 static int bio_zstd_flush(BIO *b)
687 BIO *next = BIO_next(b);
689 ctx = BIO_get_data(b);
691 /* If no data written or already flush show success */
692 if (ctx->compress.outbuf.dst == NULL)
695 BIO_clear_retry_flags(b);
696 /* No more input data */
697 ctx->compress.outbuf.pos = 0;
698 ctx->compress.outbuf.size = ctx->compress.bufsize;
699 ctx->compress.write_pos = 0;
701 /* If data in output buffer write it first */
702 while (ctx->compress.write_pos < ctx->compress.outbuf.pos) {
703 ret = BIO_write(next, (unsigned char*)ctx->compress.outbuf.dst + ctx->compress.write_pos,
704 ctx->compress.outbuf.pos - ctx->compress.write_pos);
706 BIO_copy_next_retry(b);
709 ctx->compress.write_pos += ret;
713 ctx->compress.outbuf.pos = 0;
714 ctx->compress.outbuf.size = ctx->compress.bufsize;
715 ctx->compress.write_pos = 0;
716 /* Compress some more */
717 zret = ZSTD_flushStream(ctx->compress.state, &ctx->compress.outbuf);
718 if (ZSTD_isError(zret)) {
719 ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_DECODE_ERROR);
720 ERR_add_error_data(1, ZSTD_getErrorName(zret));
728 static long bio_zstd_ctrl(BIO *b, int cmd, long num, void *ptr)
734 BIO *next = BIO_next(b);
738 ctx = BIO_get_data(b);
742 ctx->compress.write_pos = 0;
743 ctx->compress.bufsize = 0;
748 ret = bio_zstd_flush(b);
750 ret = BIO_flush(next);
753 case BIO_C_SET_BUFF_SIZE:
754 ibs = ctx->decompress.bufsize;
755 obs = ctx->compress.bufsize;
763 obs = ibs = (size_t)num;
766 if (ibs > 0 && ibs != ctx->decompress.bufsize) {
767 if (ctx->decompress.buffer != NULL) {
768 tmp = OPENSSL_realloc(ctx->decompress.buffer, ibs);
771 if (ctx->decompress.inbuf.src == ctx->decompress.buffer)
772 ctx->decompress.inbuf.src = tmp;
773 ctx->decompress.buffer = tmp;
775 ctx->decompress.bufsize = ibs;
778 if (obs > 0 && obs != ctx->compress.bufsize) {
779 if (ctx->compress.outbuf.dst != NULL) {
780 tmp = OPENSSL_realloc(ctx->compress.outbuf.dst, obs);
783 ctx->compress.outbuf.dst = tmp;
785 ctx->compress.bufsize = obs;
790 case BIO_C_DO_STATE_MACHINE:
791 BIO_clear_retry_flags(b);
792 ret = BIO_ctrl(next, cmd, num, ptr);
793 BIO_copy_next_retry(b);
796 case BIO_CTRL_WPENDING:
797 if (ctx->compress.outbuf.pos < ctx->compress.outbuf.size)
800 ret = BIO_ctrl(next, cmd, num, ptr);
803 case BIO_CTRL_PENDING:
804 if (ctx->decompress.inbuf.pos < ctx->decompress.inbuf.size)
807 ret = BIO_ctrl(next, cmd, num, ptr);
811 ret = BIO_ctrl(next, cmd, num, ptr);
819 static long bio_zstd_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
821 BIO *next = BIO_next(b);
824 return BIO_callback_ctrl(next, cmd, fp);