Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/comp/c_zstd.c
Line
Count
Source
1
/*
2
 * Copyright 1998-2026 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
#ifdef OPENSSL_NO_ZSTD
29
#undef ZSTD_SHARED
30
#else
31
32
#ifndef ZSTD_SHARED
33
#define ZSTD_STATIC_LINKING_ONLY
34
#endif
35
#include <zstd.h>
36
37
/* Note: There is also a linux zstd.h file in the kernel source */
38
#ifndef ZSTD_H_235446
39
#error Wrong (i.e. linux) zstd.h included.
40
#endif
41
42
#if ZSTD_VERSION_MAJOR != 1 && ZSTD_VERSION_MINOR < 4
43
#error Expecting version 1.4 or greater of ZSTD
44
#endif
45
46
#ifndef ZSTD_SHARED
47
/* memory allocations functions for zstd initialisation */
48
static void *zstd_alloc(void *opaque, size_t size)
49
{
50
    return OPENSSL_zalloc(size);
51
}
52
53
static void zstd_free(void *opaque, void *address)
54
{
55
    OPENSSL_free(address);
56
}
57
58
static ZSTD_customMem zstd_mem_funcs = {
59
    zstd_alloc,
60
    zstd_free,
61
    NULL
62
};
63
#endif
64
65
/*
66
 * When OpenSSL is built on Windows, we do not want to require that
67
 * the LIBZSTD.DLL be available in order for the OpenSSL DLLs to
68
 * work.  Therefore, all ZSTD routines are loaded at run time
69
 * and we do not link to a .LIB file when ZSTD_SHARED is set.
70
 */
