1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
23 #include "curl_setup.h"
28 #include <curl/curl.h>
30 #include "content_encoding.h"
31 #include "curl_memory.h"
35 /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
36 (doing so will reduce code size slightly). */
37 #define OLD_ZLIB_SUPPORT 1
39 #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
41 #define GZIP_MAGIC_0 0x1f
42 #define GZIP_MAGIC_1 0x8b
45 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
46 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
47 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
48 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
49 #define COMMENT 0x10 /* bit 4 set: file comment present */
50 #define RESERVED 0xE0 /* bits 5..7: reserved */
53 zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
56 /* not a typo, keep it calloc() */
57 return (voidpf) calloc(items, size);
61 zfree_cb(voidpf opaque, voidpf ptr)
68 process_zlib_error(struct connectdata *conn, z_stream *z)
70 struct SessionHandle *data = conn->data;
72 failf (data, "Error while processing content unencoding: %s",
75 failf (data, "Error while processing content unencoding: "
76 "Unknown failure within decompression software.");
78 return CURLE_BAD_CONTENT_ENCODING;
82 exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
85 *zlib_init = ZLIB_UNINIT;
90 inflate_stream(struct connectdata *conn,
91 struct SingleRequest *k)
93 int allow_restart = 1;
94 z_stream *z = &k->z; /* zlib state structure */
95 uInt nread = z->avail_in;
96 Bytef *orig_in = z->next_in;
97 int status; /* zlib status */
98 CURLcode result = CURLE_OK; /* Curl_client_write status */
99 char *decomp; /* Put the decompressed data here. */
101 /* Dynamically allocate a buffer for decompression because it's uncommonly
102 large to hold on the stack */
103 decomp = malloc(DSIZ);
105 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
108 /* because the buffer size is fixed, iteratively decompress and transfer to
109 the client via client_write. */
111 /* (re)set buffer for decompressed output for every iteration */
112 z->next_out = (Bytef *)decomp;
115 status = inflate(z, Z_SYNC_FLUSH);
116 if(status == Z_OK || status == Z_STREAM_END) {
118 if((DSIZ - z->avail_out) && (!k->ignorebody)) {
119 result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
120 DSIZ - z->avail_out);
121 /* if !CURLE_OK, clean up, return */
124 return exit_zlib(z, &k->zlib_init, result);
128 /* Done? clean up, return */
129 if(status == Z_STREAM_END) {
131 if(inflateEnd(z) == Z_OK)
132 return exit_zlib(z, &k->zlib_init, result);
134 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
137 /* Done with these bytes, exit */
139 /* status is always Z_OK at this point! */
140 if(z->avail_in == 0) {
145 else if(allow_restart && status == Z_DATA_ERROR) {
146 /* some servers seem to not generate zlib headers, so this is an attempt
147 to fix and continue anyway */
149 (void) inflateEnd(z); /* don't care about the return code */
150 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
152 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
154 z->next_in = orig_in;
159 else { /* Error; exit loop, handle below */
161 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
164 /* Will never get here */
168 Curl_unencode_deflate_write(struct connectdata *conn,
169 struct SingleRequest *k,
172 z_stream *z = &k->z; /* zlib state structure */
174 /* Initialize zlib? */
175 if(k->zlib_init == ZLIB_UNINIT) {
176 memset(z, 0, sizeof(z_stream));
177 z->zalloc = (alloc_func)zalloc_cb;
178 z->zfree = (free_func)zfree_cb;
180 if(inflateInit(z) != Z_OK)
181 return process_zlib_error(conn, z);
182 k->zlib_init = ZLIB_INIT;
185 /* Set the compressed input when this function is called */
186 z->next_in = (Bytef *)k->str;
187 z->avail_in = (uInt)nread;
189 /* Now uncompress the data */
190 return inflate_stream(conn, k);
193 #ifdef OLD_ZLIB_SUPPORT
194 /* Skip over the gzip header */
199 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
202 const ssize_t totallen = len;
204 /* The shortest header is 10 bytes */
206 return GZIP_UNDERFLOW;
208 if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
214 if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
215 /* Can't handle this compression method or unknown flag */
219 /* Skip over time, xflags, OS code and all previous bytes */
223 if(flags & EXTRA_FIELD) {
227 return GZIP_UNDERFLOW;
229 extra_len = (data[1] << 8) | data[0];
231 if(len < (extra_len+2))
232 return GZIP_UNDERFLOW;
234 len -= (extra_len + 2);
235 data += (extra_len + 2);
238 if(flags & ORIG_NAME) {
239 /* Skip over NUL-terminated file name */
240 while(len && *data) {
245 return GZIP_UNDERFLOW;
247 /* Skip over the NUL */
252 if(flags & COMMENT) {
253 /* Skip over NUL-terminated comment */
254 while(len && *data) {
259 return GZIP_UNDERFLOW;
261 /* Skip over the NUL */
265 if(flags & HEAD_CRC) {
267 return GZIP_UNDERFLOW;
272 *headerlen = totallen - len;
278 Curl_unencode_gzip_write(struct connectdata *conn,
279 struct SingleRequest *k,
282 z_stream *z = &k->z; /* zlib state structure */
284 /* Initialize zlib? */
285 if(k->zlib_init == ZLIB_UNINIT) {
286 memset(z, 0, sizeof(z_stream));
287 z->zalloc = (alloc_func)zalloc_cb;
288 z->zfree = (free_func)zfree_cb;
290 if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
291 /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
292 if(inflateInit2(z, MAX_WBITS+32) != Z_OK) {
293 return process_zlib_error(conn, z);
295 k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
298 /* we must parse the gzip header ourselves */
299 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
300 return process_zlib_error(conn, z);
302 k->zlib_init = ZLIB_INIT; /* Initial call state */
306 if(k->zlib_init == ZLIB_INIT_GZIP) {
307 /* Let zlib handle the gzip decompression entirely */
308 z->next_in = (Bytef *)k->str;
309 z->avail_in = (uInt)nread;
310 /* Now uncompress the data */
311 return inflate_stream(conn, k);
314 #ifndef OLD_ZLIB_SUPPORT
315 /* Support for old zlib versions is compiled away and we are running with
316 an old version, so return an error. */
317 return exit_zlib(z, &k->zlib_init, CURLE_FUNCTION_NOT_FOUND);
320 /* This next mess is to get around the potential case where there isn't
321 * enough data passed in to skip over the gzip header. If that happens, we
322 * malloc a block and copy what we have then wait for the next call. If
323 * there still isn't enough (this is definitely a worst-case scenario), we
324 * make the block bigger, copy the next part in and keep waiting.
326 * This is only required with zlib versions < 1.2.0.4 as newer versions
327 * can handle the gzip header themselves.
330 switch (k->zlib_init) {
331 /* Skip over gzip header? */
334 /* Initial call state */
337 switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
339 z->next_in = (Bytef *)k->str + hlen;
340 z->avail_in = (uInt)(nread - hlen);
341 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
345 /* We need more data so we can find the end of the gzip header. It's
346 * possible that the memory block we malloc here will never be freed if
347 * the transfer abruptly aborts after this point. Since it's unlikely
348 * that circumstances will be right for this code path to be followed in
349 * the first place, and it's even more unlikely for a transfer to fail
350 * immediately afterwards, it should seldom be a problem.
352 z->avail_in = (uInt)nread;
353 z->next_in = malloc(z->avail_in);
354 if(z->next_in == NULL) {
355 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
357 memcpy(z->next_in, k->str, z->avail_in);
358 k->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
359 /* We don't have any data to inflate yet */
364 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
370 case ZLIB_GZIP_HEADER:
372 /* Need more gzip header data state */
374 unsigned char *oldblock = z->next_in;
376 z->avail_in += (uInt)nread;
377 z->next_in = realloc(z->next_in, z->avail_in);
378 if(z->next_in == NULL) {
380 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
382 /* Append the new block of data to the previous one */
383 memcpy(z->next_in + z->avail_in - nread, k->str, nread);
385 switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
387 /* This is the zlib stream data */
389 /* Don't point into the malloced block since we just freed it */
390 z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
391 z->avail_in = (uInt)(z->avail_in - hlen);
392 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
396 /* We still don't have any data to inflate! */
402 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
408 case ZLIB_GZIP_INFLATING:
410 /* Inflating stream state */
411 z->next_in = (Bytef *)k->str;
412 z->avail_in = (uInt)nread;
416 if(z->avail_in == 0) {
417 /* We don't have any data to inflate; wait until next time */
421 /* We've parsed the header, now uncompress the data */
422 return inflate_stream(conn, k);
426 void Curl_unencode_cleanup(struct connectdata *conn)
428 struct SessionHandle *data = conn->data;
429 struct SingleRequest *k = &data->req;
431 if(k->zlib_init != ZLIB_UNINIT)
432 (void) exit_zlib(z, &k->zlib_init, CURLE_OK);
435 #endif /* HAVE_LIBZ */