2
0

content_encoding.c 31 KB

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