71
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
72
#include <windows.h>
73
#endif
74
75
#ifdef ZSTD_SHARED
76
#include "internal/dso.h"
77
78
/* Function pointers */
79
typedef ZSTD_CStream *(*createCStream_ft)(void);
80
typedef size_t (*initCStream_ft)(ZSTD_CStream *, int);
81
typedef size_t (*freeCStream_ft)(ZSTD_CStream *);
82
typedef size_t (*compressStream2_ft)(ZSTD_CCtx *, ZSTD_outBuffer *, ZSTD_inBuffer *, ZSTD_EndDirective);
83
typedef size_t (*flushStream_ft)(ZSTD_CStream *, ZSTD_outBuffer *);
84
typedef size_t (*endStream_ft)(ZSTD_CStream *, ZSTD_outBuffer *);
85
typedef size_t (*compress_ft)(void *, size_t, const void *, size_t, int);
86
typedef ZSTD_DStream *(*createDStream_ft)(void);
87
typedef size_t (*initDStream_ft)(ZSTD_DStream *);
88
typedef size_t (*freeDStream_ft)(ZSTD_DStream *);
89
typedef size_t (*decompressStream_ft)(ZSTD_DStream *, ZSTD_outBuffer *, ZSTD_inBuffer *);
90
typedef size_t (*decompress_ft)(void *, size_t, const void *, size_t);
91
typedef unsigned (*isError_ft)(size_t);
92
typedef const char *(*getErrorName_ft)(size_t);
93
typedef size_t (*DStreamInSize_ft)(void);
94
typedef size_t (*CStreamInSize_ft)(void);
95
96
static createCStream_ft p_createCStream = NULL;
97
static initCStream_ft p_initCStream = NULL;
98
static freeCStream_ft p_freeCStream = NULL;
99
static compressStream2_ft p_compressStream2 = NULL;
100
static flushStream_ft p_flushStream = NULL;
101
static endStream_ft p_endStream = NULL;
102
static compress_ft p_compress = NULL;
103
static createDStream_ft p_createDStream = NULL;
104
static initDStream_ft p_initDStream = NULL;
105
static freeDStream_ft p_freeDStream = NULL;
106
static decompressStream_ft p_decompressStream = NULL;
107
static decompress_ft p_decompress = NULL;
108
static isError_ft p_isError = NULL;
109
static getErrorName_ft p_getErrorName = NULL;
110
static DStreamInSize_ft p_DStreamInSize = NULL;
111
static CStreamInSize_ft p_CStreamInSize = NULL;
112
113
static DSO *zstd_dso = NULL;
114
115
#define ZSTD_createCStream p_createCStream
116
#define ZSTD_initCStream p_initCStream
117
#define ZSTD_freeCStream p_freeCStream
118
#define ZSTD_compressStream2 p_compressStream2
119
#define ZSTD_flushStream p_flushStream
120
#define ZSTD_endStream p_endStream
121
#define ZSTD_compress p_compress
122
#define ZSTD_createDStream p_createDStream
123
#define ZSTD_initDStream p_initDStream
124
#define ZSTD_freeDStream p_freeDStream
125
#define ZSTD_decompressStream p_decompressStream
126
#define ZSTD_decompress p_decompress
127
#define ZSTD_isError p_isError
128
#define ZSTD_getErrorName p_getErrorName
129
#define ZSTD_DStreamInSize p_DStreamInSize
130
#define ZSTD_CStreamInSize p_CStreamInSize
131
132
#endif /* ifdef ZSTD_SHARED */
133
134
struct zstd_state {
135
    ZSTD_CStream *compressor;
136
    ZSTD_DStream *decompressor;
137
};
138
139
static int zstd_stateful_init(COMP_CTX *ctx)
140
{
141
    struct zstd_state *state = OPENSSL_zalloc(sizeof(*state));
142
143
    if (state == NULL)
144
        return 0;
145
146
#ifdef ZSTD_SHARED
147
    state->compressor = ZSTD_createCStream();
148
#else
149
    state->compressor = ZSTD_createCStream_advanced(zstd_mem_funcs);
150
#endif
151
    if (state->compressor == NULL)
152
        goto err;
153
    ZSTD_initCStream(state->compressor, ZSTD_CLEVEL_DEFAULT);
154
155
#ifdef ZSTD_SHARED
156
    state->decompressor = ZSTD_createDStream();
157
#else
158
    state->decompressor = ZSTD_createDStream_advanced(zstd_mem_funcs);
159
#endif
160
    if (state->decompressor == NULL)
161
        goto err;
162
    ZSTD_initDStream(state->decompressor);
163
164
    ctx->data = state;
165
    return 1;
166
err:
167
    ZSTD_freeCStream(state->compressor);
168
    ZSTD_freeDStream(state->decompressor);
169
    OPENSSL_free(state);
170
    return 0;
171
}
172
173
static void zstd_stateful_finish(COMP_CTX *ctx)
174
{
175
    struct zstd_state *state = ctx->data;
176
177
    if (state != NULL) {
178
        ZSTD_freeCStream(state->compressor);
179
        ZSTD_freeDStream(state->decompressor);
180
        OPENSSL_free(state);
181
        ctx->data = NULL;
182
    }
183
}
184
185
static ossl_ssize_t zstd_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
186
    size_t olen, unsigned char *in,
187
    size_t ilen)
188
{
189
    ZSTD_inBuffer inbuf;
190
    ZSTD_outBuffer outbuf;
191
    size_t ret;
192
    ossl_ssize_t fret;
193
    struct zstd_state *state = ctx->data;
194
195
    inbuf.src = in;
196
    inbuf.size = ilen;
197
    inbuf.pos = 0;
198
    outbuf.dst = out;
199
    outbuf.size = olen;
200
    outbuf.pos = 0;
201
202
    if (state == NULL)
203
        return -1;
204
205
    /* If input length is zero, end the stream/frame ? */
206
    if (ilen == 0) {
207
        ret = ZSTD_endStream(state->compressor, &outbuf);
208
        if (ZSTD_isError(ret))
209
            return -1;
210
        goto end;
211
    }
212
213
    /*
214
     * The finish API does not provide a final output buffer,
215
     * so each compress operation has to be ended, if all
216
     * the input data can't be accepted, or there is more output,
217
     * this has to be considered an error, since there is no more
218
     * output buffer space.
219
     */
220
    do {
221
        ret = ZSTD_compressStream2(state->compressor, &outbuf, &inbuf, ZSTD_e_continue);
222
        if (ZSTD_isError(ret))
223
            return -1;
224
        /* do I need to check for ret == 0 ? */
225
    } while (inbuf.pos < inbuf.size);
226
227
    /* Did not consume all the data */
228
    if (inbuf.pos < inbuf.size)
229
        return -1;
230
231
    ret = ZSTD_flushStream(state->compressor, &outbuf);
232
    if (ZSTD_isError(ret))
233
        return -1;
234
235
end:
236
    if (outbuf.pos > OSSL_SSIZE_MAX)
237
        return -1;
238
    fret = (ossl_ssize_t)outbuf.pos;
239
    if (fret < 0)
240
        return -1;
241
    return fret;
242
}
243
244
static ossl_ssize_t zstd_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
245
    size_t olen, unsigned char *in,
