Coverage Report

Created: 2024-02-25 06:14

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