Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / content_encoding.c
blob3aa88deb504ad6108139de962833f43d5a920bb6
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.2 2007/03/15 19:22:13 andy 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 enum zlibState {
57 ZLIB_UNINIT, /* uninitialized */
58 ZLIB_INIT, /* initialized */
59 ZLIB_GZIP_HEADER, /* reading gzip header */
60 ZLIB_GZIP_INFLATING, /* inflating gzip stream */
61 ZLIB_INIT_GZIP /* initialized in transparent gzip mode */
64 static CURLcode
65 process_zlib_error(struct connectdata *conn, z_stream *z)
67 struct SessionHandle *data = conn->data;
68 if (z->msg)
69 failf (data, "Error while processing content unencoding: %s",
70 z->msg);
71 else
72 failf (data, "Error while processing content unencoding: "
73 "Unknown failure within decompression software.");
75 return CURLE_BAD_CONTENT_ENCODING;
78 static CURLcode
79 exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
81 inflateEnd(z);
82 *zlib_init = ZLIB_UNINIT;
83 return result;
86 static CURLcode
87 inflate_stream(struct connectdata *conn,
88 struct Curl_transfer_keeper *k)
90 int allow_restart = 1;
91 z_stream *z = &k->z; /* zlib state structure */
92 uInt nread = z->avail_in;
93 Bytef *orig_in = z->next_in;
94 int status; /* zlib status */
95 CURLcode result = CURLE_OK; /* Curl_client_write status */
96 char *decomp; /* Put the decompressed data here. */
98 /* Dynamically allocate a buffer for decompression because it's uncommonly
99 large to hold on the stack */
100 decomp = (char*)malloc(DSIZ);
101 if (decomp == NULL) {
102 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
105 /* because the buffer size is fixed, iteratively decompress and transfer to
106 the client via client_write. */
107 for (;;) {
108 /* (re)set buffer for decompressed output for every iteration */
109 z->next_out = (Bytef *)decomp;
110 z->avail_out = DSIZ;
112 status = inflate(z, Z_SYNC_FLUSH);
113 if (status == Z_OK || status == Z_STREAM_END) {
114 allow_restart = 0;
115 if(DSIZ - z->avail_out) {
116 result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
117 DSIZ - z->avail_out);
118 /* if !CURLE_OK, clean up, return */
119 if (result) {
120 free(decomp);
121 return exit_zlib(z, &k->zlib_init, result);
125 /* Done? clean up, return */
126 if (status == Z_STREAM_END) {
127 free(decomp);
128 if (inflateEnd(z) == Z_OK)
129 return exit_zlib(z, &k->zlib_init, result);
130 else
131 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
134 /* Done with these bytes, exit */
135 if (status == Z_OK && z->avail_in == 0) {
136 free(decomp);
137 return result;
140 else if (allow_restart && status == Z_DATA_ERROR) {
141 /* some servers seem to not generate zlib headers, so this is an attempt
142 to fix and continue anyway */
144 inflateReset(z);
145 if (inflateInit2(z, -MAX_WBITS) != Z_OK) {
146 return process_zlib_error(conn, z);
148 z->next_in = orig_in;
149 z->avail_in = nread;
150 allow_restart = 0;
151 continue;
153 else { /* Error; exit loop, handle below */
154 free(decomp);
155 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
158 /* Will never get here */
161 CURLcode
162 Curl_unencode_deflate_write(struct connectdata *conn,
163 struct Curl_transfer_keeper *k,
164 ssize_t nread)
166 z_stream *z = &k->z; /* zlib state structure */
168 /* Initialize zlib? */
169 if (k->zlib_init == ZLIB_UNINIT) {
170 z->zalloc = (alloc_func)Z_NULL;
171 z->zfree = (free_func)Z_NULL;
172 z->opaque = 0;
173 z->next_in = NULL;
174 z->avail_in = 0;
175 if (inflateInit(z) != Z_OK)
176 return process_zlib_error(conn, z);
177 k->zlib_init = ZLIB_INIT;
180 /* Set the compressed input when this function is called */
181 z->next_in = (Bytef *)k->str;
182 z->avail_in = (uInt)nread;
184 /* Now uncompress the data */
185 return inflate_stream(conn, k);
188 #ifdef OLD_ZLIB_SUPPORT
189 /* Skip over the gzip header */
190 static enum {
191 GZIP_OK,
192 GZIP_BAD,
193 GZIP_UNDERFLOW
194 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
196 int method, flags;
197 const ssize_t totallen = len;
199 /* The shortest header is 10 bytes */
200 if (len < 10)
201 return GZIP_UNDERFLOW;
203 if ((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
204 return GZIP_BAD;
206 method = data[2];
207 flags = data[3];
209 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
210 /* Can't handle this compression method or unknown flag */
211 return GZIP_BAD;
214 /* Skip over time, xflags, OS code and all previous bytes */
215 len -= 10;
216 data += 10;
218 if (flags & EXTRA_FIELD) {
219 ssize_t extra_len;
221 if (len < 2)
222 return GZIP_UNDERFLOW;
224 extra_len = (data[1] << 8) | data[0];
226 if (len < (extra_len+2))
227 return GZIP_UNDERFLOW;
229 len -= (extra_len + 2);
230 data += (extra_len + 2);
233 if (flags & ORIG_NAME) {
234 /* Skip over NUL-terminated file name */
235 while (len && *data) {
236 --len;
237 ++data;
239 if (!len || *data)
240 return GZIP_UNDERFLOW;
242 /* Skip over the NUL */
243 --len;
244 ++data;
247 if (flags & COMMENT) {
248 /* Skip over NUL-terminated comment */
249 while (len && *data) {
250 --len;
251 ++data;
253 if (!len || *data)
254 return GZIP_UNDERFLOW;
256 /* Skip over the NUL */
257 --len;
258 ++data;
261 if (flags & HEAD_CRC) {
262 if (len < 2)
263 return GZIP_UNDERFLOW;
265 len -= 2;
266 data += 2;
269 *headerlen = totallen - len;
270 return GZIP_OK;
272 #endif
274 CURLcode
275 Curl_unencode_gzip_write(struct connectdata *conn,
276 struct Curl_transfer_keeper *k,
277 ssize_t nread)
279 z_stream *z = &k->z; /* zlib state structure */
281 /* Initialize zlib? */
282 if (k->zlib_init == ZLIB_UNINIT) {
283 z->zalloc = (alloc_func)Z_NULL;
284 z->zfree = (free_func)Z_NULL;
285 z->opaque = 0;
286 z->next_in = NULL;
287 z->avail_in = 0;
289 if (strcmp(zlibVersion(), "1.2.0.4") >= 0) {
290 /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
291 if (inflateInit2(z, MAX_WBITS+32) != Z_OK) {
292 return process_zlib_error(conn, z);
294 k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
296 } else {
297 /* we must parse the gzip header ourselves */
298 if (inflateInit2(z, -MAX_WBITS) != Z_OK) {
299 return process_zlib_error(conn, z);
301 k->zlib_init = ZLIB_INIT; /* Initial call state */
305 if (k->zlib_init == ZLIB_INIT_GZIP) {
306 /* Let zlib handle the gzip decompression entirely */
307 z->next_in = (Bytef *)k->str;
308 z->avail_in = (uInt)nread;
309 /* Now uncompress the data */
310 return inflate_stream(conn, k);
313 #ifndef OLD_ZLIB_SUPPORT
314 /* Support for old zlib versions is compiled away and we are running with
315 an old version, so return an error. */
316 return exit_zlib(z, &k->zlib_init, CURLE_FUNCTION_NOT_FOUND);
318 #else
319 /* This next mess is to get around the potential case where there isn't
320 * enough data passed in to skip over the gzip header. If that happens, we
321 * malloc a block and copy what we have then wait for the next call. If
322 * there still isn't enough (this is definitely a worst-case scenario), we
323 * make the block bigger, copy the next part in and keep waiting.
325 * This is only required with zlib versions < 1.2.0.4 as newer versions
326 * can handle the gzip header themselves.
329 switch (k->zlib_init) {
330 /* Skip over gzip header? */
331 case ZLIB_INIT:
333 /* Initial call state */
334 ssize_t hlen;
336 switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
337 case GZIP_OK:
338 z->next_in = (Bytef *)k->str + hlen;
339 z->avail_in = (uInt)(nread - hlen);
340 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
341 break;
343 case GZIP_UNDERFLOW:
344 /* We need more data so we can find the end of the gzip header. It's
345 * possible that the memory block we malloc here will never be freed if
346 * the transfer abruptly aborts after this point. Since it's unlikely
347 * that circumstances will be right for this code path to be followed in
348 * the first place, and it's even more unlikely for a transfer to fail
349 * immediately afterwards, it should seldom be a problem.
351 z->avail_in = (uInt)nread;
352 z->next_in = malloc(z->avail_in);
353 if (z->next_in == NULL) {
354 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
356 memcpy(z->next_in, k->str, z->avail_in);
357 k->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
358 /* We don't have any data to inflate yet */
359 return CURLE_OK;
361 case GZIP_BAD:
362 default:
363 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
367 break;
369 case ZLIB_GZIP_HEADER:
371 /* Need more gzip header data state */
372 ssize_t hlen;
373 unsigned char *oldblock = z->next_in;
375 z->avail_in += nread;
376 z->next_in = realloc(z->next_in, z->avail_in);
377 if (z->next_in == NULL) {
378 free(oldblock);
379 return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
381 /* Append the new block of data to the previous one */
382 memcpy(z->next_in + z->avail_in - nread, k->str, nread);
384 switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
385 case GZIP_OK:
386 /* This is the zlib stream data */
387 free(z->next_in);
388 /* Don't point into the malloced block since we just freed it */
389 z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
390 z->avail_in = (uInt)(z->avail_in - hlen);
391 k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
392 break;
394 case GZIP_UNDERFLOW:
395 /* We still don't have any data to inflate! */
396 return CURLE_OK;
398 case GZIP_BAD:
399 default:
400 free(z->next_in);
401 return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
405 break;
407 case ZLIB_GZIP_INFLATING:
408 default:
409 /* Inflating stream state */
410 z->next_in = (Bytef *)k->str;
411 z->avail_in = (uInt)nread;
412 break;
415 if (z->avail_in == 0) {
416 /* We don't have any data to inflate; wait until next time */
417 return CURLE_OK;
420 /* We've parsed the header, now uncompress the data */
421 return inflate_stream(conn, k);
422 #endif
424 #endif /* HAVE_LIBZ */