246
    size_t ilen)
247
{
248
    ZSTD_inBuffer inbuf;
249
    ZSTD_outBuffer outbuf;
250
    size_t ret;
251
    ossl_ssize_t fret;
252
    struct zstd_state *state = ctx->data;
253
254
    inbuf.src = in;
255
    inbuf.size = ilen;
256
    inbuf.pos = 0;
257
    outbuf.dst = out;
258
    outbuf.size = olen;
259
    outbuf.pos = 0;
260
261
    if (state == NULL)
262
        return -1;
263
264
    if (ilen == 0)
265
        return 0;
266
267
    do {
268
        ret = ZSTD_decompressStream(state->decompressor, &outbuf, &inbuf);
269
        if (ZSTD_isError(ret))
270
            return -1;
271
        /* If we completed a frame, and there's more data, try again */
272
    } while (ret == 0 && inbuf.pos < inbuf.size);
273
274
    /* Did not consume all the data */
275
    if (inbuf.pos < inbuf.size)
276
        return -1;
277
278
    if (outbuf.pos > OSSL_SSIZE_MAX)
279
        return -1;
280
    fret = (ossl_ssize_t)outbuf.pos;
281
    if (fret < 0)
282
        return -1;
283
    return fret;
284
}
285
286
static COMP_METHOD zstd_stateful_method = {
287
    NID_zstd,
288
    LN_zstd,
289
    zstd_stateful_init,
290
    zstd_stateful_finish,
291
    zstd_stateful_compress_block,
292
    zstd_stateful_expand_block
293
};
294
295
static int zstd_oneshot_init(COMP_CTX *ctx)
296
{
297
    return 1;
298
}
299
300
static void zstd_oneshot_finish(COMP_CTX *ctx)
301
{
302
}
303
304
static ossl_ssize_t zstd_oneshot_compress_block(COMP_CTX *ctx, unsigned char *out,
305
    size_t olen, unsigned char *in,
306
    size_t ilen)
307
{
308
    size_t out_size;
309
    ossl_ssize_t ret;
310
311
    if (ilen == 0)
312
        return 0;
313
314
    /* Note: uses STDLIB memory allocators */
315
    out_size = ZSTD_compress(out, olen, in, ilen, ZSTD_CLEVEL_DEFAULT);
316
    if (ZSTD_isError(out_size))
317
        return -1;
318
319
    if (out_size > OSSL_SSIZE_MAX)
320
        return -1;
321
    ret = (ossl_ssize_t)out_size;
322
    if (ret < 0)
323
        return -1;
324
    return ret;
325
}
326
327
static ossl_ssize_t zstd_oneshot_expand_block(COMP_CTX *ctx, unsigned char *out,
328
    size_t olen, unsigned char *in,
329
    size_t ilen)
