content_encoding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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.haxx.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. #ifdef HAVE_LIBZ
  24. #include "urldata.h"
  25. #include <curl/curl.h>
  26. #include "sendf.h"
  27. #include "content_encoding.h"
  28. #include "strdup.h"
  29. #include "curl_memory.h"
  30. #include "memdebug.h"
  31. /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
  32. (doing so will reduce code size slightly). */
  33. #define OLD_ZLIB_SUPPORT 1
  34. #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
  35. #define GZIP_MAGIC_0 0x1f
  36. #define GZIP_MAGIC_1 0x8b
  37. /* gzip flag byte */
  38. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  39. #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
  40. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  41. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  42. #define COMMENT 0x10 /* bit 4 set: file comment present */
  43. #define RESERVED 0xE0 /* bits 5..7: reserved */
  44. static voidpf
  45. zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
  46. {
  47. (void) opaque;
  48. /* not a typo, keep it calloc() */
  49. return (voidpf) calloc(items, size);
  50. }
  51. static void
  52. zfree_cb(voidpf opaque, voidpf ptr)
  53. {
  54. (void) opaque;
  55. free(ptr);
  56. }
  57. static CURLcode
  58. process_zlib_error(struct connectdata *conn, z_stream *z)
  59. {
  60. struct Curl_easy *data = conn->data;
  61. if(z->msg)
  62. failf(data, "Error while processing content unencoding: %s",
  63. z->msg);
  64. else
  65. failf(data, "Error while processing content unencoding: "
  66. "Unknown failure within decompression software.");
  67. return CURLE_BAD_CONTENT_ENCODING;
  68. }
  69. static CURLcode
  70. exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
  71. {
  72. inflateEnd(z);
  73. *zlib_init = ZLIB_UNINIT;
  74. return result;
  75. }
  76. static CURLcode
  77. inflate_stream(struct connectdata *conn,
  78. struct SingleRequest *k)
  79. {
  80. int allow_restart = 1;
  81. z_stream *z = &k->z; /* zlib state structure */
  82. uInt nread = z->avail_in;
  83. Bytef *orig_in = z->next_in;
  84. int status; /* zlib status */
  85. CURLcode result = CURLE_OK; /* Curl_client_write status */
  86. char *decomp; /* Put the decompressed data here. */
  87. /* Dynamically allocate a buffer for decompression because it's uncommonly
  88. large to hold on the stack */
  89. decomp = malloc(DSIZ);
  90. if(decomp == NULL) {
  91. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  92. }
  93. /* because the buffer size is fixed, iteratively decompress and transfer to
  94. the client via client_write. */
  95. for(;;) {
  96. /* (re)set buffer for decompressed output for every iteration */
  97. z->next_out = (Bytef *)decomp;
  98. z->avail_out = DSIZ;
  99. status = inflate(z, Z_SYNC_FLUSH);
  100. if(status == Z_OK || status == Z_STREAM_END) {
  101. allow_restart = 0;
  102. if((DSIZ - z->avail_out) && (!k->ignorebody)) {
  103. result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
  104. DSIZ - z->avail_out);
  105. /* if !CURLE_OK, clean up, return */
  106. if(result) {
  107. free(decomp);
  108. return exit_zlib(z, &k->zlib_init, result);
  109. }
  110. }
  111. /* Done? clean up, return */
  112. if(status == Z_STREAM_END) {
  113. free(decomp);
  114. if(inflateEnd(z) == Z_OK)
  115. return exit_zlib(z, &k->zlib_init, result);
  116. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  117. }
  118. /* Done with these bytes, exit */
  119. /* status is always Z_OK at this point! */
  120. if(z->avail_in == 0) {
  121. free(decomp);
  122. return result;
  123. }
  124. }
  125. else if(allow_restart && status == Z_DATA_ERROR) {
  126. /* some servers seem to not generate zlib headers, so this is an attempt
  127. to fix and continue anyway */
  128. (void) inflateEnd(z); /* don't care about the return code */
  129. if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
  130. free(decomp);
  131. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  132. }
  133. z->next_in = orig_in;
  134. z->avail_in = nread;
  135. allow_restart = 0;
  136. continue;
  137. }
  138. else { /* Error; exit loop, handle below */
  139. free(decomp);
  140. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  141. }
  142. }
  143. /* Will never get here */
  144. }
  145. CURLcode
  146. Curl_unencode_deflate_write(struct connectdata *conn,
  147. struct SingleRequest *k,
  148. ssize_t nread)
  149. {
  150. z_stream *z = &k->z; /* zlib state structure */
  151. /* Initialize zlib? */
  152. if(k->zlib_init == ZLIB_UNINIT) {
  153. memset(z, 0, sizeof(z_stream));
  154. z->zalloc = (alloc_func)zalloc_cb;
  155. z->zfree = (free_func)zfree_cb;
  156. if(inflateInit(z) != Z_OK)
  157. return process_zlib_error(conn, z);
  158. k->zlib_init = ZLIB_INIT;
  159. }
  160. /* Set the compressed input when this function is called */
  161. z->next_in = (Bytef *)k->str;
  162. z->avail_in = (uInt)nread;
  163. /* Now uncompress the data */
  164. return inflate_stream(conn, k);
  165. }
  166. #ifdef OLD_ZLIB_SUPPORT
  167. /* Skip over the gzip header */
  168. static enum {
  169. GZIP_OK,
  170. GZIP_BAD,
  171. GZIP_UNDERFLOW
  172. } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
  173. {
  174. int method, flags;
  175. const ssize_t totallen = len;
  176. /* The shortest header is 10 bytes */
  177. if(len < 10)
  178. return GZIP_UNDERFLOW;
  179. if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
  180. return GZIP_BAD;
  181. method = data[2];
  182. flags = data[3];
  183. if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
  184. /* Can't handle this compression method or unknown flag */
  185. return GZIP_BAD;
  186. }
  187. /* Skip over time, xflags, OS code and all previous bytes */
  188. len -= 10;
  189. data += 10;
  190. if(flags & EXTRA_FIELD) {
  191. ssize_t extra_len;
  192. if(len < 2)
  193. return GZIP_UNDERFLOW;
  194. extra_len = (data[1] << 8) | data[0];
  195. if(len < (extra_len+2))
  196. return GZIP_UNDERFLOW;
  197. len -= (extra_len + 2);
  198. data += (extra_len + 2);
  199. }
  200. if(flags & ORIG_NAME) {
  201. /* Skip over NUL-terminated file name */
  202. while(len && *data) {
  203. --len;
  204. ++data;
  205. }
  206. if(!len || *data)
  207. return GZIP_UNDERFLOW;
  208. /* Skip over the NUL */
  209. --len;
  210. ++data;
  211. }
  212. if(flags & COMMENT) {
  213. /* Skip over NUL-terminated comment */
  214. while(len && *data) {
  215. --len;
  216. ++data;
  217. }
  218. if(!len || *data)
  219. return GZIP_UNDERFLOW;
  220. /* Skip over the NUL */
  221. --len;
  222. }
  223. if(flags & HEAD_CRC) {
  224. if(len < 2)
  225. return GZIP_UNDERFLOW;
  226. len -= 2;
  227. }
  228. *headerlen = totallen - len;
  229. return GZIP_OK;
  230. }
  231. #endif
  232. CURLcode
  233. Curl_unencode_gzip_write(struct connectdata *conn,
  234. struct SingleRequest *k,
  235. ssize_t nread)
  236. {
  237. z_stream *z = &k->z; /* zlib state structure */
  238. /* Initialize zlib? */
  239. if(k->zlib_init == ZLIB_UNINIT) {
  240. memset(z, 0, sizeof(z_stream));
  241. z->zalloc = (alloc_func)zalloc_cb;
  242. z->zfree = (free_func)zfree_cb;
  243. if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
  244. /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
  245. if(inflateInit2(z, MAX_WBITS+32) != Z_OK) {
  246. return process_zlib_error(conn, z);
  247. }
  248. k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
  249. }
  250. else {
  251. /* we must parse the gzip header ourselves */
  252. if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
  253. return process_zlib_error(conn, z);
  254. }
  255. k->zlib_init = ZLIB_INIT; /* Initial call state */
  256. }
  257. }
  258. if(k->zlib_init == ZLIB_INIT_GZIP) {
  259. /* Let zlib handle the gzip decompression entirely */
  260. z->next_in = (Bytef *)k->str;
  261. z->avail_in = (uInt)nread;
  262. /* Now uncompress the data */
  263. return inflate_stream(conn, k);
  264. }
  265. #ifndef OLD_ZLIB_SUPPORT
  266. /* Support for old zlib versions is compiled away and we are running with
  267. an old version, so return an error. */
  268. return exit_zlib(z, &k->zlib_init, CURLE_WRITE_ERROR);
  269. #else
  270. /* This next mess is to get around the potential case where there isn't
  271. * enough data passed in to skip over the gzip header. If that happens, we
  272. * malloc a block and copy what we have then wait for the next call. If
  273. * there still isn't enough (this is definitely a worst-case scenario), we
  274. * make the block bigger, copy the next part in and keep waiting.
  275. *
  276. * This is only required with zlib versions < 1.2.0.4 as newer versions
  277. * can handle the gzip header themselves.
  278. */
  279. switch(k->zlib_init) {
  280. /* Skip over gzip header? */
  281. case ZLIB_INIT:
  282. {
  283. /* Initial call state */
  284. ssize_t hlen;
  285. switch(check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
  286. case GZIP_OK:
  287. z->next_in = (Bytef *)k->str + hlen;
  288. z->avail_in = (uInt)(nread - hlen);
  289. k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  290. break;
  291. case GZIP_UNDERFLOW:
  292. /* We need more data so we can find the end of the gzip header. It's
  293. * possible that the memory block we malloc here will never be freed if
  294. * the transfer abruptly aborts after this point. Since it's unlikely
  295. * that circumstances will be right for this code path to be followed in
  296. * the first place, and it's even more unlikely for a transfer to fail
  297. * immediately afterwards, it should seldom be a problem.
  298. */
  299. z->avail_in = (uInt)nread;
  300. z->next_in = malloc(z->avail_in);
  301. if(z->next_in == NULL) {
  302. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  303. }
  304. memcpy(z->next_in, k->str, z->avail_in);
  305. k->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
  306. /* We don't have any data to inflate yet */
  307. return CURLE_OK;
  308. case GZIP_BAD:
  309. default:
  310. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  311. }
  312. }
  313. break;
  314. case ZLIB_GZIP_HEADER:
  315. {
  316. /* Need more gzip header data state */
  317. ssize_t hlen;
  318. z->avail_in += (uInt)nread;
  319. z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
  320. if(z->next_in == NULL) {
  321. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  322. }
  323. /* Append the new block of data to the previous one */
  324. memcpy(z->next_in + z->avail_in - nread, k->str, nread);
  325. switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
  326. case GZIP_OK:
  327. /* This is the zlib stream data */
  328. free(z->next_in);
  329. /* Don't point into the malloced block since we just freed it */
  330. z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
  331. z->avail_in = (uInt)(z->avail_in - hlen);
  332. k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  333. break;
  334. case GZIP_UNDERFLOW:
  335. /* We still don't have any data to inflate! */
  336. return CURLE_OK;
  337. case GZIP_BAD:
  338. default:
  339. free(z->next_in);
  340. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  341. }
  342. }
  343. break;
  344. case ZLIB_GZIP_INFLATING:
  345. default:
  346. /* Inflating stream state */
  347. z->next_in = (Bytef *)k->str;
  348. z->avail_in = (uInt)nread;
  349. break;
  350. }
  351. if(z->avail_in == 0) {
  352. /* We don't have any data to inflate; wait until next time */
  353. return CURLE_OK;
  354. }
  355. /* We've parsed the header, now uncompress the data */
  356. return inflate_stream(conn, k);
  357. #endif
  358. }
  359. void Curl_unencode_cleanup(struct connectdata *conn)
  360. {
  361. struct Curl_easy *data = conn->data;
  362. struct SingleRequest *k = &data->req;
  363. z_stream *z = &k->z;
  364. if(k->zlib_init != ZLIB_UNINIT)
  365. (void) exit_zlib(z, &k->zlib_init, CURLE_OK);
  366. }
  367. #endif /* HAVE_LIBZ */