content_encoding.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. /* allow no more than 5 "chained" compression steps */
  57. #define MAX_ENCODE_STACK 5
  58. #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
  59. #ifdef HAVE_LIBZ
  60. /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
  61. (doing so will reduce code size slightly). */
  62. #define OLD_ZLIB_SUPPORT 1
  63. #define GZIP_MAGIC_0 0x1f
  64. #define GZIP_MAGIC_1 0x8b
  65. /* gzip flag byte */
  66. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  67. #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
  68. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  69. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  70. #define COMMENT 0x10 /* bit 4 set: file comment present */
  71. #define RESERVED 0xE0 /* bits 5..7: reserved */
  72. typedef enum {
  73. ZLIB_UNINIT, /* uninitialized */
  74. ZLIB_INIT, /* initialized */
  75. ZLIB_INFLATING, /* inflating started. */
  76. ZLIB_EXTERNAL_TRAILER, /* reading external trailer */
  77. ZLIB_GZIP_HEADER, /* reading gzip header */
  78. ZLIB_GZIP_INFLATING, /* inflating gzip stream */
  79. ZLIB_INIT_GZIP /* initialized in transparent gzip mode */
  80. } zlibInitState;
  81. /* Deflate and gzip writer. */
  82. struct zlib_writer {
  83. struct Curl_cwriter super;
  84. zlibInitState zlib_init; /* zlib init state */
  85. uInt trailerlen; /* Remaining trailer byte count. */
  86. z_stream z; /* State structure for zlib. */
  87. };
  88. static voidpf
  89. zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
  90. {
  91. (void) opaque;
  92. /* not a typo, keep it calloc() */
  93. return (voidpf) calloc(items, size);
  94. }
  95. static void
  96. zfree_cb(voidpf opaque, voidpf ptr)
  97. {
  98. (void) opaque;
  99. free(ptr);
  100. }
  101. static CURLcode
  102. process_zlib_error(struct Curl_easy *data, z_stream *z)
  103. {
  104. if(z->msg)
  105. failf(data, "Error while processing content unencoding: %s",
  106. z->msg);
  107. else
  108. failf(data, "Error while processing content unencoding: "
  109. "Unknown failure within decompression software.");
  110. return CURLE_BAD_CONTENT_ENCODING;
  111. }
  112. static CURLcode
  113. exit_zlib(struct Curl_easy *data,
  114. z_stream *z, zlibInitState *zlib_init, CURLcode result)
  115. {
  116. if(*zlib_init == ZLIB_GZIP_HEADER)
  117. Curl_safefree(z->next_in);
  118. if(*zlib_init != ZLIB_UNINIT) {
  119. if(inflateEnd(z) != Z_OK && result == CURLE_OK)
  120. result = process_zlib_error(data, z);
  121. *zlib_init = ZLIB_UNINIT;
  122. }
  123. return result;
  124. }
  125. static CURLcode process_trailer(struct Curl_easy *data,
  126. struct zlib_writer *zp)
  127. {
  128. z_stream *z = &zp->z;
  129. CURLcode result = CURLE_OK;
  130. uInt len = z->avail_in < zp->trailerlen? z->avail_in: zp->trailerlen;
  131. /* Consume expected trailer bytes. Terminate stream if exhausted.
  132. Issue an error if unexpected bytes follow. */
  133. zp->trailerlen -= len;
  134. z->avail_in -= len;
  135. z->next_in += len;
  136. if(z->avail_in)
  137. result = CURLE_WRITE_ERROR;
  138. if(result || !zp->trailerlen)
  139. result = exit_zlib(data, z, &zp->zlib_init, result);
  140. else {
  141. /* Only occurs for gzip with zlib < 1.2.0.4 or raw deflate. */
  142. zp->zlib_init = ZLIB_EXTERNAL_TRAILER;
  143. }
  144. return result;
  145. }
  146. static CURLcode inflate_stream(struct Curl_easy *data,
  147. struct Curl_cwriter *writer, int type,
  148. zlibInitState started)
  149. {
  150. struct zlib_writer *zp = (struct zlib_writer *) writer;
  151. z_stream *z = &zp->z; /* zlib state structure */
  152. uInt nread = z->avail_in;
  153. Bytef *orig_in = z->next_in;
  154. bool done = FALSE;
  155. CURLcode result = CURLE_OK; /* Curl_client_write status */
  156. char *decomp; /* Put the decompressed data here. */
  157. /* Check state. */
  158. if(zp->zlib_init != ZLIB_INIT &&
  159. zp->zlib_init != ZLIB_INFLATING &&
  160. zp->zlib_init != ZLIB_INIT_GZIP &&
  161. zp->zlib_init != ZLIB_GZIP_INFLATING)
  162. return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR);
  163. /* Dynamically allocate a buffer for decompression because it's uncommonly
  164. large to hold on the stack */
  165. decomp = malloc(DSIZ);
  166. if(!decomp)
  167. return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
  168. /* because the buffer size is fixed, iteratively decompress and transfer to
  169. the client via next_write function. */
  170. while(!done) {
  171. int status; /* zlib status */
  172. done = TRUE;
  173. /* (re)set buffer for decompressed output for every iteration */
  174. z->next_out = (Bytef *) decomp;
  175. z->avail_out = DSIZ;
  176. #ifdef Z_BLOCK
  177. /* Z_BLOCK is only available in zlib ver. >= 1.2.0.5 */
  178. status = inflate(z, Z_BLOCK);
  179. #else
  180. /* fallback for zlib ver. < 1.2.0.5 */
  181. status = inflate(z, Z_SYNC_FLUSH);
  182. #endif
  183. /* Flush output data if some. */
  184. if(z->avail_out != DSIZ) {
  185. if(status == Z_OK || status == Z_STREAM_END) {
  186. zp->zlib_init = started; /* Data started. */
  187. result = Curl_cwriter_write(data, writer->next, type, decomp,
  188. DSIZ - z->avail_out);
  189. if(result) {
  190. exit_zlib(data, z, &zp->zlib_init, result);
  191. break;
  192. }
  193. }
  194. }
  195. /* Dispatch by inflate() status. */
  196. switch(status) {
  197. case Z_OK:
  198. /* Always loop: there may be unflushed latched data in zlib state. */
  199. done = FALSE;
  200. break;
  201. case Z_BUF_ERROR:
  202. /* No more data to flush: just exit loop. */
  203. break;
  204. case Z_STREAM_END:
  205. result = process_trailer(data, zp);
  206. break;
  207. case Z_DATA_ERROR:
  208. /* some servers seem to not generate zlib headers, so this is an attempt
  209. to fix and continue anyway */
  210. if(zp->zlib_init == ZLIB_INIT) {
  211. /* Do not use inflateReset2(): only available since zlib 1.2.3.4. */
  212. (void) inflateEnd(z); /* don't care about the return code */
  213. if(inflateInit2(z, -MAX_WBITS) == Z_OK) {
  214. z->next_in = orig_in;
  215. z->avail_in = nread;
  216. zp->zlib_init = ZLIB_INFLATING;
  217. zp->trailerlen = 4; /* Tolerate up to 4 unknown trailer bytes. */
  218. done = FALSE;
  219. break;
  220. }
  221. zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
  222. }
  223. result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
  224. break;
  225. default:
  226. result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
  227. break;
  228. }
  229. }
  230. free(decomp);
  231. /* We're about to leave this call so the `nread' data bytes won't be seen
  232. again. If we are in a state that would wrongly allow restart in raw mode
  233. at the next call, assume output has already started. */
  234. if(nread && zp->zlib_init == ZLIB_INIT)
  235. zp->zlib_init = started; /* Cannot restart anymore. */
  236. return result;
  237. }
  238. /* Deflate handler. */
  239. static CURLcode deflate_do_init(struct Curl_easy *data,
  240. struct Curl_cwriter *writer)
  241. {
  242. struct zlib_writer *zp = (struct zlib_writer *) writer;
  243. z_stream *z = &zp->z; /* zlib state structure */
  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_do_write(struct Curl_easy *data,
  253. struct Curl_cwriter *writer, int type,
  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. if(!(type & CLIENTWRITE_BODY))
  259. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  260. /* Set the compressed input when this function is called */
  261. z->next_in = (Bytef *) buf;
  262. z->avail_in = (uInt) nbytes;
  263. if(zp->zlib_init == ZLIB_EXTERNAL_TRAILER)
  264. return process_trailer(data, zp);
  265. /* Now uncompress the data */
  266. return inflate_stream(data, writer, type, ZLIB_INFLATING);
  267. }
  268. static void deflate_do_close(struct Curl_easy *data,
  269. struct Curl_cwriter *writer)
  270. {
  271. struct zlib_writer *zp = (struct zlib_writer *) writer;
  272. z_stream *z = &zp->z; /* zlib state structure */
  273. exit_zlib(data, z, &zp->zlib_init, CURLE_OK);
  274. }
  275. static const struct Curl_cwtype deflate_encoding = {
  276. "deflate",
  277. NULL,
  278. deflate_do_init,
  279. deflate_do_write,
  280. deflate_do_close,
  281. sizeof(struct zlib_writer)
  282. };
  283. /* Gzip handler. */
  284. static CURLcode gzip_do_init(struct Curl_easy *data,
  285. struct Curl_cwriter *writer)
  286. {
  287. struct zlib_writer *zp = (struct zlib_writer *) writer;
  288. z_stream *z = &zp->z; /* zlib state structure */
  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_do_write(struct Curl_easy *data,
  376. struct Curl_cwriter *writer, int type,
  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(!(type & CLIENTWRITE_BODY))
  382. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  383. if(zp->zlib_init == ZLIB_INIT_GZIP) {
  384. /* Let zlib handle the gzip decompression entirely */
  385. z->next_in = (Bytef *) buf;
  386. z->avail_in = (uInt) nbytes;
  387. /* Now uncompress the data */
  388. return inflate_stream(data, writer, type, ZLIB_INIT_GZIP);
  389. }
  390. #ifndef OLD_ZLIB_SUPPORT
  391. /* Support for old zlib versions is compiled away and we are running with
  392. an old version, so return an error. */
  393. return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR);
  394. #else
  395. /* This next mess is to get around the potential case where there isn't
  396. * enough data passed in to skip over the gzip header. If that happens, we
  397. * malloc a block and copy what we have then wait for the next call. If
  398. * there still isn't enough (this is definitely a worst-case scenario), we
  399. * make the block bigger, copy the next part in and keep waiting.
  400. *
  401. * This is only required with zlib versions < 1.2.0.4 as newer versions
  402. * can handle the gzip header themselves.
  403. */
  404. switch(zp->zlib_init) {
  405. /* Skip over gzip header? */
  406. case ZLIB_INIT:
  407. {
  408. /* Initial call state */
  409. ssize_t hlen;
  410. switch(check_gzip_header((unsigned char *) buf, nbytes, &hlen)) {
  411. case GZIP_OK:
  412. z->next_in = (Bytef *) buf + hlen;
  413. z->avail_in = (uInt) (nbytes - hlen);
  414. zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  415. break;
  416. case GZIP_UNDERFLOW:
  417. /* We need more data so we can find the end of the gzip header. It's
  418. * possible that the memory block we malloc here will never be freed if
  419. * the transfer abruptly aborts after this point. Since it's unlikely
  420. * that circumstances will be right for this code path to be followed in
  421. * the first place, and it's even more unlikely for a transfer to fail
  422. * immediately afterwards, it should seldom be a problem.
  423. */
  424. z->avail_in = (uInt) nbytes;
  425. z->next_in = malloc(z->avail_in);
  426. if(!z->next_in) {
  427. return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
  428. }
  429. memcpy(z->next_in, buf, z->avail_in);
  430. zp->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
  431. /* We don't have any data to inflate yet */
  432. return CURLE_OK;
  433. case GZIP_BAD:
  434. default:
  435. return exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
  436. }
  437. }
  438. break;
  439. case ZLIB_GZIP_HEADER:
  440. {
  441. /* Need more gzip header data state */
  442. ssize_t hlen;
  443. z->avail_in += (uInt) nbytes;
  444. z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
  445. if(!z->next_in) {
  446. return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
  447. }
  448. /* Append the new block of data to the previous one */
  449. memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
  450. switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
  451. case GZIP_OK:
  452. /* This is the zlib stream data */
  453. free(z->next_in);
  454. /* Don't point into the malloced block since we just freed it */
  455. z->next_in = (Bytef *) buf + hlen + nbytes - z->avail_in;
  456. z->avail_in = (uInt) (z->avail_in - hlen);
  457. zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  458. break;
  459. case GZIP_UNDERFLOW:
  460. /* We still don't have any data to inflate! */
  461. return CURLE_OK;
  462. case GZIP_BAD:
  463. default:
  464. return exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
  465. }
  466. }
  467. break;
  468. case ZLIB_EXTERNAL_TRAILER:
  469. z->next_in = (Bytef *) buf;
  470. z->avail_in = (uInt) nbytes;
  471. return process_trailer(data, zp);
  472. case ZLIB_GZIP_INFLATING:
  473. default:
  474. /* Inflating stream state */
  475. z->next_in = (Bytef *) buf;
  476. z->avail_in = (uInt) nbytes;
  477. break;
  478. }
  479. if(z->avail_in == 0) {
  480. /* We don't have any data to inflate; wait until next time */
  481. return CURLE_OK;
  482. }
  483. /* We've parsed the header, now uncompress the data */
  484. return inflate_stream(data, writer, type, ZLIB_GZIP_INFLATING);
  485. #endif
  486. }
  487. static void gzip_do_close(struct Curl_easy *data,
  488. struct Curl_cwriter *writer)
  489. {
  490. struct zlib_writer *zp = (struct zlib_writer *) writer;
  491. z_stream *z = &zp->z; /* zlib state structure */
  492. exit_zlib(data, z, &zp->zlib_init, CURLE_OK);
  493. }
  494. static const struct Curl_cwtype gzip_encoding = {
  495. "gzip",
  496. "x-gzip",
  497. gzip_do_init,
  498. gzip_do_write,
  499. gzip_do_close,
  500. sizeof(struct zlib_writer)
  501. };
  502. #endif /* HAVE_LIBZ */
  503. #ifdef HAVE_BROTLI
  504. /* Brotli writer. */
  505. struct brotli_writer {
  506. struct Curl_cwriter super;
  507. BrotliDecoderState *br; /* State structure for brotli. */
  508. };
  509. static CURLcode brotli_map_error(BrotliDecoderErrorCode be)
  510. {
  511. switch(be) {
  512. case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE:
  513. case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE:
  514. case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET:
  515. case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME:
  516. case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE:
  517. case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE:
  518. case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT:
  519. case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1:
  520. case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2:
  521. case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM:
  522. case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY:
  523. case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS:
  524. case BROTLI_DECODER_ERROR_FORMAT_PADDING_1:
  525. case BROTLI_DECODER_ERROR_FORMAT_PADDING_2:
  526. #ifdef BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY
  527. case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY:
  528. #endif
  529. #ifdef BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET
  530. case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET:
  531. #endif
  532. case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS:
  533. return CURLE_BAD_CONTENT_ENCODING;
  534. case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES:
  535. case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS:
  536. case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP:
  537. case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1:
  538. case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2:
  539. case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES:
  540. return CURLE_OUT_OF_MEMORY;
  541. default:
  542. break;
  543. }
  544. return CURLE_WRITE_ERROR;
  545. }
  546. static CURLcode brotli_do_init(struct Curl_easy *data,
  547. struct Curl_cwriter *writer)
  548. {
  549. struct brotli_writer *bp = (struct brotli_writer *) writer;
  550. (void) data;
  551. bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL);
  552. return bp->br? CURLE_OK: CURLE_OUT_OF_MEMORY;
  553. }
  554. static CURLcode brotli_do_write(struct Curl_easy *data,
  555. struct Curl_cwriter *writer, int type,
  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(!(type & CLIENTWRITE_BODY))
  566. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  567. if(!bp->br)
  568. return CURLE_WRITE_ERROR; /* Stream already ended. */
  569. decomp = malloc(DSIZ);
  570. if(!decomp)
  571. return CURLE_OUT_OF_MEMORY;
  572. while((nbytes || r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) &&
  573. result == CURLE_OK) {
  574. dst = (uint8_t *) decomp;
  575. dstleft = DSIZ;
  576. r = BrotliDecoderDecompressStream(bp->br,
  577. &nbytes, &src, &dstleft, &dst, NULL);
  578. result = Curl_cwriter_write(data, writer->next, type,
  579. decomp, DSIZ - dstleft);
  580. if(result)
  581. break;
  582. switch(r) {
  583. case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
  584. case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
  585. break;
  586. case BROTLI_DECODER_RESULT_SUCCESS:
  587. BrotliDecoderDestroyInstance(bp->br);
  588. bp->br = NULL;
  589. if(nbytes)
  590. result = CURLE_WRITE_ERROR;
  591. break;
  592. default:
  593. result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br));
  594. break;
  595. }
  596. }
  597. free(decomp);
  598. return result;
  599. }
  600. static void brotli_do_close(struct Curl_easy *data,
  601. struct Curl_cwriter *writer)
  602. {
  603. struct brotli_writer *bp = (struct brotli_writer *) writer;
  604. (void) data;
  605. if(bp->br) {
  606. BrotliDecoderDestroyInstance(bp->br);
  607. bp->br = NULL;
  608. }
  609. }
  610. static const struct Curl_cwtype brotli_encoding = {
  611. "br",
  612. NULL,
  613. brotli_do_init,
  614. brotli_do_write,
  615. brotli_do_close,
  616. sizeof(struct brotli_writer)
  617. };
  618. #endif
  619. #ifdef HAVE_ZSTD
  620. /* Zstd writer. */
  621. struct zstd_writer {
  622. struct Curl_cwriter super;
  623. ZSTD_DStream *zds; /* State structure for zstd. */
  624. void *decomp;
  625. };
  626. static CURLcode zstd_do_init(struct Curl_easy *data,
  627. struct Curl_cwriter *writer)
  628. {
  629. struct zstd_writer *zp = (struct zstd_writer *) writer;
  630. (void)data;
  631. zp->zds = ZSTD_createDStream();
  632. zp->decomp = NULL;
  633. return zp->zds ? CURLE_OK : CURLE_OUT_OF_MEMORY;
  634. }
  635. static CURLcode zstd_do_write(struct Curl_easy *data,
  636. struct Curl_cwriter *writer, int type,
  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(!(type & CLIENTWRITE_BODY))
  645. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  646. if(!zp->decomp) {
  647. zp->decomp = malloc(DSIZ);
  648. if(!zp->decomp)
  649. return CURLE_OUT_OF_MEMORY;
  650. }
  651. in.pos = 0;
  652. in.src = buf;
  653. in.size = nbytes;
  654. for(;;) {
  655. out.pos = 0;
  656. out.dst = zp->decomp;
  657. out.size = DSIZ;
  658. errorCode = ZSTD_decompressStream(zp->zds, &out, &in);
  659. if(ZSTD_isError(errorCode)) {
  660. return CURLE_BAD_CONTENT_ENCODING;
  661. }
  662. if(out.pos > 0) {
  663. result = Curl_cwriter_write(data, writer->next, type,
  664. zp->decomp, out.pos);
  665. if(result)
  666. break;
  667. }
  668. if((in.pos == nbytes) && (out.pos < out.size))
  669. break;
  670. }
  671. return result;
  672. }
  673. static void zstd_do_close(struct Curl_easy *data,
  674. struct Curl_cwriter *writer)
  675. {
  676. struct zstd_writer *zp = (struct zstd_writer *) writer;
  677. (void)data;
  678. if(zp->decomp) {
  679. free(zp->decomp);
  680. zp->decomp = NULL;
  681. }
  682. if(zp->zds) {
  683. ZSTD_freeDStream(zp->zds);
  684. zp->zds = NULL;
  685. }
  686. }
  687. static const struct Curl_cwtype zstd_encoding = {
  688. "zstd",
  689. NULL,
  690. zstd_do_init,
  691. zstd_do_write,
  692. zstd_do_close,
  693. sizeof(struct zstd_writer)
  694. };
  695. #endif
  696. /* Identity handler. */
  697. static const struct Curl_cwtype identity_encoding = {
  698. "identity",
  699. "none",
  700. Curl_cwriter_def_init,
  701. Curl_cwriter_def_write,
  702. Curl_cwriter_def_close,
  703. sizeof(struct Curl_cwriter)
  704. };
  705. /* supported content encodings table. */
  706. static const struct Curl_cwtype * 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. /* Provide a list of comma-separated names of supported encodings.
  721. */
  722. void Curl_all_content_encodings(char *buf, size_t blen)
  723. {
  724. size_t len = 0;
  725. const struct Curl_cwtype * const *cep;
  726. const struct Curl_cwtype *ce;
  727. DEBUGASSERT(buf);
  728. DEBUGASSERT(blen);
  729. buf[0] = 0;
  730. for(cep = encodings; *cep; cep++) {
  731. ce = *cep;
  732. if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT))
  733. len += strlen(ce->name) + 2;
  734. }
  735. if(!len) {
  736. if(blen >= sizeof(CONTENT_ENCODING_DEFAULT))
  737. strcpy(buf, CONTENT_ENCODING_DEFAULT);
  738. }
  739. else if(blen > len) {
  740. char *p = buf;
  741. for(cep = encodings; *cep; cep++) {
  742. ce = *cep;
  743. if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
  744. strcpy(p, ce->name);
  745. p += strlen(p);
  746. *p++ = ',';
  747. *p++ = ' ';
  748. }
  749. }
  750. p[-2] = '\0';
  751. }
  752. }
  753. /* Deferred error dummy writer. */
  754. static CURLcode error_do_init(struct Curl_easy *data,
  755. struct Curl_cwriter *writer)
  756. {
  757. (void)data;
  758. (void)writer;
  759. return CURLE_OK;
  760. }
  761. static CURLcode error_do_write(struct Curl_easy *data,
  762. struct Curl_cwriter *writer, int type,
  763. const char *buf, size_t nbytes)
  764. {
  765. char all[256];
  766. (void)Curl_all_content_encodings(all, sizeof(all));
  767. (void) writer;
  768. (void) buf;
  769. (void) nbytes;
  770. if(!(type & CLIENTWRITE_BODY))
  771. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  772. failf(data, "Unrecognized content encoding type. "
  773. "libcurl understands %s content encodings.", all);
  774. return CURLE_BAD_CONTENT_ENCODING;
  775. }
  776. static void error_do_close(struct Curl_easy *data,
  777. struct Curl_cwriter *writer)
  778. {
  779. (void) data;
  780. (void) writer;
  781. }
  782. static const struct Curl_cwtype error_writer = {
  783. "ce-error",
  784. NULL,
  785. error_do_init,
  786. error_do_write,
  787. error_do_close,
  788. sizeof(struct Curl_cwriter)
  789. };
  790. /* Find the content encoding by name. */
  791. static const struct Curl_cwtype *find_encoding(const char *name,
  792. size_t len)
  793. {
  794. const struct Curl_cwtype * const *cep;
  795. for(cep = encodings; *cep; cep++) {
  796. const struct Curl_cwtype *ce = *cep;
  797. if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
  798. (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
  799. return ce;
  800. }
  801. return NULL;
  802. }
  803. /* Set-up the unencoding stack from the Content-Encoding header value.
  804. * See RFC 7231 section 3.1.2.2. */
  805. CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
  806. const char *enclist, int is_transfer)
  807. {
  808. struct SingleRequest *k = &data->req;
  809. Curl_cwriter_phase phase = is_transfer?
  810. CURL_CW_TRANSFER_DECODE:CURL_CW_CONTENT_DECODE;
  811. CURLcode result;
  812. do {
  813. const char *name;
  814. size_t namelen;
  815. /* Parse a single encoding name. */
  816. while(ISBLANK(*enclist) || *enclist == ',')
  817. enclist++;
  818. name = enclist;
  819. for(namelen = 0; *enclist && *enclist != ','; enclist++)
  820. if(!ISSPACE(*enclist))
  821. namelen = enclist - name + 1;
  822. /* Special case: chunked encoding is handled at the reader level. */
  823. if(is_transfer && namelen == 7 && strncasecompare(name, "chunked", 7)) {
  824. k->chunk = TRUE; /* chunks coming our way. */
  825. Curl_httpchunk_init(data); /* init our chunky engine. */
  826. }
  827. else if(namelen) {
  828. const struct Curl_cwtype *cwt;
  829. struct Curl_cwriter *writer;
  830. if((is_transfer && !data->set.http_transfer_encoding) ||
  831. (!is_transfer && data->set.http_ce_skip)) {
  832. /* not requested, ignore */
  833. return CURLE_OK;
  834. }
  835. if(Curl_cwriter_count(data, phase) + 1 >= MAX_ENCODE_STACK) {
  836. failf(data, "Reject response due to more than %u content encodings",
  837. MAX_ENCODE_STACK);
  838. return CURLE_BAD_CONTENT_ENCODING;
  839. }
  840. cwt = find_encoding(name, namelen);
  841. if(!cwt)
  842. cwt = &error_writer; /* Defer error at use. */
  843. result = Curl_cwriter_create(&writer, data, cwt, phase);
  844. if(result)
  845. return result;
  846. result = Curl_cwriter_add(data, writer);
  847. if(result) {
  848. Curl_cwriter_free(data, writer);
  849. return result;
  850. }
  851. }
  852. } while(*enclist);
  853. return CURLE_OK;
  854. }
  855. #else
  856. /* Stubs for builds without HTTP. */
  857. CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
  858. const char *enclist, int is_transfer)
  859. {
  860. (void) data;
  861. (void) enclist;
  862. (void) is_transfer;
  863. return CURLE_NOT_BUILT_IN;
  864. }
  865. void Curl_all_content_encodings(char *buf, size_t blen)
  866. {
  867. DEBUGASSERT(buf);
  868. DEBUGASSERT(blen);
  869. if(blen < sizeof(CONTENT_ENCODING_DEFAULT))
  870. buf[0] = 0;
  871. else
  872. strcpy(buf, CONTENT_ENCODING_DEFAULT);
  873. }
  874. #endif /* CURL_DISABLE_HTTP */