330
{
331
    size_t out_size;
332
    ossl_ssize_t ret;
333
334
    if (ilen == 0)
335
        return 0;
336
337
    /* Note: uses STDLIB memory allocators */
338
    out_size = ZSTD_decompress(out, olen, in, ilen);
339
    if (ZSTD_isError(out_size))
340
        return -1;
341
342
    if (out_size > OSSL_SSIZE_MAX)
343
        return -1;
344
    ret = (ossl_ssize_t)out_size;
345
    if (ret < 0)
346
        return -1;
347
    return ret;
348
}
349
350
static COMP_METHOD zstd_oneshot_method = {
351
    NID_zstd,
352
    LN_zstd,
353
    zstd_oneshot_init,
354
    zstd_oneshot_finish,
355
    zstd_oneshot_compress_block,
356
    zstd_oneshot_expand_block
357
};
358
359
static CRYPTO_ONCE zstd_once = CRYPTO_ONCE_STATIC_INIT;
360
DEFINE_RUN_ONCE_STATIC(ossl_comp_zstd_init)
361
{
362
#ifdef ZSTD_SHARED
363
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
364
#define LIBZSTD "LIBZSTD"
365
#else
366
#define LIBZSTD "zstd"
367
#endif
368
369
    ERR_set_mark();
370
    zstd_dso = DSO_load(NULL, LIBZSTD, NULL, 0);
371
    if (zstd_dso != NULL) {
372
        p_createCStream = (createCStream_ft)DSO_bind_func(zstd_dso, "ZSTD_createCStream");
373
        p_initCStream = (initCStream_ft)DSO_bind_func(zstd_dso, "ZSTD_initCStream");
374
        p_freeCStream = (freeCStream_ft)DSO_bind_func(zstd_dso, "ZSTD_freeCStream");
375
        p_compressStream2 = (compressStream2_ft)DSO_bind_func(zstd_dso, "ZSTD_compressStream2");
376
        p_flushStream = (flushStream_ft)DSO_bind_func(zstd_dso, "ZSTD_flushStream");
377
        p_endStream = (endStream_ft)DSO_bind_func(zstd_dso, "ZSTD_endStream");
378
        p_compress = (compress_ft)DSO_bind_func(zstd_dso, "ZSTD_compress");
379
        p_createDStream = (createDStream_ft)DSO_bind_func(zstd_dso, "ZSTD_createDStream");
380
        p_initDStream = (initDStream_ft)DSO_bind_func(zstd_dso, "ZSTD_initDStream");
381
        p_freeDStream = (freeDStream_ft)DSO_bind_func(zstd_dso, "ZSTD_freeDStream");
382
        p_decompressStream = (decompressStream_ft)DSO_bind_func(zstd_dso, "ZSTD_decompressStream");
383
        p_decompress = (decompress_ft)DSO_bind_func(zstd_dso, "ZSTD_decompress");
384
        p_isError = (isError_ft)DSO_bind_func(zstd_dso, "ZSTD_isError");
385
        p_getErrorName = (getErrorName_ft)DSO_bind_func(zstd_dso, "ZSTD_getErrorName");
386
        p_DStreamInSize = (DStreamInSize_ft)DSO_bind_func(zstd_dso, "ZSTD_DStreamInSize");
387
        p_CStreamInSize = (CStreamInSize_ft)DSO_bind_func(zstd_dso, "ZSTD_CStreamInSize");
388
    }
389
390
    if (p_createCStream == NULL || p_initCStream == NULL || p_freeCStream == NULL
391
        || p_compressStream2 == NULL || p_flushStream == NULL || p_endStream == NULL
392
        || p_compress == NULL || p_createDStream == NULL || p_initDStream == NULL
393
        || p_freeDStream == NULL || p_decompressStream == NULL || p_decompress == NULL
394
        || p_isError == NULL || p_getErrorName == NULL || p_DStreamInSize == NULL
395
        || p_CStreamInSize == NULL) {
396
        ERR_clear_last_mark();
397
        ossl_comp_zstd_cleanup();
398
        return 0;
399
    }
400
    /* Do not leave errors behind on success. */
401
    ERR_pop_to_mark();
402
#endif
403
    return 1;
404
}
405
#endif /* ifndef ZSTD / else */
406
407
COMP_METHOD *COMP_zstd(void)
408
0
{
409
0
    COMP_METHOD *meth = NULL;
410
411
#ifndef OPENSSL_NO_ZSTD
412
    if (RUN_ONCE(&zstd_once, ossl_comp_zstd_init))
413
        meth = &zstd_stateful_method;
414
#endif
415
0
    return meth;
416
0
}
417
418
COMP_METHOD *COMP_zstd_oneshot(void)
419
0
{
420
0
    COMP_METHOD *meth = NULL;
421
422
#ifndef OPENSSL_NO_ZSTD
423
    if (RUN_ONCE(&zstd_once, ossl_comp_zstd_init))
424
        meth = &zstd_oneshot_method;
425
#endif
426
0
    return meth;
427
0
}
428
429
/* Also called from OPENSSL_cleanup() */
430
void ossl_comp_zstd_cleanup(void)
431
154
{
432
#ifdef ZSTD_SHARED
433
    DSO_free(zstd_dso);
434
    zstd_dso = NULL;
435
    p_createCStream = NULL;
436
    p_initCStream = NULL;
437
    p_freeCStream = NULL;
438
    p_compressStream2 = NULL;
439
    p_flushStream = NULL;
440
    p_endStream = NULL;
441
    p_compress = NULL;
442
    p_createDStream = NULL;
443
    p_initDStream = NULL;
444
    p_freeDStream = NULL;
445
    p_decompressStream = NULL;
446
    p_decompress = NULL;
447
    p_isError = NULL;
448
    p_getErrorName = NULL;
449
    p_DStreamInSize = NULL;
450
    p_CStreamInSize = NULL;
451
#endif
452
154
}
453
454
#ifndef OPENSSL_NO_ZSTD
455
456
/* Zstd-based compression/decompression filter BIO */
457
458
typedef struct {
459
    struct { /* input structure */
460
        ZSTD_DStream *state;
461
        ZSTD_inBuffer inbuf; /* has const src */
462
        size_t bufsize;
463
        void *buffer;
464
    } decompress;
465
    struct { /* output structure */
466
        ZSTD_CStream *state;
467
        ZSTD_outBuffer outbuf;
468
        size_t bufsize;
469
        size_t write_pos;
470
    } compress;
471
} BIO_ZSTD_CTX;
472
473
#define ZSTD_DEFAULT_BUFSIZE 1024
474
475
static int bio_zstd_new(BIO *bi);
476
static int bio_zstd_free(BIO *bi);
477
static int bio_zstd_read(BIO *b, char *out, int outl);
478
static int bio_zstd_write(BIO *b, const char *in, int inl);
479
static long bio_zstd_ctrl(BIO *b, int cmd, long num, void *ptr);
480
static long bio_zstd_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
481
482
static const BIO_METHOD bio_meth_zstd = {
483
    BIO_TYPE_COMP,
484
    "zstd",
485
    /* TODO: Convert to new style write function */
486
    bwrite_conv,
487
    bio_zstd_write,
488
    /* TODO: Convert to new style read function */
489
    bread_conv,
490
    bio_zstd_read,
491
    NULL, /* bio_zstd_puts, */
492
    NULL, /* bio_zstd_gets, */
493
    bio_zstd_ctrl,
494
    bio_zstd_new,
495
    bio_zstd_free,
496
    bio_zstd_callback_ctrl
497
};
498
#endif
499
500
const BIO_METHOD *BIO_f_zstd(void)
501
0
{
502
#ifndef OPENSSL_NO_ZSTD
503
    if (RUN_ONCE(&zstd_once, ossl_comp_zstd_init))
504
        return &bio_meth_zstd;
505
#endif
506
    return NULL;
507
0
}
508
509
#ifndef OPENSSL_NO_ZSTD
510
static int bio_zstd_new(BIO *bi)
511
{
512
    BIO_ZSTD_CTX *ctx;
513
514
#ifdef ZSTD_SHARED
515
    (void)COMP_zstd();
516
    if (zstd_dso == NULL) {
517
        ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_NOT_SUPPORTED);
518
        return 0;
519
    }
