content_encoding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. ***************************************************************************/
  22. #include "setup.h"
  23. #ifdef HAVE_LIBZ
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "urldata.h"
  27. #include <curl/curl.h>
  28. #include "sendf.h"
  29. #include "content_encoding.h"
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
  33. (doing so will reduce code size slightly). */
  34. #define OLD_ZLIB_SUPPORT 1
  35. #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
  36. #define GZIP_MAGIC_0 0x1f
  37. #define GZIP_MAGIC_1 0x8b
  38. /* gzip flag byte */
  39. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  40. #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
  41. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  42. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  43. #define COMMENT 0x10 /* bit 4 set: file comment present */
  44. #define RESERVED 0xE0 /* bits 5..7: reserved */
  45. static CURLcode
  46. process_zlib_error(struct connectdata *conn, z_stream *z)
  47. {
  48. struct SessionHandle *data = conn->data;
  49. if(z->msg)
  50. failf (data, "Error while processing content unencoding: %s",
  51. z->msg);
  52. else
  53. failf (data, "Error while processing content unencoding: "
  54. "Unknown failure within decompression software.");
  55. return CURLE_BAD_CONTENT_ENCODING;
  56. }
  57. static CURLcode
  58. exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
  59. {
  60. inflateEnd(z);
  61. *zlib_init = ZLIB_UNINIT;
  62. return result;
  63. }
  64. static CURLcode
  65. inflate_stream(struct connectdata *conn,
  66. struct SingleRequest *k)
  67. {
  68. int allow_restart = 1;
  69. z_stream *z = &k->z; /* zlib state structure */
  70. uInt nread = z->avail_in;
  71. Bytef *orig_in = z->next_in;
  72. int status; /* zlib status */
  73. CURLcode result = CURLE_OK; /* Curl_client_write status */
  74. char *decomp; /* Put the decompressed data here. */
  75. /* Dynamically allocate a buffer for decompression because it's uncommonly
  76. large to hold on the stack */
  77. decomp = malloc(DSIZ);
  78. if(decomp == NULL) {
  79. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  80. }
  81. /* because the buffer size is fixed, iteratively decompress and transfer to
  82. the client via client_write. */
  83. for (;;) {
  84. /* (re)set buffer for decompressed output for every iteration */
  85. z->next_out = (Bytef *)decomp;
  86. z->avail_out = DSIZ;
  87. status = inflate(z, Z_SYNC_FLUSH);
  88. if(status == Z_OK || status == Z_STREAM_END) {
  89. allow_restart = 0;
  90. if((DSIZ - z->avail_out) && (!k->ignorebody)) {
  91. result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
  92. DSIZ - z->avail_out);
  93. /* if !CURLE_OK, clean up, return */
  94. if(result) {
  95. free(decomp);
  96. return exit_zlib(z, &k->zlib_init, result);
  97. }
  98. }
  99. /* Done? clean up, return */
  100. if(status == Z_STREAM_END) {
  101. free(decomp);
  102. if(inflateEnd(z) == Z_OK)
  103. return exit_zlib(z, &k->zlib_init, result);
  104. else
  105. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  106. }
  107. /* Done with these bytes, exit */
  108. /* status is always Z_OK at this point! */
  109. if(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. }
  214. if(flags & HEAD_CRC) {
  215. if(len < 2)
  216. return GZIP_UNDERFLOW;
  217. len -= 2;
  218. }
  219. *headerlen = totallen - len;
  220. return GZIP_OK;
  221. }
  222. #endif
  223. CURLcode
  224. Curl_unencode_gzip_write(struct connectdata *conn,
  225. struct SingleRequest *k,
  226. ssize_t nread)
  227. {
  228. z_stream *z = &k->z; /* zlib state structure */
  229. /* Initialize zlib? */
  230. if(k->zlib_init == ZLIB_UNINIT) {
  231. z->zalloc = (alloc_func)Z_NULL;
  232. z->zfree = (free_func)Z_NULL;
  233. z->opaque = 0;
  234. z->next_in = NULL;
  235. z->avail_in = 0;
  236. if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
  237. /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
  238. if(inflateInit2(z, MAX_WBITS+32) != Z_OK) {
  239. return process_zlib_error(conn, z);
  240. }
  241. k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
  242. }
  243. else {
  244. /* we must parse the gzip header ourselves */
  245. if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
  246. return process_zlib_error(conn, z);
  247. }
  248. k->zlib_init = ZLIB_INIT; /* Initial call state */
  249. }
  250. }
  251. if(k->zlib_init == ZLIB_INIT_GZIP) {
  252. /* Let zlib handle the gzip decompression entirely */
  253. z->next_in = (Bytef *)k->str;
  254. z->avail_in = (uInt)nread;
  255. /* Now uncompress the data */
  256. return inflate_stream(conn, k);
  257. }
  258. #ifndef OLD_ZLIB_SUPPORT
  259. /* Support for old zlib versions is compiled away and we are running with
  260. an old version, so return an error. */
  261. return exit_zlib(z, &k->zlib_init, CURLE_FUNCTION_NOT_FOUND);
  262. #else
  263. /* This next mess is to get around the potential case where there isn't
  264. * enough data passed in to skip over the gzip header. If that happens, we
  265. * malloc a block and copy what we have then wait for the next call. If
  266. * there still isn't enough (this is definitely a worst-case scenario), we
  267. * make the block bigger, copy the next part in and keep waiting.
  268. *
  269. * This is only required with zlib versions < 1.2.0.4 as newer versions
  270. * can handle the gzip header themselves.
  271. */
  272. switch (k->zlib_init) {
  273. /* Skip over gzip header? */
  274. case ZLIB_INIT:
  275. {
  276. /* Initial call state */
  277. ssize_t hlen;
  278. switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
  279. case GZIP_OK:
  280. z->next_in = (Bytef *)k->str + hlen;
  281. z->avail_in = (uInt)(nread - hlen);
  282. k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  283. break;
  284. case GZIP_UNDERFLOW:
  285. /* We need more data so we can find the end of the gzip header. It's
  286. * possible that the memory block we malloc here will never be freed if
  287. * the transfer abruptly aborts after this point. Since it's unlikely
  288. * that circumstances will be right for this code path to be followed in
  289. * the first place, and it's even more unlikely for a transfer to fail
  290. * immediately afterwards, it should seldom be a problem.
  291. */
  292. z->avail_in = (uInt)nread;
  293. z->next_in = malloc(z->avail_in);
  294. if(z->next_in == NULL) {
  295. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  296. }
  297. memcpy(z->next_in, k->str, z->avail_in);
  298. k->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
  299. /* We don't have any data to inflate yet */
  300. return CURLE_OK;
  301. case GZIP_BAD:
  302. default:
  303. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  304. }
  305. }
  306. break;
  307. case ZLIB_GZIP_HEADER:
  308. {
  309. /* Need more gzip header data state */
  310. ssize_t hlen;
  311. unsigned char *oldblock = z->next_in;
  312. z->avail_in += (uInt)nread;
  313. z->next_in = realloc(z->next_in, z->avail_in);
  314. if(z->next_in == NULL) {
  315. free(oldblock);
  316. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  317. }
  318. /* Append the new block of data to the previous one */
  319. memcpy(z->next_in + z->avail_in - nread, k->str, nread);
  320. switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
  321. case GZIP_OK:
  322. /* This is the zlib stream data */
  323. free(z->next_in);
  324. /* Don't point into the malloced block since we just freed it */
  325. z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
  326. z->avail_in = (uInt)(z->avail_in - hlen);
  327. k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  328. break;
  329. case GZIP_UNDERFLOW:
  330. /* We still don't have any data to inflate! */
  331. return CURLE_OK;
  332. case GZIP_BAD:
  333. default:
  334. free(z->next_in);
  335. return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
  336. }
  337. }
  338. break;
  339. case ZLIB_GZIP_INFLATING:
  340. default:
  341. /* Inflating stream state */
  342. z->next_in = (Bytef *)k->str;
  343. z->avail_in = (uInt)nread;
  344. break;
  345. }
  346. if(z->avail_in == 0) {
  347. /* We don't have any data to inflate; wait until next time */
  348. return CURLE_OK;
  349. }
  350. /* We've parsed the header, now uncompress the data */
  351. return inflate_stream(conn, k);
  352. #endif
  353. }
  354. void Curl_unencode_cleanup(struct connectdata *conn)
  355. {
  356. struct SessionHandle *data = conn->data;
  357. struct SingleRequest *k = &data->req;
  358. z_stream *z = &k->z;
  359. if(k->zlib_init != ZLIB_UNINIT)
  360. (void) exit_zlib(z, &k->zlib_init, CURLE_OK);
  361. }
  362. #endif /* HAVE_LIBZ */