content_encoding.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 Curl_easy *data, z_stream *z)
  88. {
  89. if(z->msg)
  90. failf(data, "Error while processing content unencoding: %s",
  91. z->msg);
  92. else
  93. failf(data, "Error while processing content unencoding: "
  94. "Unknown failure within decompression software.");
  95. return CURLE_BAD_CONTENT_ENCODING;
  96. }
  97. static CURLcode
  98. exit_zlib(struct Curl_easy *data,
  99. z_stream *z, zlibInitState *zlib_init, CURLcode result)
  100. {
  101. if(*zlib_init == ZLIB_GZIP_HEADER)
  102. Curl_safefree(z->next_in);
  103. if(*zlib_init != ZLIB_UNINIT) {
  104. if(inflateEnd(z) != Z_OK && result == CURLE_OK)
  105. result = process_zlib_error(data, z);
  106. *zlib_init = ZLIB_UNINIT;
  107. }
  108. return result;
  109. }
  110. static CURLcode process_trailer(struct Curl_easy *data,
  111. struct zlib_params *zp)
  112. {
  113. z_stream *z = &zp->z;
  114. CURLcode result = CURLE_OK;
  115. uInt len = z->avail_in < zp->trailerlen? z->avail_in: zp->trailerlen;
  116. /* Consume expected trailer bytes. Terminate stream if exhausted.
  117. Issue an error if unexpected bytes follow. */
  118. zp->trailerlen -= len;
  119. z->avail_in -= len;
  120. z->next_in += len;
  121. if(z->avail_in)
  122. result = CURLE_WRITE_ERROR;
  123. if(result || !zp->trailerlen)
  124. result = exit_zlib(data, z, &zp->zlib_init, result);
  125. else {
  126. /* Only occurs for gzip with zlib < 1.2.0.4 or raw deflate. */
  127. zp->zlib_init = ZLIB_EXTERNAL_TRAILER;
  128. }
  129. return result;
  130. }
  131. static CURLcode inflate_stream(struct Curl_easy *data,
  132. struct contenc_writer *writer,
  133. zlibInitState started)
  134. {
  135. struct zlib_params *zp = (struct zlib_params *) &writer->params;
  136. z_stream *z = &zp->z; /* zlib state structure */
  137. uInt nread = z->avail_in;
  138. Bytef *orig_in = z->next_in;
  139. bool done = FALSE;
  140. CURLcode result = CURLE_OK; /* Curl_client_write status */
  141. char *decomp; /* Put the decompressed data here. */
  142. /* Check state. */
  143. if(zp->zlib_init != ZLIB_INIT &&
  144. zp->zlib_init != ZLIB_INFLATING &&
  145. zp->zlib_init != ZLIB_INIT_GZIP &&
  146. zp->zlib_init != ZLIB_GZIP_INFLATING)
  147. return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR);
  148. /* Dynamically allocate a buffer for decompression because it's uncommonly
  149. large to hold on the stack */
  150. decomp = malloc(DSIZ);
  151. if(!decomp)
  152. return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
  153. /* because the buffer size is fixed, iteratively decompress and transfer to
  154. the client via downstream_write function. */
  155. while(!done) {
  156. int status; /* zlib status */
  157. done = TRUE;
  158. /* (re)set buffer for decompressed output for every iteration */
  159. z->next_out = (Bytef *) decomp;
  160. z->avail_out = DSIZ;
  161. #ifdef Z_BLOCK
  162. /* Z_BLOCK is only available in zlib ver. >= 1.2.0.5 */
  163. status = inflate(z, Z_BLOCK);
  164. #else
  165. /* fallback for zlib ver. < 1.2.0.5 */
  166. status = inflate(z, Z_SYNC_FLUSH);
  167. #endif
  168. /* Flush output data if some. */
  169. if(z->avail_out != DSIZ) {
  170. if(status == Z_OK || status == Z_STREAM_END) {
  171. zp->zlib_init = started; /* Data started. */
  172. result = Curl_unencode_write(data, writer->downstream, decomp,
  173. DSIZ - z->avail_out);
  174. if(result) {
  175. exit_zlib(data, z, &zp->zlib_init, result);
  176. break;
  177. }
  178. }
  179. }
  180. /* Dispatch by inflate() status. */
  181. switch(status) {
  182. case Z_OK:
  183. /* Always loop: there may be unflushed latched data in zlib state. */
  184. done = FALSE;
  185. break;
  186. case Z_BUF_ERROR:
  187. /* No more data to flush: just exit loop. */
  188. break;
  189. case Z_STREAM_END:
  190. result = process_trailer(data, zp);
  191. break;
  192. case Z_DATA_ERROR:
  193. /* some servers seem to not generate zlib headers, so this is an attempt
  194. to fix and continue anyway */
  195. if(zp->zlib_init == ZLIB_INIT) {
  196. /* Do not use inflateReset2(): only available since zlib 1.2.3.4. */
  197. (void) inflateEnd(z); /* don't care about the return code */
  198. if(inflateInit2(z, -MAX_WBITS) == Z_OK) {
  199. z->next_in = orig_in;
  200. z->avail_in = nread;
  201. zp->zlib_init = ZLIB_INFLATING;
  202. zp->trailerlen = 4; /* Tolerate up to 4 unknown trailer bytes. */
  203. done = FALSE;
  204. break;
  205. }
  206. zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
  207. }
  208. result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
  209. break;
  210. default:
  211. result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, 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 Curl_easy *data,
  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(data, z);
  236. zp->zlib_init = ZLIB_INIT;
  237. return CURLE_OK;
  238. }
  239. static CURLcode deflate_unencode_write(struct Curl_easy *data,
  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(data, zp);
  250. /* Now uncompress the data */
  251. return inflate_stream(data, writer, ZLIB_INFLATING);
  252. }
  253. static void deflate_close_writer(struct Curl_easy *data,
  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(data, 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 Curl_easy *data,
  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(data, 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(data, 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 Curl_easy *data,
  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(data, 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(data, 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) {
  412. return exit_zlib(data, 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(data, z, &zp->zlib_init, process_zlib_error(data, 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) {
  431. return exit_zlib(data, 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(data, z, &zp->zlib_init, process_zlib_error(data, 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(data, 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(data, writer, ZLIB_GZIP_INFLATING);
  470. #endif
  471. }
  472. static void gzip_close_writer(struct Curl_easy *data,
  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(data, 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 Curl_easy *data,
  531. struct contenc_writer *writer)
  532. {
  533. struct brotli_params *bp = (struct brotli_params *) &writer->params;
  534. (void) data;
  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 Curl_easy *data,
  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(data, 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 Curl_easy *data,
  585. struct contenc_writer *writer)
  586. {
  587. struct brotli_params *bp = (struct brotli_params *) &writer->params;
  588. (void) data;
  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 Curl_easy *data,
  610. struct contenc_writer *writer)
  611. {
  612. struct zstd_params *zp = (struct zstd_params *)&writer->params;
  613. (void)data;
  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 Curl_easy *data,
  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(data, 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 Curl_easy *data,
  657. struct contenc_writer *writer)
  658. {
  659. struct zstd_params *zp = (struct zstd_params *)&writer->params;
  660. (void)data;
  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 Curl_easy *data,
  681. struct contenc_writer *writer)
  682. {
  683. (void) data;
  684. return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
  685. }
  686. static CURLcode identity_unencode_write(struct Curl_easy *data,
  687. struct contenc_writer *writer,
  688. const char *buf, size_t nbytes)
  689. {
  690. return Curl_unencode_write(data, writer->downstream, buf, nbytes);
  691. }
  692. static void identity_close_writer(struct Curl_easy *data,
  693. struct contenc_writer *writer)
  694. {
  695. (void) data;
  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 Curl_easy *data,
  753. struct contenc_writer *writer)
  754. {
  755. (void) data;
  756. return writer->downstream? CURLE_WRITE_ERROR: CURLE_OK;
  757. }
  758. static CURLcode client_unencode_write(struct Curl_easy *data,
  759. struct contenc_writer *writer,
  760. const char *buf, size_t nbytes)
  761. {
  762. struct SingleRequest *k = &data->req;
  763. (void) writer;
  764. if(!nbytes || k->ignorebody)
  765. return CURLE_OK;
  766. return Curl_client_write(data, CLIENTWRITE_BODY, (char *) buf, nbytes);
  767. }
  768. static void client_close_writer(struct Curl_easy *data,
  769. struct contenc_writer *writer)
  770. {
  771. (void) data;
  772. (void) writer;
  773. }
  774. static const struct content_encoding client_encoding = {
  775. NULL,
  776. NULL,
  777. client_init_writer,
  778. client_unencode_write,
  779. client_close_writer,
  780. 0
  781. };
  782. /* Deferred error dummy writer. */
  783. static CURLcode error_init_writer(struct Curl_easy *data,
  784. struct contenc_writer *writer)
  785. {
  786. (void) data;
  787. return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
  788. }
  789. static CURLcode error_unencode_write(struct Curl_easy *data,
  790. struct contenc_writer *writer,
  791. const char *buf, size_t nbytes)
  792. {
  793. char *all = Curl_all_content_encodings();
  794. (void) writer;
  795. (void) buf;
  796. (void) nbytes;
  797. if(!all)
  798. return CURLE_OUT_OF_MEMORY;
  799. failf(data, "Unrecognized content encoding type. "
  800. "libcurl understands %s content encodings.", all);
  801. free(all);
  802. return CURLE_BAD_CONTENT_ENCODING;
  803. }
  804. static void error_close_writer(struct Curl_easy *data,
  805. struct contenc_writer *writer)
  806. {
  807. (void) data;
  808. (void) writer;
  809. }
  810. static const struct content_encoding error_encoding = {
  811. NULL,
  812. NULL,
  813. error_init_writer,
  814. error_unencode_write,
  815. error_close_writer,
  816. 0
  817. };
  818. /* Create an unencoding writer stage using the given handler. */
  819. static struct contenc_writer *
  820. new_unencoding_writer(struct Curl_easy *data,
  821. const struct content_encoding *handler,
  822. struct contenc_writer *downstream)
  823. {
  824. size_t sz = offsetof(struct contenc_writer, params) + handler->paramsize;
  825. struct contenc_writer *writer = (struct contenc_writer *)calloc(1, sz);
  826. if(writer) {
  827. writer->handler = handler;
  828. writer->downstream = downstream;
  829. if(handler->init_writer(data, writer)) {
  830. free(writer);
  831. writer = NULL;
  832. }
  833. }
  834. return writer;
  835. }
  836. /* Write data using an unencoding writer stack. "nbytes" is not
  837. allowed to be 0. */
  838. CURLcode Curl_unencode_write(struct Curl_easy *data,
  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(data, writer, buf, nbytes);
  845. }
  846. /* Close and clean-up the connection's writer stack. */
  847. void Curl_unencode_cleanup(struct Curl_easy *data)
  848. {
  849. struct SingleRequest *k = &data->req;
  850. struct contenc_writer *writer = k->writer_stack;
  851. while(writer) {
  852. k->writer_stack = writer->downstream;
  853. writer->handler->close_writer(data, writer);
  854. free(writer);
  855. writer = k->writer_stack;
  856. }
  857. }
  858. /* Find the content encoding by name. */
  859. static const struct content_encoding *find_encoding(const char *name,
  860. size_t len)
  861. {
  862. const struct content_encoding * const *cep;
  863. for(cep = encodings; *cep; cep++) {
  864. const struct content_encoding *ce = *cep;
  865. if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
  866. (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
  867. return ce;
  868. }
  869. return NULL;
  870. }
  871. /* Set-up the unencoding stack from the Content-Encoding header value.
  872. * See RFC 7231 section 3.1.2.2. */
  873. CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
  874. const char *enclist, int maybechunked)
  875. {
  876. struct SingleRequest *k = &data->req;
  877. do {
  878. const char *name;
  879. size_t namelen;
  880. /* Parse a single encoding name. */
  881. while(ISSPACE(*enclist) || *enclist == ',')
  882. enclist++;
  883. name = enclist;
  884. for(namelen = 0; *enclist && *enclist != ','; enclist++)
  885. if(!ISSPACE(*enclist))
  886. namelen = enclist - name + 1;
  887. /* Special case: chunked encoding is handled at the reader level. */
  888. if(maybechunked && namelen == 7 && strncasecompare(name, "chunked", 7)) {
  889. k->chunk = TRUE; /* chunks coming our way. */
  890. Curl_httpchunk_init(data); /* init our chunky engine. */
  891. }
  892. else if(namelen) {
  893. const struct content_encoding *encoding = find_encoding(name, namelen);
  894. struct contenc_writer *writer;
  895. if(!k->writer_stack) {
  896. k->writer_stack = new_unencoding_writer(data, &client_encoding, NULL);
  897. if(!k->writer_stack)
  898. return CURLE_OUT_OF_MEMORY;
  899. }
  900. if(!encoding)
  901. encoding = &error_encoding; /* Defer error at stack use. */
  902. /* Stack the unencoding stage. */
  903. writer = new_unencoding_writer(data, encoding, k->writer_stack);
  904. if(!writer)
  905. return CURLE_OUT_OF_MEMORY;
  906. k->writer_stack = writer;
  907. }
  908. } while(*enclist);
  909. return CURLE_OK;
  910. }
  911. #else
  912. /* Stubs for builds without HTTP. */
  913. CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
  914. const char *enclist, int maybechunked)
  915. {
  916. (void) data;
  917. (void) enclist;
  918. (void) maybechunked;
  919. return CURLE_NOT_BUILT_IN;
  920. }
  921. CURLcode Curl_unencode_write(struct Curl_easy *data,
  922. struct contenc_writer *writer,
  923. const char *buf, size_t nbytes)
  924. {
  925. (void) data;
  926. (void) writer;
  927. (void) buf;
  928. (void) nbytes;
  929. return CURLE_NOT_BUILT_IN;
  930. }
  931. void Curl_unencode_cleanup(struct Curl_easy *data)
  932. {
  933. (void) data;
  934. }
  935. char *Curl_all_content_encodings(void)
  936. {
  937. return strdup(CONTENT_ENCODING_DEFAULT); /* Satisfy caller. */
  938. }
  939. #endif /* CURL_DISABLE_HTTP */