520
#endif
521
    ctx = OPENSSL_zalloc(sizeof(*ctx));
522
    if (ctx == NULL) {
523
        ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
524
        return 0;
525
    }
526
527
#ifdef ZSTD_SHARED
528
    ctx->decompress.state = ZSTD_createDStream();
529
#else
530
    ctx->decompress.state = ZSTD_createDStream_advanced(zstd_mem_funcs);
531
#endif
532
    if (ctx->decompress.state == NULL)
533
        goto err;
534
    ZSTD_initDStream(ctx->decompress.state);
535
    ctx->decompress.bufsize = ZSTD_DStreamInSize();
536
537
#ifdef ZSTD_SHARED
538
    ctx->compress.state = ZSTD_createCStream();
539
#else
540
    ctx->compress.state = ZSTD_createCStream_advanced(zstd_mem_funcs);
541
#endif
542
    if (ctx->compress.state == NULL)
543
        goto err;
544
    ZSTD_initCStream(ctx->compress.state, ZSTD_CLEVEL_DEFAULT);
545
    ctx->compress.bufsize = ZSTD_CStreamInSize();
546
547
    BIO_set_init(bi, 1);
548
    BIO_set_data(bi, ctx);
549
550
    return 1;
551
err:
552
    ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
553
    ZSTD_freeDStream(ctx->decompress.state);
