content_encoding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, 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 http://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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifdef HAVE_LIBZ
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "urldata.h"
  28. #include <curl/curl.h>
  29. #include "sendf.h"
  30. #include "content_encoding.h"
  31. #include "memory.h"
  32. #include "memdebug.h"
  33. /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
  34. (doing so will reduce code size slightly). */
  35. #define OLD_ZLIB_SUPPORT 1
  36. #define DSIZ 0x10000 /* buffer size for decompressed data */
  37. #define GZIP_MAGIC_0 0x1f
  38. #define GZIP_MAGIC_1 0x8b
  39. /* gzip flag byte */
  40. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  41. #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
  42. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  43. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  44. #define COMMENT 0x10 /* bit 4 set: file comment present */
  45. #define RESERVED 0xE0 /* bits 5..7: reserved */
  46. enum zlibState {
  47. ZLIB_UNINIT, /* uninitialized */
  48. ZLIB_INIT, /* initialized */
  49. ZLIB_GZIP_HEADER, /* reading gzip header */
  50. ZLIB_GZIP_INFLATING, /* inflating gzip stream */
  51. ZLIB_INIT_GZIP /* initialized in transparent gzip mode */
  52. };
  53. static CURLcode
  54. process_zlib_error(struct connectdata *conn, z_stream *z)
  55. {
  56. struct SessionHandle *data = conn->data;
  57. if (z->msg)
  58. failf (data, "Error while processing content unencoding: %s",
  59. z->msg);
  60. else
  61. failf (data, "Error while processing content unencoding: "
  62. "Unknown failure within decompression software.");
  63. return CURLE_BAD_CONTENT_ENCODING;
  64. }
  65. static CURLcode
  66. exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
  67. {
  68. inflateEnd(z);
  69. *zlib_init = ZLIB_UNINIT;
  70. return result;
  71. }
  72. static CURLcode
  73. inflate_stream(struct connectdata *conn,
  74. struct Curl_transfer_keeper *k)
  75. {
  76. int allow_restart = 1;
  77. z_stream *z = &k->z; /* zlib state structure */
  78. uInt nread = z->avail_in;
  79. Bytef *orig_in = z->next_in;
  80. int status; /* zlib status */
  81. CURLcode result = CURLE_OK; /* Curl_client_write status */
  82. char *decomp; /* Put the decompressed data here. */
  83. /* Dynamically allocate a buffer for decompression because it's uncommonly
  84. large to hold on the stack */
  85. decomp = (char*)malloc(DSIZ);
  86. if (decomp == NULL) {
  87. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  88. }
  89. /* because the buffer size is fixed, iteratively decompress and transfer to
  90. the client via client_write. */
  91. for (;;) {
  92. /* (re)set buffer for decompressed output for every iteration */
  93. z->next_out = (Bytef *)decomp;
  94. z->avail_out = DSIZ;
  95. status = inflate(z, Z_SYNC_FLUSH);
  96. if (status == Z_OK || status == Z_STREAM_END) {
  97. allow_restart = 0;
  98. if(DSIZ - z->avail_out) {
  99. result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
  100. DSIZ - z->avail_out);
  101. /* if !CURLE_OK, clean up, return */
  102. if (result) {
  103. free(decomp);
  104. return exit_zlib(z, &k->zlib_init, result);
  105. }
  106. }
  107. /* Done? clean up, return */
  108. if (status == Z_STREAM_END) {
  109. free(decomp);
  110. if (inflateEnd(z) == Z_OK)
  111. return exit_zlib(z, &k->zlib_init, result);
  112. else
  113. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  114. }
  115. /* Done with these bytes, exit */
  116. if (status == Z_OK && z->avail_in == 0) {
  117. free(decomp);
  118. return result;
  119. }
  120. }
  121. else if (allow_restart && status == Z_DATA_ERROR) {
  122. /* some servers seem to not generate zlib headers, so this is an attempt
  123. to fix and continue anyway */
  124. inflateReset(z);
  125. if (inflateInit2(z, -MAX_WBITS) != Z_OK) {
  126. return process_zlib_error(conn, z);
  127. }
  128. z->next_in = orig_in;
  129. z->avail_in = nread;
  130. allow_restart = 0;
  131. continue;
  132. }
  133. else { /* Error; exit loop, handle below */
  134. free(decomp);
  135. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  136. }
  137. }
  138. /* Will never get here */
  139. }
  140. CURLcode
  141. Curl_unencode_deflate_write(struct connectdata *conn,
  142. struct Curl_transfer_keeper *k,
  143. ssize_t nread)
  144. {
  145. z_stream *z = &k->z; /* zlib state structure */
  146. /* Initialize zlib? */
  147. if (k->zlib_init == ZLIB_UNINIT) {
  148. z->zalloc = (alloc_func)Z_NULL;
  149. z->zfree = (free_func)Z_NULL;
  150. z->opaque = 0;
  151. z->next_in = NULL;
  152. z->avail_in = 0;
  153. if (inflateInit(z) != Z_OK)
  154. return process_zlib_error(conn, z);
  155. k->zlib_init = ZLIB_INIT;
  156. }
  157. /* Set the compressed input when this function is called */
  158. z->next_in = (Bytef *)k->str;
  159. z->avail_in = (uInt)nread;
  160. /* Now uncompress the data */
  161. return inflate_stream(conn, k);
  162. }
  163. #ifdef OLD_ZLIB_SUPPORT
  164. /* Skip over the gzip header */
  165. static enum {
  166. GZIP_OK,
  167. GZIP_BAD,
  168. GZIP_UNDERFLOW
  169. } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
  170. {
  171. int method, flags;
  172. const ssize_t totallen = len;
  173. /* The shortest header is 10 bytes */
  174. if (len < 10)
  175. return GZIP_UNDERFLOW;
  176. if ((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
  177. return GZIP_BAD;
  178. method = data[2];
  179. flags = data[3];
  180. if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
  181. /* Can't handle this compression method or unknown flag */
  182. return GZIP_BAD;
  183. }
  184. /* Skip over time, xflags, OS code and all previous bytes */
  185. len -= 10;
  186. data += 10;
  187. if (flags & EXTRA_FIELD) {
  188. ssize_t extra_len;
  189. if (len < 2)
  190. return GZIP_UNDERFLOW;
  191. extra_len = (data[1] << 8) | data[0];
  192. if (len < (extra_len+2))
  193. return GZIP_UNDERFLOW;
  194. len -= (extra_len + 2);
  195. data += (extra_len + 2);
  196. }
  197. if (flags & ORIG_NAME) {
  198. /* Skip over NUL-terminated file name */
  199. while (len && *data) {
  200. --len;
  201. ++data;
  202. }
  203. if (!len || *data)
  204. return GZIP_UNDERFLOW;
  205. /* Skip over the NUL */
  206. --len;
  207. ++data;
  208. }
  209. if (flags & COMMENT) {
  210. /* Skip over NUL-terminated comment */
  211. while (len && *data) {
  212. --len;
  213. ++data;
  214. }
  215. if (!len || *data)
  216. return GZIP_UNDERFLOW;
  217. /* Skip over the NUL */
  218. --len;
  219. ++data;
  220. }
  221. if (flags & HEAD_CRC) {
  222. if (len < 2)
  223. return GZIP_UNDERFLOW;
  224. len -= 2;
  225. data += 2;
  226. }
  227. *headerlen = totallen - len;
  228. return GZIP_OK;
  229. }
  230. #endif
  231. CURLcode
  232. Curl_unencode_gzip_write(struct connectdata *conn,
  233. struct Curl_transfer_keeper *k,
  234. ssize_t nread)
  235. {
  236. z_stream *z = &k->z; /* zlib state structure */
  237. /* Initialize zlib? */
  238. if (k->zlib_init == ZLIB_UNINIT) {
  239. z->zalloc = (alloc_func)Z_NULL;
  240. z->zfree = (free_func)Z_NULL;
  241. z->opaque = 0;
  242. z->next_in = NULL;
  243. z->avail_in = 0;
  244. if (strcmp(zlibVersion(), "1.2.0.4") >= 0) {
  245. /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
  246. if (inflateInit2(z, MAX_WBITS+32) != Z_OK) {
  247. return process_zlib_error(conn, z);
  248. }
  249. k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
  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_FUNCTION_NOT_FOUND);
  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. unsigned char *oldblock = z->next_in;
  319. z->avail_in += nread;
  320. z->next_in = realloc(z->next_in, z->avail_in);
  321. if (z->next_in == NULL) {
  322. free(oldblock);
  323. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  324. }
  325. /* Append the new block of data to the previous one */
  326. memcpy(z->next_in + z->avail_in - nread, k->str, nread);
  327. switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
  328. case GZIP_OK:
  329. /* This is the zlib stream data */
  330. free(z->next_in);
  331. /* Don't point into the malloced block since we just freed it */
  332. z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
  333. z->avail_in = (uInt)(z->avail_in - hlen);
  334. k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  335. break;
  336. case GZIP_UNDERFLOW:
  337. /* We still don't have any data to inflate! */
  338. return CURLE_OK;
  339. case GZIP_BAD:
  340. default:
  341. free(z->next_in);
  342. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  343. }
  344. }
  345. break;
  346. case ZLIB_GZIP_INFLATING:
  347. default:
  348. /* Inflating stream state */
  349. z->next_in = (Bytef *)k->str;
  350. z->avail_in = (uInt)nread;
  351. break;
  352. }
  353. if (z->avail_in == 0) {
  354. /* We don't have any data to inflate; wait until next time */
  355. return CURLE_OK;
  356. }
  357. /* We've parsed the header, now uncompress the data */
  358. return inflate_stream(conn, k);
  359. #endif
  360. }
  361. #endif /* HAVE_LIBZ */