content_encoding.c 30 KB

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