content_encoding.c 12 KB

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