554
    ZSTD_freeCStream(ctx->compress.state);
555
    OPENSSL_free(ctx);
556
    return 0;
557
}
558
559
static int bio_zstd_free(BIO *bi)
560
{
561
    BIO_ZSTD_CTX *ctx;
562
563
    if (bi == NULL)
564
        return 0;
565
566
    ctx = BIO_get_data(bi);
567
    if (ctx != NULL) {
568
        ZSTD_freeDStream(ctx->decompress.state);
569
        OPENSSL_free(ctx->decompress.buffer);
570
        ZSTD_freeCStream(ctx->compress.state);
571
        OPENSSL_free(ctx->compress.outbuf.dst);
572
        OPENSSL_free(ctx);
573
    }
574
    BIO_set_data(bi, NULL);
575
    BIO_set_init(bi, 0);
576
577
    return 1;
578
}
579
580
static int bio_zstd_read(BIO *b, char *out, int outl)
581
{
582
    BIO_ZSTD_CTX *ctx;
583
    size_t zret;
584
    int ret;
585
    ZSTD_outBuffer outBuf;
586
    BIO *next = BIO_next(b);
587
588
    if (out == NULL) {
589
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_NULL_PARAMETER);
590
        return -1;
591
    }
592
    if (outl <= 0)
593
        return 0;
594
595
    ctx = BIO_get_data(b);
596
    BIO_clear_retry_flags(b);
597
    if (ctx->decompress.buffer == NULL) {
598
        ctx->decompress.buffer = OPENSSL_malloc(ctx->decompress.bufsize);
599
        if (ctx->decompress.buffer == NULL) {
600
            ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
601
            return -1;
602
        }
603
        ctx->decompress.inbuf.src = ctx->decompress.buffer;
604
        ctx->decompress.inbuf.size = 0;
605
        ctx->decompress.inbuf.pos = 0;
606
    }
607
608
    /* Copy output data directly to supplied buffer */
609
    outBuf.dst = out;
610
    outBuf.size = (size_t)outl;
611
    outBuf.pos = 0;
612
    for (;;) {
613
        /* Decompress while data available */
614
        do {
615
            zret = ZSTD_decompressStream(ctx->decompress.state, &outBuf, &ctx->decompress.inbuf);
616
            if (ZSTD_isError(zret)) {
617
                ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_DECOMPRESS_ERROR);
618
                ERR_add_error_data(1, ZSTD_getErrorName(zret));
619
                return -1;
620
            }
621
            /* No more output space */
622
            if (outBuf.pos == outBuf.size)
623
                return outBuf.pos;
624
        } while (ctx->decompress.inbuf.pos < ctx->decompress.inbuf.size);
625
626
        /*
627
         * No data in input buffer try to read some in, if an error then
628
         * return the total data read.
629
         */
630
        ret = BIO_read(next, ctx->decompress.buffer, ctx->decompress.bufsize);
631
        if (ret <= 0) {
632
            BIO_copy_next_retry(b);
633
            if (ret < 0 && outBuf.pos == 0)
634
                return ret;
635
            return outBuf.pos;
636
        }
