99d326219e2e0408205c8cb297649a6b9dca843b
[openssl.git] / crypto / comp / c_zstd.c
1 /*
2  * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  *
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.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
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"
25
26 COMP_METHOD *COMP_zstd(void);
27
28 static COMP_METHOD zstd_method_nozstd = {
29     NID_undef,
30     "(undef)",
31     NULL,
32     NULL,
33     NULL,
34     NULL,
35 };
36
37 #ifdef OPENSSL_NO_ZSTD
38 # undef ZSTD_SHARED
39 #else
40
41 # ifndef ZSTD_SHARED
42 #  define ZSTD_STATIC_LINKING_ONLY
43 # endif
44 # include <zstd.h>
45
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.
49 # endif
50
51 # if ZSTD_VERSION_MAJOR != 1 && ZSTD_VERSION_MINOR < 4
52 #  error Expecting version 1.4 or greater of ZSTD
53 # endif
54
55 # ifndef ZSTD_SHARED
56 /* memory allocations functions for zstd initialisation */
57 static void *zstd_alloc(void *opaque, size_t size)
58 {
59     return OPENSSL_zalloc(size);
60 }
61
62 static void zstd_free(void *opaque, void *address)
63 {
64     OPENSSL_free(address);
65 }
66
67 static ZSTD_customMem zstd_mem_funcs = {
68     zstd_alloc,
69     zstd_free,
70     NULL
71 };
72 # endif
73
74 /*
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.
79  */
80 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
81 #  include <windows.h>
82 # endif
83
84 # ifdef ZSTD_SHARED
85 #  include "internal/dso.h"
86
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);
104
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;
121
122 static DSO *zstd_dso = NULL;
123
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
140
141 # endif /* ifdef ZSTD_SHARED */
142
143 struct zstd_state {
144     ZSTD_CStream *compressor;
145     ZSTD_DStream *decompressor;
146 };
147
148 static int zstd_stateful_init(COMP_CTX *ctx)
149 {
150     struct zstd_state *state = OPENSSL_zalloc(sizeof(*state));
151
152     if (state == NULL)
153         return 0;
154
155 # ifdef ZSTD_SHARED
156     state->compressor = ZSTD_createCStream();
157 # else
158     state->compressor = ZSTD_createCStream_advanced(zstd_mem_funcs);
159 # endif
160     if (state->compressor == NULL)
161         goto err;
162     ZSTD_initCStream(state->compressor, ZSTD_CLEVEL_DEFAULT);
163
164 # ifdef ZSTD_SHARED
165     state->decompressor = ZSTD_createDStream();
166 # else
167     state->decompressor = ZSTD_createDStream_advanced(zstd_mem_funcs);
168 # endif
169     if (state->decompressor == NULL)
170         goto err;
171     ZSTD_initDStream(state->decompressor);
172
173     ctx->data = state;
174     return 1;
175  err:
176     ZSTD_freeCStream(state->compressor);
177     ZSTD_freeDStream(state->decompressor);
178     OPENSSL_free(state);
179     return 0;
180 }
181
182 static void zstd_stateful_finish(COMP_CTX *ctx)
183 {
184     struct zstd_state *state = ctx->data;
185
186     if (state != NULL) {
187         ZSTD_freeCStream(state->compressor);
188         ZSTD_freeDStream(state->decompressor);
189         OPENSSL_free(state);
190         ctx->data = NULL;
191     }
192 }
193
194 static int zstd_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
195                                         unsigned int olen, unsigned char *in,
196                                         unsigned int ilen)
197 {
198     ZSTD_inBuffer inbuf;
199     ZSTD_outBuffer outbuf;
200     size_t ret;
201     struct zstd_state *state = ctx->data;
202
203     inbuf.src = in;
204     inbuf.size = ilen;
205     inbuf.pos = 0;
206     outbuf.dst = out;
207     outbuf.size = olen;
208     outbuf.pos = 0;
209
210     if (state == NULL)
211         return -1;
212
213     /* If input length is zero, end the stream/frame ? */
214     if (ilen == 0) {
215         ret = ZSTD_endStream(state->compressor, &outbuf);
216         if (ZSTD_isError(ret))
217             return -1;
218         return outbuf.pos;
219     }
220
221     /*
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.
227      */
228     do {
229         ret = ZSTD_compressStream2(state->compressor, &outbuf, &inbuf, ZSTD_e_continue);
230         if (ZSTD_isError(ret))
231             return -1;
232         /* do I need to check for ret == 0 ? */
233     } while (inbuf.pos < inbuf.size);
234
235     /* Did not consume all the data */
236     if (inbuf.pos < inbuf.size)
237         return -1;
238
239     ret = ZSTD_flushStream(state->compressor, &outbuf);
240     if (ZSTD_isError(ret))
241         return -1;
242
243     return outbuf.pos;
244 }
245
246 static int zstd_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
247                                       unsigned int olen, unsigned char *in,
248                                       unsigned int ilen)
249 {
250     ZSTD_inBuffer inbuf;
251     ZSTD_outBuffer outbuf;
252     size_t ret;
253     struct zstd_state *state = ctx->data;
254
255     inbuf.src = in;
256     inbuf.size = ilen;
257     inbuf.pos = 0;
258     outbuf.dst = out;
259     outbuf.size = olen;
260     outbuf.pos = 0;
261
262     if (state == NULL)
263         return -1;
264
265     if (ilen == 0)
266         return 0;
267
268     do {
269         ret = ZSTD_decompressStream(state->decompressor, &outbuf, &inbuf);
270         if (ZSTD_isError(ret))
271             return -1;
272         /* If we completed a frame, and there's more data, try again */
273     } while (ret == 0 && inbuf.pos < inbuf.size);
274
275     /* Did not consume all the data */
276     if (inbuf.pos < inbuf.size)
277         return -1;
278
279     return outbuf.pos;
280 }
281
282
283 static COMP_METHOD zstd_stateful_method = {
284     NID_zstd,
285     LN_zstd,
286     zstd_stateful_init,
287     zstd_stateful_finish,
288     zstd_stateful_compress_block,
289     zstd_stateful_expand_block
290 };
291
292 static int zstd_oneshot_init(COMP_CTX *ctx)
293 {
294     return 1;
295 }
296
297 static void zstd_oneshot_finish(COMP_CTX *ctx)
298 {
299 }
300
301 static int zstd_oneshot_compress_block(COMP_CTX *ctx, unsigned char *out,
302                                        unsigned int olen, unsigned char *in,
303                                        unsigned int ilen)
304 {
305     size_t out_size;
306
307     if (ilen == 0)
308         return 0;
309
310     /* Note: uses STDLIB memory allocators */
311     out_size = ZSTD_compress(out, olen, in, ilen, ZSTD_CLEVEL_DEFAULT);
312     if (ZSTD_isError(out_size))
313         return -1;
314
315     return out_size;
316 }
317
318 static int zstd_oneshot_expand_block(COMP_CTX *ctx, unsigned char *out,
319                                      unsigned int olen, unsigned char *in,
320                                      unsigned int ilen)
321 {
322     size_t out_size;
323
324     if (ilen == 0)
325         return 0;
326
327     /* Note: uses STDLIB memory allocators */
328     out_size = ZSTD_decompress(out, olen, in, ilen);
329     if (ZSTD_isError(out_size))
330         return -1;
331
332     return out_size;
333 }
334
335 static COMP_METHOD zstd_oneshot_method = {
336     NID_zstd,
337     LN_zstd,
338     zstd_oneshot_init,
339     zstd_oneshot_finish,
340     zstd_oneshot_compress_block,
341     zstd_oneshot_expand_block
342 };
343
344 static CRYPTO_ONCE zstd_once = CRYPTO_ONCE_STATIC_INIT;
345 DEFINE_RUN_ONCE_STATIC(ossl_comp_zstd_init)
346 {
347 # ifdef ZSTD_SHARED
348 #  if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
349 #   define LIBZSTD "LIBZSTD"
350 #  else
351 #   define LIBZSTD  "zstd"
352 #  endif
353
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");
372     }
373
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();
381         return 0;
382     }
383 # endif
384     return 1;
385 }
386 #endif /* ifndef ZSTD / else */
387
388 COMP_METHOD *COMP_zstd(void)
389 {
390     COMP_METHOD *meth = &zstd_method_nozstd;
391
392 #ifndef OPENSSL_NO_ZSTD
393     if (RUN_ONCE(&zstd_once, ossl_comp_zstd_init))
394         meth = &zstd_stateful_method;
395 #endif
396     return meth;
397 }
398
399 COMP_METHOD *COMP_zstd_oneshot(void)
400 {
401     COMP_METHOD *meth = &zstd_method_nozstd;
402
403 #ifndef OPENSSL_NO_ZSTD
404     if (RUN_ONCE(&zstd_once, ossl_comp_zstd_init))
405         meth = &zstd_oneshot_method;
406 #endif
407     return meth;
408 }
409
410 /* Also called from OPENSSL_cleanup() */
411 void ossl_comp_zstd_cleanup(void)
412 {
413 #ifdef ZSTD_SHARED
414     DSO_free(zstd_dso);
415     zstd_dso = NULL;
416     p_createCStream = NULL;
417     p_initCStream = NULL;
418     p_freeCStream = NULL;
419     p_compressStream2 = NULL;
420     p_flushStream = NULL;
421     p_endStream = NULL;
422     p_compress = NULL;
423     p_createDStream = NULL;
424     p_initDStream = NULL;
425     p_freeDStream = NULL;
426     p_decompressStream = NULL;
427     p_decompress = NULL;
428     p_isError = NULL;
429     p_getErrorName = NULL;
430     p_DStreamInSize = NULL;
431     p_CStreamInSize = NULL;
432 #endif
433 }
434
435 #ifndef OPENSSL_NO_ZSTD
436
437 /* Zstd-based compression/decompression filter BIO */
438
439 typedef struct {
440     struct { /* input structure */
441         ZSTD_DStream *state;
442         ZSTD_inBuffer inbuf; /* has const src */
443         size_t bufsize;
444         void* buffer;
445     } decompress;
446     struct { /* output structure */
447         ZSTD_CStream *state;
448         ZSTD_outBuffer outbuf;
449         size_t bufsize;
450         size_t write_pos;
451     } compress;
452 } BIO_ZSTD_CTX;
453
454 # define ZSTD_DEFAULT_BUFSIZE 1024
455
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);
462
463 static const BIO_METHOD bio_meth_zstd = {
464     BIO_TYPE_COMP,
465     "zstd",
466     /* TODO: Convert to new style write function */
467     bwrite_conv,
468     bio_zstd_write,
469     /* TODO: Convert to new style read function */
470     bread_conv,
471     bio_zstd_read,
472     NULL,                      /* bio_zstd_puts, */
473     NULL,                      /* bio_zstd_gets, */
474     bio_zstd_ctrl,
475     bio_zstd_new,
476     bio_zstd_free,
477     bio_zstd_callback_ctrl
478 };
479 #endif
480
481 const BIO_METHOD *BIO_f_zstd(void)
482 {
483 #ifndef OPENSSL_NO_ZSTD
484     return &bio_meth_zstd;
485 #else
486     return NULL;
487 #endif
488 }
489
490 #ifndef OPENSSL_NO_ZSTD
491 static int bio_zstd_new(BIO *bi)
492 {
493     BIO_ZSTD_CTX *ctx;
494
495 # ifdef ZSTD_SHARED
496     (void)COMP_zstd();
497     if (zstd_dso == NULL) {
498         ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_NOT_SUPPORTED);
499         return 0;
500     }
501 # endif
502     ctx = OPENSSL_zalloc(sizeof(*ctx));
503     if (ctx == NULL) {
504         ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
505         return 0;
506     }
507
508 # ifdef ZSTD_SHARED
509     ctx->decompress.state =  ZSTD_createDStream();
510 # else
511     ctx->decompress.state =  ZSTD_createDStream_advanced(zstd_mem_funcs);
512 # endif
513     if (ctx->decompress.state == NULL)
514         goto err;
515     ZSTD_initDStream(ctx->decompress.state);
516     ctx->decompress.bufsize = ZSTD_DStreamInSize();
517
518 # ifdef ZSTD_SHARED
519     ctx->compress.state = ZSTD_createCStream();
520 # else
521     ctx->compress.state = ZSTD_createCStream_advanced(zstd_mem_funcs);
522 # endif
523     if (ctx->compress.state == NULL)
524         goto err;
525     ZSTD_initCStream(ctx->compress.state, ZSTD_CLEVEL_DEFAULT);
526     ctx->compress.bufsize = ZSTD_CStreamInSize();
527
528     BIO_set_init(bi, 1);
529     BIO_set_data(bi, ctx);
530
531     return 1;
532  err:
533     ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
534     ZSTD_freeDStream(ctx->decompress.state);
535     ZSTD_freeCStream(ctx->compress.state);
536     OPENSSL_free(ctx);
537     return 0;
538 }
539
540 static int bio_zstd_free(BIO *bi)
541 {
542     BIO_ZSTD_CTX *ctx;
543
544     if (bi == NULL)
545         return 0;
546
547     ctx = BIO_get_data(bi);
548     if (ctx != NULL) {
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);
553         OPENSSL_free(ctx);
554     }
555     BIO_set_data(bi, NULL);
556     BIO_set_init(bi, 0);
557
558     return 1;
559 }
560
561 static int bio_zstd_read(BIO *b, char *out, int outl)
562 {
563     BIO_ZSTD_CTX *ctx;
564     size_t zret;
565     int ret;
566     ZSTD_outBuffer outBuf;
567     BIO *next = BIO_next(b);
568
569     if (out == NULL || outl <= 0)
570         return 0;
571
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);
578             return 0;
579         }
580         ctx->decompress.inbuf.src = ctx->decompress.buffer;
581         ctx->decompress.inbuf.size = 0;
582         ctx->decompress.inbuf.pos = 0;
583     }
584
585     /* Copy output data directly to supplied buffer */
586     outBuf.dst = out;
587     outBuf.size = (size_t)outl;
588     outBuf.pos = 0;
589     for (;;) {
590         /* Decompress while data available */
591         do {
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));
596                 return -1;
597             }
598             /* No more output space */
599             if (outBuf.pos == outBuf.size)
600                 return outBuf.pos;
601         } while (ctx->decompress.inbuf.pos < ctx->decompress.inbuf.size);
602
603         /*
604          * No data in input buffer try to read some in, if an error then
605          * return the total data read.
606          */
607         ret = BIO_read(next, ctx->decompress.buffer, ctx->decompress.bufsize);
608         if (ret <= 0) {
609             BIO_copy_next_retry(b);
610             if (ret < 0 && outBuf.pos == 0)
611                 return ret;
612             return outBuf.pos;
613         }
614         ctx->decompress.inbuf.size = ret;
615         ctx->decompress.inbuf.pos = 0;
616     }
617 }
618
619 static int bio_zstd_write(BIO *b, const char *in, int inl)
620 {
621     BIO_ZSTD_CTX *ctx;
622     size_t zret;
623     ZSTD_inBuffer inBuf;
624     int ret;
625     int done = 0;
626     BIO *next = BIO_next(b);
627
628     if (in == NULL || inl <= 0)
629         return 0;
630
631     ctx = BIO_get_data(b);
632
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);
638             return 0;
639         }
640         ctx->compress.outbuf.size = ctx->compress.bufsize;
641         ctx->compress.outbuf.pos = 0;
642         ctx->compress.write_pos = 0;
643     }
644     /* Obtain input data directly from supplied buffer */
645     inBuf.src = in;
646     inBuf.size = inl;
647     inBuf.pos = 0;
648     for (;;) {
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);
653             if (ret <= 0) {
654                 BIO_copy_next_retry(b);
655                 if (ret < 0 && inBuf.pos == 0)
656                     return ret;
657                 return inBuf.pos;
658             }
659             ctx->compress.write_pos += ret;
660         }
661
662         /* Have we consumed all supplied data? */
663         if (done)
664             return inBuf.pos;
665
666         /* Reset buffer */
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));
675             return 0;
676         } else if (zret == 0) {
677             done = 1;
678         }
679     }
680 }
681
682 static int bio_zstd_flush(BIO *b)
683 {
684     BIO_ZSTD_CTX *ctx;
685     size_t zret;
686     int ret;
687     BIO *next = BIO_next(b);
688
689     ctx = BIO_get_data(b);
690
691     /* If no data written or already flush show success */
692     if (ctx->compress.outbuf.dst == NULL)
693         return 1;
694
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;
700     for (;;) {
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);
705             if (ret <= 0) {
706                 BIO_copy_next_retry(b);
707                 return ret;
708             }
709             ctx->compress.write_pos += ret;
710         }
711
712         /* Reset buffer */
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));
721             return 0;
722         }
723         if (zret == 0)
724             return 1;
725     }
726 }
727
728 static long bio_zstd_ctrl(BIO *b, int cmd, long num, void *ptr)
729 {
730     BIO_ZSTD_CTX *ctx;
731     int ret = 0, *ip;
732     size_t ibs, obs;
733     unsigned char *tmp;
734     BIO *next = BIO_next(b);
735
736     if (next == NULL)
737         return 0;
738     ctx = BIO_get_data(b);
739     switch (cmd) {
740
741     case BIO_CTRL_RESET:
742         ctx->compress.write_pos = 0;
743         ctx->compress.bufsize = 0;
744         ret = 1;
745         break;
746
747     case BIO_CTRL_FLUSH:
748         ret = bio_zstd_flush(b);
749         if (ret > 0)
750             ret = BIO_flush(next);
751         break;
752
753     case BIO_C_SET_BUFF_SIZE:
754         ibs = ctx->decompress.bufsize;
755         obs = ctx->compress.bufsize;
756         if (ptr != NULL) {
757             ip = ptr;
758             if (*ip == 0)
759                 ibs = (size_t)num;
760             else
761                 obs = (size_t)num;
762         } else {
763             obs = ibs = (size_t)num;
764         }
765
766         if (ibs > 0 && ibs != ctx->decompress.bufsize) {
767             if (ctx->decompress.buffer != NULL) {
768                 tmp = OPENSSL_realloc(ctx->decompress.buffer, ibs);
769                 if (tmp == NULL)
770                     return 0;
771                 if (ctx->decompress.inbuf.src == ctx->decompress.buffer)
772                     ctx->decompress.inbuf.src = tmp;
773                 ctx->decompress.buffer = tmp;
774             }
775             ctx->decompress.bufsize = ibs;
776         }
777
778         if (obs > 0 && obs != ctx->compress.bufsize) {
779             if (ctx->compress.outbuf.dst != NULL) {
780                 tmp = OPENSSL_realloc(ctx->compress.outbuf.dst, obs);
781                 if (tmp == NULL)
782                     return 0;
783                 ctx->compress.outbuf.dst = tmp;
784             }
785             ctx->compress.bufsize = obs;
786         }
787         ret = 1;
788         break;
789
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);
794         break;
795
796    case BIO_CTRL_WPENDING:
797         if (ctx->compress.outbuf.pos < ctx->compress.outbuf.size)
798             ret = 1;
799         else
800             ret = BIO_ctrl(next, cmd, num, ptr);
801         break;
802
803     case BIO_CTRL_PENDING:
804         if (ctx->decompress.inbuf.pos < ctx->decompress.inbuf.size)
805             ret = 1;
806         else
807             ret = BIO_ctrl(next, cmd, num, ptr);
808         break;
809
810     default:
811         ret = BIO_ctrl(next, cmd, num, ptr);
812         break;
813
814     }
815
816     return ret;
817 }
818
819 static long bio_zstd_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
820 {
821     BIO *next = BIO_next(b);
822     if (next == NULL)
823         return 0;
824     return BIO_callback_ctrl(next, cmd, fp);
825 }
826
827 #endif