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