content_encoding.c 30 KB

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