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.5 2004/10/05 16:42:37 hoffman Exp $
22
***************************************************************************/
32
#include <curl/curl.h>
34
#include "content_encoding.h"
35
#include "curl_memory.h"
39
#define DSIZ 0x10000 /* buffer size for decompressed data */
41
#define GZIP_MAGIC_0 0x1f
42
#define GZIP_MAGIC_1 0x8b
45
#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
46
#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
47
#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
48
#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
49
#define COMMENT 0x10 /* bit 4 set: file comment present */
50
#define RESERVED 0xE0 /* bits 5..7: reserved */
53
process_zlib_error(struct SessionHandle *data, z_stream *z)
56
failf (data, "Error while processing content unencoding.\n%s",
59
failf (data, "Error while processing content unencoding.\n"
60
"Unknown failure within decompression software.");
62
return CURLE_BAD_CONTENT_ENCODING;
66
exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
74
Curl_unencode_deflate_write(struct SessionHandle *data,
75
struct Curl_transfer_keeper *k,
78
int status; /* zlib status */
79
CURLcode result = CURLE_OK; /* Curl_client_write status */
80
char decomp[DSIZ]; /* Put the decompressed data here. */
81
z_stream *z = &k->z; /* zlib state structure */
83
/* Initialize zlib? */
85
z->zalloc = (alloc_func)Z_NULL;
86
z->zfree = (free_func)Z_NULL;
90
if (inflateInit(z) != Z_OK)
91
return process_zlib_error(data, z);
95
/* Set the compressed input when this function is called */
96
z->next_in = (Bytef *)k->str;
97
z->avail_in = (uInt)nread;
99
/* because the buffer size is fixed, iteratively decompress
100
and transfer to the client via client_write. */
102
/* (re)set buffer for decompressed output for every iteration */
103
z->next_out = (Bytef *)&decomp[0];
106
status = inflate(z, Z_SYNC_FLUSH);
107
if (status == Z_OK || status == Z_STREAM_END) {
108
if (DSIZ - z->avail_out) {
109
result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
110
DSIZ - z->avail_out);
111
/* if !CURLE_OK, clean up, return */
113
return exit_zlib(z, &k->zlib_init, result);
116
/* Done?; clean up, return */
117
if (status == Z_STREAM_END) {
118
if (inflateEnd(z) == Z_OK)
119
return exit_zlib(z, &k->zlib_init, result);
121
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
124
/* Done with these bytes, exit */
125
if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
128
else { /* Error; exit loop, handle below */
129
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
134
/* Skip over the gzip header */
139
} check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
142
const ssize_t totallen = len;
144
/* The shortest header is 10 bytes */
146
return GZIP_UNDERFLOW;
148
if ((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
154
if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
155
/* Can't handle this compression method or unknown flag */
159
/* Skip over time, xflags, OS code and all previous bytes */
163
if (flags & EXTRA_FIELD) {
167
return GZIP_UNDERFLOW;
169
extra_len = (data[1] << 8) | data[0];
171
if (len < (extra_len+2))
172
return GZIP_UNDERFLOW;
174
len -= (extra_len + 2);
177
if (flags & ORIG_NAME) {
178
/* Skip over NUL-terminated file name */
179
while (len && *data) {
184
return GZIP_UNDERFLOW;
186
/* Skip over the NUL */
191
if (flags & COMMENT) {
192
/* Skip over NUL-terminated comment */
193
while (len && *data) {
198
return GZIP_UNDERFLOW;
200
/* Skip over the NUL */
205
if (flags & HEAD_CRC) {
207
return GZIP_UNDERFLOW;
213
*headerlen = totallen - len;
218
Curl_unencode_gzip_write(struct SessionHandle *data,
219
struct Curl_transfer_keeper *k,
222
int status; /* zlib status */
223
CURLcode result = CURLE_OK; /* Curl_client_write status */
224
char decomp[DSIZ]; /* Put the decompressed data here. */
225
z_stream *z = &k->z; /* zlib state structure */
227
/* Initialize zlib? */
229
z->zalloc = (alloc_func)Z_NULL;
230
z->zfree = (free_func)Z_NULL;
234
if (inflateInit2(z, -MAX_WBITS) != Z_OK)
235
return process_zlib_error(data, z);
236
k->zlib_init = 1; /* Initial call state */
239
/* This next mess is to get around the potential case where there isn't
240
* enough data passed in to skip over the gzip header. If that happens, we
241
* malloc a block and copy what we have then wait for the next call. If
242
* there still isn't enough (this is definitely a worst-case scenario), we
243
* make the block bigger, copy the next part in and keep waiting.
246
/* Skip over gzip header? */
247
if (k->zlib_init == 1) {
248
/* Initial call state */
251
switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
253
z->next_in = (Bytef *)k->str + hlen;
254
z->avail_in = (uInt)(nread - hlen);
255
k->zlib_init = 3; /* Inflating stream state */
259
/* We need more data so we can find the end of the gzip header. It's
260
* possible that the memory block we malloc here will never be freed if
261
* the transfer abruptly aborts after this point. Since it's unlikely
262
* that circumstances will be right for this code path to be followed in
263
* the first place, and it's even more unlikely for a transfer to fail
264
* immediately afterwards, it should seldom be a problem.
266
z->avail_in = (uInt)nread;
267
z->next_in = malloc(z->avail_in);
268
if (z->next_in == NULL) {
269
return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
271
memcpy(z->next_in, k->str, z->avail_in);
272
k->zlib_init = 2; /* Need more gzip header data state */
273
/* We don't have any data to inflate yet */
278
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
282
else if (k->zlib_init == 2) {
283
/* Need more gzip header data state */
285
unsigned char *oldblock = z->next_in;
287
z->avail_in += nread;
288
z->next_in = realloc(z->next_in, z->avail_in);
289
if (z->next_in == NULL) {
291
return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
293
/* Append the new block of data to the previous one */
294
memcpy(z->next_in + z->avail_in - nread, k->str, nread);
296
switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
298
/* This is the zlib stream data */
300
/* Don't point into the malloced block since we just freed it */
301
z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
302
z->avail_in = (uInt)(z->avail_in - hlen);
303
k->zlib_init = 3; /* Inflating stream state */
307
/* We still don't have any data to inflate! */
313
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
318
/* Inflating stream state */
319
z->next_in = (Bytef *)k->str;
320
z->avail_in = (uInt)nread;
323
if (z->avail_in == 0) {
324
/* We don't have any data to inflate; wait until next time */
328
/* because the buffer size is fixed, iteratively decompress and transfer to
329
the client via client_write. */
331
/* (re)set buffer for decompressed output for every iteration */
332
z->next_out = (Bytef *)&decomp[0];
335
status = inflate(z, Z_SYNC_FLUSH);
336
if (status == Z_OK || status == Z_STREAM_END) {
337
if(DSIZ - z->avail_out) {
338
result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
339
DSIZ - z->avail_out);
340
/* if !CURLE_OK, clean up, return */
342
return exit_zlib(z, &k->zlib_init, result);
345
/* Done?; clean up, return */
346
/* We should really check the gzip CRC here */
347
if (status == Z_STREAM_END) {
348
if (inflateEnd(z) == Z_OK)
349
return exit_zlib(z, &k->zlib_init, result);
351
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
354
/* Done with these bytes, exit */
355
if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
358
else { /* Error; exit loop, handle below */
359
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
363
#endif /* HAVE_LIBZ */