content_encoding.c 32 KB

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