1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
/*
* Copyright (C) Andrew Clayton
* Copyright (C) F5, Inc.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <brotli/encode.h>
#include "nxt_http_compression.h"
static int
nxt_brotli_init(nxt_http_comp_compressor_ctx_t *ctx)
{
BrotliEncoderState **brotli = &ctx->brotli_ctx;
*brotli = BrotliEncoderCreateInstance(NULL, NULL, NULL);
if (*brotli == NULL) {
return -1;
}
BrotliEncoderSetParameter(*brotli, BROTLI_PARAM_QUALITY, ctx->level);
return 0;
}
static size_t
nxt_brotli_bound(const nxt_http_comp_compressor_ctx_t *ctx, size_t in_len)
{
return BrotliEncoderMaxCompressedSize(in_len);
}
static ssize_t
nxt_brotli_compress(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf,
size_t in_len, uint8_t *out_buf, size_t out_len, bool last)
{
bool ok;
size_t out_bytes = out_len;
BrotliEncoderState *brotli = ctx->brotli_ctx;
ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_PROCESS,
&in_len, &in_buf, &out_bytes, &out_buf,
NULL);
if (!ok) {
return -1;
}
ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FLUSH,
&in_len, &in_buf, &out_bytes, &out_buf,
NULL);
if (!ok) {
return -1;
}
if (last) {
ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FINISH,
&in_len, &in_buf, &out_bytes,
&out_buf, NULL);
if (!ok) {
return -1;
}
BrotliEncoderDestroyInstance(brotli);
}
return out_len - out_bytes;
}
const nxt_http_comp_operations_t nxt_http_comp_brotli_ops = {
.init = nxt_brotli_init,
.bound = nxt_brotli_bound,
.deflate = nxt_brotli_compress,
};
|