1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2009, 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: content_encoding.c,v 1.31 2009-02-17 12:14:52 bagder Exp $
22 ***************************************************************************/
23
24 #include "setup.h"
25
26 #ifdef HAVE_LIBZ
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "urldata.h"
32 #include <curl/curl.h>
33 #include "sendf.h"
34 #include "content_encoding.h"
35 #include "memory.h"
36
37 #include "memdebug.h"
38
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
42
43 #define DSIZ 0x10000 /* buffer size for decompressed data */
44
45 #define GZIP_MAGIC_0 0x1f
46 #define GZIP_MAGIC_1 0x8b
47
48 /* gzip flag byte */
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 */
55
56 static CURLcode
process_zlib_error(struct connectdata * conn,z_stream * z)57 process_zlib_error(struct connectdata *conn, z_stream *z)
58 {
59 struct SessionHandle *data = conn->data;
60 if(z->msg)
61 failf (data, "Error while processing content unencoding: %s",
62 z->msg);
63 else
64 failf (data, "Error while processing content unencoding: "
65 "Unknown failure within decompression software.");
66
67 return CURLE_BAD_CONTENT_ENCODING;
68 }
69
70 static CURLcode
exit_zlib(z_stream * z,zlibInitState * zlib_init,CURLcode result)71 exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
72 {
73 inflateEnd(z);
74 *zlib_init = ZLIB_UNINIT;
75 return result;
76 }
77
78 static CURLcode
inflate_stream(struct connectdata * conn,struct SingleRequest * k)79 inflate_stream(struct connectdata *conn,
80 struct SingleRequest *k)
81 {
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. */
89
90 /* Dynamically allocate a buffer for decompression because it's uncommonly
91 large to hold on the stack */
92 decomp = malloc(DSIZ);
93 if(decomp == NULL) {
94 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
95 }
96
97 /* because the buffer size is fixed, iteratively decompress and transfer to
98 the client via client_write. */
99 for (;;) {
100 /* (re)set buffer for decompressed output for every iteration */
101 z->next_out = (Bytef *)decomp;
102 z->avail_out = DSIZ;
103
104 status = inflate(z, Z_SYNC_FLUSH);
105 if(status == Z_OK || status == Z_STREAM_END) {
106 allow_restart = 0;
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 */
111 if(result) {
112 free(decomp);
113 return exit_zlib(z, &k->zlib_init, result);
114 }
115 }
116
117 /* Done? clean up, return */
118 if(status == Z_STREAM_END) {
119 free(decomp);
120 if(inflateEnd(z) == Z_OK)
121 return exit_zlib(z, &k->zlib_init, result);
122 else
123 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
124 }
125
126 /* Done with these bytes, exit */
127 if(status == Z_OK && z->avail_in == 0) {
128 free(decomp);
129 return result;
130 }
131 }
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 */
135
136 (void) inflateEnd(z); /* don't care about the return code */
137 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
138 free(decomp);
139 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
140 }
141 z->next_in = orig_in;
142 z->avail_in = nread;
143 allow_restart = 0;
144 continue;
145 }
146 else { /* Error; exit loop, handle below */
147 free(decomp);
148 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
149 }
150 }
151 /* Will never get here */
152 }
153
154 CURLcode
Curl_unencode_deflate_write(struct connectdata * conn,struct SingleRequest * k,ssize_t nread)155 Curl_unencode_deflate_write(struct connectdata *conn,
156 struct SingleRequest *k,
157 ssize_t nread)
158 {
159 z_stream *z = &k->z; /* zlib state structure */
160
161 /* Initialize zlib? */
162 if(k->zlib_init == ZLIB_UNINIT) {
163 z->zalloc = (alloc_func)Z_NULL;
164 z->zfree = (free_func)Z_NULL;
165 z->opaque = 0;
166 z->next_in = NULL;
167 z->avail_in = 0;
168 if(inflateInit(z) != Z_OK)
169 return process_zlib_error(conn, z);
170 k->zlib_init = ZLIB_INIT;
171 }
172
173 /* Set the compressed input when this function is called */
174 z->next_in = (Bytef *)k->str;
175 z->avail_in = (uInt)nread;
176
177 /* Now uncompress the data */
178 return inflate_stream(conn, k);
179 }
180
181 #ifdef OLD_ZLIB_SUPPORT
182 /* Skip over the gzip header */
183 static enum {
184 GZIP_OK,
185 GZIP_BAD,
186 GZIP_UNDERFLOW
check_gzip_header(unsigned char const * data,ssize_t len,ssize_t * headerlen)187 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
188 {
189 int method, flags;
190 const ssize_t totallen = len;
191
192 /* The shortest header is 10 bytes */
193 if(len < 10)
194 return GZIP_UNDERFLOW;
195
196 if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
197 return GZIP_BAD;
198
199 method = data[2];
200 flags = data[3];
201
202 if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
203 /* Can't handle this compression method or unknown flag */
204 return GZIP_BAD;
205 }
206
207 /* Skip over time, xflags, OS code and all previous bytes */
208 len -= 10;
209 data += 10;
210
211 if(flags & EXTRA_FIELD) {
212 ssize_t extra_len;
213
214 if(len < 2)
215 return GZIP_UNDERFLOW;
216
217 extra_len = (data[1] << 8) | data[0];
218
219 if(len < (extra_len+2))
220 return GZIP_UNDERFLOW;
221
222 len -= (extra_len + 2);
223 data += (extra_len + 2);
224 }
225
226 if(flags & ORIG_NAME) {
227 /* Skip over NUL-terminated file name */
228 while(len && *data) {
229 --len;
230 ++data;
231 }
232 if(!len || *data)
233 return GZIP_UNDERFLOW;
234
235 /* Skip over the NUL */
236 --len;
237 ++data;
238 }
239
240 if(flags & COMMENT) {
241 /* Skip over NUL-terminated comment */
242 while(len && *data) {
243 --len;
244 ++data;
245 }
246 if(!len || *data)
247 return GZIP_UNDERFLOW;
248
249 /* Skip over the NUL */
250 --len;
251 ++data;
252 }
253
254 if(flags & HEAD_CRC) {
255 if(len < 2)
256 return GZIP_UNDERFLOW;
257
258 len -= 2;
259 data += 2;
260 }
261
262 *headerlen = totallen - len;
263 return GZIP_OK;
264 }
265 #endif
266
267 CURLcode
Curl_unencode_gzip_write(struct connectdata * conn,struct SingleRequest * k,ssize_t nread)268 Curl_unencode_gzip_write(struct connectdata *conn,
269 struct SingleRequest *k,
270 ssize_t nread)
271 {
272 z_stream *z = &k->z; /* zlib state structure */
273
274 /* Initialize zlib? */
275 if(k->zlib_init == ZLIB_UNINIT) {
276 z->zalloc = (alloc_func)Z_NULL;
277 z->zfree = (free_func)Z_NULL;
278 z->opaque = 0;
279 z->next_in = NULL;
280 z->avail_in = 0;
281
282 if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
283 /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
284 if(inflateInit2(z, MAX_WBITS+32) != Z_OK) {
285 return process_zlib_error(conn, z);
286 }
287 k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
288
289 } else {
290 /* we must parse the gzip header ourselves */
291 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
292 return process_zlib_error(conn, z);
293 }
294 k->zlib_init = ZLIB_INIT; /* Initial call state */
295 }
296 }
297
298 if(k->zlib_init == ZLIB_INIT_GZIP) {
299 /* Let zlib handle the gzip decompression entirely */
300 z->next_in = (Bytef *)k->str;
301 z->avail_in = (uInt)nread;
302 /* Now uncompress the data */
303 return inflate_stream(conn, k);
304 }
305
306 #ifndef OLD_ZLIB_SUPPORT
307 /* Support for old zlib versions is compiled away and we are running with
308 an old version, so return an error. */
309 return exit_zlib(z, &k->zlib_init, CURLE_FUNCTION_NOT_FOUND);
310
311 #else
312 /* This next mess is to get around the potential case where there isn't
313 * enough data passed in to skip over the gzip header. If that happens, we
314 * malloc a block and copy what we have then wait for the next call. If
315 * there still isn't enough (this is definitely a worst-case scenario), we
316 * make the block bigger, copy the next part in and keep waiting.
317 *
318 * This is only required with zlib versions < 1.2.0.4 as newer versions
319 * can handle the gzip header themselves.
320 */
321
322 switch (k->zlib_init) {
323 /* Skip over gzip header? */
324 case ZLIB_INIT:
325 {
326 /* Initial call state */
327 ssize_t hlen;
328
329 switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
330 case GZIP_OK:
331 z->next_in = (Bytef *)k->str + hlen;
332 z->avail_in = (uInt)(nread - hlen);
333 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
334 break;
335
336 case GZIP_UNDERFLOW:
337 /* We need more data so we can find the end of the gzip header. It's
338 * possible that the memory block we malloc here will never be freed if
339 * the transfer abruptly aborts after this point. Since it's unlikely
340 * that circumstances will be right for this code path to be followed in
341 * the first place, and it's even more unlikely for a transfer to fail
342 * immediately afterwards, it should seldom be a problem.
343 */
344 z->avail_in = (uInt)nread;
345 z->next_in = malloc(z->avail_in);
346 if(z->next_in == NULL) {
347 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
348 }
349 memcpy(z->next_in, k->str, z->avail_in);
350 k->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
351 /* We don't have any data to inflate yet */
352 return CURLE_OK;
353
354 case GZIP_BAD:
355 default:
356 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
357 }
358
359 }
360 break;
361
362 case ZLIB_GZIP_HEADER:
363 {
364 /* Need more gzip header data state */
365 ssize_t hlen;
366 unsigned char *oldblock = z->next_in;
367
368 z->avail_in += nread;
369 z->next_in = realloc(z->next_in, z->avail_in);
370 if(z->next_in == NULL) {
371 free(oldblock);
372 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
373 }
374 /* Append the new block of data to the previous one */
375 memcpy(z->next_in + z->avail_in - nread, k->str, nread);
376
377 switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
378 case GZIP_OK:
379 /* This is the zlib stream data */
380 free(z->next_in);
381 /* Don't point into the malloced block since we just freed it */
382 z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
383 z->avail_in = (uInt)(z->avail_in - hlen);
384 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
385 break;
386
387 case GZIP_UNDERFLOW:
388 /* We still don't have any data to inflate! */
389 return CURLE_OK;
390
391 case GZIP_BAD:
392 default:
393 free(z->next_in);
394 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
395 }
396
397 }
398 break;
399
400 case ZLIB_GZIP_INFLATING:
401 default:
402 /* Inflating stream state */
403 z->next_in = (Bytef *)k->str;
404 z->avail_in = (uInt)nread;
405 break;
406 }
407
408 if(z->avail_in == 0) {
409 /* We don't have any data to inflate; wait until next time */
410 return CURLE_OK;
411 }
412
413 /* We've parsed the header, now uncompress the data */
414 return inflate_stream(conn, k);
415 #endif
416 }
417 #endif /* HAVE_LIBZ */
418