Coverage Report

Created: 2024-02-25 06:15

/src/h2o/lib/handler/compress/brotli.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2017 Fastly, Kazuho Oku
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
#include <assert.h>
23
#include <stdlib.h>
24
#include "h2o.h"
25
#include "brotli/encode.h"
26
27
struct st_brotli_context_t {
28
    h2o_compress_context_t super;
29
    BrotliEncoderState *state;
30
    H2O_VECTOR(h2o_sendvec_t) bufs;
31
    size_t buf_capacity;
32
};
33
34
static void expand_buf(struct st_brotli_context_t *self)
35
0
{
36
0
    h2o_vector_reserve(NULL, &self->bufs, self->bufs.size + 1);
37
0
    h2o_sendvec_init_raw(self->bufs.entries + self->bufs.size++, h2o_mem_alloc(self->buf_capacity), 0);
38
0
}
39
40
static void shrink_buf(struct st_brotli_context_t *self, size_t new_size)
41
0
{
42
0
    while (new_size < self->bufs.size)
43
0
        free(self->bufs.entries[--self->bufs.size].raw);
44
0
}
45
46
static void compress_core(struct st_brotli_context_t *self, BrotliEncoderOperation op, const uint8_t **src, size_t *srclen)
47
0
{
48
0
    size_t bufindex = self->bufs.size - 1;
49
50
0
    if (self->bufs.entries[bufindex].len == self->buf_capacity) {
51
0
        expand_buf(self);
52
0
        ++bufindex;
53
0
    }
54
0
    uint8_t *dst = (uint8_t *)self->bufs.entries[bufindex].raw + self->bufs.entries[bufindex].len;
55
0
    size_t dstlen = self->buf_capacity - self->bufs.entries[bufindex].len;
56
57
0
    if (!BrotliEncoderCompressStream(self->state, op, srclen, src, &dstlen, &dst, NULL))
58
0
        h2o_fatal("BrotliEncoderCompressStream");
59
60
0
    self->bufs.entries[bufindex].len = self->buf_capacity - dstlen;
61
0
}
62
63
static h2o_send_state_t compress_(h2o_compress_context_t *_self, h2o_sendvec_t *inbufs, size_t inbufcnt, h2o_send_state_t state,
64
                                  h2o_sendvec_t **outbufs, size_t *outbufcnt)
65
0
{
66
0
    struct st_brotli_context_t *self = (void *)_self;
67
0
    BrotliEncoderOperation final_op = h2o_send_state_is_in_progress(state) ? BROTLI_OPERATION_FLUSH : BROTLI_OPERATION_FINISH;
68
0
    const uint8_t *src;
69
0
    size_t i, srclen;
70
71
0
    shrink_buf(self, 1);
72
0
    self->bufs.entries[0].len = 0;
73
74
    /* encode chunks and flush */
75
0
    if (inbufcnt != 0) {
76
0
        for (i = 0; i < inbufcnt; ++i) {
77
0
            assert(inbufs[i].callbacks->read_ == h2o_sendvec_read_raw);
78
0
            src = (void *)inbufs[i].raw;
79
0
            srclen = inbufs[i].len;
80
0
            BrotliEncoderOperation op = i + 1 == inbufcnt ? final_op : BROTLI_OPERATION_PROCESS;
81
0
            while (srclen != 0)
82
0
                compress_core(self, op, &src, &srclen);
83
0
        }
84
0
    } else {
85
0
        src = NULL;
86
0
        srclen = 0;
87
0
        compress_core(self, final_op, &src, &srclen);
88
0
    }
89
90
    /* emit pending output, if any */
91
0
    while (BrotliEncoderHasMoreOutput(self->state)) {
92
0
        src = NULL;
93
0
        srclen = 0;
94
0
        compress_core(self, final_op, &src, &srclen);
95
0
    }
96
97
0
    *outbufs = self->bufs.entries;
98
0
    *outbufcnt = self->bufs.size - (self->bufs.entries[self->bufs.size - 1].len == 0);
99
100
0
    return state;
101
0
}
102
103
static void on_dispose(void *_self)
104
0
{
105
0
    struct st_brotli_context_t *self = _self;
106
107
0
    BrotliEncoderDestroyInstance(self->state);
108
0
    shrink_buf(self, 0);
109
0
    free(self->bufs.entries);
110
0
    free(self->super.push_buf);
111
0
}
112
113
h2o_compress_context_t *h2o_compress_brotli_open(h2o_mem_pool_t *pool, int quality, size_t estimated_content_length,
114
                                                 size_t preferred_chunk_size)
115
0
{
116
0
    struct st_brotli_context_t *self = h2o_mem_alloc_shared(pool, sizeof(struct st_brotli_context_t), on_dispose);
117
118
0
    self->super.name = h2o_iovec_init(H2O_STRLIT("br"));
119
0
    self->super.do_transform = compress_;
120
0
    self->super.push_buf = NULL;
121
0
    self->state = BrotliEncoderCreateInstance(NULL, NULL, NULL);
122
0
    memset(&self->bufs, 0, sizeof(self->bufs));
123
0
    self->buf_capacity = preferred_chunk_size;
124
0
    if (self->buf_capacity > estimated_content_length)
125
0
        self->buf_capacity = estimated_content_length;
126
0
    if (self->buf_capacity > 65536)
127
0
        self->buf_capacity = 65536;
128
0
    if (self->buf_capacity < 1024)
129
0
        self->buf_capacity = 1024;
130
0
    expand_buf(self);
131
132
0
    BrotliEncoderSetParameter(self->state, BROTLI_PARAM_QUALITY, quality);
133
0
    if (estimated_content_length < (1 << BROTLI_DEFAULT_WINDOW) / 2) {
134
0
        unsigned bits =
135
0
            estimated_content_length > 1 ? sizeof(unsigned long long) * 8 - __builtin_clzll(estimated_content_length - 1) : 1;
136
0
        if (bits < 5) {
137
0
            bits = 5;
138
0
        }
139
0
        BrotliEncoderSetParameter(self->state, BROTLI_PARAM_LGWIN, bits);
140
0
    }
141
142
0
    return &self->super;
143
0
}