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