/src/curl/lib/content_encoding.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | |
26 | | #include "urldata.h" |
27 | | #include "curlx/dynbuf.h" |
28 | | |
29 | | #ifdef HAVE_LIBZ |
30 | | #include <zlib.h> |
31 | | #endif |
32 | | |
33 | | #ifdef HAVE_BROTLI |
34 | | #if defined(__GNUC__) || defined(__clang__) |
35 | | /* Ignore -Wvla warnings in brotli headers */ |
36 | | #pragma GCC diagnostic push |
37 | | #pragma GCC diagnostic ignored "-Wvla" |
38 | | #endif |
39 | | #include <brotli/decode.h> |
40 | | #if defined(__GNUC__) || defined(__clang__) |
41 | | #pragma GCC diagnostic pop |
42 | | #endif |
43 | | #endif |
44 | | |
45 | | #ifdef HAVE_ZSTD |
46 | | #include <zstd.h> |
47 | | #endif |
48 | | |
49 | | #include "sendf.h" |
50 | | #include "curl_trc.h" |
51 | | #include "content_encoding.h" |
52 | | |
53 | 6.57k | #define CONTENT_ENCODING_DEFAULT "identity" |
54 | | |
55 | | #ifndef CURL_DISABLE_HTTP |
56 | | |
57 | | /* allow no more than 5 "chained" compression steps */ |
58 | 6.43k | #define MAX_ENCODE_STACK 5 |
59 | | |
60 | | #if defined(HAVE_LIBZ) || defined(HAVE_BROTLI) || defined(HAVE_ZSTD) |
61 | 1.20M | #define DECOMPRESS_BUFFER_SIZE 16384 /* buffer size for decompressed data */ |
62 | | #endif |
63 | | |
64 | | #ifdef HAVE_LIBZ |
65 | | |
66 | | #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1252) |
67 | | #error "requires zlib 1.2.5.2 or newer" |
68 | | #endif |
69 | | |
70 | | typedef enum { |
71 | | ZLIB_UNINIT, /* uninitialized */ |
72 | | ZLIB_INIT, /* initialized */ |
73 | | ZLIB_INFLATING, /* inflating started. */ |
74 | | ZLIB_EXTERNAL_TRAILER, /* reading external trailer */ |
75 | | ZLIB_INIT_GZIP /* initialized in transparent gzip mode */ |
76 | | } zlibInitState; |
77 | | |
78 | | /* Deflate and gzip writer. */ |
79 | | struct zlib_writer { |
80 | | struct Curl_cwriter super; |
81 | | zlibInitState zlib_init; /* zlib init state */ |
82 | | char buffer[DECOMPRESS_BUFFER_SIZE]; /* Put the decompressed data here. */ |
83 | | uInt trailerlen; /* Remaining trailer byte count. */ |
84 | | z_stream z; /* State structure for zlib. */ |
85 | | }; |
86 | | |
87 | | static voidpf zalloc_cb(voidpf opaque, unsigned int items, unsigned int size) |
88 | 4.67k | { |
89 | 4.67k | (void)opaque; |
90 | | /* not a typo, keep it curlx_calloc() */ |
91 | 4.67k | return (voidpf)curlx_calloc(items, size); |
92 | 4.67k | } |
93 | | |
94 | | static void zfree_cb(voidpf opaque, voidpf ptr) |
95 | 4.67k | { |
96 | 4.67k | (void)opaque; |
97 | 4.67k | curlx_free(ptr); |
98 | 4.67k | } |
99 | | |
100 | | static CURLcode process_zlib_error(struct Curl_easy *data, z_stream *z) |
101 | 883 | { |
102 | 883 | if(z->msg) |
103 | 878 | failf(data, "Error while processing content unencoding: %s", |
104 | 878 | z->msg); |
105 | 5 | else |
106 | 5 | failf(data, "Error while processing content unencoding: " |
107 | 5 | "Unknown failure within decompression software."); |
108 | | |
109 | 883 | return CURLE_BAD_CONTENT_ENCODING; |
110 | 883 | } |
111 | | |
112 | | static CURLcode exit_zlib(struct Curl_easy *data, z_stream *z, |
113 | | zlibInitState *zlib_init, CURLcode result) |
114 | 3.93k | { |
115 | 3.93k | if(*zlib_init != ZLIB_UNINIT) { |
116 | 2.71k | if(inflateEnd(z) != Z_OK && result == CURLE_OK) |
117 | 0 | result = process_zlib_error(data, z); |
118 | 2.71k | *zlib_init = ZLIB_UNINIT; |
119 | 2.71k | } |
120 | | |
121 | 3.93k | return result; |
122 | 3.93k | } |
123 | | |
124 | | static CURLcode process_trailer(struct Curl_easy *data, struct zlib_writer *zp) |
125 | 140 | { |
126 | 140 | z_stream *z = &zp->z; |
127 | 140 | CURLcode result = CURLE_OK; |
128 | 140 | uInt len = z->avail_in < zp->trailerlen ? z->avail_in : zp->trailerlen; |
129 | | |
130 | | /* Consume expected trailer bytes. Terminate stream if exhausted. |
131 | | Issue an error if unexpected bytes follow. */ |
132 | | |
133 | 140 | zp->trailerlen -= len; |
134 | 140 | z->avail_in -= len; |
135 | 140 | z->next_in += len; |
136 | 140 | if(z->avail_in) |
137 | 46 | result = CURLE_WRITE_ERROR; |
138 | 140 | if(result || !zp->trailerlen) |
139 | 71 | result = exit_zlib(data, z, &zp->zlib_init, result); |
140 | 69 | else { |
141 | | /* Only occurs for gzip with zlib < 1.2.0.4 or raw deflate. */ |
142 | 69 | zp->zlib_init = ZLIB_EXTERNAL_TRAILER; |
143 | 69 | } |
144 | 140 | return result; |
145 | 140 | } |
146 | | |
147 | | static CURLcode inflate_stream(struct Curl_easy *data, |
148 | | struct Curl_cwriter *writer, int type, |
149 | | zlibInitState started) |
150 | 214k | { |
151 | 214k | struct zlib_writer *zp = (struct zlib_writer *)writer; |
152 | 214k | z_stream *z = &zp->z; /* zlib state structure */ |
153 | 214k | uInt nread = z->avail_in; |
154 | 214k | z_const Bytef *orig_in = z->next_in; |
155 | 214k | bool done = FALSE; |
156 | 214k | CURLcode result = CURLE_OK; /* Curl_client_write status */ |
157 | | |
158 | | /* Check state. */ |
159 | 214k | if(zp->zlib_init != ZLIB_INIT && |
160 | 212k | zp->zlib_init != ZLIB_INFLATING && |
161 | 776 | zp->zlib_init != ZLIB_INIT_GZIP) |
162 | 7 | return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR); |
163 | | |
164 | | /* because the buffer size is fixed, iteratively decompress and transfer to |
165 | | the client via next_write function. */ |
166 | 690k | while(!done) { |
167 | 475k | int status; /* zlib status */ |
168 | 475k | done = TRUE; |
169 | | |
170 | | /* (re)set buffer for decompressed output for every iteration */ |
171 | 475k | z->next_out = (Bytef *)zp->buffer; |
172 | 475k | z->avail_out = DECOMPRESS_BUFFER_SIZE; |
173 | | |
174 | 475k | status = inflate(z, Z_BLOCK); |
175 | | |
176 | | /* Flush output data if some. */ |
177 | 475k | if(z->avail_out != DECOMPRESS_BUFFER_SIZE) { |
178 | 254k | if(status == Z_OK || status == Z_STREAM_END) { |
179 | 254k | zp->zlib_init = started; /* Data started. */ |
180 | 254k | result = Curl_cwriter_write(data, writer->next, type, zp->buffer, |
181 | 254k | DECOMPRESS_BUFFER_SIZE - z->avail_out); |
182 | 254k | if(result) { |
183 | 260 | exit_zlib(data, z, &zp->zlib_init, result); |
184 | 260 | break; |
185 | 260 | } |
186 | 254k | } |
187 | 254k | } |
188 | | |
189 | | /* Dispatch by inflate() status. */ |
190 | 475k | switch(status) { |
191 | 259k | case Z_OK: |
192 | | /* Always loop: there may be unflushed latched data in zlib state. */ |
193 | 259k | done = FALSE; |
194 | 259k | break; |
195 | 213k | case Z_BUF_ERROR: |
196 | | /* No more data to flush: just exit loop. */ |
197 | 213k | break; |
198 | 100 | case Z_STREAM_END: |
199 | 100 | result = process_trailer(data, zp); |
200 | 100 | break; |
201 | 2.36k | case Z_DATA_ERROR: |
202 | | /* some servers seem to not generate zlib headers, so this is an attempt |
203 | | to fix and continue anyway */ |
204 | 2.36k | if(zp->zlib_init == ZLIB_INIT) { |
205 | 1.48k | if(inflateReset2(z, -MAX_WBITS) == Z_OK) { |
206 | 1.48k | z->next_in = orig_in; |
207 | 1.48k | z->avail_in = nread; |
208 | 1.48k | zp->zlib_init = ZLIB_INFLATING; |
209 | 1.48k | zp->trailerlen = 4; /* Tolerate up to 4 unknown trailer bytes. */ |
210 | 1.48k | done = FALSE; |
211 | 1.48k | break; |
212 | 1.48k | } |
213 | 0 | zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */ |
214 | 0 | } |
215 | 878 | result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z)); |
216 | 878 | break; |
217 | 5 | default: |
218 | 5 | result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z)); |
219 | 5 | break; |
220 | 475k | } |
221 | 475k | } |
222 | | |
223 | | /* We are about to leave this call so the `nread' data bytes will not be seen |
224 | | again. If we are in a state that would wrongly allow restart in raw mode |
225 | | at the next call, assume output has already started. */ |
226 | 214k | if(nread && zp->zlib_init == ZLIB_INIT) |
227 | 58 | zp->zlib_init = started; /* Cannot restart anymore. */ |
228 | | |
229 | 214k | return result; |
230 | 214k | } |
231 | | |
232 | | /* Deflate handler. */ |
233 | | static CURLcode deflate_do_init(struct Curl_easy *data, |
234 | | struct Curl_cwriter *writer) |
235 | 2.17k | { |
236 | 2.17k | struct zlib_writer *zp = (struct zlib_writer *)writer; |
237 | 2.17k | z_stream *z = &zp->z; /* zlib state structure */ |
238 | | |
239 | | /* Initialize zlib */ |
240 | 2.17k | z->zalloc = (alloc_func)zalloc_cb; |
241 | 2.17k | z->zfree = (free_func)zfree_cb; |
242 | | |
243 | 2.17k | if(inflateInit(z) != Z_OK) |
244 | 0 | return process_zlib_error(data, z); |
245 | 2.17k | zp->zlib_init = ZLIB_INIT; |
246 | 2.17k | return CURLE_OK; |
247 | 2.17k | } |
248 | | |
249 | | static CURLcode deflate_do_write(struct Curl_easy *data, |
250 | | struct Curl_cwriter *writer, int type, |
251 | | const char *buf, size_t nbytes) |
252 | 221k | { |
253 | 221k | struct zlib_writer *zp = (struct zlib_writer *)writer; |
254 | 221k | z_stream *z = &zp->z; /* zlib state structure */ |
255 | | |
256 | 221k | if(!(type & CLIENTWRITE_BODY) || !nbytes) |
257 | 7.55k | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
258 | | |
259 | | /* Set the compressed input when this function is called */ |
260 | 213k | z->next_in = (z_const Bytef *)buf; |
261 | 213k | z->avail_in = (uInt)nbytes; |
262 | | |
263 | 213k | if(zp->zlib_init == ZLIB_EXTERNAL_TRAILER) |
264 | 40 | return process_trailer(data, zp); |
265 | | |
266 | | /* Now uncompress the data */ |
267 | 213k | return inflate_stream(data, writer, type, ZLIB_INFLATING); |
268 | 213k | } |
269 | | |
270 | | static void deflate_do_close(struct Curl_easy *data, |
271 | | struct Curl_cwriter *writer) |
272 | 2.17k | { |
273 | 2.17k | struct zlib_writer *zp = (struct zlib_writer *)writer; |
274 | 2.17k | z_stream *z = &zp->z; /* zlib state structure */ |
275 | | |
276 | 2.17k | exit_zlib(data, z, &zp->zlib_init, CURLE_OK); |
277 | 2.17k | } |
278 | | |
279 | | static const struct Curl_cwtype deflate_encoding = { |
280 | | "deflate", |
281 | | NULL, |
282 | | deflate_do_init, |
283 | | deflate_do_write, |
284 | | deflate_do_close, |
285 | | sizeof(struct zlib_writer) |
286 | | }; |
287 | | |
288 | | /* |
289 | | * Gzip handler. |
290 | | */ |
291 | | |
292 | | static CURLcode gzip_do_init(struct Curl_easy *data, |
293 | | struct Curl_cwriter *writer) |
294 | 543 | { |
295 | 543 | struct zlib_writer *zp = (struct zlib_writer *)writer; |
296 | 543 | z_stream *z = &zp->z; /* zlib state structure */ |
297 | | |
298 | | /* Initialize zlib */ |
299 | 543 | z->zalloc = (alloc_func)zalloc_cb; |
300 | 543 | z->zfree = (free_func)zfree_cb; |
301 | | |
302 | 543 | if(inflateInit2(z, MAX_WBITS + 32) != Z_OK) |
303 | 0 | return process_zlib_error(data, z); |
304 | | |
305 | 543 | zp->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */ |
306 | 543 | return CURLE_OK; |
307 | 543 | } |
308 | | |
309 | | static CURLcode gzip_do_write(struct Curl_easy *data, |
310 | | struct Curl_cwriter *writer, int type, |
311 | | const char *buf, size_t nbytes) |
312 | 3.43k | { |
313 | 3.43k | struct zlib_writer *zp = (struct zlib_writer *)writer; |
314 | 3.43k | z_stream *z = &zp->z; /* zlib state structure */ |
315 | | |
316 | 3.43k | if(!(type & CLIENTWRITE_BODY) || !nbytes) |
317 | 2.66k | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
318 | | |
319 | 770 | if(zp->zlib_init == ZLIB_INIT_GZIP) { |
320 | | /* Let zlib handle the gzip decompression entirely */ |
321 | 769 | z->next_in = (z_const Bytef *)buf; |
322 | 769 | z->avail_in = (uInt)nbytes; |
323 | | /* Now uncompress the data */ |
324 | 769 | return inflate_stream(data, writer, type, ZLIB_INIT_GZIP); |
325 | 769 | } |
326 | | |
327 | | /* We are running with an old version: return error. */ |
328 | 1 | return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR); |
329 | 770 | } |
330 | | |
331 | | static void gzip_do_close(struct Curl_easy *data, |
332 | | struct Curl_cwriter *writer) |
333 | 543 | { |
334 | 543 | struct zlib_writer *zp = (struct zlib_writer *)writer; |
335 | 543 | z_stream *z = &zp->z; /* zlib state structure */ |
336 | | |
337 | 543 | exit_zlib(data, z, &zp->zlib_init, CURLE_OK); |
338 | 543 | } |
339 | | |
340 | | static const struct Curl_cwtype gzip_encoding = { |
341 | | "gzip", |
342 | | "x-gzip", |
343 | | gzip_do_init, |
344 | | gzip_do_write, |
345 | | gzip_do_close, |
346 | | sizeof(struct zlib_writer) |
347 | | }; |
348 | | |
349 | | #endif /* HAVE_LIBZ */ |
350 | | |
351 | | #ifdef HAVE_BROTLI |
352 | | /* Brotli writer. */ |
353 | | struct brotli_writer { |
354 | | struct Curl_cwriter super; |
355 | | char buffer[DECOMPRESS_BUFFER_SIZE]; |
356 | | BrotliDecoderState *br; /* State structure for brotli. */ |
357 | | }; |
358 | | |
359 | | static CURLcode brotli_map_error(BrotliDecoderErrorCode be) |
360 | | { |
361 | | switch(be) { |
362 | | case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE: |
363 | | case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE: |
364 | | case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET: |
365 | | case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME: |
366 | | case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE: |
367 | | case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE: |
368 | | case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT: |
369 | | case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1: |
370 | | case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2: |
371 | | case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM: |
372 | | case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY: |
373 | | case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS: |
374 | | case BROTLI_DECODER_ERROR_FORMAT_PADDING_1: |
375 | | case BROTLI_DECODER_ERROR_FORMAT_PADDING_2: |
376 | | #ifdef BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY /* brotli v1.1.0+ */ |
377 | | case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY: |
378 | | #endif |
379 | | case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET: |
380 | | case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS: |
381 | | return CURLE_BAD_CONTENT_ENCODING; |
382 | | case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES: |
383 | | case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS: |
384 | | case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP: |
385 | | case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1: |
386 | | case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2: |
387 | | case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES: |
388 | | return CURLE_OUT_OF_MEMORY; |
389 | | default: |
390 | | break; |
391 | | } |
392 | | return CURLE_WRITE_ERROR; |
393 | | } |
394 | | |
395 | | static CURLcode brotli_do_init(struct Curl_easy *data, |
396 | | struct Curl_cwriter *writer) |
397 | | { |
398 | | struct brotli_writer *bp = (struct brotli_writer *)writer; |
399 | | (void)data; |
400 | | |
401 | | bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL); |
402 | | return bp->br ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
403 | | } |
404 | | |
405 | | static CURLcode brotli_do_write(struct Curl_easy *data, |
406 | | struct Curl_cwriter *writer, int type, |
407 | | const char *buf, size_t nbytes) |
408 | | { |
409 | | struct brotli_writer *bp = (struct brotli_writer *)writer; |
410 | | const uint8_t *src = (const uint8_t *)buf; |
411 | | uint8_t *dst; |
412 | | size_t dstleft; |
413 | | CURLcode result = CURLE_OK; |
414 | | BrotliDecoderResult r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; |
415 | | |
416 | | if(!(type & CLIENTWRITE_BODY) || !nbytes) |
417 | | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
418 | | |
419 | | if(!bp->br) |
420 | | return CURLE_WRITE_ERROR; /* Stream already ended. */ |
421 | | |
422 | | while((nbytes || r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) && |
423 | | result == CURLE_OK) { |
424 | | dst = (uint8_t *)bp->buffer; |
425 | | dstleft = DECOMPRESS_BUFFER_SIZE; |
426 | | r = BrotliDecoderDecompressStream(bp->br, |
427 | | &nbytes, &src, &dstleft, &dst, NULL); |
428 | | result = Curl_cwriter_write(data, writer->next, type, |
429 | | bp->buffer, DECOMPRESS_BUFFER_SIZE - dstleft); |
430 | | if(result) |
431 | | break; |
432 | | switch(r) { |
433 | | case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: |
434 | | case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: |
435 | | break; |
436 | | case BROTLI_DECODER_RESULT_SUCCESS: |
437 | | BrotliDecoderDestroyInstance(bp->br); |
438 | | bp->br = NULL; |
439 | | if(nbytes) |
440 | | result = CURLE_WRITE_ERROR; |
441 | | break; |
442 | | default: |
443 | | result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br)); |
444 | | break; |
445 | | } |
446 | | } |
447 | | return result; |
448 | | } |
449 | | |
450 | | static void brotli_do_close(struct Curl_easy *data, |
451 | | struct Curl_cwriter *writer) |
452 | | { |
453 | | struct brotli_writer *bp = (struct brotli_writer *)writer; |
454 | | (void)data; |
455 | | |
456 | | if(bp->br) { |
457 | | BrotliDecoderDestroyInstance(bp->br); |
458 | | bp->br = NULL; |
459 | | } |
460 | | } |
461 | | |
462 | | static const struct Curl_cwtype brotli_encoding = { |
463 | | "br", |
464 | | NULL, |
465 | | brotli_do_init, |
466 | | brotli_do_write, |
467 | | brotli_do_close, |
468 | | sizeof(struct brotli_writer) |
469 | | }; |
470 | | #endif |
471 | | |
472 | | #ifdef HAVE_ZSTD |
473 | | /* Zstd writer. */ |
474 | | struct zstd_writer { |
475 | | struct Curl_cwriter super; |
476 | | ZSTD_DStream *zds; /* State structure for zstd. */ |
477 | | char buffer[DECOMPRESS_BUFFER_SIZE]; |
478 | | }; |
479 | | |
480 | | #ifdef ZSTD_STATIC_LINKING_ONLY |
481 | | static void *Curl_zstd_alloc(void *opaque, size_t size) |
482 | | { |
483 | | (void)opaque; |
484 | | return Curl_cmalloc(size); |
485 | | } |
486 | | |
487 | | static void Curl_zstd_free(void *opaque, void *address) |
488 | | { |
489 | | (void)opaque; |
490 | | Curl_cfree(address); |
491 | | } |
492 | | #endif |
493 | | |
494 | | static CURLcode zstd_do_init(struct Curl_easy *data, |
495 | | struct Curl_cwriter *writer) |
496 | 0 | { |
497 | 0 | struct zstd_writer *zp = (struct zstd_writer *)writer; |
498 | |
|
499 | 0 | (void)data; |
500 | |
|
501 | | #ifdef ZSTD_STATIC_LINKING_ONLY |
502 | | zp->zds = ZSTD_createDStream_advanced((ZSTD_customMem) { |
503 | | .customAlloc = Curl_zstd_alloc, |
504 | | .customFree = Curl_zstd_free, |
505 | | .opaque = NULL |
506 | | }); |
507 | | #else |
508 | 0 | zp->zds = ZSTD_createDStream(); |
509 | 0 | #endif |
510 | |
|
511 | 0 | return zp->zds ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
512 | 0 | } |
513 | | |
514 | | static CURLcode zstd_do_write(struct Curl_easy *data, |
515 | | struct Curl_cwriter *writer, int type, |
516 | | const char *buf, size_t nbytes) |
517 | 0 | { |
518 | 0 | CURLcode result = CURLE_OK; |
519 | 0 | struct zstd_writer *zp = (struct zstd_writer *)writer; |
520 | 0 | ZSTD_inBuffer in; |
521 | 0 | ZSTD_outBuffer out; |
522 | 0 | size_t errorCode; |
523 | |
|
524 | 0 | if(!(type & CLIENTWRITE_BODY) || !nbytes) |
525 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
526 | | |
527 | 0 | in.pos = 0; |
528 | 0 | in.src = buf; |
529 | 0 | in.size = nbytes; |
530 | |
|
531 | 0 | for(;;) { |
532 | 0 | out.pos = 0; |
533 | 0 | out.dst = zp->buffer; |
534 | 0 | out.size = DECOMPRESS_BUFFER_SIZE; |
535 | |
|
536 | 0 | errorCode = ZSTD_decompressStream(zp->zds, &out, &in); |
537 | 0 | if(ZSTD_isError(errorCode)) { |
538 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
539 | 0 | } |
540 | 0 | if(out.pos > 0) { |
541 | 0 | result = Curl_cwriter_write(data, writer->next, type, |
542 | 0 | zp->buffer, out.pos); |
543 | 0 | if(result) |
544 | 0 | break; |
545 | 0 | } |
546 | 0 | if((in.pos == nbytes) && (out.pos < out.size)) |
547 | 0 | break; |
548 | 0 | } |
549 | | |
550 | 0 | return result; |
551 | 0 | } |
552 | | |
553 | | static void zstd_do_close(struct Curl_easy *data, |
554 | | struct Curl_cwriter *writer) |
555 | 0 | { |
556 | 0 | struct zstd_writer *zp = (struct zstd_writer *)writer; |
557 | 0 | (void)data; |
558 | |
|
559 | 0 | if(zp->zds) { |
560 | 0 | ZSTD_freeDStream(zp->zds); |
561 | 0 | zp->zds = NULL; |
562 | 0 | } |
563 | 0 | } |
564 | | |
565 | | static const struct Curl_cwtype zstd_encoding = { |
566 | | "zstd", |
567 | | NULL, |
568 | | zstd_do_init, |
569 | | zstd_do_write, |
570 | | zstd_do_close, |
571 | | sizeof(struct zstd_writer) |
572 | | }; |
573 | | #endif |
574 | | |
575 | | /* Identity handler. */ |
576 | | static const struct Curl_cwtype identity_encoding = { |
577 | | "identity", |
578 | | "none", |
579 | | Curl_cwriter_def_init, |
580 | | Curl_cwriter_def_write, |
581 | | Curl_cwriter_def_close, |
582 | | sizeof(struct Curl_cwriter) |
583 | | }; |
584 | | |
585 | | /* supported general content decoders. */ |
586 | | static const struct Curl_cwtype * const general_unencoders[] = { |
587 | | &identity_encoding, |
588 | | #ifdef HAVE_LIBZ |
589 | | &deflate_encoding, |
590 | | &gzip_encoding, |
591 | | #endif |
592 | | #ifdef HAVE_BROTLI |
593 | | &brotli_encoding, |
594 | | #endif |
595 | | #ifdef HAVE_ZSTD |
596 | | &zstd_encoding, |
597 | | #endif |
598 | | NULL |
599 | | }; |
600 | | |
601 | | /* supported content decoders only for transfer encodings */ |
602 | | static const struct Curl_cwtype * const transfer_unencoders[] = { |
603 | | #ifndef CURL_DISABLE_HTTP |
604 | | &Curl_httpchunk_unencoder, |
605 | | #endif |
606 | | NULL |
607 | | }; |
608 | | |
609 | | /* Return the list of comma-separated names of supported encodings. |
610 | | */ |
611 | | char *Curl_get_content_encodings(void) |
612 | 1.64k | { |
613 | 1.64k | struct dynbuf enc; |
614 | 1.64k | const struct Curl_cwtype * const *cep; |
615 | 1.64k | CURLcode result = CURLE_OK; |
616 | 1.64k | curlx_dyn_init(&enc, 255); |
617 | | |
618 | 8.22k | for(cep = general_unencoders; *cep && !result; cep++) { |
619 | 6.57k | const struct Curl_cwtype *ce = *cep; |
620 | 6.57k | if(!curl_strequal(ce->name, CONTENT_ENCODING_DEFAULT)) { |
621 | 4.93k | if(curlx_dyn_len(&enc)) |
622 | 3.28k | result = curlx_dyn_addn(&enc, ", ", 2); |
623 | 4.93k | if(!result) |
624 | 4.93k | result = curlx_dyn_add(&enc, ce->name); |
625 | 4.93k | } |
626 | 6.57k | } |
627 | 1.64k | if(!result) |
628 | 1.64k | return curlx_dyn_ptr(&enc); |
629 | 0 | return NULL; |
630 | 1.64k | } |
631 | | |
632 | | /* Deferred error dummy writer. */ |
633 | | static CURLcode error_do_init(struct Curl_easy *data, |
634 | | struct Curl_cwriter *writer) |
635 | 2.04k | { |
636 | 2.04k | (void)data; |
637 | 2.04k | (void)writer; |
638 | 2.04k | return CURLE_OK; |
639 | 2.04k | } |
640 | | |
641 | | static CURLcode error_do_write(struct Curl_easy *data, |
642 | | struct Curl_cwriter *writer, int type, |
643 | | const char *buf, size_t nbytes) |
644 | 32.6k | { |
645 | 32.6k | (void)writer; |
646 | 32.6k | (void)buf; |
647 | 32.6k | (void)nbytes; |
648 | | |
649 | 32.6k | if(!(type & CLIENTWRITE_BODY) || !nbytes) |
650 | 32.5k | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
651 | 55 | failf(data, "Unrecognized content encoding type"); |
652 | 55 | return CURLE_BAD_CONTENT_ENCODING; |
653 | 32.6k | } |
654 | | |
655 | | static void error_do_close(struct Curl_easy *data, |
656 | | struct Curl_cwriter *writer) |
657 | 2.04k | { |
658 | 2.04k | (void)data; |
659 | 2.04k | (void)writer; |
660 | 2.04k | } |
661 | | |
662 | | static const struct Curl_cwtype error_writer = { |
663 | | "ce-error", |
664 | | NULL, |
665 | | error_do_init, |
666 | | error_do_write, |
667 | | error_do_close, |
668 | | sizeof(struct Curl_cwriter) |
669 | | }; |
670 | | |
671 | | /* Find the content encoding by name. */ |
672 | | static const struct Curl_cwtype *find_unencode_writer(const char *name, |
673 | | size_t len, |
674 | | Curl_cwriter_phase phase) |
675 | 6.36k | { |
676 | 6.36k | const struct Curl_cwtype * const *cep; |
677 | | |
678 | 6.36k | if(phase == CURL_CW_TRANSFER_DECODE) { |
679 | 3.61k | for(cep = transfer_unencoders; *cep; cep++) { |
680 | 2.56k | const struct Curl_cwtype *ce = *cep; |
681 | 2.56k | if((curl_strnequal(name, ce->name, len) && !ce->name[len]) || |
682 | 1.05k | (ce->alias && curl_strnequal(name, ce->alias, len) |
683 | 0 | && !ce->alias[len])) |
684 | 1.50k | return ce; |
685 | 2.56k | } |
686 | 2.56k | } |
687 | | /* look among the general decoders */ |
688 | 16.3k | for(cep = general_unencoders; *cep; cep++) { |
689 | 14.2k | const struct Curl_cwtype *ce = *cep; |
690 | 14.2k | if((curl_strnequal(name, ce->name, len) && !ce->name[len]) || |
691 | 11.5k | (ce->alias && curl_strnequal(name, ce->alias, len) && !ce->alias[len])) |
692 | 2.80k | return ce; |
693 | 14.2k | } |
694 | 2.05k | return NULL; |
695 | 4.85k | } |
696 | | |
697 | | /* Setup the unencoding stack from the Content-Encoding header value. |
698 | | * See RFC 7231 section 3.1.2.2. */ |
699 | | CURLcode Curl_build_unencoding_stack(struct Curl_easy *data, |
700 | | const char *enclist, int is_transfer) |
701 | 8.62k | { |
702 | 8.62k | Curl_cwriter_phase phase = is_transfer ? |
703 | 4.65k | CURL_CW_TRANSFER_DECODE : CURL_CW_CONTENT_DECODE; |
704 | 8.62k | CURLcode result; |
705 | 8.62k | bool has_chunked = FALSE; |
706 | | |
707 | 12.5k | do { |
708 | 12.5k | const char *name; |
709 | 12.5k | size_t namelen; |
710 | 12.5k | bool is_chunked = FALSE; |
711 | | |
712 | | /* Parse a single encoding name. */ |
713 | 25.3k | while(ISBLANK(*enclist) || *enclist == ',') |
714 | 12.7k | enclist++; |
715 | | |
716 | 12.5k | name = enclist; |
717 | | |
718 | 167k | for(namelen = 0; *enclist && *enclist != ','; enclist++) |
719 | 154k | if(*enclist > ' ') |
720 | 51.4k | namelen = enclist - name + 1; |
721 | | |
722 | 12.5k | if(namelen) { |
723 | 7.05k | const struct Curl_cwtype *cwt; |
724 | 7.05k | struct Curl_cwriter *writer; |
725 | | |
726 | 7.05k | CURL_TRC_WRITE(data, "looking for %s decoder: %.*s", |
727 | 7.05k | is_transfer ? "transfer" : "content", (int)namelen, name); |
728 | 7.05k | is_chunked = (is_transfer && (namelen == 7) && |
729 | 2.35k | curl_strnequal(name, "chunked", 7)); |
730 | | /* if we skip the decoding in this phase, do not look further. |
731 | | * Exception is "chunked" transfer-encoding which always must happen */ |
732 | 7.05k | if((is_transfer && !data->set.http_transfer_encoding && !is_chunked) || |
733 | 6.79k | (!is_transfer && data->set.http_ce_skip)) { |
734 | 654 | bool is_identity = curl_strnequal(name, "identity", 8); |
735 | | /* not requested, ignore */ |
736 | 654 | CURL_TRC_WRITE(data, "decoder not requested, ignored: %.*s", |
737 | 654 | (int)namelen, name); |
738 | 654 | if(is_transfer && !data->set.http_te_skip) { |
739 | 65 | if(has_chunked) |
740 | 7 | failf(data, "A Transfer-Encoding (%.*s) was listed after chunked", |
741 | 7 | (int)namelen, name); |
742 | 58 | else if(is_identity) |
743 | 18 | continue; |
744 | 40 | else |
745 | 40 | failf(data, "Unsolicited Transfer-Encoding (%.*s) found", |
746 | 40 | (int)namelen, name); |
747 | 47 | return CURLE_BAD_CONTENT_ENCODING; |
748 | 65 | } |
749 | 589 | return CURLE_OK; |
750 | 654 | } |
751 | | |
752 | 6.39k | if(Curl_cwriter_count(data, phase) + 1 >= MAX_ENCODE_STACK) { |
753 | 34 | failf(data, "Reject response due to more than %u content encodings", |
754 | 34 | MAX_ENCODE_STACK); |
755 | 34 | return CURLE_BAD_CONTENT_ENCODING; |
756 | 34 | } |
757 | | |
758 | 6.36k | cwt = find_unencode_writer(name, namelen, phase); |
759 | 6.36k | if(cwt && is_chunked && Curl_cwriter_get_by_type(data, cwt)) { |
760 | | /* A 'chunked' transfer encoding has already been added. |
761 | | * Ignore duplicates. See #13451. |
762 | | * Also RFC 9112, ch. 6.1: |
763 | | * "A sender MUST NOT apply the chunked transfer coding more than |
764 | | * once to a message body." |
765 | | */ |
766 | 810 | CURL_TRC_WRITE(data, "ignoring duplicate 'chunked' decoder"); |
767 | 810 | return CURLE_OK; |
768 | 810 | } |
769 | | |
770 | 5.55k | if(is_transfer && !is_chunked && |
771 | 1.05k | Curl_cwriter_get_by_name(data, "chunked")) { |
772 | | /* RFC 9112, ch. 6.1: |
773 | | * "If any transfer coding other than chunked is applied to a |
774 | | * response's content, the sender MUST either apply chunked as the |
775 | | * final transfer coding or terminate the message by closing the |
776 | | * connection." |
777 | | * "chunked" must be the last added to be the first in its phase, |
778 | | * reject this. |
779 | | */ |
780 | 4 | failf(data, "Reject response due to 'chunked' not being the last " |
781 | 4 | "Transfer-Encoding"); |
782 | 4 | return CURLE_BAD_CONTENT_ENCODING; |
783 | 4 | } |
784 | | |
785 | 5.54k | if(!cwt) |
786 | 2.04k | cwt = &error_writer; /* Defer error at use. */ |
787 | | |
788 | 5.54k | result = Curl_cwriter_create(&writer, data, cwt, phase); |
789 | 5.54k | CURL_TRC_WRITE(data, "added %s decoder %s -> %d", |
790 | 5.54k | is_transfer ? "transfer" : "content", cwt->name, result); |
791 | 5.54k | if(result) |
792 | 0 | return result; |
793 | | |
794 | 5.54k | result = Curl_cwriter_add(data, writer); |
795 | 5.54k | if(result) { |
796 | 0 | Curl_cwriter_free(data, writer); |
797 | 0 | return result; |
798 | 0 | } |
799 | 5.54k | if(is_chunked) |
800 | 697 | has_chunked = TRUE; |
801 | 5.54k | } |
802 | 12.5k | } while(*enclist); |
803 | | |
804 | 7.14k | return CURLE_OK; |
805 | 8.62k | } |
806 | | |
807 | | #else |
808 | | /* Stubs for builds without HTTP. */ |
809 | | CURLcode Curl_build_unencoding_stack(struct Curl_easy *data, |
810 | | const char *enclist, int is_transfer) |
811 | | { |
812 | | (void)data; |
813 | | (void)enclist; |
814 | | (void)is_transfer; |
815 | | return CURLE_NOT_BUILT_IN; |
816 | | } |
817 | | |
818 | | char *Curl_get_content_encodings(void) |
819 | | { |
820 | | return curlx_strdup(CONTENT_ENCODING_DEFAULT); |
821 | | } |
822 | | |
823 | | #endif /* CURL_DISABLE_HTTP */ |