content_encoding.c 11 KB

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