Coverage Report

Created: 2026-07-19 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/comp/c_brotli.c
Line
Count
Source
1
/*
2
 * Copyright 1998-2025 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 brotli compression library from https://github.com/google/brotli
10
 */
11
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <string.h>
15
#include <openssl/objects.h>
16
#include "internal/e_os.h"
17
#include "internal/comp.h"
18
#include <openssl/err.h>
19
#include "crypto/cryptlib.h"
20
#include "internal/bio.h"
21
#include "internal/thread_once.h"
22
#include "comp_local.h"
23
24
COMP_METHOD *COMP_brotli(void);
25
26
#ifdef OPENSSL_NO_BROTLI
27
#undef BROTLI_SHARED
28
#else
29
30
#include <brotli/decode.h>
31
#include <brotli/encode.h>
32
33
/* memory allocations functions for brotli initialisation */
34
static void *brotli_alloc(void *opaque, size_t size)
35
{
36
    return OPENSSL_zalloc(size);
37
}
38
39
static void brotli_free(void *opaque, void *address)
40
{
41
    OPENSSL_free(address);
42
}
43
44
/*
45
 * When OpenSSL is built on Windows, we do not want to require that
46
 * the BROTLI.DLL be available in order for the OpenSSL DLLs to
47
 * work.  Therefore, all BROTLI routines are loaded at run time
48
 * and we do not link to a .LIB file when BROTLI_SHARED is set.
49
 */
