2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 - 2018 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "core/private.h"
26 lcs_init_compression_brotli(lws_comp_ctx_t *ctx, int decomp)
28 ctx->is_decompression = decomp;
31 ctx->u.br_en = BrotliEncoderCreateInstance(NULL, NULL, NULL);
33 BrotliEncoderSetParameter(ctx->u.br_en,
34 BROTLI_PARAM_MODE, BROTLI_MODE_TEXT);
35 BrotliEncoderSetParameter(ctx->u.br_en,
36 BROTLI_PARAM_QUALITY, BROTLI_MIN_QUALITY);
40 ctx->u.br_de = BrotliDecoderCreateInstance(NULL, NULL, NULL);
46 lcs_process_brotli(lws_comp_ctx_t *ctx, const void *in, size_t *ilen_iused,
47 void *out, size_t *olen_oused)
49 size_t a_in, a_out, t_out;
60 if (!ctx->is_decompression) {
62 if (!a_in && !BrotliEncoderHasMoreOutput(ctx->u.br_en)) {
68 n = BROTLI_OPERATION_PROCESS;
69 if (!ctx->buflist_comp && ctx->final_on_input_side)
70 n = BROTLI_OPERATION_FINISH;
72 if (BrotliEncoderCompressStream(ctx->u.br_en, n, &a_in, &n_in,
73 &a_out, &n_out, &t_out) ==
75 lwsl_err("brotli encode failed\n");
80 ctx->may_have_more = !a_out;
83 n = BrotliDecoderDecompressStream(ctx->u.br_de, &a_in, &n_in,
84 &a_out, &n_out, &t_out);
87 case BROTLI_DECODER_RESULT_ERROR:
88 lwsl_err("brotli decoder error\n");
97 if (!ctx->is_decompression)
98 return BrotliEncoderIsFinished(ctx->u.br_en);
100 return BrotliDecoderIsFinished(ctx->u.br_de);
104 lcs_destroy_brotli(lws_comp_ctx_t *ctx)
109 if (!(*ctx).is_decompression)
110 BrotliEncoderDestroyInstance((*ctx).u.br_en);
112 BrotliDecoderDestroyInstance((*ctx).u.br_de);
114 (*ctx).u.generic_ctx_ptr = NULL;
117 struct lws_compression_support lcs_brotli = {
118 /* .encoding_name */ "br",
119 /* .init_compression */ lcs_init_compression_brotli,
120 /* .process */ lcs_process_brotli,
121 /* .destroy */ lcs_destroy_brotli,