Coverage Report

Created: 2026-09-17 06:47

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-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 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
    ERR_set_mark();
293
    brotli_encode_dso = DSO_load(NULL, LIBBROTLIENC, NULL, 0);
294
    if (brotli_encode_dso != NULL) {
295
        p_encode_init = (encode_init_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderCreateInstance");
296
        p_encode_stream = (encode_stream_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderCompressStream");
297
        p_encode_has_more = (encode_has_more_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderHasMoreOutput");
298
        p_encode_end = (encode_end_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderDestroyInstance");
299
        p_encode_oneshot = (encode_oneshot_ft)DSO_bind_func(brotli_encode_dso, "BrotliEncoderCompress");
300
    }
301
302
    brotli_decode_dso = DSO_load(NULL, LIBBROTLIDEC, NULL, 0);
303
    if (brotli_decode_dso != NULL) {
304
        p_decode_init = (decode_init_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderCreateInstance");
305
        p_decode_stream = (decode_stream_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderDecompressStream");
306
        p_decode_has_more = (decode_has_more_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderHasMoreOutput");
307
        p_decode_end = (decode_end_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderDestroyInstance");
308
        p_decode_error = (decode_error_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderGetErrorCode");
309
        p_decode_error_string = (decode_error_string_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderErrorString");
310
        p_decode_is_finished = (decode_is_finished_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderIsFinished");
311
        p_decode_oneshot = (decode_oneshot_ft)DSO_bind_func(brotli_decode_dso, "BrotliDecoderDecompress");
312
    }
313
314
    if (p_encode_init == NULL || p_encode_stream == NULL || p_encode_has_more == NULL
315
        || p_encode_end == NULL || p_encode_oneshot == NULL || p_decode_init == NULL
316
        || p_decode_stream == NULL || p_decode_has_more == NULL || p_decode_end == NULL
317
        || p_decode_error == NULL || p_decode_error_string == NULL || p_decode_is_finished == NULL
318
        || p_decode_oneshot == NULL) {
319
        ERR_clear_last_mark();
320
        ossl_comp_brotli_cleanup();
321
        return 0;
322
    }
323
    /* Do not leave errors behind on success. */
324
    ERR_pop_to_mark();
325
#endif
326
    return 1;
327
}
328
#endif /* ifndef BROTLI / else */
329
330
COMP_METHOD *COMP_brotli(void)
331
0
{
332
0
    COMP_METHOD *meth = NULL;
333
334
#ifndef OPENSSL_NO_BROTLI
335
    if (RUN_ONCE(&brotli_once, ossl_comp_brotli_init))
336
        meth = &brotli_stateful_method;
337
#endif
338
0
    return meth;
339
0
}
340
341
COMP_METHOD *COMP_brotli_oneshot(void)
342
0
{
343
0
    COMP_METHOD *meth = NULL;
344
345
#ifndef OPENSSL_NO_BROTLI
346
    if (RUN_ONCE(&brotli_once, ossl_comp_brotli_init))
347
        meth = &brotli_oneshot_method;
348
#endif
349
0
    return meth;
350
0
}
351
352
/* Also called from OPENSSL_cleanup() */
353
void ossl_comp_brotli_cleanup(void)
354
0
{
355
#ifdef BROTLI_SHARED
356
    DSO_free(brotli_encode_dso);
357
    brotli_encode_dso = NULL;
358
    DSO_free(brotli_decode_dso);
359
    brotli_decode_dso = NULL;
360
    p_encode_init = NULL;
361
    p_encode_stream = NULL;
362
    p_encode_has_more = NULL;
363
    p_encode_end = NULL;
364
    p_encode_oneshot = NULL;
365
    p_decode_init = NULL;
366
    p_decode_stream = NULL;
367
    p_decode_has_more = NULL;
368
    p_decode_end = NULL;
369
    p_decode_error = NULL;
370
    p_decode_error_string = NULL;
371
    p_decode_is_finished = NULL;
372
    p_decode_oneshot = NULL;
373
#endif
374
0
}
375
376
#ifndef OPENSSL_NO_BROTLI
377
378
/* Brotli-based compression/decompression filter BIO */
379
380
typedef struct {
381
    struct { /* input structure */
382
        size_t avail_in;
383
        unsigned char *next_in;
384
        size_t avail_out;
385
        unsigned char *next_out;
386
        unsigned char *buf;
387
        size_t bufsize;
388
        BrotliDecoderState *state;
389
    } decode;
390
    struct { /* output structure */
391
        size_t avail_in;
392
        unsigned char *next_in;
393
        size_t avail_out;
394
        unsigned char *next_out;
395
        unsigned char *buf;
396
        size_t bufsize;
397
        BrotliEncoderState *state;
398
        int mode; /* Encoder mode to use */
399
        int done;
400
        unsigned char *ptr;
401
        size_t count;
402
    } encode;
403
} BIO_BROTLI_CTX;
404
405
#define BROTLI_DEFAULT_BUFSIZE 1024
406
407
static int bio_brotli_new(BIO *bi);
408
static int bio_brotli_free(BIO *bi);
409
static int bio_brotli_read(BIO *b, char *out, int outl);
410
static int bio_brotli_write(BIO *b, const char *in, int inl);
411
static long bio_brotli_ctrl(BIO *b, int cmd, long num, void *ptr);
412
static long bio_brotli_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
413
414
static const BIO_METHOD bio_meth_brotli = {
415
    BIO_TYPE_COMP,
416
    "brotli",
417
    /* TODO: Convert to new style write function */
418
    bwrite_conv,
419
    bio_brotli_write,
420
    /* TODO: Convert to new style read function */
421
    bread_conv,
422
    bio_brotli_read,
423
    NULL, /* bio_brotli_puts, */
424
    NULL, /* bio_brotli_gets, */
425
    bio_brotli_ctrl,
426
    bio_brotli_new,
427
    bio_brotli_free,
428
    bio_brotli_callback_ctrl
429
};
430
#endif
431
432
const BIO_METHOD *BIO_f_brotli(void)
433
0
{
434
#ifndef OPENSSL_NO_BROTLI
435
    if (RUN_ONCE(&brotli_once, ossl_comp_brotli_init))
436
        return &bio_meth_brotli;
437
#endif
438
    return NULL;
439
0
}
440
441
#ifndef OPENSSL_NO_BROTLI
442
443
static int bio_brotli_new(BIO *bi)
444
{
445
    BIO_BROTLI_CTX *ctx;
446
447
#ifdef BROTLI_SHARED
448
    if (!RUN_ONCE(&brotli_once, ossl_comp_brotli_init)) {
449
        ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_NOT_SUPPORTED);
450
        return 0;
451
    }
452
#endif
453
    ctx = OPENSSL_zalloc(sizeof(*ctx));
454
    if (ctx == NULL) {
455
        ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
456
        return 0;
457
    }
458
    ctx->decode.bufsize = BROTLI_DEFAULT_BUFSIZE;
459
    ctx->decode.state = BrotliDecoderCreateInstance(brotli_alloc, brotli_free, NULL);
460
    if (ctx->decode.state == NULL)
461
        goto err;
462
    ctx->encode.bufsize = BROTLI_DEFAULT_BUFSIZE;
463
    ctx->encode.state = BrotliEncoderCreateInstance(brotli_alloc, brotli_free, NULL);
464
    if (ctx->encode.state == NULL)
465
        goto err;
466
    ctx->encode.mode = BROTLI_DEFAULT_MODE;
467
    BIO_set_init(bi, 1);
468
    BIO_set_data(bi, ctx);
469
470
    return 1;
471
472
err:
473
    ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
474
    BrotliDecoderDestroyInstance(ctx->decode.state);
475
    BrotliEncoderDestroyInstance(ctx->encode.state);
476
    OPENSSL_free(ctx);
477
    return 0;
478
}
479
480
static int bio_brotli_free(BIO *bi)
481
{
482
    BIO_BROTLI_CTX *ctx;
483
484
    if (bi == NULL)
485
        return 0;
486
487
    ctx = BIO_get_data(bi);
488
    if (ctx != NULL) {
489
        BrotliDecoderDestroyInstance(ctx->decode.state);
490
        OPENSSL_free(ctx->decode.buf);
491
        BrotliEncoderDestroyInstance(ctx->encode.state);
492
        OPENSSL_free(ctx->encode.buf);
493
        OPENSSL_free(ctx);
494
    }
495
    BIO_set_data(bi, NULL);
496
    BIO_set_init(bi, 0);
497
498
    return 1;
499
}
500
501
static int bio_brotli_read(BIO *b, char *out, int outl)
502
{
503
    BIO_BROTLI_CTX *ctx;
504
    BrotliDecoderResult bret;
505
    int ret;
506
    BIO *next = BIO_next(b);
507
508
    if (out == NULL || outl <= 0) {
509
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
510
        return 0;
511
    }
512
#if INT_MAX > SIZE_MAX
513
    if ((unsigned int)outl > SIZE_MAX) {
514
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
515
        return 0;
516
    }
517
#endif
518
519
    ctx = BIO_get_data(b);
520
    BIO_clear_retry_flags(b);
521
    if (ctx->decode.buf == NULL) {
522
        ctx->decode.buf = OPENSSL_malloc(ctx->decode.bufsize);
523
        if (ctx->decode.buf == NULL) {
524
            ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
525
            return 0;
526
        }
527
        ctx->decode.next_in = ctx->decode.buf;
528
        ctx->decode.avail_in = 0;
529
    }
530
531
    /* Copy output data directly to supplied buffer */
532
    ctx->decode.next_out = (unsigned char *)out;
533
    ctx->decode.avail_out = (size_t)outl;
534
    for (;;) {
535
        /* Decompress while data available */
536
        while (ctx->decode.avail_in > 0 || BrotliDecoderHasMoreOutput(ctx->decode.state)) {
537
            bret = BrotliDecoderDecompressStream(ctx->decode.state, &ctx->decode.avail_in, (const uint8_t **)&ctx->decode.next_in,
538
                &ctx->decode.avail_out, &ctx->decode.next_out, NULL);
539
            if (bret == BROTLI_DECODER_RESULT_ERROR) {
540
                ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_DECODE_ERROR);
541
                ERR_add_error_data(1, BrotliDecoderErrorString(BrotliDecoderGetErrorCode(ctx->decode.state)));
542
                return 0;
543
            }
544
            /* If EOF or we've read everything then return */
545
            if (BrotliDecoderIsFinished(ctx->decode.state) || ctx->decode.avail_out == 0)
546
                return (int)(outl - ctx->decode.avail_out);
547
        }
548
549
        /* If EOF */
550
        if (BrotliDecoderIsFinished(ctx->decode.state))
551
            return 0;
552
553
        /*
554
         * No data in input buffer try to read some in, if an error then
555
         * return the total data read.
556
         */
557
        ret = BIO_read(next, ctx->decode.buf, (int)ctx->decode.bufsize);
558
        if (ret <= 0) {
559
            /* Total data read */
560
            int tot = outl - (int)ctx->decode.avail_out;
561
562
            BIO_copy_next_retry(b);
563
            if (ret < 0)
564
                return (tot > 0) ? tot : ret;
565
            return tot;
566
        }
567
        ctx->decode.avail_in = ret;
568
        ctx->decode.next_in = ctx->decode.buf;
569
    }
570
}
571
572
static int bio_brotli_write(BIO *b, const char *in, int inl)
573
{
574
    BIO_BROTLI_CTX *ctx;
575
    BROTLI_BOOL brret;
576
    int ret;
577
    BIO *next = BIO_next(b);
578
579
    if (in == NULL || inl <= 0) {
580
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
581
        return 0;
582
    }
583
#if INT_MAX > SIZE_MAX
584
    if ((unsigned int)inl > SIZE_MAX) {
585
        ERR_raise(ERR_LIB_COMP, ERR_R_PASSED_INVALID_ARGUMENT);
586
        return 0;
587
    }
588
#endif
589
590
    ctx = BIO_get_data(b);
591
    if (ctx->encode.done)
592
        return 0;
593
594
    BIO_clear_retry_flags(b);
595
    if (ctx->encode.buf == NULL) {
596
        ctx->encode.buf = OPENSSL_malloc(ctx->encode.bufsize);
597
        if (ctx->encode.buf == NULL) {
598
            ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
599
            return 0;
600
        }
601
        ctx->encode.ptr = ctx->encode.buf;
602
        ctx->encode.count = 0;
603
        ctx->encode.next_out = ctx->encode.buf;
604
        ctx->encode.avail_out = ctx->encode.bufsize;
605
    }
606
    /* Obtain input data directly from supplied buffer */
607
    ctx->encode.next_in = (unsigned char *)in;
608
    ctx->encode.avail_in = (size_t)inl;
609
    for (;;) {
610
        /* If data in output buffer write it first */
611
        while (ctx->encode.count > 0) {
612
            ret = BIO_write(next, ctx->encode.ptr, (int)ctx->encode.count);
613
            if (ret <= 0) {
614
                /* Total data written */
615
                int tot = inl - (int)ctx->encode.avail_in;
616
617
                BIO_copy_next_retry(b);
618
                if (ret < 0)
619
                    return (tot > 0) ? tot : ret;
620
                return tot;
621
            }
622
            ctx->encode.ptr += ret;
623
            ctx->encode.count -= ret;
624
        }
625
626
        /* Have we consumed all supplied data? */
627
        if (ctx->encode.avail_in == 0 && !BrotliEncoderHasMoreOutput(ctx->encode.state))
628
            return inl;
629
630
        /* Compress some more */
631
632
        /* Reset buffer */
633
        ctx->encode.ptr = ctx->encode.buf;
634
        ctx->encode.next_out = ctx->encode.buf;
635
        ctx->encode.avail_out = ctx->encode.bufsize;
636
        /* Compress some more */
637
        brret = BrotliEncoderCompressStream(ctx->encode.state, BROTLI_OPERATION_FLUSH, &ctx->encode.avail_in, (const uint8_t **)&ctx->encode.next_in,
638
            &ctx->encode.avail_out, &ctx->encode.next_out, NULL);
639
        if (brret != BROTLI_TRUE) {
640
            ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_ENCODE_ERROR);
641
            ERR_add_error_data(1, "brotli encoder error");
642
            return 0;
643
        }
644
        ctx->encode.count = ctx->encode.bufsize - ctx->encode.avail_out;
645
    }
646
}
647
648
static int bio_brotli_flush(BIO *b)
649
{
650
    BIO_BROTLI_CTX *ctx;
651
    BROTLI_BOOL brret;
652
    int ret;
653
    BIO *next = BIO_next(b);
654
655
    ctx = BIO_get_data(b);
656
657
    /* If no data written or already flush show success */
658
    if (ctx->encode.buf == NULL || (ctx->encode.done && ctx->encode.count == 0))
659
        return 1;
660
661
    BIO_clear_retry_flags(b);
662
    /* No more input data */
663
    ctx->encode.next_in = NULL;
664
    ctx->encode.avail_in = 0;
665
    for (;;) {
666
        /* If data in output buffer write it first */
667
        while (ctx->encode.count > 0) {
668
            ret = BIO_write(next, ctx->encode.ptr, (int)ctx->encode.count);
669
            if (ret <= 0) {
670
                BIO_copy_next_retry(b);
671
                return ret;
672
            }
673
            ctx->encode.ptr += ret;
674
            ctx->encode.count -= ret;
675
        }
676
        if (ctx->encode.done)
677
            return 1;
678
679
        /* Compress some more */
680
681
        /* Reset buffer */
682
        ctx->encode.ptr = ctx->encode.buf;
683
        ctx->encode.next_out = ctx->encode.buf;
684
        ctx->encode.avail_out = ctx->encode.bufsize;
685
        /* Compress some more */
686
        brret = BrotliEncoderCompressStream(ctx->encode.state, BROTLI_OPERATION_FINISH, &ctx->encode.avail_in,
687
            (const uint8_t **)&ctx->encode.next_in, &ctx->encode.avail_out, &ctx->encode.next_out, NULL);
688
        if (brret != BROTLI_TRUE) {
689
            ERR_raise(ERR_LIB_COMP, COMP_R_BROTLI_ENCODE_ERROR);
690
            ERR_add_error_data(1, "brotli encoder error");
691
            return 0;
692
        }
693
        if (!BrotliEncoderHasMoreOutput(ctx->encode.state) && ctx->encode.avail_in == 0)
694
            ctx->encode.done = 1;
695
        ctx->encode.count = ctx->encode.bufsize - ctx->encode.avail_out;
696
    }
697
}
698
699
static long bio_brotli_ctrl(BIO *b, int cmd, long num, void *ptr)
700
{
701
    BIO_BROTLI_CTX *ctx;
702
    unsigned char *tmp;
703
    int ret = 0, *ip;
704
    size_t ibs, obs;
705
    BIO *next = BIO_next(b);
706
707
    if (next == NULL)
708
        return 0;
709
    ctx = BIO_get_data(b);
710
    switch (cmd) {
711
712
    case BIO_CTRL_RESET:
713
        ctx->encode.count = 0;
714
        ctx->encode.done = 0;
715
        ret = 1;
716
        break;
717
718
    case BIO_CTRL_FLUSH:
719
        ret = bio_brotli_flush(b);
720
        if (ret > 0) {
721
            ret = BIO_flush(next);
722
            BIO_copy_next_retry(b);
723
        }
724
        break;
725
726
    case BIO_C_SET_BUFF_SIZE:
727
        ibs = ctx->decode.bufsize;
728
        obs = ctx->encode.bufsize;
729
        if (ptr != NULL) {
730
            ip = ptr;
731
            if (*ip == 0)
732
                ibs = (size_t)num;
733
            else
734
                obs = (size_t)num;
735
        } else {
736
            ibs = (size_t)num;
737
            obs = ibs;
738
        }
739
740
        if (ibs > 0 && ibs != ctx->decode.bufsize) {
741
            /* Do not free/alloc, only reallocate */
742
            if (ctx->decode.buf != NULL) {
743
                tmp = OPENSSL_realloc(ctx->decode.buf, ibs);
744
                if (tmp == NULL)
745
                    return 0;
746
                ctx->decode.buf = tmp;
747
            }
748
            ctx->decode.bufsize = ibs;
749
        }
750
751
        if (obs > 0 && obs != ctx->encode.bufsize) {
752
            /* Do not free/alloc, only reallocate */
753
            if (ctx->encode.buf != NULL) {
754
                tmp = OPENSSL_realloc(ctx->encode.buf, obs);
755
                if (tmp == NULL)
756
                    return 0;
757
                ctx->encode.buf = tmp;
758
            }
759
            ctx->encode.bufsize = obs;
760
        }
761
        ret = 1;
762
        break;
763
764
    case BIO_C_DO_STATE_MACHINE:
765
        BIO_clear_retry_flags(b);
766
        ret = BIO_ctrl(next, cmd, num, ptr);
767
        BIO_copy_next_retry(b);
768
        break;
769
770
    case BIO_CTRL_WPENDING:
771
        if (BrotliEncoderHasMoreOutput(ctx->encode.state))
772
            ret = 1;
773
        else
774
            ret = BIO_ctrl(next, cmd, num, ptr);
775
        break;
776
777
    case BIO_CTRL_PENDING:
778
        if (!BrotliDecoderIsFinished(ctx->decode.state))
779
            ret = 1;
780
        else
781
            ret = BIO_ctrl(next, cmd, num, ptr);
782
        break;
783
784
    default:
785
        ret = BIO_ctrl(next, cmd, num, ptr);
786
        break;
787
    }
788
789
    return ret;
790
}
791
792
static long bio_brotli_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
793
{
794
    BIO *next = BIO_next(b);
795
    if (next == NULL)
796
        return 0;
797
    return BIO_callback_ctrl(next, cmd, fp);
798
}
799
800
#endif