50
#ifdef BROTLI_SHARED
51
#include "internal/dso.h"
52
53
/* Function pointers */
54
typedef BrotliEncoderState *(*encode_init_ft)(brotli_alloc_func, brotli_free_func, void *);
55
typedef BROTLI_BOOL (*encode_stream_ft)(BrotliEncoderState *, BrotliEncoderOperation, size_t *, const uint8_t **, size_t *, uint8_t **, size_t *);
56
typedef BROTLI_BOOL (*encode_has_more_ft)(BrotliEncoderState *);
57
typedef void (*encode_end_ft)(BrotliEncoderState *);
58
typedef BROTLI_BOOL (*encode_oneshot_ft)(int, int, BrotliEncoderMode, size_t, const uint8_t in[], size_t *, uint8_t out[]);
59
60
typedef BrotliDecoderState *(*decode_init_ft)(brotli_alloc_func, brotli_free_func, void *);
61
typedef BROTLI_BOOL (*decode_stream_ft)(BrotliDecoderState *, size_t *, const uint8_t **, size_t *, uint8_t **, size_t *);
62
typedef BROTLI_BOOL (*decode_has_more_ft)(BrotliDecoderState *);
63
typedef void (*decode_end_ft)(BrotliDecoderState *);
64
typedef BrotliDecoderErrorCode (*decode_error_ft)(BrotliDecoderState *);
65
typedef const char *(*decode_error_string_ft)(BrotliDecoderErrorCode);
66
typedef BROTLI_BOOL (*decode_is_finished_ft)(BrotliDecoderState *);
67
typedef BrotliDecoderResult (*decode_oneshot_ft)(size_t, const uint8_t in[], size_t *, uint8_t out[]);
68
69
static encode_init_ft p_encode_init = NULL;
70
static encode_stream_ft p_encode_stream = NULL;
71
static encode_has_more_ft p_encode_has_more = NULL;
72
static encode_end_ft p_encode_end = NULL;
73
static encode_oneshot_ft p_encode_oneshot = NULL;
74
75
static decode_init_ft p_decode_init = NULL;
76
static decode_stream_ft p_decode_stream = NULL;
77
static decode_has_more_ft p_decode_has_more = NULL;
78
static decode_end_ft p_decode_end = NULL;
79
static decode_error_ft p_decode_error = NULL;
80
static decode_error_string_ft p_decode_error_string = NULL;
81
static decode_is_finished_ft p_decode_is_finished = NULL;
82
static decode_oneshot_ft p_decode_oneshot = NULL;
83
84
static DSO *brotli_encode_dso = NULL;
85
static DSO *brotli_decode_dso = NULL;
86
87
#define BrotliEncoderCreateInstance p_encode_init
88
#define BrotliEncoderCompressStream p_encode_stream
89
#define BrotliEncoderHasMoreOutput p_encode_has_more
90
#define BrotliEncoderDestroyInstance p_encode_end
91
#define BrotliEncoderCompress p_encode_oneshot
92
93
#define BrotliDecoderCreateInstance p_decode_init
94
#define BrotliDecoderDecompressStream p_decode_stream
95
#define BrotliDecoderHasMoreOutput p_decode_has_more
96
#define BrotliDecoderDestroyInstance p_decode_end
97
#define BrotliDecoderGetErrorCode p_decode_error
98
#define BrotliDecoderErrorString p_decode_error_string
99
#define BrotliDecoderIsFinished p_decode_is_finished
100
#define BrotliDecoderDecompress p_decode_oneshot
101
102
#endif /* ifdef BROTLI_SHARED */
103
104
struct brotli_state {
105
    BrotliEncoderState *encoder;
106
    BrotliDecoderState *decoder;
107
};
108
109
static int brotli_stateful_init(COMP_CTX *ctx)
110
{
111
    struct brotli_state *state = OPENSSL_zalloc(sizeof(*state));
112
113
    if (state == NULL)
114
        return 0;
115
116
    state->encoder = BrotliEncoderCreateInstance(brotli_alloc, brotli_free, NULL);
117
    if (state->encoder == NULL)
118
        goto err;
119
120
    state->decoder = BrotliDecoderCreateInstance(brotli_alloc, brotli_free, NULL);
121
    if (state->decoder == NULL)
122
        goto err;
123
124
    ctx->data = state;
125
    return 1;
126
err:
127
    BrotliDecoderDestroyInstance(state->decoder);
128
    BrotliEncoderDestroyInstance(state->encoder);
129
    OPENSSL_free(state);
130
    return 0;
131
}
132
133
static void brotli_stateful_finish(COMP_CTX *ctx)
134
{
135
    struct brotli_state *state = ctx->data;
136
137
    if (state != NULL) {
138
        BrotliDecoderDestroyInstance(state->decoder);
139
        BrotliEncoderDestroyInstance(state->encoder);
140
        OPENSSL_free(state);
141
        ctx->data = NULL;
142
    }
143
}
144
145
static ossl_ssize_t brotli_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
146
    size_t olen, unsigned char *in,
147
    size_t ilen)
148
{
149
    BROTLI_BOOL done;
150
    struct brotli_state *state = ctx->data;
151
    size_t in_avail = ilen;
152
    size_t out_avail = olen;
153
154
    if (state == NULL || olen > OSSL_SSIZE_MAX)
155
        return -1;
156
157
    if (ilen == 0)
158
        return 0;
159
160
    /*
161
     * The finish API does not provide a final output buffer,
162
     * so each compress operation has to be flushed, if all
163
     * the input data can't be accepted, or there is more output,
164
     * this has to be considered an error, since there is no more
165
     * output buffer space
166
     */
167
    done = BrotliEncoderCompressStream(state->encoder, BROTLI_OPERATION_FLUSH,
168
        &in_avail, (const uint8_t **)&in,
169
        &out_avail, &out, NULL);
170
    if (done == BROTLI_FALSE
171
        || in_avail != 0
172
        || BrotliEncoderHasMoreOutput(state->encoder))
173
        return -1;
174
175
    if (out_avail > olen)
176
        return -1;
177
    return (ossl_ssize_t)(olen - out_avail);
178
}
179
180
static ossl_ssize_t brotli_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
181
    size_t olen, unsigned char *in,
182
    size_t ilen)
