1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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 * $Id: content_encoding.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
32 #include <curl/curl.h>
34 #include "content_encoding.h"
39 /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
40 (doing so will reduce code size slightly). */
41 #define OLD_ZLIB_SUPPORT 1
43 #define DSIZ 0x10000 /* buffer size for decompressed data */
45 #define GZIP_MAGIC_0 0x1f
46 #define GZIP_MAGIC_1 0x8b
49 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
50 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
51 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
52 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
53 #define COMMENT 0x10 /* bit 4 set: file comment present */
54 #define RESERVED 0xE0 /* bits 5..7: reserved */
57 process_zlib_error(struct connectdata
*conn
, z_stream
*z
)
59 struct SessionHandle
*data
= conn
->data
;
61 failf (data
, "Error while processing content unencoding: %s",
64 failf (data
, "Error while processing content unencoding: "
65 "Unknown failure within decompression software.");
67 return CURLE_BAD_CONTENT_ENCODING
;
71 exit_zlib(z_stream
*z
, zlibInitState
*zlib_init
, CURLcode result
)
74 *zlib_init
= ZLIB_UNINIT
;
79 inflate_stream(struct connectdata
*conn
,
80 struct SingleRequest
*k
)
82 int allow_restart
= 1;
83 z_stream
*z
= &k
->z
; /* zlib state structure */
84 uInt nread
= z
->avail_in
;
85 Bytef
*orig_in
= z
->next_in
;
86 int status
; /* zlib status */
87 CURLcode result
= CURLE_OK
; /* Curl_client_write status */
88 char *decomp
; /* Put the decompressed data here. */
90 /* Dynamically allocate a buffer for decompression because it's uncommonly
91 large to hold on the stack */
92 decomp
= (char*)malloc(DSIZ
);
94 return exit_zlib(z
, &k
->zlib_init
, CURLE_OUT_OF_MEMORY
);
97 /* because the buffer size is fixed, iteratively decompress and transfer to
98 the client via client_write. */
100 /* (re)set buffer for decompressed output for every iteration */
101 z
->next_out
= (Bytef
*)decomp
;
104 status
= inflate(z
, Z_SYNC_FLUSH
);
105 if(status
== Z_OK
|| status
== Z_STREAM_END
) {
107 if(DSIZ
- z
->avail_out
) {
108 result
= Curl_client_write(conn
, CLIENTWRITE_BODY
, decomp
,
109 DSIZ
- z
->avail_out
);
110 /* if !CURLE_OK, clean up, return */
113 return exit_zlib(z
, &k
->zlib_init
, result
);
117 /* Done? clean up, return */
118 if(status
== Z_STREAM_END
) {
120 if(inflateEnd(z
) == Z_OK
)
121 return exit_zlib(z
, &k
->zlib_init
, result
);
123 return exit_zlib(z
, &k
->zlib_init
, process_zlib_error(conn
, z
));
126 /* Done with these bytes, exit */
127 if(status
== Z_OK
&& z
->avail_in
== 0) {
132 else if(allow_restart
&& status
== Z_DATA_ERROR
) {
133 /* some servers seem to not generate zlib headers, so this is an attempt
134 to fix and continue anyway */
136 (void) inflateEnd(z
); /* don't care about the return code */
137 if(inflateInit2(z
, -MAX_WBITS
) != Z_OK
) {
138 return process_zlib_error(conn
, z
);
140 z
->next_in
= orig_in
;
145 else { /* Error; exit loop, handle below */
147 return exit_zlib(z
, &k
->zlib_init
, process_zlib_error(conn
, z
));
150 /* Will never get here */
154 Curl_unencode_deflate_write(struct connectdata
*conn
,
155 struct SingleRequest
*k
,
158 z_stream
*z
= &k
->z
; /* zlib state structure */
160 /* Initialize zlib? */
161 if(k
->zlib_init
== ZLIB_UNINIT
) {
162 z
->zalloc
= (alloc_func
)Z_NULL
;
163 z
->zfree
= (free_func
)Z_NULL
;
167 if(inflateInit(z
) != Z_OK
)
168 return process_zlib_error(conn
, z
);
169 k
->zlib_init
= ZLIB_INIT
;
172 /* Set the compressed input when this function is called */
173 z
->next_in
= (Bytef
*)k
->str
;
174 z
->avail_in
= (uInt
)nread
;
176 /* Now uncompress the data */
177 return inflate_stream(conn
, k
);
180 #ifdef OLD_ZLIB_SUPPORT
181 /* Skip over the gzip header */
186 } check_gzip_header(unsigned char const *data
, ssize_t len
, ssize_t
*headerlen
)
189 const ssize_t totallen
= len
;
191 /* The shortest header is 10 bytes */
193 return GZIP_UNDERFLOW
;
195 if((data
[0] != GZIP_MAGIC_0
) || (data
[1] != GZIP_MAGIC_1
))
201 if(method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
202 /* Can't handle this compression method or unknown flag */
206 /* Skip over time, xflags, OS code and all previous bytes */
210 if(flags
& EXTRA_FIELD
) {
214 return GZIP_UNDERFLOW
;
216 extra_len
= (data
[1] << 8) | data
[0];
218 if(len
< (extra_len
+2))
219 return GZIP_UNDERFLOW
;
221 len
-= (extra_len
+ 2);
222 data
+= (extra_len
+ 2);
225 if(flags
& ORIG_NAME
) {
226 /* Skip over NUL-terminated file name */
227 while(len
&& *data
) {
232 return GZIP_UNDERFLOW
;
234 /* Skip over the NUL */
239 if(flags
& COMMENT
) {
240 /* Skip over NUL-terminated comment */
241 while(len
&& *data
) {
246 return GZIP_UNDERFLOW
;
248 /* Skip over the NUL */
253 if(flags
& HEAD_CRC
) {
255 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
+= 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
);
416 #endif /* HAVE_LIBZ */