Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / content_encoding.c
blob55705025605406d039378303196b5c1077fa8503
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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 ***************************************************************************/
24 #include "setup.h"
26 #ifdef HAVE_LIBZ
28 #include <stdlib.h>
29 #include <string.h>
31 #include "urldata.h"
32 #include <curl/curl.h>
33 #include "sendf.h"
34 #include "content_encoding.h"
35 #include "memory.h"
37 #include "memdebug.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
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 */
56 static CURLcode
57 process_zlib_error(struct connectdata *conn, z_stream *z)
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.");
67 return CURLE_BAD_CONTENT_ENCODING;
70 static CURLcode
71 exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
73 inflateEnd(z);
74 *zlib_init = ZLIB_UNINIT;
75 return result;
78 static CURLcode
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);
93 if(decomp == NULL) {
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. */
99 for (;;) {
100 /* (re)set buffer for decompressed output for every iteration */
101 z->next_out = (Bytef *)decomp;
102 z->avail_out = DSIZ;
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);
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));
126 /* Done with these bytes, exit */
127 if(status == Z_OK && z->avail_in == 0) {
128 free(decomp);
129 return result;
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;
141 z->avail_in = nread;
142 allow_restart = 0;
143 continue;
145 else { /* Error; exit loop, handle below */
146 free(decomp);
147 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
150 /* Will never get here */
153 CURLcode
154 Curl_unencode_deflate_write(struct connectdata *conn,
155 struct SingleRequest *k,
156 ssize_t nread)
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;
164 z->opaque = 0;
165 z->next_in = NULL;
166 z->avail_in = 0;
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 */
182 static enum {
183 GZIP_OK,
184 GZIP_BAD,
185 GZIP_UNDERFLOW
186 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
188 int method, flags;
189 const ssize_t totallen = len;
191 /* The shortest header is 10 bytes */
192 if(len < 10)
193 return GZIP_UNDERFLOW;
195 if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
196 return GZIP_BAD;
198 method = data[2];
199 flags = data[3];
201 if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
202 /* Can't handle this compression method or unknown flag */
203 return GZIP_BAD;
206 /* Skip over time, xflags, OS code and all previous bytes */
207 len -= 10;
208 data += 10;
210 if(flags & EXTRA_FIELD) {
211 ssize_t extra_len;
213 if(len < 2)
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) {
228 --len;
229 ++data;
231 if(!len || *data)
232 return GZIP_UNDERFLOW;
234 /* Skip over the NUL */
235 --len;
236 ++data;
239 if(flags & COMMENT) {
240 /* Skip over NUL-terminated comment */
241 while(len && *data) {
242 --len;
243 ++data;
245 if(!len || *data)
246 return GZIP_UNDERFLOW;
248 /* Skip over the NUL */
249 --len;
250 ++data;
253 if(flags & HEAD_CRC) {
254 if(len < 2)
255 return GZIP_UNDERFLOW;
257 len -= 2;
258 data += 2;
261 *headerlen = totallen - len;
262 return GZIP_OK;
264 #endif
266 CURLcode
267 Curl_unencode_gzip_write(struct connectdata *conn,
268 struct SingleRequest *k,
269 ssize_t nread)
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;
277 z->opaque = 0;
278 z->next_in = NULL;
279 z->avail_in = 0;
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 */
288 } else {
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);
310 #else
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? */
323 case ZLIB_INIT:
325 /* Initial call state */
326 ssize_t hlen;
328 switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
329 case GZIP_OK:
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 */
333 break;
335 case GZIP_UNDERFLOW:
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 */
351 return CURLE_OK;
353 case GZIP_BAD:
354 default:
355 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
359 break;
361 case ZLIB_GZIP_HEADER:
363 /* Need more gzip header data state */
364 ssize_t hlen;
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) {
370 free(oldblock);
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)) {
377 case GZIP_OK:
378 /* This is the zlib stream data */
379 free(z->next_in);
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 */
384 break;
386 case GZIP_UNDERFLOW:
387 /* We still don't have any data to inflate! */
388 return CURLE_OK;
390 case GZIP_BAD:
391 default:
392 free(z->next_in);
393 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
397 break;
399 case ZLIB_GZIP_INFLATING:
400 default:
401 /* Inflating stream state */
402 z->next_in = (Bytef *)k->str;
403 z->avail_in = (uInt)nread;
404 break;
407 if(z->avail_in == 0) {
408 /* We don't have any data to inflate; wait until next time */
409 return CURLE_OK;
412 /* We've parsed the header, now uncompress the data */
413 return inflate_stream(conn, k);
414 #endif
416 #endif /* HAVE_LIBZ */