183
{
184
    BrotliDecoderResult result;
185
    struct brotli_state *state = ctx->data;
186
    size_t in_avail = ilen;
187
    size_t out_avail = olen;
188
189
    if (state == NULL || olen > OSSL_SSIZE_MAX)
190
        return -1;
191
192
    if (ilen == 0)
193
        return 0;
194
195
    result = BrotliDecoderDecompressStream(state->decoder, &in_avail,
196
        (const uint8_t **)&in, &out_avail,
197
        &out, NULL);
198
    if (result == BROTLI_DECODER_RESULT_ERROR
199
        || in_avail != 0
200
        || BrotliDecoderHasMoreOutput(state->decoder))
201
        return -1;
202
203
    if (out_avail > olen)
204
        return -1;
205
    return (ossl_ssize_t)(olen - out_avail);
206
}
207
208
static COMP_METHOD brotli_stateful_method = {
209
    NID_brotli,
210
    LN_brotli,
211
    brotli_stateful_init,
212
    brotli_stateful_finish,
213
    brotli_stateful_compress_block,
214
    brotli_stateful_expand_block
215
};
216
217
static int brotli_oneshot_init(COMP_CTX *ctx)
218
{
219
    return 1;
220
}
221
222
static void brotli_oneshot_finish(COMP_CTX *ctx)
223
{
224
}
225
226
static ossl_ssize_t brotli_oneshot_compress_block(COMP_CTX *ctx, unsigned char *out,
227
    size_t olen, unsigned char *in,
228
    size_t ilen)
229
{
230
    size_t out_size = olen;
231
    ossl_ssize_t ret;
232
233
    if (ilen == 0)
234
        return 0;
235
236
    if (BrotliEncoderCompress(BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW,
237
            BROTLI_DEFAULT_MODE, ilen, in,
238
            &out_size, out)
239
        == BROTLI_FALSE)
240
        return -1;
241
242
    if (out_size > OSSL_SSIZE_MAX)
243
        return -1;
244
    ret = (ossl_ssize_t)out_size;
245
    if (ret < 0)
246
        return -1;
247
    return ret;
248
}
249
250
static ossl_ssize_t brotli_oneshot_expand_block(COMP_CTX *ctx, unsigned char *out,
251
    size_t olen, unsigned char *in,
252
    size_t ilen)