637
        ctx->decompress.inbuf.size = ret;
638
        ctx->decompress.inbuf.pos = 0;
639
    }
640
}
641
642
static int bio_zstd_write(BIO *b, const char *in, int inl)
643
{
644
    BIO_ZSTD_CTX *ctx;
645
    size_t zret;
646
    ZSTD_inBuffer inBuf;
647
    int ret;
648
    int done = 0;
649
    BIO *next = BIO_next(b);
650
651
    if (in == NULL || inl <= 0)
652
        return 0;
653
654
    ctx = BIO_get_data(b);
655
656
    BIO_clear_retry_flags(b);
657
    if (ctx->compress.outbuf.dst == NULL) {
658
        ctx->compress.outbuf.dst = OPENSSL_malloc(ctx->compress.bufsize);
659
        if (ctx->compress.outbuf.dst == NULL) {
660
            ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
661
            return 0;
662
        }
663
        ctx->compress.outbuf.size = ctx->compress.bufsize;
664
        ctx->compress.outbuf.pos = 0;
665
        ctx->compress.write_pos = 0;
666
    }
667
    /* Obtain input data directly from supplied buffer */
668
    inBuf.src = in;
669
    inBuf.size = inl;
670
    inBuf.pos = 0;
671
    for (;;) {
672
        /* If data in output buffer write it first */
673
        while (ctx->compress.write_pos < ctx->compress.outbuf.pos) {
674
            ret = BIO_write(next, (unsigned char *)ctx->compress.outbuf.dst + ctx->compress.write_pos,
675
                ctx->compress.outbuf.pos - ctx->compress.write_pos);
676
            if (ret <= 0) {
677
                BIO_copy_next_retry(b);
678
                if (ret < 0 && inBuf.pos == 0)
679
                    return ret;
680
                return inBuf.pos;
681
            }
682
            ctx->compress.write_pos += ret;
683
        }
684
685
        /* Have we consumed all supplied data? */
686
        if (done)
687
            return inBuf.pos;
688
689
        /* Reset buffer */
690
        ctx->compress.outbuf.pos = 0;
691
        ctx->compress.outbuf.size = ctx->compress.bufsize;
692
        ctx->compress.write_pos = 0;
693
        /* Compress some more */
694
        zret = ZSTD_compressStream2(ctx->compress.state, &ctx->compress.outbuf, &inBuf, ZSTD_e_end);
695
        if (ZSTD_isError(zret)) {
696
            ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_COMPRESS_ERROR);
697
            ERR_add_error_data(1, ZSTD_getErrorName(zret));
698
            return 0;
699
        } else if (zret == 0) {
700
            done = 1;
701
        }
702
    }
