1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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.13 2004/02/15 13:58:57 bagder Exp $
22 ***************************************************************************/
32 #include <curl/curl.h>
33 #include <curl/types.h>
35 #include "content_encoding.h"
37 #define DSIZ 0x10000 /* buffer size for decompressed data */
39 #define GZIP_MAGIC_0 0x1f
40 #define GZIP_MAGIC_1 0x8b
43 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
44 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
45 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
46 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
47 #define COMMENT 0x10 /* bit 4 set: file comment present */
48 #define RESERVED 0xE0 /* bits 5..7: reserved */
51 process_zlib_error(struct SessionHandle *data, z_stream *z)
54 failf (data, "Error while processing content unencoding.\n%s",
57 failf (data, "Error while processing content unencoding.\n"
58 "Unknown failure within decompression software.");
60 return CURLE_BAD_CONTENT_ENCODING;
64 exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
72 Curl_unencode_deflate_write(struct SessionHandle *data,
73 struct Curl_transfer_keeper *k,
76 int status; /* zlib status */
77 CURLcode result = CURLE_OK; /* Curl_client_write status */
78 char decomp[DSIZ]; /* Put the decompressed data here. */
79 z_stream *z = &k->z; /* zlib state structure */
81 /* Initialize zlib? */
83 z->zalloc = (alloc_func)Z_NULL;
84 z->zfree = (free_func)Z_NULL;
85 z->opaque = 0; /* of dubious use 08/27/02 jhrg */
88 if (inflateInit(z) != Z_OK)
89 return process_zlib_error(data, z);
93 /* Set the compressed input when this function is called */
94 z->next_in = (Bytef *)k->str;
97 /* because the buffer size is fixed, iteratively decompress
98 and transfer to the client via client_write. */
100 /* (re)set buffer for decompressed output for every iteration */
101 z->next_out = (Bytef *)&decomp[0];
104 status = inflate(z, Z_SYNC_FLUSH);
105 if (status == Z_OK || status == Z_STREAM_END) {
106 if (DSIZ - z->avail_out) {
107 result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
108 DSIZ - z->avail_out);
109 /* if !CURLE_OK, clean up, return */
111 return exit_zlib(z, &k->zlib_init, result);
114 /* Done?; clean up, return */
115 if (status == Z_STREAM_END) {
116 if (inflateEnd(z) == Z_OK)
117 return exit_zlib(z, &k->zlib_init, result);
119 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
122 /* Done with these bytes, exit */
123 if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
126 else { /* Error; exit loop, handle below */
127 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
132 /* Skip over the gzip header */
137 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
140 const ssize_t totallen = len;
142 /* The shortest header is 10 bytes */
144 return GZIP_UNDERFLOW;
146 if ((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
152 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
153 /* Can't handle this compression method or unknown flag */
157 /* Skip over time, xflags, OS code and all previous bytes */
161 if (flags & EXTRA_FIELD) {
165 return GZIP_UNDERFLOW;
167 extra_len = (data[1] << 8) | data[0];
169 if (len < (extra_len+2))
170 return GZIP_UNDERFLOW;
172 len -= (extra_len + 2);
175 if (flags & ORIG_NAME) {
176 /* Skip over NUL-terminated file name */
177 while (len && *data) {
182 return GZIP_UNDERFLOW;
184 /* Skip over the NUL */
189 if (flags & COMMENT) {
190 /* Skip over NUL-terminated comment */
191 while (len && *data) {
196 return GZIP_UNDERFLOW;
198 /* Skip over the NUL */
203 if (flags & HEAD_CRC) {
205 return GZIP_UNDERFLOW;
211 *headerlen = totallen - len;
216 Curl_unencode_gzip_write(struct SessionHandle *data,
217 struct Curl_transfer_keeper *k,
220 int status; /* zlib status */
221 CURLcode result = CURLE_OK; /* Curl_client_write status */
222 char decomp[DSIZ]; /* Put the decompressed data here. */
223 z_stream *z = &k->z; /* zlib state structure */
225 /* Initialize zlib? */
227 z->zalloc = (alloc_func)Z_NULL;
228 z->zfree = (free_func)Z_NULL;
229 z->opaque = 0; /* of dubious use 08/27/02 jhrg */
232 if (inflateInit2(z, -MAX_WBITS) != Z_OK)
233 return process_zlib_error(data, z);
234 k->zlib_init = 1; /* Initial call state */
237 /* This next mess is to get around the potential case where there isn't
238 enough data passed in to skip over the gzip header. If that happens,
239 we malloc a block and copy what we have then wait for the next call. If
240 there still isn't enough (this is definitely a worst-case scenario), we
241 make the block bigger, copy the next part in and keep waiting. */
243 /* Skip over gzip header? */
244 if (k->zlib_init == 1) {
245 /* Initial call state */
248 switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
250 z->next_in = (Bytef *)k->str + hlen;
251 z->avail_in = nread - hlen;
252 k->zlib_init = 3; /* Inflating stream state */
256 /* We need more data so we can find the end of the gzip header.
257 It's possible that the memory block we malloc here will never be
258 freed if the transfer abruptly aborts after this point. Since it's
259 unlikely that circumstances will be right for this code path to be
260 followed in the first place, and it's even more unlikely for a transfer
261 to fail immediately afterwards, it should seldom be a problem. */
263 z->next_in = malloc(z->avail_in);
264 if (z->next_in == NULL) {
265 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
267 memcpy(z->next_in, k->str, z->avail_in);
268 k->zlib_init = 2; /* Need more gzip header data state */
269 /* We don't have any data to inflate yet */
274 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
278 else if (k->zlib_init == 2) {
279 /* Need more gzip header data state */
281 unsigned char *oldblock = z->next_in;
283 z->avail_in += nread;
284 z->next_in = realloc(z->next_in, z->avail_in);
285 if (z->next_in == NULL) {
287 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
289 /* Append the new block of data to the previous one */
290 memcpy(z->next_in + z->avail_in - nread, k->str, nread);
292 switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
294 /* This is the zlib stream data */
296 /* Don't point into the malloced block since we just freed it */
297 z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
298 z->avail_in = z->avail_in - hlen;
299 k->zlib_init = 3; /* Inflating stream state */
303 /* We still don't have any data to inflate! */
309 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
314 /* Inflating stream state */
315 z->next_in = (Bytef *)k->str;
319 if (z->avail_in == 0) {
320 /* We don't have any data to inflate; wait until next time */
324 /* because the buffer size is fixed, iteratively decompress
325 and transfer to the client via client_write. */
327 /* (re)set buffer for decompressed output for every iteration */
328 z->next_out = (Bytef *)&decomp[0];
331 status = inflate(z, Z_SYNC_FLUSH);
332 if (status == Z_OK || status == Z_STREAM_END) {
333 if(DSIZ - z->avail_out) {
334 result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
335 DSIZ - z->avail_out);
336 /* if !CURLE_OK, clean up, return */
338 return exit_zlib(z, &k->zlib_init, result);
341 /* Done?; clean up, return */
342 /* We should really check the gzip CRC here */
343 if (status == Z_STREAM_END) {
344 if (inflateEnd(z) == Z_OK)
345 return exit_zlib(z, &k->zlib_init, result);
347 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
350 /* Done with these bytes, exit */
351 if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
354 else { /* Error; exit loop, handle below */
355 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
359 #endif /* HAVE_LIBZ */