253
{
254
    size_t out_size = olen;
255
    ossl_ssize_t ret;
256
257
    if (ilen == 0)
258
        return 0;
259
260
    if (BrotliDecoderDecompress(ilen, in, &out_size, out) != BROTLI_DECODER_RESULT_SUCCESS)
261
        return -1;
262
263
    if (out_size > OSSL_SSIZE_MAX)
264
        return -1;
265
    ret = (ossl_ssize_t)out_size;
266
    if (ret < 0)
267
        return -1;
268
    return ret;
269
}
270
271
static COMP_METHOD brotli_oneshot_method = {
272
    NID_brotli,
273
    LN_brotli,
274
    brotli_oneshot_init,
275
    brotli_oneshot_finish,
276
    brotli_oneshot_compress_block,
277
    brotli_oneshot_expand_block
278
};
279
280
static CRYPTO_ONCE brotli_once = CRYPTO_ONCE_STATIC_INIT;
281
DEFINE_RUN_ONCE_STATIC(ossl_comp_brotli_init)
282
{
283
#ifdef BROTLI_SHARED
284
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
285
#define LIBBROTLIENC "BROTLIENC"
286
#define LIBBROTLIDEC "BROTLIDEC"
287
#else
288
#define LIBBROTLIENC "brotlienc"
289
#define LIBBROTLIDEC "brotlidec"
290
#endif
291
292
    brotli_encode_dso = DSO_load(NULL, LIBBROTLIENC, NULL, 0);
293
    if (brotli_encode_dso != NULL) {
294
        p_encode_init = (encode_init_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderCreateInstance");
295
        p_encode_stream = (encode_stream_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderCompressStream");
296
        p_encode_has_more = (encode_has_more_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderHasMoreOutput");
297
        p_encode_end = (encode_end_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderDestroyInstance");
298
        p_encode_oneshot = (encode_oneshot_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderCompress");
299
    }
300
301
    brotli_decode_dso = DSO_load(NULL, LIBBROTLIDEC, NULL, 0);
302
    if (brotli_decode_dso != NULL) {
303
        p_decode_init = (decode_init_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderCreateInstance");
304
        p_decode_stream = (decode_stream_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderDecompressStream");
305
        p_decode_has_more = (decode_has_more_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderHasMoreOutput");
306
        p_decode_end = (decode_end_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderDestroyInstance");
307
        p_decode_error = (decode_error_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderGetErrorCode");
308
        p_decode_error_string = (decode_error_string_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderErrorString");
309
        p_decode_is_finished = (decode_is_finished_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderIsFinished");
310
        p_decode_oneshot = (decode_oneshot_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderDecompress");
311
    }
312
313
    if (p_encode_init == NULL || p_encode_stream == NULL || p_encode_has_more == NULL
314
        || p_encode_end == NULL || p_encode_oneshot == NULL || p_decode_init == NULL
315
        || p_decode_stream == NULL || p_decode_has_more == NULL || p_decode_end == NULL
316
        || p_decode_error == NULL || p_decode_error_string == NULL || p_decode_is_finished == NULL
317
        || p_decode_oneshot == NULL) {
318
        ossl_comp_brotli_cleanup();
319
        return 0;
320
    }
321
#endif
322
    return 1;
323
}
324
#endif /* ifndef BROTLI / else */
325
326
COMP_METHOD *COMP_brotli(void)
327
0
{
328
0
    COMP_METHOD *meth = NULL;
329
330
#ifndef OPENSSL_NO_BROTLI
331
    if (RUN_ONCE(&brotli_once, ossl_comp_brotli_init))
332
        meth = &brotli_stateful_method;
333
#endif
334
0
    return meth;
335
0
}
336
337
COMP_METHOD *COMP_brotli_oneshot(void)
338
0
{
339
0
    COMP_METHOD *meth = NULL;
340
341
#ifndef OPENSSL_NO_BROTLI
342
    if (RUN_ONCE(&brotli_once, ossl_comp_brotli_init))
343
        meth = &brotli_oneshot_method;
344
#endif
345
0
    return meth;
346
0
}
347
348
/* Also called from OPENSSL_cleanup() */
349
void ossl_comp_brotli_cleanup(void)
350
0
{
351
#ifdef BROTLI_SHARED
352
    DSO_free(brotli_encode_dso);
353
    brotli_encode_dso = NULL;
354
    DSO_free(brotli_decode_dso);
355
    brotli_decode_dso = NULL;
356
    p_encode_init = NULL;
357
    p_encode_stream = NULL;
358
    p_encode_has_more = NULL;
359
    p_encode_end = NULL;
360
    p_encode_oneshot = NULL;
361
    p_decode_init = NULL;
362
    p_decode_stream = NULL;
363
    p_decode_has_more = NULL;
364
    p_decode_end = NULL;
365
    p_decode_error = NULL;
366
    p_decode_error_string = NULL;
367
    p_decode_is_finished = NULL;
368
    p_decode_oneshot = NULL;
369
#endif
370
0
}
371
372
#ifndef OPENSSL_NO_BROTLI
373
374
/* Brotli-based compression/decompression filter BIO */
375
376
typedef struct {
377
    struct { /* input structure */
378
        size_t avail_in;
379
        unsigned char *next_in;
380
        size_t avail_out;
381
        unsigned char *next_out;
382
        unsigned char *buf;
383
        size_t bufsize;
384
        BrotliDecoderState *state;
385
    } decode;
386
    struct { /* output structure */
387
        size_t avail_in;
388
        unsigned char *next_in;
389
        size_t avail_out;
390
        unsigned char *next_out;
391
        unsigned char *buf;
392
        size_t bufsize;
393
        BrotliEncoderState *state;
394
        int mode; /* Encoder mode to use */
395
        int done;
396
        unsigned char *ptr;
397
        size_t count;
398
    } encode;
399
} BIO_BROTLI_CTX;
400
401
#define BROTLI_DEFAULT_BUFSIZE 1024
402
403
static int bio_brotli_new(BIO *bi);
404
static int bio_brotli_free(BIO *bi);
405
static int bio_brotli_read(BIO *b, char *out, int outl);
406
static int bio_brotli_write(BIO *b, const char *in, int inl);
407
static long bio_brotli_ctrl(BIO *b, int cmd, long num, void *ptr);
408
static long bio_brotli_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
409
410
static const BIO_METHOD bio_meth_brotli = {
411
    BIO_TYPE_COMP,
412
    "brotli",
413
    /* TODO: Convert to new style write function */
414
    bwrite_conv,
415
    bio_brotli_write,
416
    /* TODO: Convert to new style read function */
417
    bread_conv,
418
    bio_brotli_read,
419
    NULL, /* bio_brotli_puts, */
420
    NULL, /* bio_brotli_gets, */
421
    bio_brotli_ctrl,
422
    bio_brotli_new,
423
    bio_brotli_free,
424
    bio_brotli_callback_ctrl
425
};
426
#endif
427
428
const BIO_METHOD *BIO_f_brotli(void)
429
0
{
430
#ifndef OPENSSL_NO_BROTLI
431
    if (RUN_ONCE(&brotli_once, ossl_comp_brotli_init))
432
        return &bio_meth_brotli;
433
#endif
434
    return NULL;
435
0
}
436
437
#ifndef OPENSSL_NO_BROTLI
438
439
static int bio_brotli_new(BIO *bi)
440
{
441
    BIO_BROTLI_CTX *ctx;
442
443
#ifdef BROTLI_SHARED
444
    if (!RUN_ONCE(&brotli_once, ossl_comp_brotli_init)) {
445
        ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_NOT_SUPPORTED);
446
        return 0;
447
    }
448
#endif
449
    ctx = OPENSSL_zalloc(sizeof(*ctx));
450
    if (ctx == NULL) {
451
        ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
452
        return 0;
453
    }
454
    ctx->decode.bufsize = BROTLI_DEFAULT_BUFSIZE;
455
    ctx->decode.state = BrotliDecoderCreateInstance(brotli_alloc, brotli_free, NULL);
456
    if (ctx->decode.state == NULL)
457
        goto err;
458
    ctx->encode.bufsize = BROTLI_DEFAULT_BUFSIZE;
459
    ctx->encode.state = BrotliEncoderCreateInstance(brotli_alloc, brotli_free, NULL);
460
    if (ctx->encode.state == NULL)
461
        goto err;
462
    ctx->encode.mode = BROTLI_DEFAULT_MODE;
463
    BIO_set_init(bi, 1);
464
    BIO_set_data(bi, ctx);
465
466
    return 1;
467
468
err:
469
    ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
470
    BrotliDecoderDestroyInstance(ctx->decode.state);
471
    BrotliEncoderDestroyInstance(ctx->encode.state);
472
    OPENSSL_free(ctx);
473
    return 0;
474
}
475
476
static int bio_brotli_free(BIO *bi)
477
{
478
    BIO_BROTLI_CTX *ctx;
479
480
    if (bi == NULL)
481
        return 0;
482
483
    ctx = BIO_get_data(bi);
484
    if (ctx != NULL) {
485
        BrotliDecoderDestroyInstance(ctx->decode.state);
486
        OPENSSL_free(ctx->decode.buf);
487
        BrotliEncoderDestroyInstance(ctx->encode.state);
488
        OPENSSL_free(ctx->encode.buf);
489
        OPENSSL_free(ctx);
490
    }
491
    BIO_set_data(bi, NULL);
492
    BIO_set_init(bi, 0);
493
494
    return 1;
495
}
496
497
static int bio_brotli_read(BIO *b, char *out, int outl)
498
{
499
    BIO_BROTLI_CTX *ctx;
500
    BrotliDecoderResult bret;
501
    int ret;
502
    BIO *next = BIO_next(b);
503
504
    if (out == NULL || outl <= 0) {
505
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
506
        return 0;
507
    }
508
#if INT_MAX > SIZE_MAX
509
    if ((unsigned int)outl > SIZE_MAX) {
510
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
511
        return 0;
512
    }
513
#endif
514
515
    ctx = BIO_get_data(b);
516
    BIO_clear_retry_flags(b);
517
    if (ctx->decode.buf == NULL) {
518
        ctx->decode.buf = OPENSSL_malloc(ctx->decode.bufsize);
519
        if (ctx->decode.buf == NULL) {
520
            ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
521
            return 0;
522
        }
523
        ctx->decode.next_in = ctx->decode.buf;
524
        ctx->decode.avail_in = 0;
525
    }
526
527
    /* Copy output data directly to supplied buffer */
528
    ctx->decode.next_out = (unsigned char *)out;
529
    ctx->decode.avail_out = (size_t)outl;
530
    for (;;) {
531
        /* Decompress while data available */
532
        while (ctx->decode.avail_in > 0 || BrotliDecoderHasMoreOutput(ctx->decode.state)) {
533
            bret = BrotliDecoderDecompressStream(ctx->decode.state, &ctx->decode.avail_in, (const uint8_t **)&ctx->decode.next_in,
534
                &ctx->decode.avail_out, &ctx->decode.next_out, NULL);
535
            if (bret == BROTLI_DECODER_RESULT_ERROR) {
536
                ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_DECODE_ERROR);
537
                ERR_add_error_data(1, BrotliDecoderErrorString(BrotliDecoderGetErrorCode(ctx->decode.state)));
538
                return 0;
539
            }
540
            /* If EOF or we've read everything then return */
541
            if (BrotliDecoderIsFinished(ctx->decode.state) || ctx->decode.avail_out == 0)
542
                return (int)(outl - ctx->decode.avail_out);
543
        }
544
545
        /* If EOF */
546
        if (BrotliDecoderIsFinished(ctx->decode.state))
547
            return 0;
548
549
        /*
550
         * No data in input buffer try to read some in, if an error then
551
         * return the total data read.
552
         */
553
        ret = BIO_read(next, ctx->decode.buf, (int)ctx->decode.bufsize);
554
        if (ret <= 0) {
555
            /* Total data read */
556
            int tot = outl - (int)ctx->decode.avail_out;
557
558
            BIO_copy_next_retry(b);
559
            if (ret < 0)
560
                return (tot > 0) ? tot : ret;
561
            return tot;
562
        }
563
        ctx->decode.avail_in = ret;
564
        ctx->decode.next_in = ctx->decode.buf;
565
    }
566
}
567
568
static int bio_brotli_write(BIO *b, const char *in, int inl)
569
{
570
    BIO_BROTLI_CTX *ctx;
571
    BROTLI_BOOL brret;
572
    int ret;
573
    BIO *next = BIO_next(b);
574
575
    if (in == NULL || inl <= 0) {
576
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
577
        return 0;
578
    }
579
#if INT_MAX > SIZE_MAX
580
    if ((unsigned int)inl > SIZE_MAX) {
581
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
582
        return 0;
583
    }
584
#endif
585
586
    ctx = BIO_get_data(b);
587
    if (ctx->encode.done)
588
        return 0;
589
590
    BIO_clear_retry_flags(b);
591
    if (ctx->encode.buf == NULL) {
592
        ctx->encode.buf = OPENSSL_malloc(ctx->encode.bufsize);
593
        if (ctx->encode.buf == NULL) {
594
            ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
595
            return 0;
596
        }
597
        ctx->encode.ptr = ctx->encode.buf;
598
        ctx->encode.count = 0;
599
        ctx->encode.next_out = ctx->encode.buf;
600
        ctx->encode.avail_out = ctx->encode.bufsize;
601
    }
602
    /* Obtain input data directly from supplied buffer */
603
    ctx->encode.next_in = (unsigned char *)in;
604
    ctx->encode.avail_in = (size_t)inl;
605
    for (;;) {
606
        /* If data in output buffer write it first */
607
        while (ctx->encode.count > 0) {
608
            ret = BIO_write(next, ctx->encode.ptr, (int)ctx->encode.count);
609
            if (ret <= 0) {
610
                /* Total data written */
611
                int tot = inl - (int)ctx->encode.avail_in;
612
613
                BIO_copy_next_retry(b);
614
                if (ret < 0)
615
                    return (tot > 0) ? tot : ret;
616
                return tot;
617
            }
618
            ctx->encode.ptr += ret;
619
            ctx->encode.count -= ret;
620
        }
621
622
        /* Have we consumed all supplied data? */
623
        if (ctx->encode.avail_in == 0 && !BrotliEncoderHasMoreOutput(ctx->encode.state))
624
            return inl;
625
626
        /* Compress some more */
627
628
        /* Reset buffer */
629
        ctx->encode.ptr = ctx->encode.buf;
630
        ctx->encode.next_out = ctx->encode.buf;
631
        ctx->encode.avail_out = ctx->encode.bufsize;
632
        /* Compress some more */
633
        brret = BrotliEncoderCompressStream(ctx->encode.state, BROTLI_OPERATION_FLUSH, &ctx->encode.avail_in, (const uint8_t **)&ctx->encode.next_in,
634
            &ctx->encode.avail_out, &ctx->encode.next_out, NULL);
635
        if (brret != BROTLI_TRUE) {
636
            ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_ENCODE_ERROR);
637
            ERR_add_error_data(1, "brotli encoder error");
638
            return 0;
639
        }
640
        ctx->encode.count = ctx->encode.bufsize - ctx->encode.avail_out;
641
    }
642
}
643
644
static int bio_brotli_flush(BIO *b)
645
{
646
    BIO_BROTLI_CTX *ctx;
647
    BROTLI_BOOL brret;
648
    int ret;
649
    BIO *next = BIO_next(b);
650
651
    ctx = BIO_get_data(b);
652
653
    /* If no data written or already flush show success */
654
    if (ctx->encode.buf == NULL || (ctx->encode.done && ctx->encode.count == 0))
655
        return 1;
656
657
    BIO_clear_retry_flags(b);
658
    /* No more input data */
659
    ctx->encode.next_in = NULL;
660
    ctx->encode.avail_in = 0;
661
    for (;;) {
662
        /* If data in output buffer write it first */
663
        while (ctx->encode.count > 0) {
664
            ret = BIO_write(next, ctx->encode.ptr, (int)ctx->encode.count);
665
            if (ret <= 0) {
666
                BIO_copy_next_retry(b);
667
                return ret;
668
            }
669
            ctx->encode.ptr += ret;
670
            ctx->encode.count -= ret;
671
        }
672
        if (ctx->encode.done)
673
            return 1;
674
675
        /* Compress some more */
676
677
        /* Reset buffer */
678
        ctx->encode.ptr = ctx->encode.buf;
679
        ctx->encode.next_out = ctx->encode.buf;
680
        ctx->encode.avail_out = ctx->encode.bufsize;
681
        /* Compress some more */
682
        brret = BrotliEncoderCompressStream(ctx->encode.state, BROTLI_OPERATION_FINISH, &ctx->encode.avail_in,
683
            (const uint8_t **)&ctx->encode.next_in, &ctx->encode.avail_out, &ctx->encode.next_out, NULL);
684
        if (brret != BROTLI_TRUE) {
685
            ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_ENCODE_ERROR);
686
            ERR_add_error_data(1, "brotli encoder error");
687
            return 0;
688
        }
689
        if (!BrotliEncoderHasMoreOutput(ctx->encode.state) && ctx->encode.avail_in == 0)
690
            ctx->encode.done = 1;
691
        ctx->encode.count = ctx->encode.bufsize - ctx->encode.avail_out;
692
    }
693
}
694
695
static long bio_brotli_ctrl(BIO *b, int cmd, long num, void *ptr)
696
{
697
    BIO_BROTLI_CTX *ctx;
698
    unsigned char *tmp;
699
    int ret = 0, *ip;
700
    size_t ibs, obs;
701
    BIO *next = BIO_next(b);
702
703
    if (next == NULL)
704
        return 0;
705
    ctx = BIO_get_data(b);
706
    switch (cmd) {
707
708
    case BIO_CTRL_RESET:
709
        ctx->encode.count = 0;
710
        ctx->encode.done = 0;
711
        ret = 1;
712
        break;
713
714
    case BIO_CTRL_FLUSH:
715
        ret = bio_brotli_flush(b);
716
        if (ret > 0) {
717
            ret = BIO_flush(next);
718
            BIO_copy_next_retry(b);
719
        }
720
        break;
721
722
    case BIO_C_SET_BUFF_SIZE:
723
        ibs = ctx->decode.bufsize;
724
        obs = ctx->encode.bufsize;
725
        if (ptr != NULL) {
726
            ip = ptr;
727
            if (*ip == 0)
728
                ibs = (size_t)num;
729
            else
730
                obs = (size_t)num;
731
        } else {
732
            ibs = (size_t)num;
733
            obs = ibs;
734
        }
735
736
        if (ibs > 0 && ibs != ctx->decode.bufsize) {
737
            /* Do not free/alloc, only reallocate */
738
            if (ctx->decode.buf != NULL) {
739
                tmp = OPENSSL_realloc(ctx->decode.buf, ibs);
740
                if (tmp == NULL)
741
                    return 0;
742
                ctx->decode.buf = tmp;
743
            }
744
            ctx->decode.bufsize = ibs;
745
        }
746
747
        if (obs > 0 && obs != ctx->encode.bufsize) {
748
            /* Do not free/alloc, only reallocate */
749
            if (ctx->encode.buf != NULL) {
750
                tmp = OPENSSL_realloc(ctx->encode.buf, obs);
751
                if (tmp == NULL)
752
                    return 0;
753
                ctx->encode.buf = tmp;
754
            }
755
            ctx->encode.bufsize = obs;
756
        }
757
        ret = 1;
758
        break;
759
760
    case BIO_C_DO_STATE_MACHINE:
761
        BIO_clear_retry_flags(b);
762
        ret = BIO_ctrl(next, cmd, num, ptr);
763
        BIO_copy_next_retry(b);
764
        break;
765
766
    case BIO_CTRL_WPENDING:
767
        if (BrotliEncoderHasMoreOutput(ctx->encode.state))
768
            ret = 1;
769
        else
770
            ret = BIO_ctrl(next, cmd, num, ptr);
771
        break;
772
773
    case BIO_CTRL_PENDING:
774
        if (!BrotliDecoderIsFinished(ctx->decode.state))
775
            ret = 1;
776
        else
777
            ret = BIO_ctrl(next, cmd, num, ptr);
778
        break;
779
780
    default:
781
        ret = BIO_ctrl(next, cmd, num, ptr);
782
        break;
783
    }
784
785
    return ret;
786
}
787
788
static long bio_brotli_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
789
{
790
    BIO *next = BIO_next(b);
791
    if (next == NULL)
792
        return 0;
793
    return BIO_callback_ctrl(next, cmd, fp);
794
}
795
796
#endif