content_encoding.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <[email protected]>, 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. #define DSIZ 0x10000 /* 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 CURLcode
  44. process_zlib_error(struct SessionHandle *data, z_stream *z)
  45. {
  46. if (z->msg)
  47. failf (data, "Error while processing content unencoding.\n%s",
  48. z->msg);
  49. else
  50. failf (data, "Error while processing content unencoding.\n"
  51. "Unknown failure within decompression software.");
  52. return CURLE_BAD_CONTENT_ENCODING;
  53. }
  54. static CURLcode
  55. exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
  56. {
  57. inflateEnd(z);
  58. *zlib_init = 0;
  59. return result;
  60. }
  61. CURLcode
  62. Curl_unencode_deflate_write(struct SessionHandle *data,
  63. struct Curl_transfer_keeper *k,
  64. ssize_t nread)
  65. {
  66. int status; /* zlib status */
  67. CURLcode result = CURLE_OK; /* Curl_client_write status */
  68. char decomp[DSIZ]; /* Put the decompressed data here. */
  69. z_stream *z = &k->z; /* zlib state structure */
  70. /* Initialize zlib? */
  71. if (!k->zlib_init) {
  72. z->zalloc = (alloc_func)Z_NULL;
  73. z->zfree = (free_func)Z_NULL;
  74. z->opaque = 0;
  75. z->next_in = NULL;
  76. z->avail_in = 0;
  77. if (inflateInit(z) != Z_OK)
  78. return process_zlib_error(data, z);
  79. k->zlib_init = 1;
  80. }
  81. /* Set the compressed input when this function is called */
  82. z->next_in = (Bytef *)k->str;
  83. z->avail_in = (uInt)nread;
  84. /* because the buffer size is fixed, iteratively decompress
  85. and transfer to the client via client_write. */
  86. for (;;) {
  87. /* (re)set buffer for decompressed output for every iteration */
  88. z->next_out = (Bytef *)&decomp[0];
  89. z->avail_out = DSIZ;
  90. status = inflate(z, Z_SYNC_FLUSH);
  91. if (status == Z_OK || status == Z_STREAM_END) {
  92. if (DSIZ - z->avail_out) {
  93. result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
  94. DSIZ - z->avail_out);
  95. /* if !CURLE_OK, clean up, return */
  96. if (result)
  97. return exit_zlib(z, &k->zlib_init, result);
  98. }
  99. /* Done?; clean up, return */
  100. if (status == Z_STREAM_END) {
  101. if (inflateEnd(z) == Z_OK)
  102. return exit_zlib(z, &k->zlib_init, result);
  103. else
  104. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  105. }
  106. /* Done with these bytes, exit */
  107. if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
  108. return result;
  109. }
  110. else { /* Error; exit loop, handle below */
  111. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  112. }
  113. }
  114. }
  115. /* Skip over the gzip header */
  116. static enum {
  117. GZIP_OK,
  118. GZIP_BAD,
  119. GZIP_UNDERFLOW
  120. } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
  121. {
  122. int method, flags;
  123. const ssize_t totallen = len;
  124. /* The shortest header is 10 bytes */
  125. if (len < 10)
  126. return GZIP_UNDERFLOW;
  127. if ((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
  128. return GZIP_BAD;
  129. method = data[2];
  130. flags = data[3];
  131. if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
  132. /* Can't handle this compression method or unknown flag */
  133. return GZIP_BAD;
  134. }
  135. /* Skip over time, xflags, OS code and all previous bytes */
  136. len -= 10;
  137. data += 10;
  138. if (flags & EXTRA_FIELD) {
  139. ssize_t extra_len;
  140. if (len < 2)
  141. return GZIP_UNDERFLOW;
  142. extra_len = (data[1] << 8) | data[0];
  143. if (len < (extra_len+2))
  144. return GZIP_UNDERFLOW;
  145. len -= (extra_len + 2);
  146. }
  147. if (flags & ORIG_NAME) {
  148. /* Skip over NUL-terminated file name */
  149. while (len && *data) {
  150. --len;
  151. ++data;
  152. }
  153. if (!len || *data)
  154. return GZIP_UNDERFLOW;
  155. /* Skip over the NUL */
  156. --len;
  157. ++data;
  158. }
  159. if (flags & COMMENT) {
  160. /* Skip over NUL-terminated comment */
  161. while (len && *data) {
  162. --len;
  163. ++data;
  164. }
  165. if (!len || *data)
  166. return GZIP_UNDERFLOW;
  167. /* Skip over the NUL */
  168. --len;
  169. ++data;
  170. }
  171. if (flags & HEAD_CRC) {
  172. if (len < 2)
  173. return GZIP_UNDERFLOW;
  174. len -= 2;
  175. data += 2;
  176. }
  177. *headerlen = totallen - len;
  178. return GZIP_OK;
  179. }
  180. CURLcode
  181. Curl_unencode_gzip_write(struct SessionHandle *data,
  182. struct Curl_transfer_keeper *k,
  183. ssize_t nread)
  184. {
  185. int status; /* zlib status */
  186. CURLcode result = CURLE_OK; /* Curl_client_write status */
  187. char decomp[DSIZ]; /* Put the decompressed data here. */
  188. z_stream *z = &k->z; /* zlib state structure */
  189. /* Initialize zlib? */
  190. if (!k->zlib_init) {
  191. z->zalloc = (alloc_func)Z_NULL;
  192. z->zfree = (free_func)Z_NULL;
  193. z->opaque = 0;
  194. z->next_in = NULL;
  195. z->avail_in = 0;
  196. if (inflateInit2(z, -MAX_WBITS) != Z_OK)
  197. return process_zlib_error(data, z);
  198. k->zlib_init = 1; /* Initial call state */
  199. }
  200. /* This next mess is to get around the potential case where there isn't
  201. * enough data passed in to skip over the gzip header. If that happens, we
  202. * malloc a block and copy what we have then wait for the next call. If
  203. * there still isn't enough (this is definitely a worst-case scenario), we
  204. * make the block bigger, copy the next part in and keep waiting.
  205. */
  206. /* Skip over gzip header? */
  207. if (k->zlib_init == 1) {
  208. /* Initial call state */
  209. ssize_t hlen;
  210. switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
  211. case GZIP_OK:
  212. z->next_in = (Bytef *)k->str + hlen;
  213. z->avail_in = (uInt)(nread - hlen);
  214. k->zlib_init = 3; /* Inflating stream state */
  215. break;
  216. case GZIP_UNDERFLOW:
  217. /* We need more data so we can find the end of the gzip header. It's
  218. * possible that the memory block we malloc here will never be freed if
  219. * the transfer abruptly aborts after this point. Since it's unlikely
  220. * that circumstances will be right for this code path to be followed in
  221. * the first place, and it's even more unlikely for a transfer to fail
  222. * immediately afterwards, it should seldom be a problem.
  223. */
  224. z->avail_in = (uInt)nread;
  225. z->next_in = malloc(z->avail_in);
  226. if (z->next_in == NULL) {
  227. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  228. }
  229. memcpy(z->next_in, k->str, z->avail_in);
  230. k->zlib_init = 2; /* Need more gzip header data state */
  231. /* We don't have any data to inflate yet */
  232. return CURLE_OK;
  233. case GZIP_BAD:
  234. default:
  235. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  236. }
  237. }
  238. else if (k->zlib_init == 2) {
  239. /* Need more gzip header data state */
  240. ssize_t hlen;
  241. unsigned char *oldblock = z->next_in;
  242. z->avail_in += nread;
  243. z->next_in = realloc(z->next_in, z->avail_in);
  244. if (z->next_in == NULL) {
  245. free(oldblock);
  246. return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
  247. }
  248. /* Append the new block of data to the previous one */
  249. memcpy(z->next_in + z->avail_in - nread, k->str, nread);
  250. switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
  251. case GZIP_OK:
  252. /* This is the zlib stream data */
  253. free(z->next_in);
  254. /* Don't point into the malloced block since we just freed it */
  255. z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
  256. z->avail_in = (uInt)(z->avail_in - hlen);
  257. k->zlib_init = 3; /* Inflating stream state */
  258. break;
  259. case GZIP_UNDERFLOW:
  260. /* We still don't have any data to inflate! */
  261. return CURLE_OK;
  262. case GZIP_BAD:
  263. default:
  264. free(z->next_in);
  265. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  266. }
  267. }
  268. else {
  269. /* Inflating stream state */
  270. z->next_in = (Bytef *)k->str;
  271. z->avail_in = (uInt)nread;
  272. }
  273. if (z->avail_in == 0) {
  274. /* We don't have any data to inflate; wait until next time */
  275. return CURLE_OK;
  276. }
  277. /* because the buffer size is fixed, iteratively decompress and transfer to
  278. the client via client_write. */
  279. for (;;) {
  280. /* (re)set buffer for decompressed output for every iteration */
  281. z->next_out = (Bytef *)&decomp[0];
  282. z->avail_out = DSIZ;
  283. status = inflate(z, Z_SYNC_FLUSH);
  284. if (status == Z_OK || status == Z_STREAM_END) {
  285. if(DSIZ - z->avail_out) {
  286. result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
  287. DSIZ - z->avail_out);
  288. /* if !CURLE_OK, clean up, return */
  289. if (result)
  290. return exit_zlib(z, &k->zlib_init, result);
  291. }
  292. /* Done?; clean up, return */
  293. /* We should really check the gzip CRC here */
  294. if (status == Z_STREAM_END) {
  295. if (inflateEnd(z) == Z_OK)
  296. return exit_zlib(z, &k->zlib_init, result);
  297. else
  298. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  299. }
  300. /* Done with these bytes, exit */
  301. if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
  302. return result;
  303. }
  304. else { /* Error; exit loop, handle below */
  305. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  306. }
  307. }
  308. }
  309. #endif /* HAVE_LIBZ */