content_encoding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. } else {
  245. /* we must parse the gzip header ourselves */
  246. if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
  247. return process_zlib_error(conn, z);
  248. }
  249. k->zlib_init = ZLIB_INIT; /* Initial call state */
  250. }
  251. }
  252. if(k->zlib_init == ZLIB_INIT_GZIP) {
  253. /* Let zlib handle the gzip decompression entirely */
  254. z->next_in = (Bytef *)k->str;
  255. z->avail_in = (uInt)nread;
  256. /* Now uncompress the data */
  257. return inflate_stream(conn, k);
  258. }
  259. #ifndef OLD_ZLIB_SUPPORT
  260. /* Support for old zlib versions is compiled away and we are running with
  261. an old version, so return an error. */
  262. return exit_zlib(z, &k->zlib_init, CURLE_FUNCTION_NOT_FOUND);
  263. #else
  264. /* This next mess is to get around the potential case where there isn't
  265. * enough data passed in to skip over the gzip header. If that happens, we
  266. * malloc a block and copy what we have then wait for the next call. If
  267. * there still isn't enough (this is definitely a worst-case scenario), we
  268. * make the block bigger, copy the next part in and keep waiting.
  269. *
  270. * This is only required with zlib versions < 1.2.0.4 as newer versions
  271. * can handle the gzip header themselves.
  272. */
  273. switch (k->zlib_init) {
  274. /* Skip over gzip header? */
  275. case ZLIB_INIT:
  276. {
  277. /* Initial call state */
  278. ssize_t hlen;
  279. switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
  280. case GZIP_OK:
  281. z->next_in = (Bytef *)k->str + hlen;
  282. z->avail_in = (uInt)(nread - hlen);
  283. k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  284. break;
  285. case GZIP_UNDERFLOW:
  286. /* We need more data so we can find the end of the gzip header. It's
  287. * possible that the memory block we malloc here will never be freed if
  288. * the transfer abruptly aborts after this point. Since it's unlikely
  289. * that circumstances will be right for this code path to be followed in
  290. * the first place, and it's even more unlikely for a transfer to fail
  291. * immediately afterwards, it should seldom be a problem.
  292. */
  293. z->avail_in = (uInt)nread;
  294. z->next_in = malloc(z->avail_in);
  295. if(z->next_in == NULL) {
  296. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  297. }
  298. memcpy(z->next_in, k->str, z->avail_in);
  299. k->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
  300. /* We don't have any data to inflate yet */
  301. return CURLE_OK;
  302. case GZIP_BAD:
  303. default:
  304. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  305. }
  306. }
  307. break;
  308. case ZLIB_GZIP_HEADER:
  309. {
  310. /* Need more gzip header data state */
  311. ssize_t hlen;
  312. unsigned char *oldblock = z->next_in;
  313. z->avail_in += nread;
  314. z->next_in = realloc(z->next_in, z->avail_in);
  315. if(z->next_in == NULL) {
  316. free(oldblock);
  317. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  318. }
  319. /* Append the new block of data to the previous one */
  320. memcpy(z->next_in + z->avail_in - nread, k->str, nread);
  321. switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
  322. case GZIP_OK:
  323. /* This is the zlib stream data */
  324. free(z->next_in);
  325. /* Don't point into the malloced block since we just freed it */
  326. z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
  327. z->avail_in = (uInt)(z->avail_in - hlen);
  328. k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  329. break;
  330. case GZIP_UNDERFLOW:
  331. /* We still don't have any data to inflate! */
  332. return CURLE_OK;
  333. case GZIP_BAD:
  334. default:
  335. free(z->next_in);
  336. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  337. }
  338. }
  339. break;
  340. case ZLIB_GZIP_INFLATING:
  341. default:
  342. /* Inflating stream state */
  343. z->next_in = (Bytef *)k->str;
  344. z->avail_in = (uInt)nread;
  345. break;
  346. }
  347. if(z->avail_in == 0) {
  348. /* We don't have any data to inflate; wait until next time */
  349. return CURLE_OK;
  350. }
  351. /* We've parsed the header, now uncompress the data */
  352. return inflate_stream(conn, k);
  353. #endif
  354. }
  355. #endif /* HAVE_LIBZ */