content_encoding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 "urldata.h"
  25. #include <curl/curl.h>
  26. #include "sendf.h"
  27. #include "content_encoding.h"
  28. #include "curl_memory.h"
  29. #include "memdebug.h"
  30. /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
  31. (doing so will reduce code size slightly). */
  32. #define OLD_ZLIB_SUPPORT 1
  33. #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
  34. #define GZIP_MAGIC_0 0x1f
  35. #define GZIP_MAGIC_1 0x8b
  36. /* gzip flag byte */
  37. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  38. #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
  39. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  40. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  41. #define COMMENT 0x10 /* bit 4 set: file comment present */
  42. #define RESERVED 0xE0 /* bits 5..7: reserved */
  43. static voidpf
  44. zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
  45. {
  46. (void) opaque;
  47. /* not a typo, keep it calloc() */
  48. return (voidpf) calloc(items, size);
  49. }
  50. static void
  51. zfree_cb(voidpf opaque, voidpf ptr)
  52. {
  53. (void) opaque;
  54. free(ptr);
  55. }
  56. static CURLcode
  57. process_zlib_error(struct connectdata *conn, z_stream *z)
  58. {
  59. struct SessionHandle *data = conn->data;
  60. if(z->msg)
  61. failf (data, "Error while processing content unencoding: %s",
  62. z->msg);
  63. else
  64. failf (data, "Error while processing content unencoding: "
  65. "Unknown failure within decompression software.");
  66. return CURLE_BAD_CONTENT_ENCODING;
  67. }
  68. static CURLcode
  69. exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
  70. {
  71. inflateEnd(z);
  72. *zlib_init = ZLIB_UNINIT;
  73. return result;
  74. }
  75. static CURLcode
  76. inflate_stream(struct connectdata *conn,
  77. struct SingleRequest *k)
  78. {
  79. int allow_restart = 1;
  80. z_stream *z = &k->z; /* zlib state structure */
  81. uInt nread = z->avail_in;
  82. Bytef *orig_in = z->next_in;
  83. int status; /* zlib status */
  84. CURLcode result = CURLE_OK; /* Curl_client_write status */
  85. char *decomp; /* Put the decompressed data here. */
  86. /* Dynamically allocate a buffer for decompression because it's uncommonly
  87. large to hold on the stack */
  88. decomp = malloc(DSIZ);
  89. if(decomp == NULL) {
  90. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  91. }
  92. /* because the buffer size is fixed, iteratively decompress and transfer to
  93. the client via client_write. */
  94. for(;;) {
  95. /* (re)set buffer for decompressed output for every iteration */
  96. z->next_out = (Bytef *)decomp;
  97. z->avail_out = DSIZ;
  98. status = inflate(z, Z_SYNC_FLUSH);
  99. if(status == Z_OK || status == Z_STREAM_END) {
  100. allow_restart = 0;
  101. if((DSIZ - z->avail_out) && (!k->ignorebody)) {
  102. result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
  103. DSIZ - z->avail_out);
  104. /* if !CURLE_OK, clean up, return */
  105. if(result) {
  106. free(decomp);
  107. return exit_zlib(z, &k->zlib_init, result);
  108. }
  109. }
  110. /* Done? clean up, return */
  111. if(status == Z_STREAM_END) {
  112. free(decomp);
  113. if(inflateEnd(z) == Z_OK)
  114. return exit_zlib(z, &k->zlib_init, result);
  115. else
  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_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 += (uInt)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. void Curl_unencode_cleanup(struct connectdata *conn)
  362. {
  363. struct SessionHandle *data = conn->data;
  364. struct SingleRequest *k = &data->req;
  365. z_stream *z = &k->z;
  366. if(k->zlib_init != ZLIB_UNINIT)
  367. (void) exit_zlib(z, &k->zlib_init, CURLE_OK);
  368. }
  369. #endif /* HAVE_LIBZ */