1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
31 #include <curl/curl.h>
33 #include "content_encoding.h"
34 #include "curl_memory.h"
38 /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
39 (doing so will reduce code size slightly). */
40 #define OLD_ZLIB_SUPPORT 1
42 #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
44 #define GZIP_MAGIC_0 0x1f
45 #define GZIP_MAGIC_1 0x8b
48 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
49 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
50 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
51 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
52 #define COMMENT 0x10 /* bit 4 set: file comment present */
53 #define RESERVED 0xE0 /* bits 5..7: reserved */
56 process_zlib_error(struct connectdata *conn, z_stream *z)
58 struct SessionHandle *data = conn->data;
60 failf (data, "Error while processing content unencoding: %s",
63 failf (data, "Error while processing content unencoding: "
64 "Unknown failure within decompression software.");
66 return CURLE_BAD_CONTENT_ENCODING;
70 exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
73 *zlib_init = ZLIB_UNINIT;
78 inflate_stream(struct connectdata *conn,
79 struct SingleRequest *k)
81 int allow_restart = 1;
82 z_stream *z = &k->z; /* zlib state structure */
83 uInt nread = z->avail_in;
84 Bytef *orig_in = z->next_in;
85 int status; /* zlib status */
86 CURLcode result = CURLE_OK; /* Curl_client_write status */
87 char *decomp; /* Put the decompressed data here. */
89 /* Dynamically allocate a buffer for decompression because it's uncommonly
90 large to hold on the stack */
91 decomp = malloc(DSIZ);
93 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
96 /* because the buffer size is fixed, iteratively decompress and transfer to
97 the client via client_write. */
99 /* (re)set buffer for decompressed output for every iteration */
100 z->next_out = (Bytef *)decomp;
103 status = inflate(z, Z_SYNC_FLUSH);
104 if(status == Z_OK || status == Z_STREAM_END) {
106 if((DSIZ - z->avail_out) && (!k->ignorebody)) {
107 result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
108 DSIZ - z->avail_out);
109 /* if !CURLE_OK, clean up, return */
112 return exit_zlib(z, &k->zlib_init, result);
116 /* Done? clean up, return */
117 if(status == Z_STREAM_END) {
119 if(inflateEnd(z) == Z_OK)
120 return exit_zlib(z, &k->zlib_init, result);
122 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
125 /* Done with these bytes, exit */
127 /* status is always Z_OK at this point! */
128 if(z->avail_in == 0) {
133 else if(allow_restart && status == Z_DATA_ERROR) {
134 /* some servers seem to not generate zlib headers, so this is an attempt
135 to fix and continue anyway */
137 (void) inflateEnd(z); /* don't care about the return code */
138 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
140 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
142 z->next_in = orig_in;
147 else { /* Error; exit loop, handle below */
149 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
152 /* Will never get here */
156 Curl_unencode_deflate_write(struct connectdata *conn,
157 struct SingleRequest *k,
160 z_stream *z = &k->z; /* zlib state structure */
162 /* Initialize zlib? */
163 if(k->zlib_init == ZLIB_UNINIT) {
164 z->zalloc = (alloc_func)Z_NULL;
165 z->zfree = (free_func)Z_NULL;
169 if(inflateInit(z) != Z_OK)
170 return process_zlib_error(conn, z);
171 k->zlib_init = ZLIB_INIT;
174 /* Set the compressed input when this function is called */
175 z->next_in = (Bytef *)k->str;
176 z->avail_in = (uInt)nread;
178 /* Now uncompress the data */
179 return inflate_stream(conn, k);
182 #ifdef OLD_ZLIB_SUPPORT
183 /* Skip over the gzip header */
188 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
191 const ssize_t totallen = len;
193 /* The shortest header is 10 bytes */
195 return GZIP_UNDERFLOW;
197 if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
203 if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
204 /* Can't handle this compression method or unknown flag */
208 /* Skip over time, xflags, OS code and all previous bytes */
212 if(flags & EXTRA_FIELD) {
216 return GZIP_UNDERFLOW;
218 extra_len = (data[1] << 8) | data[0];
220 if(len < (extra_len+2))
221 return GZIP_UNDERFLOW;
223 len -= (extra_len + 2);
224 data += (extra_len + 2);
227 if(flags & ORIG_NAME) {
228 /* Skip over NUL-terminated file name */
229 while(len && *data) {
234 return GZIP_UNDERFLOW;
236 /* Skip over the NUL */
241 if(flags & COMMENT) {
242 /* Skip over NUL-terminated comment */
243 while(len && *data) {
248 return GZIP_UNDERFLOW;
250 /* Skip over the NUL */
254 if(flags & HEAD_CRC) {
256 return GZIP_UNDERFLOW;
261 *headerlen = totallen - len;
267 Curl_unencode_gzip_write(struct connectdata *conn,
268 struct SingleRequest *k,
271 z_stream *z = &k->z; /* zlib state structure */
273 /* Initialize zlib? */
274 if(k->zlib_init == ZLIB_UNINIT) {
275 z->zalloc = (alloc_func)Z_NULL;
276 z->zfree = (free_func)Z_NULL;
281 if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
282 /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
283 if(inflateInit2(z, MAX_WBITS+32) != Z_OK) {
284 return process_zlib_error(conn, z);
286 k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
289 /* we must parse the gzip header ourselves */
290 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
291 return process_zlib_error(conn, z);
293 k->zlib_init = ZLIB_INIT; /* Initial call state */
297 if(k->zlib_init == ZLIB_INIT_GZIP) {
298 /* Let zlib handle the gzip decompression entirely */
299 z->next_in = (Bytef *)k->str;
300 z->avail_in = (uInt)nread;
301 /* Now uncompress the data */
302 return inflate_stream(conn, k);
305 #ifndef OLD_ZLIB_SUPPORT
306 /* Support for old zlib versions is compiled away and we are running with
307 an old version, so return an error. */
308 return exit_zlib(z, &k->zlib_init, CURLE_FUNCTION_NOT_FOUND);
311 /* This next mess is to get around the potential case where there isn't
312 * enough data passed in to skip over the gzip header. If that happens, we
313 * malloc a block and copy what we have then wait for the next call. If
314 * there still isn't enough (this is definitely a worst-case scenario), we
315 * make the block bigger, copy the next part in and keep waiting.
317 * This is only required with zlib versions < 1.2.0.4 as newer versions
318 * can handle the gzip header themselves.
321 switch (k->zlib_init) {
322 /* Skip over gzip header? */
325 /* Initial call state */
328 switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
330 z->next_in = (Bytef *)k->str + hlen;
331 z->avail_in = (uInt)(nread - hlen);
332 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
336 /* We need more data so we can find the end of the gzip header. It's
337 * possible that the memory block we malloc here will never be freed if
338 * the transfer abruptly aborts after this point. Since it's unlikely
339 * that circumstances will be right for this code path to be followed in
340 * the first place, and it's even more unlikely for a transfer to fail
341 * immediately afterwards, it should seldom be a problem.
343 z->avail_in = (uInt)nread;
344 z->next_in = malloc(z->avail_in);
345 if(z->next_in == NULL) {
346 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
348 memcpy(z->next_in, k->str, z->avail_in);
349 k->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
350 /* We don't have any data to inflate yet */
355 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
361 case ZLIB_GZIP_HEADER:
363 /* Need more gzip header data state */
365 unsigned char *oldblock = z->next_in;
367 z->avail_in += (uInt)nread;
368 z->next_in = realloc(z->next_in, z->avail_in);
369 if(z->next_in == NULL) {
371 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
373 /* Append the new block of data to the previous one */
374 memcpy(z->next_in + z->avail_in - nread, k->str, nread);
376 switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
378 /* This is the zlib stream data */
380 /* Don't point into the malloced block since we just freed it */
381 z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
382 z->avail_in = (uInt)(z->avail_in - hlen);
383 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
387 /* We still don't have any data to inflate! */
393 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
399 case ZLIB_GZIP_INFLATING:
401 /* Inflating stream state */
402 z->next_in = (Bytef *)k->str;
403 z->avail_in = (uInt)nread;
407 if(z->avail_in == 0) {
408 /* We don't have any data to inflate; wait until next time */
412 /* We've parsed the header, now uncompress the data */
413 return inflate_stream(conn, k);
417 void Curl_unencode_cleanup(struct connectdata *conn)
419 struct SessionHandle *data = conn->data;
420 struct SingleRequest *k = &data->req;
422 if(k->zlib_init != ZLIB_UNINIT)
423 (void) exit_zlib(z, &k->zlib_init, CURLE_OK);
426 #endif /* HAVE_LIBZ */