content_encoding.c 29 KB

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