/src/curl/lib/content_encoding.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include "urldata.h" |
28 | | #include <curl/curl.h> |
29 | | #include <stddef.h> |
30 | | |
31 | | #ifdef HAVE_LIBZ |
32 | | #include <zlib.h> |
33 | | #endif |
34 | | |
35 | | #ifdef HAVE_BROTLI |
36 | | #if defined(__GNUC__) |
37 | | /* Ignore -Wvla warnings in brotli headers */ |
38 | | #pragma GCC diagnostic push |
39 | | #pragma GCC diagnostic ignored "-Wvla" |
40 | | #endif |
41 | | #include <brotli/decode.h> |
42 | | #if defined(__GNUC__) |
43 | | #pragma GCC diagnostic pop |
44 | | #endif |
45 | | #endif |
46 | | |
47 | | #ifdef HAVE_ZSTD |
48 | | #include <zstd.h> |
49 | | #endif |
50 | | |
51 | | #include "sendf.h" |
52 | | #include "http.h" |
53 | | #include "content_encoding.h" |
54 | | #include "strdup.h" |
55 | | #include "strcase.h" |
56 | | |
57 | | /* The last 3 #include files should be in this order */ |
58 | | #include "curl_printf.h" |
59 | | #include "curl_memory.h" |
60 | | #include "memdebug.h" |
61 | | |
62 | 0 | #define CONTENT_ENCODING_DEFAULT "identity" |
63 | | |
64 | | #ifndef CURL_DISABLE_HTTP |
65 | | |
66 | | /* allow no more than 5 "chained" compression steps */ |
67 | 0 | #define MAX_ENCODE_STACK 5 |
68 | | |
69 | 0 | #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */ |
70 | | |
71 | | |
72 | | #ifdef HAVE_LIBZ |
73 | | |
74 | | /* Comment this out if zlib is always going to be at least ver. 1.2.0.4 |
75 | | (doing so will reduce code size slightly). */ |
76 | | #define OLD_ZLIB_SUPPORT 1 |
77 | | |
78 | 0 | #define GZIP_MAGIC_0 0x1f |
79 | 0 | #define GZIP_MAGIC_1 0x8b |
80 | | |
81 | | /* gzip flag byte */ |
82 | | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ |
83 | 0 | #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */ |
84 | 0 | #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ |
85 | 0 | #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ |
86 | 0 | #define COMMENT 0x10 /* bit 4 set: file comment present */ |
87 | 0 | #define RESERVED 0xE0 /* bits 5..7: reserved */ |
88 | | |
89 | | typedef enum { |
90 | | ZLIB_UNINIT, /* uninitialized */ |
91 | | ZLIB_INIT, /* initialized */ |
92 | | ZLIB_INFLATING, /* inflating started. */ |
93 | | ZLIB_EXTERNAL_TRAILER, /* reading external trailer */ |
94 | | ZLIB_GZIP_HEADER, /* reading gzip header */ |
95 | | ZLIB_GZIP_INFLATING, /* inflating gzip stream */ |
96 | | ZLIB_INIT_GZIP /* initialized in transparent gzip mode */ |
97 | | } zlibInitState; |
98 | | |
99 | | /* Deflate and gzip writer. */ |
100 | | struct zlib_writer { |
101 | | struct Curl_cwriter super; |
102 | | zlibInitState zlib_init; /* zlib init state */ |
103 | | uInt trailerlen; /* Remaining trailer byte count. */ |
104 | | z_stream z; /* State structure for zlib. */ |
105 | | }; |
106 | | |
107 | | |
108 | | static voidpf |
109 | | zalloc_cb(voidpf opaque, unsigned int items, unsigned int size) |
110 | 0 | { |
111 | 0 | (void) opaque; |
112 | | /* not a typo, keep it calloc() */ |
113 | 0 | return (voidpf) calloc(items, size); |
114 | 0 | } |
115 | | |
116 | | static void |
117 | | zfree_cb(voidpf opaque, voidpf ptr) |
118 | 0 | { |
119 | 0 | (void) opaque; |
120 | 0 | free(ptr); |
121 | 0 | } |
122 | | |
123 | | static CURLcode |
124 | | process_zlib_error(struct Curl_easy *data, z_stream *z) |
125 | 0 | { |
126 | 0 | if(z->msg) |
127 | 0 | failf(data, "Error while processing content unencoding: %s", |
128 | 0 | z->msg); |
129 | 0 | else |
130 | 0 | failf(data, "Error while processing content unencoding: " |
131 | 0 | "Unknown failure within decompression software."); |
132 | |
|
133 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
134 | 0 | } |
135 | | |
136 | | static CURLcode |
137 | | exit_zlib(struct Curl_easy *data, |
138 | | z_stream *z, zlibInitState *zlib_init, CURLcode result) |
139 | 0 | { |
140 | 0 | if(*zlib_init == ZLIB_GZIP_HEADER) |
141 | 0 | Curl_safefree(z->next_in); |
142 | |
|
143 | 0 | if(*zlib_init != ZLIB_UNINIT) { |
144 | 0 | if(inflateEnd(z) != Z_OK && result == CURLE_OK) |
145 | 0 | result = process_zlib_error(data, z); |
146 | 0 | *zlib_init = ZLIB_UNINIT; |
147 | 0 | } |
148 | |
|
149 | 0 | return result; |
150 | 0 | } |
151 | | |
152 | | static CURLcode process_trailer(struct Curl_easy *data, |
153 | | struct zlib_writer *zp) |
154 | 0 | { |
155 | 0 | z_stream *z = &zp->z; |
156 | 0 | CURLcode result = CURLE_OK; |
157 | 0 | uInt len = z->avail_in < zp->trailerlen? z->avail_in: zp->trailerlen; |
158 | | |
159 | | /* Consume expected trailer bytes. Terminate stream if exhausted. |
160 | | Issue an error if unexpected bytes follow. */ |
161 | |
|
162 | 0 | zp->trailerlen -= len; |
163 | 0 | z->avail_in -= len; |
164 | 0 | z->next_in += len; |
165 | 0 | if(z->avail_in) |
166 | 0 | result = CURLE_WRITE_ERROR; |
167 | 0 | if(result || !zp->trailerlen) |
168 | 0 | result = exit_zlib(data, z, &zp->zlib_init, result); |
169 | 0 | else { |
170 | | /* Only occurs for gzip with zlib < 1.2.0.4 or raw deflate. */ |
171 | 0 | zp->zlib_init = ZLIB_EXTERNAL_TRAILER; |
172 | 0 | } |
173 | 0 | return result; |
174 | 0 | } |
175 | | |
176 | | static CURLcode inflate_stream(struct Curl_easy *data, |
177 | | struct Curl_cwriter *writer, int type, |
178 | | zlibInitState started) |
179 | 0 | { |
180 | 0 | struct zlib_writer *zp = (struct zlib_writer *) writer; |
181 | 0 | z_stream *z = &zp->z; /* zlib state structure */ |
182 | 0 | uInt nread = z->avail_in; |
183 | 0 | Bytef *orig_in = z->next_in; |
184 | 0 | bool done = FALSE; |
185 | 0 | CURLcode result = CURLE_OK; /* Curl_client_write status */ |
186 | 0 | char *decomp; /* Put the decompressed data here. */ |
187 | | |
188 | | /* Check state. */ |
189 | 0 | if(zp->zlib_init != ZLIB_INIT && |
190 | 0 | zp->zlib_init != ZLIB_INFLATING && |
191 | 0 | zp->zlib_init != ZLIB_INIT_GZIP && |
192 | 0 | zp->zlib_init != ZLIB_GZIP_INFLATING) |
193 | 0 | return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR); |
194 | | |
195 | | /* Dynamically allocate a buffer for decompression because it's uncommonly |
196 | | large to hold on the stack */ |
197 | 0 | decomp = malloc(DSIZ); |
198 | 0 | if(!decomp) |
199 | 0 | return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY); |
200 | | |
201 | | /* because the buffer size is fixed, iteratively decompress and transfer to |
202 | | the client via next_write function. */ |
203 | 0 | while(!done) { |
204 | 0 | int status; /* zlib status */ |
205 | 0 | done = TRUE; |
206 | | |
207 | | /* (re)set buffer for decompressed output for every iteration */ |
208 | 0 | z->next_out = (Bytef *) decomp; |
209 | 0 | z->avail_out = DSIZ; |
210 | |
|
211 | 0 | #ifdef Z_BLOCK |
212 | | /* Z_BLOCK is only available in zlib ver. >= 1.2.0.5 */ |
213 | 0 | status = inflate(z, Z_BLOCK); |
214 | | #else |
215 | | /* fallback for zlib ver. < 1.2.0.5 */ |
216 | | status = inflate(z, Z_SYNC_FLUSH); |
217 | | #endif |
218 | | |
219 | | /* Flush output data if some. */ |
220 | 0 | if(z->avail_out != DSIZ) { |
221 | 0 | if(status == Z_OK || status == Z_STREAM_END) { |
222 | 0 | zp->zlib_init = started; /* Data started. */ |
223 | 0 | result = Curl_cwriter_write(data, writer->next, type, decomp, |
224 | 0 | DSIZ - z->avail_out); |
225 | 0 | if(result) { |
226 | 0 | exit_zlib(data, z, &zp->zlib_init, result); |
227 | 0 | break; |
228 | 0 | } |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | | /* Dispatch by inflate() status. */ |
233 | 0 | switch(status) { |
234 | 0 | case Z_OK: |
235 | | /* Always loop: there may be unflushed latched data in zlib state. */ |
236 | 0 | done = FALSE; |
237 | 0 | break; |
238 | 0 | case Z_BUF_ERROR: |
239 | | /* No more data to flush: just exit loop. */ |
240 | 0 | break; |
241 | 0 | case Z_STREAM_END: |
242 | 0 | result = process_trailer(data, zp); |
243 | 0 | break; |
244 | 0 | case Z_DATA_ERROR: |
245 | | /* some servers seem to not generate zlib headers, so this is an attempt |
246 | | to fix and continue anyway */ |
247 | 0 | if(zp->zlib_init == ZLIB_INIT) { |
248 | | /* Do not use inflateReset2(): only available since zlib 1.2.3.4. */ |
249 | 0 | (void) inflateEnd(z); /* don't care about the return code */ |
250 | 0 | if(inflateInit2(z, -MAX_WBITS) == Z_OK) { |
251 | 0 | z->next_in = orig_in; |
252 | 0 | z->avail_in = nread; |
253 | 0 | zp->zlib_init = ZLIB_INFLATING; |
254 | 0 | zp->trailerlen = 4; /* Tolerate up to 4 unknown trailer bytes. */ |
255 | 0 | done = FALSE; |
256 | 0 | break; |
257 | 0 | } |
258 | 0 | zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */ |
259 | 0 | } |
260 | 0 | result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z)); |
261 | 0 | break; |
262 | 0 | default: |
263 | 0 | result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z)); |
264 | 0 | break; |
265 | 0 | } |
266 | 0 | } |
267 | 0 | free(decomp); |
268 | | |
269 | | /* We're about to leave this call so the `nread' data bytes won't be seen |
270 | | again. If we are in a state that would wrongly allow restart in raw mode |
271 | | at the next call, assume output has already started. */ |
272 | 0 | if(nread && zp->zlib_init == ZLIB_INIT) |
273 | 0 | zp->zlib_init = started; /* Cannot restart anymore. */ |
274 | |
|
275 | 0 | return result; |
276 | 0 | } |
277 | | |
278 | | |
279 | | /* Deflate handler. */ |
280 | | static CURLcode deflate_do_init(struct Curl_easy *data, |
281 | | struct Curl_cwriter *writer) |
282 | 0 | { |
283 | 0 | struct zlib_writer *zp = (struct zlib_writer *) writer; |
284 | 0 | z_stream *z = &zp->z; /* zlib state structure */ |
285 | | |
286 | | /* Initialize zlib */ |
287 | 0 | z->zalloc = (alloc_func) zalloc_cb; |
288 | 0 | z->zfree = (free_func) zfree_cb; |
289 | |
|
290 | 0 | if(inflateInit(z) != Z_OK) |
291 | 0 | return process_zlib_error(data, z); |
292 | 0 | zp->zlib_init = ZLIB_INIT; |
293 | 0 | return CURLE_OK; |
294 | 0 | } |
295 | | |
296 | | static CURLcode deflate_do_write(struct Curl_easy *data, |
297 | | struct Curl_cwriter *writer, int type, |
298 | | const char *buf, size_t nbytes) |
299 | 0 | { |
300 | 0 | struct zlib_writer *zp = (struct zlib_writer *) writer; |
301 | 0 | z_stream *z = &zp->z; /* zlib state structure */ |
302 | |
|
303 | 0 | if(!(type & CLIENTWRITE_BODY)) |
304 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
305 | | |
306 | | /* Set the compressed input when this function is called */ |
307 | 0 | z->next_in = (Bytef *) buf; |
308 | 0 | z->avail_in = (uInt) nbytes; |
309 | |
|
310 | 0 | if(zp->zlib_init == ZLIB_EXTERNAL_TRAILER) |
311 | 0 | return process_trailer(data, zp); |
312 | | |
313 | | /* Now uncompress the data */ |
314 | 0 | return inflate_stream(data, writer, type, ZLIB_INFLATING); |
315 | 0 | } |
316 | | |
317 | | static void deflate_do_close(struct Curl_easy *data, |
318 | | struct Curl_cwriter *writer) |
319 | 0 | { |
320 | 0 | struct zlib_writer *zp = (struct zlib_writer *) writer; |
321 | 0 | z_stream *z = &zp->z; /* zlib state structure */ |
322 | |
|
323 | 0 | exit_zlib(data, z, &zp->zlib_init, CURLE_OK); |
324 | 0 | } |
325 | | |
326 | | static const struct Curl_cwtype deflate_encoding = { |
327 | | "deflate", |
328 | | NULL, |
329 | | deflate_do_init, |
330 | | deflate_do_write, |
331 | | deflate_do_close, |
332 | | sizeof(struct zlib_writer) |
333 | | }; |
334 | | |
335 | | |
336 | | /* Gzip handler. */ |
337 | | static CURLcode gzip_do_init(struct Curl_easy *data, |
338 | | struct Curl_cwriter *writer) |
339 | 0 | { |
340 | 0 | struct zlib_writer *zp = (struct zlib_writer *) writer; |
341 | 0 | z_stream *z = &zp->z; /* zlib state structure */ |
342 | | |
343 | | /* Initialize zlib */ |
344 | 0 | z->zalloc = (alloc_func) zalloc_cb; |
345 | 0 | z->zfree = (free_func) zfree_cb; |
346 | |
|
347 | 0 | if(strcmp(zlibVersion(), "1.2.0.4") >= 0) { |
348 | | /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */ |
349 | 0 | if(inflateInit2(z, MAX_WBITS + 32) != Z_OK) { |
350 | 0 | return process_zlib_error(data, z); |
351 | 0 | } |
352 | 0 | zp->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */ |
353 | 0 | } |
354 | 0 | else { |
355 | | /* we must parse the gzip header and trailer ourselves */ |
356 | 0 | if(inflateInit2(z, -MAX_WBITS) != Z_OK) { |
357 | 0 | return process_zlib_error(data, z); |
358 | 0 | } |
359 | 0 | zp->trailerlen = 8; /* A CRC-32 and a 32-bit input size (RFC 1952, 2.2) */ |
360 | 0 | zp->zlib_init = ZLIB_INIT; /* Initial call state */ |
361 | 0 | } |
362 | | |
363 | 0 | return CURLE_OK; |
364 | 0 | } |
365 | | |
366 | | #ifdef OLD_ZLIB_SUPPORT |
367 | | /* Skip over the gzip header */ |
368 | | static enum { |
369 | | GZIP_OK, |
370 | | GZIP_BAD, |
371 | | GZIP_UNDERFLOW |
372 | | } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen) |
373 | 0 | { |
374 | 0 | int method, flags; |
375 | 0 | const ssize_t totallen = len; |
376 | | |
377 | | /* The shortest header is 10 bytes */ |
378 | 0 | if(len < 10) |
379 | 0 | return GZIP_UNDERFLOW; |
380 | | |
381 | 0 | if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1)) |
382 | 0 | return GZIP_BAD; |
383 | | |
384 | 0 | method = data[2]; |
385 | 0 | flags = data[3]; |
386 | |
|
387 | 0 | if(method != Z_DEFLATED || (flags & RESERVED) != 0) { |
388 | | /* Can't handle this compression method or unknown flag */ |
389 | 0 | return GZIP_BAD; |
390 | 0 | } |
391 | | |
392 | | /* Skip over time, xflags, OS code and all previous bytes */ |
393 | 0 | len -= 10; |
394 | 0 | data += 10; |
395 | |
|
396 | 0 | if(flags & EXTRA_FIELD) { |
397 | 0 | ssize_t extra_len; |
398 | |
|
399 | 0 | if(len < 2) |
400 | 0 | return GZIP_UNDERFLOW; |
401 | | |
402 | 0 | extra_len = (data[1] << 8) | data[0]; |
403 | |
|
404 | 0 | if(len < (extra_len + 2)) |
405 | 0 | return GZIP_UNDERFLOW; |
406 | | |
407 | 0 | len -= (extra_len + 2); |
408 | 0 | data += (extra_len + 2); |
409 | 0 | } |
410 | | |
411 | 0 | if(flags & ORIG_NAME) { |
412 | | /* Skip over NUL-terminated file name */ |
413 | 0 | while(len && *data) { |
414 | 0 | --len; |
415 | 0 | ++data; |
416 | 0 | } |
417 | 0 | if(!len || *data) |
418 | 0 | return GZIP_UNDERFLOW; |
419 | | |
420 | | /* Skip over the NUL */ |
421 | 0 | --len; |
422 | 0 | ++data; |
423 | 0 | } |
424 | | |
425 | 0 | if(flags & COMMENT) { |
426 | | /* Skip over NUL-terminated comment */ |
427 | 0 | while(len && *data) { |
428 | 0 | --len; |
429 | 0 | ++data; |
430 | 0 | } |
431 | 0 | if(!len || *data) |
432 | 0 | return GZIP_UNDERFLOW; |
433 | | |
434 | | /* Skip over the NUL */ |
435 | 0 | --len; |
436 | 0 | } |
437 | | |
438 | 0 | if(flags & HEAD_CRC) { |
439 | 0 | if(len < 2) |
440 | 0 | return GZIP_UNDERFLOW; |
441 | | |
442 | 0 | len -= 2; |
443 | 0 | } |
444 | | |
445 | 0 | *headerlen = totallen - len; |
446 | 0 | return GZIP_OK; |
447 | 0 | } |
448 | | #endif |
449 | | |
450 | | static CURLcode gzip_do_write(struct Curl_easy *data, |
451 | | struct Curl_cwriter *writer, int type, |
452 | | const char *buf, size_t nbytes) |
453 | 0 | { |
454 | 0 | struct zlib_writer *zp = (struct zlib_writer *) writer; |
455 | 0 | z_stream *z = &zp->z; /* zlib state structure */ |
456 | |
|
457 | 0 | if(!(type & CLIENTWRITE_BODY)) |
458 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
459 | | |
460 | 0 | if(zp->zlib_init == ZLIB_INIT_GZIP) { |
461 | | /* Let zlib handle the gzip decompression entirely */ |
462 | 0 | z->next_in = (Bytef *) buf; |
463 | 0 | z->avail_in = (uInt) nbytes; |
464 | | /* Now uncompress the data */ |
465 | 0 | return inflate_stream(data, writer, type, ZLIB_INIT_GZIP); |
466 | 0 | } |
467 | | |
468 | | #ifndef OLD_ZLIB_SUPPORT |
469 | | /* Support for old zlib versions is compiled away and we are running with |
470 | | an old version, so return an error. */ |
471 | | return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR); |
472 | | |
473 | | #else |
474 | | /* This next mess is to get around the potential case where there isn't |
475 | | * enough data passed in to skip over the gzip header. If that happens, we |
476 | | * malloc a block and copy what we have then wait for the next call. If |
477 | | * there still isn't enough (this is definitely a worst-case scenario), we |
478 | | * make the block bigger, copy the next part in and keep waiting. |
479 | | * |
480 | | * This is only required with zlib versions < 1.2.0.4 as newer versions |
481 | | * can handle the gzip header themselves. |
482 | | */ |
483 | | |
484 | 0 | switch(zp->zlib_init) { |
485 | | /* Skip over gzip header? */ |
486 | 0 | case ZLIB_INIT: |
487 | 0 | { |
488 | | /* Initial call state */ |
489 | 0 | ssize_t hlen; |
490 | |
|
491 | 0 | switch(check_gzip_header((unsigned char *) buf, nbytes, &hlen)) { |
492 | 0 | case GZIP_OK: |
493 | 0 | z->next_in = (Bytef *) buf + hlen; |
494 | 0 | z->avail_in = (uInt) (nbytes - hlen); |
495 | 0 | zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */ |
496 | 0 | break; |
497 | | |
498 | 0 | case GZIP_UNDERFLOW: |
499 | | /* We need more data so we can find the end of the gzip header. It's |
500 | | * possible that the memory block we malloc here will never be freed if |
501 | | * the transfer abruptly aborts after this point. Since it's unlikely |
502 | | * that circumstances will be right for this code path to be followed in |
503 | | * the first place, and it's even more unlikely for a transfer to fail |
504 | | * immediately afterwards, it should seldom be a problem. |
505 | | */ |
506 | 0 | z->avail_in = (uInt) nbytes; |
507 | 0 | z->next_in = malloc(z->avail_in); |
508 | 0 | if(!z->next_in) { |
509 | 0 | return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY); |
510 | 0 | } |
511 | 0 | memcpy(z->next_in, buf, z->avail_in); |
512 | 0 | zp->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */ |
513 | | /* We don't have any data to inflate yet */ |
514 | 0 | return CURLE_OK; |
515 | | |
516 | 0 | case GZIP_BAD: |
517 | 0 | default: |
518 | 0 | return exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z)); |
519 | 0 | } |
520 | |
|
521 | 0 | } |
522 | 0 | break; |
523 | | |
524 | 0 | case ZLIB_GZIP_HEADER: |
525 | 0 | { |
526 | | /* Need more gzip header data state */ |
527 | 0 | ssize_t hlen; |
528 | 0 | z->avail_in += (uInt) nbytes; |
529 | 0 | z->next_in = Curl_saferealloc(z->next_in, z->avail_in); |
530 | 0 | if(!z->next_in) { |
531 | 0 | return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY); |
532 | 0 | } |
533 | | /* Append the new block of data to the previous one */ |
534 | 0 | memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes); |
535 | |
|
536 | 0 | switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) { |
537 | 0 | case GZIP_OK: |
538 | | /* This is the zlib stream data */ |
539 | 0 | free(z->next_in); |
540 | | /* Don't point into the malloced block since we just freed it */ |
541 | 0 | z->next_in = (Bytef *) buf + hlen + nbytes - z->avail_in; |
542 | 0 | z->avail_in = (uInt) (z->avail_in - hlen); |
543 | 0 | zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */ |
544 | 0 | break; |
545 | | |
546 | 0 | case GZIP_UNDERFLOW: |
547 | | /* We still don't have any data to inflate! */ |
548 | 0 | return CURLE_OK; |
549 | | |
550 | 0 | case GZIP_BAD: |
551 | 0 | default: |
552 | 0 | return exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z)); |
553 | 0 | } |
554 | |
|
555 | 0 | } |
556 | 0 | break; |
557 | | |
558 | 0 | case ZLIB_EXTERNAL_TRAILER: |
559 | 0 | z->next_in = (Bytef *) buf; |
560 | 0 | z->avail_in = (uInt) nbytes; |
561 | 0 | return process_trailer(data, zp); |
562 | | |
563 | 0 | case ZLIB_GZIP_INFLATING: |
564 | 0 | default: |
565 | | /* Inflating stream state */ |
566 | 0 | z->next_in = (Bytef *) buf; |
567 | 0 | z->avail_in = (uInt) nbytes; |
568 | 0 | break; |
569 | 0 | } |
570 | | |
571 | 0 | if(z->avail_in == 0) { |
572 | | /* We don't have any data to inflate; wait until next time */ |
573 | 0 | return CURLE_OK; |
574 | 0 | } |
575 | | |
576 | | /* We've parsed the header, now uncompress the data */ |
577 | 0 | return inflate_stream(data, writer, type, ZLIB_GZIP_INFLATING); |
578 | 0 | #endif |
579 | 0 | } |
580 | | |
581 | | static void gzip_do_close(struct Curl_easy *data, |
582 | | struct Curl_cwriter *writer) |
583 | 0 | { |
584 | 0 | struct zlib_writer *zp = (struct zlib_writer *) writer; |
585 | 0 | z_stream *z = &zp->z; /* zlib state structure */ |
586 | |
|
587 | 0 | exit_zlib(data, z, &zp->zlib_init, CURLE_OK); |
588 | 0 | } |
589 | | |
590 | | static const struct Curl_cwtype gzip_encoding = { |
591 | | "gzip", |
592 | | "x-gzip", |
593 | | gzip_do_init, |
594 | | gzip_do_write, |
595 | | gzip_do_close, |
596 | | sizeof(struct zlib_writer) |
597 | | }; |
598 | | |
599 | | #endif /* HAVE_LIBZ */ |
600 | | |
601 | | |
602 | | #ifdef HAVE_BROTLI |
603 | | /* Brotli writer. */ |
604 | | struct brotli_writer { |
605 | | struct Curl_cwriter super; |
606 | | BrotliDecoderState *br; /* State structure for brotli. */ |
607 | | }; |
608 | | |
609 | | static CURLcode brotli_map_error(BrotliDecoderErrorCode be) |
610 | | { |
611 | | switch(be) { |
612 | | case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE: |
613 | | case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE: |
614 | | case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET: |
615 | | case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME: |
616 | | case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE: |
617 | | case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE: |
618 | | case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT: |
619 | | case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1: |
620 | | case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2: |
621 | | case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM: |
622 | | case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY: |
623 | | case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS: |
624 | | case BROTLI_DECODER_ERROR_FORMAT_PADDING_1: |
625 | | case BROTLI_DECODER_ERROR_FORMAT_PADDING_2: |
626 | | #ifdef BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY |
627 | | case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY: |
628 | | #endif |
629 | | #ifdef BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET |
630 | | case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET: |
631 | | #endif |
632 | | case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS: |
633 | | return CURLE_BAD_CONTENT_ENCODING; |
634 | | case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES: |
635 | | case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS: |
636 | | case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP: |
637 | | case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1: |
638 | | case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2: |
639 | | case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES: |
640 | | return CURLE_OUT_OF_MEMORY; |
641 | | default: |
642 | | break; |
643 | | } |
644 | | return CURLE_WRITE_ERROR; |
645 | | } |
646 | | |
647 | | static CURLcode brotli_do_init(struct Curl_easy *data, |
648 | | struct Curl_cwriter *writer) |
649 | | { |
650 | | struct brotli_writer *bp = (struct brotli_writer *) writer; |
651 | | (void) data; |
652 | | |
653 | | bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL); |
654 | | return bp->br? CURLE_OK: CURLE_OUT_OF_MEMORY; |
655 | | } |
656 | | |
657 | | static CURLcode brotli_do_write(struct Curl_easy *data, |
658 | | struct Curl_cwriter *writer, int type, |
659 | | const char *buf, size_t nbytes) |
660 | | { |
661 | | struct brotli_writer *bp = (struct brotli_writer *) writer; |
662 | | const uint8_t *src = (const uint8_t *) buf; |
663 | | char *decomp; |
664 | | uint8_t *dst; |
665 | | size_t dstleft; |
666 | | CURLcode result = CURLE_OK; |
667 | | BrotliDecoderResult r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; |
668 | | |
669 | | if(!(type & CLIENTWRITE_BODY)) |
670 | | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
671 | | |
672 | | if(!bp->br) |
673 | | return CURLE_WRITE_ERROR; /* Stream already ended. */ |
674 | | |
675 | | decomp = malloc(DSIZ); |
676 | | if(!decomp) |
677 | | return CURLE_OUT_OF_MEMORY; |
678 | | |
679 | | while((nbytes || r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) && |
680 | | result == CURLE_OK) { |
681 | | dst = (uint8_t *) decomp; |
682 | | dstleft = DSIZ; |
683 | | r = BrotliDecoderDecompressStream(bp->br, |
684 | | &nbytes, &src, &dstleft, &dst, NULL); |
685 | | result = Curl_cwriter_write(data, writer->next, type, |
686 | | decomp, DSIZ - dstleft); |
687 | | if(result) |
688 | | break; |
689 | | switch(r) { |
690 | | case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: |
691 | | case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: |
692 | | break; |
693 | | case BROTLI_DECODER_RESULT_SUCCESS: |
694 | | BrotliDecoderDestroyInstance(bp->br); |
695 | | bp->br = NULL; |
696 | | if(nbytes) |
697 | | result = CURLE_WRITE_ERROR; |
698 | | break; |
699 | | default: |
700 | | result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br)); |
701 | | break; |
702 | | } |
703 | | } |
704 | | free(decomp); |
705 | | return result; |
706 | | } |
707 | | |
708 | | static void brotli_do_close(struct Curl_easy *data, |
709 | | struct Curl_cwriter *writer) |
710 | | { |
711 | | struct brotli_writer *bp = (struct brotli_writer *) writer; |
712 | | |
713 | | (void) data; |
714 | | |
715 | | if(bp->br) { |
716 | | BrotliDecoderDestroyInstance(bp->br); |
717 | | bp->br = NULL; |
718 | | } |
719 | | } |
720 | | |
721 | | static const struct Curl_cwtype brotli_encoding = { |
722 | | "br", |
723 | | NULL, |
724 | | brotli_do_init, |
725 | | brotli_do_write, |
726 | | brotli_do_close, |
727 | | sizeof(struct brotli_writer) |
728 | | }; |
729 | | #endif |
730 | | |
731 | | |
732 | | #ifdef HAVE_ZSTD |
733 | | /* Zstd writer. */ |
734 | | struct zstd_writer { |
735 | | struct Curl_cwriter super; |
736 | | ZSTD_DStream *zds; /* State structure for zstd. */ |
737 | | void *decomp; |
738 | | }; |
739 | | |
740 | | static CURLcode zstd_do_init(struct Curl_easy *data, |
741 | | struct Curl_cwriter *writer) |
742 | | { |
743 | | struct zstd_writer *zp = (struct zstd_writer *) writer; |
744 | | |
745 | | (void)data; |
746 | | |
747 | | zp->zds = ZSTD_createDStream(); |
748 | | zp->decomp = NULL; |
749 | | return zp->zds ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
750 | | } |
751 | | |
752 | | static CURLcode zstd_do_write(struct Curl_easy *data, |
753 | | struct Curl_cwriter *writer, int type, |
754 | | const char *buf, size_t nbytes) |
755 | | { |
756 | | CURLcode result = CURLE_OK; |
757 | | struct zstd_writer *zp = (struct zstd_writer *) writer; |
758 | | ZSTD_inBuffer in; |
759 | | ZSTD_outBuffer out; |
760 | | size_t errorCode; |
761 | | |
762 | | if(!(type & CLIENTWRITE_BODY)) |
763 | | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
764 | | |
765 | | if(!zp->decomp) { |
766 | | zp->decomp = malloc(DSIZ); |
767 | | if(!zp->decomp) |
768 | | return CURLE_OUT_OF_MEMORY; |
769 | | } |
770 | | in.pos = 0; |
771 | | in.src = buf; |
772 | | in.size = nbytes; |
773 | | |
774 | | for(;;) { |
775 | | out.pos = 0; |
776 | | out.dst = zp->decomp; |
777 | | out.size = DSIZ; |
778 | | |
779 | | errorCode = ZSTD_decompressStream(zp->zds, &out, &in); |
780 | | if(ZSTD_isError(errorCode)) { |
781 | | return CURLE_BAD_CONTENT_ENCODING; |
782 | | } |
783 | | if(out.pos > 0) { |
784 | | result = Curl_cwriter_write(data, writer->next, type, |
785 | | zp->decomp, out.pos); |
786 | | if(result) |
787 | | break; |
788 | | } |
789 | | if((in.pos == nbytes) && (out.pos < out.size)) |
790 | | break; |
791 | | } |
792 | | |
793 | | return result; |
794 | | } |
795 | | |
796 | | static void zstd_do_close(struct Curl_easy *data, |
797 | | struct Curl_cwriter *writer) |
798 | | { |
799 | | struct zstd_writer *zp = (struct zstd_writer *) writer; |
800 | | |
801 | | (void)data; |
802 | | |
803 | | if(zp->decomp) { |
804 | | free(zp->decomp); |
805 | | zp->decomp = NULL; |
806 | | } |
807 | | if(zp->zds) { |
808 | | ZSTD_freeDStream(zp->zds); |
809 | | zp->zds = NULL; |
810 | | } |
811 | | } |
812 | | |
813 | | static const struct Curl_cwtype zstd_encoding = { |
814 | | "zstd", |
815 | | NULL, |
816 | | zstd_do_init, |
817 | | zstd_do_write, |
818 | | zstd_do_close, |
819 | | sizeof(struct zstd_writer) |
820 | | }; |
821 | | #endif |
822 | | |
823 | | |
824 | | /* Identity handler. */ |
825 | | static const struct Curl_cwtype identity_encoding = { |
826 | | "identity", |
827 | | "none", |
828 | | Curl_cwriter_def_init, |
829 | | Curl_cwriter_def_write, |
830 | | Curl_cwriter_def_close, |
831 | | sizeof(struct Curl_cwriter) |
832 | | }; |
833 | | |
834 | | |
835 | | /* supported content encodings table. */ |
836 | | static const struct Curl_cwtype * const encodings[] = { |
837 | | &identity_encoding, |
838 | | #ifdef HAVE_LIBZ |
839 | | &deflate_encoding, |
840 | | &gzip_encoding, |
841 | | #endif |
842 | | #ifdef HAVE_BROTLI |
843 | | &brotli_encoding, |
844 | | #endif |
845 | | #ifdef HAVE_ZSTD |
846 | | &zstd_encoding, |
847 | | #endif |
848 | | NULL |
849 | | }; |
850 | | |
851 | | |
852 | | /* Provide a list of comma-separated names of supported encodings. |
853 | | */ |
854 | | void Curl_all_content_encodings(char *buf, size_t blen) |
855 | 7 | { |
856 | 7 | size_t len = 0; |
857 | 7 | const struct Curl_cwtype * const *cep; |
858 | 7 | const struct Curl_cwtype *ce; |
859 | | |
860 | 7 | DEBUGASSERT(buf); |
861 | 7 | DEBUGASSERT(blen); |
862 | 7 | buf[0] = 0; |
863 | | |
864 | 28 | for(cep = encodings; *cep; cep++) { |
865 | 21 | ce = *cep; |
866 | 21 | if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) |
867 | 14 | len += strlen(ce->name) + 2; |
868 | 21 | } |
869 | | |
870 | 7 | if(!len) { |
871 | 0 | if(blen >= sizeof(CONTENT_ENCODING_DEFAULT)) |
872 | 0 | strcpy(buf, CONTENT_ENCODING_DEFAULT); |
873 | 0 | } |
874 | 7 | else if(blen > len) { |
875 | 7 | char *p = buf; |
876 | 28 | for(cep = encodings; *cep; cep++) { |
877 | 21 | ce = *cep; |
878 | 21 | if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) { |
879 | 14 | strcpy(p, ce->name); |
880 | 14 | p += strlen(p); |
881 | 14 | *p++ = ','; |
882 | 14 | *p++ = ' '; |
883 | 14 | } |
884 | 21 | } |
885 | 7 | p[-2] = '\0'; |
886 | 7 | } |
887 | 7 | } |
888 | | |
889 | | /* Deferred error dummy writer. */ |
890 | | static CURLcode error_do_init(struct Curl_easy *data, |
891 | | struct Curl_cwriter *writer) |
892 | 0 | { |
893 | 0 | (void)data; |
894 | 0 | (void)writer; |
895 | 0 | return CURLE_OK; |
896 | 0 | } |
897 | | |
898 | | static CURLcode error_do_write(struct Curl_easy *data, |
899 | | struct Curl_cwriter *writer, int type, |
900 | | const char *buf, size_t nbytes) |
901 | 0 | { |
902 | 0 | char all[256]; |
903 | 0 | (void)Curl_all_content_encodings(all, sizeof(all)); |
904 | |
|
905 | 0 | (void) writer; |
906 | 0 | (void) buf; |
907 | 0 | (void) nbytes; |
908 | |
|
909 | 0 | if(!(type & CLIENTWRITE_BODY)) |
910 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
911 | | |
912 | 0 | failf(data, "Unrecognized content encoding type. " |
913 | 0 | "libcurl understands %s content encodings.", all); |
914 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
915 | 0 | } |
916 | | |
917 | | static void error_do_close(struct Curl_easy *data, |
918 | | struct Curl_cwriter *writer) |
919 | 0 | { |
920 | 0 | (void) data; |
921 | 0 | (void) writer; |
922 | 0 | } |
923 | | |
924 | | static const struct Curl_cwtype error_writer = { |
925 | | "ce-error", |
926 | | NULL, |
927 | | error_do_init, |
928 | | error_do_write, |
929 | | error_do_close, |
930 | | sizeof(struct Curl_cwriter) |
931 | | }; |
932 | | |
933 | | /* Find the content encoding by name. */ |
934 | | static const struct Curl_cwtype *find_encoding(const char *name, |
935 | | size_t len) |
936 | 0 | { |
937 | 0 | const struct Curl_cwtype * const *cep; |
938 | |
|
939 | 0 | for(cep = encodings; *cep; cep++) { |
940 | 0 | const struct Curl_cwtype *ce = *cep; |
941 | 0 | if((strncasecompare(name, ce->name, len) && !ce->name[len]) || |
942 | 0 | (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len])) |
943 | 0 | return ce; |
944 | 0 | } |
945 | 0 | return NULL; |
946 | 0 | } |
947 | | |
948 | | /* Set-up the unencoding stack from the Content-Encoding header value. |
949 | | * See RFC 7231 section 3.1.2.2. */ |
950 | | CURLcode Curl_build_unencoding_stack(struct Curl_easy *data, |
951 | | const char *enclist, int is_transfer) |
952 | 0 | { |
953 | 0 | struct SingleRequest *k = &data->req; |
954 | 0 | Curl_cwriter_phase phase = is_transfer? |
955 | 0 | CURL_CW_TRANSFER_DECODE:CURL_CW_CONTENT_DECODE; |
956 | 0 | CURLcode result; |
957 | |
|
958 | 0 | do { |
959 | 0 | const char *name; |
960 | 0 | size_t namelen; |
961 | | |
962 | | /* Parse a single encoding name. */ |
963 | 0 | while(ISBLANK(*enclist) || *enclist == ',') |
964 | 0 | enclist++; |
965 | |
|
966 | 0 | name = enclist; |
967 | |
|
968 | 0 | for(namelen = 0; *enclist && *enclist != ','; enclist++) |
969 | 0 | if(!ISSPACE(*enclist)) |
970 | 0 | namelen = enclist - name + 1; |
971 | | |
972 | | /* Special case: chunked encoding is handled at the reader level. */ |
973 | 0 | if(is_transfer && namelen == 7 && strncasecompare(name, "chunked", 7)) { |
974 | 0 | k->chunk = TRUE; /* chunks coming our way. */ |
975 | 0 | Curl_httpchunk_init(data); /* init our chunky engine. */ |
976 | 0 | } |
977 | 0 | else if(namelen) { |
978 | 0 | const struct Curl_cwtype *cwt; |
979 | 0 | struct Curl_cwriter *writer; |
980 | |
|
981 | 0 | if((is_transfer && !data->set.http_transfer_encoding) || |
982 | 0 | (!is_transfer && data->set.http_ce_skip)) { |
983 | | /* not requested, ignore */ |
984 | 0 | return CURLE_OK; |
985 | 0 | } |
986 | | |
987 | 0 | if(Curl_cwriter_count(data, phase) + 1 >= MAX_ENCODE_STACK) { |
988 | 0 | failf(data, "Reject response due to more than %u content encodings", |
989 | 0 | MAX_ENCODE_STACK); |
990 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
991 | 0 | } |
992 | | |
993 | 0 | cwt = find_encoding(name, namelen); |
994 | 0 | if(!cwt) |
995 | 0 | cwt = &error_writer; /* Defer error at use. */ |
996 | |
|
997 | 0 | result = Curl_cwriter_create(&writer, data, cwt, phase); |
998 | 0 | if(result) |
999 | 0 | return result; |
1000 | | |
1001 | 0 | result = Curl_cwriter_add(data, writer); |
1002 | 0 | if(result) { |
1003 | 0 | Curl_cwriter_free(data, writer); |
1004 | 0 | return result; |
1005 | 0 | } |
1006 | 0 | } |
1007 | 0 | } while(*enclist); |
1008 | | |
1009 | 0 | return CURLE_OK; |
1010 | 0 | } |
1011 | | |
1012 | | #else |
1013 | | /* Stubs for builds without HTTP. */ |
1014 | | CURLcode Curl_build_unencoding_stack(struct Curl_easy *data, |
1015 | | const char *enclist, int is_transfer) |
1016 | | { |
1017 | | (void) data; |
1018 | | (void) enclist; |
1019 | | (void) is_transfer; |
1020 | | return CURLE_NOT_BUILT_IN; |
1021 | | } |
1022 | | |
1023 | | void Curl_all_content_encodings(char *buf, size_t blen) |
1024 | | { |
1025 | | DEBUGASSERT(buf); |
1026 | | DEBUGASSERT(blen); |
1027 | | if(blen < sizeof(CONTENT_ENCODING_DEFAULT)) |
1028 | | buf[0] = 0; |
1029 | | else |
1030 | | strcpy(buf, CONTENT_ENCODING_DEFAULT); |
1031 | | } |
1032 | | |
1033 | | |
1034 | | #endif /* CURL_DISABLE_HTTP */ |