openssl: guard against OOM on context creation
[platform/upstream/curl.git] / lib / content_encoding.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2020, 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.haxx.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
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
100 zfree_cb(voidpf opaque, voidpf ptr)
101 {
102   (void) opaque;
103   free(ptr);
104 }
105
106 static CURLcode
107 process_zlib_error(struct connectdata *conn, z_stream *z)
108 {
109   struct Curl_easy *data = conn->data;
110   if(z->msg)
111     failf(data, "Error while processing content unencoding: %s",
112           z->msg);
113   else
114     failf(data, "Error while processing content unencoding: "
115           "Unknown failure within decompression software.");
116
117   return CURLE_BAD_CONTENT_ENCODING;
118 }
119
120 static CURLcode
121 exit_zlib(struct connectdata *conn,
122           z_stream *z, zlibInitState *zlib_init, CURLcode result)
123 {
124   if(*zlib_init == ZLIB_GZIP_HEADER)
125     Curl_safefree(z->next_in);
126
127   if(*zlib_init != ZLIB_UNINIT) {
128     if(inflateEnd(z) != Z_OK && result == CURLE_OK)
129       result = process_zlib_error(conn, z);
130     *zlib_init = ZLIB_UNINIT;
131   }
132
133   return result;
134 }
135
136 static CURLcode process_trailer(struct connectdata *conn,
137                                 struct zlib_params *zp)
138 {
139   z_stream *z = &zp->z;
140   CURLcode result = CURLE_OK;
141   uInt len = z->avail_in < zp->trailerlen? z->avail_in: zp->trailerlen;
142
143   /* Consume expected trailer bytes. Terminate stream if exhausted.
144      Issue an error if unexpected bytes follow. */
145
146   zp->trailerlen -= len;
147   z->avail_in -= len;
148   z->next_in += len;
149   if(z->avail_in)
150     result = CURLE_WRITE_ERROR;
151   if(result || !zp->trailerlen)
152     result = exit_zlib(conn, z, &zp->zlib_init, result);
153   else {
154     /* Only occurs for gzip with zlib < 1.2.0.4 or raw deflate. */
155     zp->zlib_init = ZLIB_EXTERNAL_TRAILER;
156   }
157   return result;
158 }
159
160 static CURLcode inflate_stream(struct connectdata *conn,
161                                struct contenc_writer *writer,
162                                zlibInitState started)
163 {
164   struct zlib_params *zp = (struct zlib_params *) &writer->params;
165   z_stream *z = &zp->z;         /* zlib state structure */
166   uInt nread = z->avail_in;
167   Bytef *orig_in = z->next_in;
168   bool done = FALSE;
169   CURLcode result = CURLE_OK;   /* Curl_client_write status */
170   char *decomp;                 /* Put the decompressed data here. */
171
172   /* Check state. */
173   if(zp->zlib_init != ZLIB_INIT &&
174      zp->zlib_init != ZLIB_INFLATING &&
175      zp->zlib_init != ZLIB_INIT_GZIP &&
176      zp->zlib_init != ZLIB_GZIP_INFLATING)
177     return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
178
179   /* Dynamically allocate a buffer for decompression because it's uncommonly
180      large to hold on the stack */
181   decomp = malloc(DSIZ);
182   if(decomp == NULL)
183     return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
184
185   /* because the buffer size is fixed, iteratively decompress and transfer to
186      the client via downstream_write function. */
187   while(!done) {
188     int status;                   /* zlib status */
189     done = TRUE;
190
191     /* (re)set buffer for decompressed output for every iteration */
192     z->next_out = (Bytef *) decomp;
193     z->avail_out = DSIZ;
194
195 #ifdef Z_BLOCK
196     /* Z_BLOCK is only available in zlib ver. >= 1.2.0.5 */
197     status = inflate(z, Z_BLOCK);
198 #else
199     /* fallback for zlib ver. < 1.2.0.5 */
200     status = inflate(z, Z_SYNC_FLUSH);
201 #endif
202
203     /* Flush output data if some. */
204     if(z->avail_out != DSIZ) {
205       if(status == Z_OK || status == Z_STREAM_END) {
206         zp->zlib_init = started;      /* Data started. */
207         result = Curl_unencode_write(conn, writer->downstream, decomp,
208                                      DSIZ - z->avail_out);
209         if(result) {
210           exit_zlib(conn, z, &zp->zlib_init, result);
211           break;
212         }
213       }
214     }
215
216     /* Dispatch by inflate() status. */
217     switch(status) {
218     case Z_OK:
219       /* Always loop: there may be unflushed latched data in zlib state. */
220       done = FALSE;
221       break;
222     case Z_BUF_ERROR:
223       /* No more data to flush: just exit loop. */
224       break;
225     case Z_STREAM_END:
226       result = process_trailer(conn, zp);
227       break;
228     case Z_DATA_ERROR:
229       /* some servers seem to not generate zlib headers, so this is an attempt
230          to fix and continue anyway */
231       if(zp->zlib_init == ZLIB_INIT) {
232         /* Do not use inflateReset2(): only available since zlib 1.2.3.4. */
233         (void) inflateEnd(z);     /* don't care about the return code */
234         if(inflateInit2(z, -MAX_WBITS) == Z_OK) {
235           z->next_in = orig_in;
236           z->avail_in = nread;
237           zp->zlib_init = ZLIB_INFLATING;
238           zp->trailerlen = 4; /* Tolerate up to 4 unknown trailer bytes. */
239           done = FALSE;
240           break;
241         }
242         zp->zlib_init = ZLIB_UNINIT;    /* inflateEnd() already called. */
243       }
244       /* FALLTHROUGH */
245     default:
246       result = exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, 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. */
263 static CURLcode deflate_init_writer(struct connectdata *conn,
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(conn, z);
278   zp->zlib_init = ZLIB_INIT;
279   return CURLE_OK;
280 }
281
282 static CURLcode deflate_unencode_write(struct connectdata *conn,
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(conn, zp);
295
296   /* Now uncompress the data */
297   return inflate_stream(conn, writer, ZLIB_INFLATING);
298 }
299
300 static void deflate_close_writer(struct connectdata *conn,
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(conn, 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. */
320 static CURLcode gzip_init_writer(struct connectdata *conn,
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(conn, 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(conn, 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
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
436 static CURLcode gzip_unencode_write(struct connectdata *conn,
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(conn, 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(conn, 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 == NULL) {
492         return exit_zlib(conn, 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(conn, z, &zp->zlib_init, process_zlib_error(conn, 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 == NULL) {
514       return exit_zlib(conn, 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(conn, z, &zp->zlib_init, process_zlib_error(conn, 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(conn, 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(conn, writer, ZLIB_GZIP_INFLATING);
561 #endif
562 }
563
564 static void gzip_close_writer(struct connectdata *conn,
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(conn, 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
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
629 static CURLcode brotli_init_writer(struct connectdata *conn,
630                                    struct contenc_writer *writer)
631 {
632   struct brotli_params *bp = (struct brotli_params *) &writer->params;
633   (void) conn;
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
642 static CURLcode brotli_unencode_write(struct connectdata *conn,
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(conn, 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
690 static void brotli_close_writer(struct connectdata *conn,
691                                 struct contenc_writer *writer)
692 {
693   struct brotli_params *bp = (struct brotli_params *) &writer->params;
694   (void) conn;
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
720 static CURLcode zstd_init_writer(struct connectdata *conn,
721                                  struct contenc_writer *writer)
722 {
723   struct zstd_params *zp = (struct zstd_params *)&writer->params;
724   (void)conn;
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
734 static CURLcode zstd_unencode_write(struct connectdata *conn,
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(conn, 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
775 static void zstd_close_writer(struct connectdata *conn,
776     struct contenc_writer *writer)
777 {
778   struct zstd_params *zp = (struct zstd_params *)&writer->params;
779   (void)conn;
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. */
803 static CURLcode identity_init_writer(struct connectdata *conn,
804                                      struct contenc_writer *writer)
805 {
806   (void) conn;
807   return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
808 }
809
810 static CURLcode identity_unencode_write(struct connectdata *conn,
811                                         struct contenc_writer *writer,
812                                         const char *buf, size_t nbytes)
813 {
814   return Curl_unencode_write(conn, writer->downstream, buf, nbytes);
815 }
816
817 static void identity_close_writer(struct connectdata *conn,
818                                   struct contenc_writer *writer)
819 {
820   (void) conn;
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. */
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. */
888 static CURLcode client_init_writer(struct connectdata *conn,
889                                    struct contenc_writer *writer)
890 {
891   (void) conn;
892   return writer->downstream? CURLE_WRITE_ERROR: CURLE_OK;
893 }
894
895 static CURLcode client_unencode_write(struct connectdata *conn,
896                                       struct contenc_writer *writer,
897                                       const char *buf, size_t nbytes)
898 {
899   struct Curl_easy *data = conn->data;
900   struct SingleRequest *k = &data->req;
901
902   (void) writer;
903
904   if(!nbytes || k->ignorebody)
905     return CURLE_OK;
906
907   return Curl_client_write(conn, CLIENTWRITE_BODY, (char *) buf, nbytes);
908 }
909
910 static void client_close_writer(struct connectdata *conn,
911                                 struct contenc_writer *writer)
912 {
913   (void) conn;
914   (void) writer;
915 }
916
917 static const struct content_encoding client_encoding = {
918   NULL,
919   NULL,
920   client_init_writer,
921   client_unencode_write,
922   client_close_writer,
923   0
924 };
925
926
927 /* Deferred error dummy writer. */
928 static CURLcode error_init_writer(struct connectdata *conn,
929                                   struct contenc_writer *writer)
930 {
931   (void) conn;
932   return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
933 }
934
935 static CURLcode error_unencode_write(struct connectdata *conn,
936                                      struct contenc_writer *writer,
937                                      const char *buf, size_t nbytes)
938 {
939   char *all = Curl_all_content_encodings();
940
941   (void) writer;
942   (void) buf;
943   (void) nbytes;
944
945   if(!all)
946     return CURLE_OUT_OF_MEMORY;
947   failf(conn->data, "Unrecognized content encoding type. "
948                     "libcurl understands %s content encodings.", all);
949   free(all);
950   return CURLE_BAD_CONTENT_ENCODING;
951 }
952
953 static void error_close_writer(struct connectdata *conn,
954                                struct contenc_writer *writer)
955 {
956   (void) conn;
957   (void) writer;
958 }
959
960 static const struct content_encoding error_encoding = {
961   NULL,
962   NULL,
963   error_init_writer,
964   error_unencode_write,
965   error_close_writer,
966   0
967 };
968
969 /* Create an unencoding writer stage using the given handler. */
970 static struct contenc_writer *
971 new_unencoding_writer(struct connectdata *conn,
972                       const struct content_encoding *handler,
973                       struct contenc_writer *downstream)
974 {
975   size_t sz = offsetof(struct contenc_writer, params) + handler->paramsize;
976   struct contenc_writer *writer = (struct contenc_writer *)calloc(1, sz);
977
978   if(writer) {
979     writer->handler = handler;
980     writer->downstream = downstream;
981     if(handler->init_writer(conn, writer)) {
982       free(writer);
983       writer = NULL;
984     }
985   }
986
987   return writer;
988 }
989
990 /* Write data using an unencoding writer stack. */
991 CURLcode Curl_unencode_write(struct connectdata *conn,
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(conn, writer, buf, nbytes);
998 }
999
1000 /* Close and clean-up the connection's writer stack. */
1001 void Curl_unencode_cleanup(struct connectdata *conn)
1002 {
1003   struct Curl_easy *data = conn->data;
1004   struct SingleRequest *k = &data->req;
1005   struct contenc_writer *writer = k->writer_stack;
1006
1007   while(writer) {
1008     k->writer_stack = writer->downstream;
1009     writer->handler->close_writer(conn, writer);
1010     free(writer);
1011     writer = k->writer_stack;
1012   }
1013 }
1014
1015 /* Find the content encoding by name. */
1016 static const struct content_encoding *find_encoding(const char *name,
1017                                                     size_t len)
1018 {
1019   const struct content_encoding * const *cep;
1020
1021   for(cep = encodings; *cep; cep++) {
1022     const struct content_encoding *ce = *cep;
1023     if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
1024        (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
1025       return ce;
1026   }
1027   return NULL;
1028 }
1029
1030 /* Set-up the unencoding stack from the Content-Encoding header value.
1031  * See RFC 7231 section 3.1.2.2. */
1032 CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
1033                                      const char *enclist, int maybechunked)
1034 {
1035   struct Curl_easy *data = conn->data;
1036   struct SingleRequest *k = &data->req;
1037
1038   do {
1039     const char *name;
1040     size_t namelen;
1041
1042     /* Parse a single encoding name. */
1043     while(ISSPACE(*enclist) || *enclist == ',')
1044       enclist++;
1045
1046     name = enclist;
1047
1048     for(namelen = 0; *enclist && *enclist != ','; enclist++)
1049       if(!ISSPACE(*enclist))
1050         namelen = enclist - name + 1;
1051
1052     /* Special case: chunked encoding is handled at the reader level. */
1053     if(maybechunked && namelen == 7 && strncasecompare(name, "chunked", 7)) {
1054       k->chunk = TRUE;             /* chunks coming our way. */
1055       Curl_httpchunk_init(conn);   /* init our chunky engine. */
1056     }
1057     else if(namelen) {
1058       const struct content_encoding *encoding = find_encoding(name, namelen);
1059       struct contenc_writer *writer;
1060
1061       if(!k->writer_stack) {
1062         k->writer_stack = new_unencoding_writer(conn, &client_encoding, NULL);
1063
1064         if(!k->writer_stack)
1065           return CURLE_OUT_OF_MEMORY;
1066       }
1067
1068       if(!encoding)
1069         encoding = &error_encoding;  /* Defer error at stack use. */
1070
1071       /* Stack the unencoding stage. */
1072       writer = new_unencoding_writer(conn, encoding, k->writer_stack);
1073       if(!writer)
1074         return CURLE_OUT_OF_MEMORY;
1075       k->writer_stack = writer;
1076     }
1077   } while(*enclist);
1078
1079   return CURLE_OK;
1080 }
1081
1082 #else
1083 /* Stubs for builds without HTTP. */
1084 CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
1085                                      const char *enclist, int maybechunked)
1086 {
1087   (void) conn;
1088   (void) enclist;
1089   (void) maybechunked;
1090   return CURLE_NOT_BUILT_IN;
1091 }
1092
1093 CURLcode Curl_unencode_write(struct connectdata *conn,
1094                              struct contenc_writer *writer,
1095                              const char *buf, size_t nbytes)
1096 {
1097   (void) conn;
1098   (void) writer;
1099   (void) buf;
1100   (void) nbytes;
1101   return CURLE_NOT_BUILT_IN;
1102 }
1103
1104 void Curl_unencode_cleanup(struct connectdata *conn)
1105 {
1106   (void) conn;
1107 }
1108
1109 char *Curl_all_content_encodings(void)
1110 {
1111   return strdup(CONTENT_ENCODING_DEFAULT);  /* Satisfy caller. */
1112 }
1113
1114 #endif /* CURL_DISABLE_HTTP */