1  /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  *  This is included from private-lib-core.h if LWS_WITH_HTTP_STREAM_COMPRESSION
25  */
26 
27 #if defined(LWS_WITH_MINIZ)
28 #include <miniz.h>
29 #else
30 #include <zlib.h>
31 #endif
32 #if defined(LWS_WITH_HTTP_BROTLI)
33 #include <brotli/encode.h>
34 #include <brotli/decode.h>
35 #endif
36 
37 /*
38  * struct holding union of all the available compression methods' context data,
39  * and state if it's compressing or decompressing
40  */
41 
42 typedef struct lws_compression_ctx {
43 	union {
44 
45 #if defined(LWS_WITH_HTTP_BROTLI)
46 		BrotliEncoderState *br_en;
47 		BrotliDecoderState *br_de;
48 #endif
49 		z_stream *deflate;
50 		void *generic_ctx_ptr;
51 	} u;
52 
53 	struct lws_buflist *buflist_comp;
54 
55 	unsigned int is_decompression:1;
56 	unsigned int final_on_input_side:1;
57 	unsigned int may_have_more:1;
58 	unsigned int chunking:1;
59 } lws_comp_ctx_t;
60 
61 /* generic structure defining the interface to a compression method */
62 
63 struct lws_compression_support {
64 	/** compression name as used by, eg, content-ecoding */
65 	const char *encoding_name;
66 	/** create a compression context for the compression method, or NULL */
67 	int (*init_compression)(lws_comp_ctx_t *ctx, int decomp);
68 	/** pass data into the context to be processed */
69 	int (*process)(lws_comp_ctx_t *ctx, const void *in, size_t *ilen_iused,
70 		       void *out, size_t *olen_oused);
71 	/** destroy the de/compression context */
72 	void (*destroy)(lws_comp_ctx_t *ctx);
73 };
74 
75 extern struct lws_compression_support lcs_deflate;
76 extern struct lws_compression_support lcs_brotli;
77 
78 int
79 lws_http_compression_validate(struct lws *wsi);
80 
81 int
82 lws_http_compression_transform(struct lws *wsi, unsigned char *buf,
83 			       size_t len, enum lws_write_protocol *wp,
84 			       unsigned char **outbuf, size_t *olen_oused);
85 
86 void
87 lws_http_compression_destroy(struct lws *wsi);
88