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