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