703
}
704
705
static int bio_zstd_flush(BIO *b)
706
{
707
    BIO_ZSTD_CTX *ctx;
708
    size_t zret;
709
    int ret;
710
    BIO *next = BIO_next(b);
711
712
    ctx = BIO_get_data(b);
713
714
    /* If no data written or already flush show success */
715
    if (ctx->compress.outbuf.dst == NULL)
716
        return 1;
717
718
    BIO_clear_retry_flags(b);
719
    /* No more input data */
720
    ctx->compress.outbuf.pos = 0;
721
    ctx->compress.outbuf.size = ctx->compress.bufsize;
722
    ctx->compress.write_pos = 0;
723
    for (;;) {
724
        /* If data in output buffer write it first */
725
        while (ctx->compress.write_pos < ctx->compress.outbuf.pos) {
726
            ret = BIO_write(next, (unsigned char *)ctx->compress.outbuf.dst + ctx->compress.write_pos,
727
                ctx->compress.outbuf.pos - ctx->compress.write_pos);
728
            if (ret <= 0) {
729
                BIO_copy_next_retry(b);
730
                return ret;
731
            }
732
            ctx->compress.write_pos += ret;
733
        }
734
735
        /* Reset buffer */
736
        ctx->compress.outbuf.pos = 0;
737
        ctx->compress.outbuf.size = ctx->compress.bufsize;
738
        ctx->compress.write_pos = 0;
739
        /* Compress some more */
740
        zret = ZSTD_flushStream(ctx->compress.state, &ctx->compress.outbuf);
741
        if (ZSTD_isError(zret)) {
742
            ERR_raise(ERR_LIB_COMP, COMP_R_ZSTD_DECODE_ERROR);
743
            ERR_add_error_data(1, ZSTD_getErrorName(zret));
744
            return 0;
745
        }
746
        if (zret == 0)
747
            return 1;
748
    }
749
}
750
751
static long bio_zstd_ctrl(BIO *b, int cmd, long num, void *ptr)
752
{
753
    BIO_ZSTD_CTX *ctx;
754
    int ret = 0, *ip;
755
    size_t ibs, obs;
756
    unsigned char *tmp;
757
    BIO *next = BIO_next(b);
758
759
    if (next == NULL)
760
        return 0;
761
    ctx = BIO_get_data(b);
762
    switch (cmd) {
763
764
    case BIO_CTRL_RESET:
765
        ctx->compress.write_pos = 0;
766
        ctx->compress.bufsize = 0;
767
        ret = 1;
768
        break;
769
770
    case BIO_CTRL_FLUSH:
771
        ret = bio_zstd_flush(b);
772
        if (ret > 0) {
773
            ret = BIO_flush(next);
774
            BIO_copy_next_retry(b);
775
        }
776
        break;
777
778
    case BIO_C_SET_BUFF_SIZE:
779
        ibs = ctx->decompress.bufsize;
780
        obs = ctx->compress.bufsize;
781
        if (ptr != NULL) {
782
            ip = ptr;
783
            if (*ip == 0)
784
                ibs = (size_t)num;
785
            else
786
                obs = (size_t)num;
787
        } else {
788
            obs = ibs = (size_t)num;
789
        }
790
791
        if (ibs > 0 && ibs != ctx->decompress.bufsize) {
792
            if (ctx->decompress.buffer != NULL) {
793
                tmp = OPENSSL_realloc(ctx->decompress.buffer, ibs);
794
                if (tmp == NULL)
795
                    return 0;
796
                if (ctx->decompress.inbuf.src == ctx->decompress.buffer)
797
                    ctx->decompress.inbuf.src = tmp;
798
                ctx->decompress.buffer = tmp;
799
            }
800
            ctx->decompress.bufsize = ibs;
801
        }
802
803
        if (obs > 0 && obs != ctx->compress.bufsize) {
804
            if (ctx->compress.outbuf.dst != NULL) {
805
                tmp = OPENSSL_realloc(ctx->compress.outbuf.dst, obs);
806
                if (tmp == NULL)
807
                    return 0;
808
                ctx->compress.outbuf.dst = tmp;
809
            }
810
            ctx->compress.bufsize = obs;
811
        }
812
        ret = 1;
813
        break;
814
815
    case BIO_C_DO_STATE_MACHINE:
816
        BIO_clear_retry_flags(b);
817
        ret = BIO_ctrl(next, cmd, num, ptr);
818
        BIO_copy_next_retry(b);
819
        break;
820
821
    case BIO_CTRL_WPENDING:
822
        if (ctx->compress.outbuf.pos < ctx->compress.outbuf.size)
823
            ret = 1;
824
        else
825
            ret = BIO_ctrl(next, cmd, num, ptr);
826
        break;
827
828
    case BIO_CTRL_PENDING:
829
        if (ctx->decompress.inbuf.pos < ctx->decompress.inbuf.size)
830
            ret = 1;
831
        else
832
            ret = BIO_ctrl(next, cmd, num, ptr);
833
        break;
834
835
    default:
836
        ret = BIO_ctrl(next, cmd, num, ptr);
837
        break;
838
    }
839
840
    return ret;
841
}
842
843
static long bio_zstd_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
844
{
845
    BIO *next = BIO_next(b);
846
    if (next == NULL)
847
        return 0;
848
    return BIO_callback_ctrl(next, cmd, fp);
849
}
850
851
#endif