content_encoding.c 31 KB

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