~ubuntu-branches/ubuntu/jaunty/cmake/jaunty-security

« back to all changes in this revision

Viewing changes to Source/CTest/Curl/content_encoding.c

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2006-06-18 16:34:11 UTC
  • mfrom: (1.4.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060618163411-pi234s3v6jwlcmof
Tags: 2.4.2-1
* New upstream release (Closes: #338324)
* Put cmake .vim files into /usr/share/vim/addons/plugin/
  where they can be used. (Closes: #366663)
* Install cmake-mode.el so it can be used. (Closes: #366664)
* Ensure cmake FindKDE locates KDE libraries on Debian
  based distributions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2004, 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.5 2004/10/05 16:42:37 hoffman 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 "curl_memory.h"
 
36
 
 
37
#include "memdebug.h"
 
38
 
 
39
#define DSIZ 0x10000             /* buffer size for decompressed data */
 
40
 
 
41
#define GZIP_MAGIC_0 0x1f
 
42
#define GZIP_MAGIC_1 0x8b
 
43
 
 
44
/* gzip flag byte */
 
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 */
 
51
 
 
52
static CURLcode
 
53
process_zlib_error(struct SessionHandle *data, z_stream *z)
 
54
{
 
55
  if (z->msg)
 
56
    failf (data, "Error while processing content unencoding.\n%s",
 
57
           z->msg);
 
58
  else
 
59
    failf (data, "Error while processing content unencoding.\n"
 
60
           "Unknown failure within decompression software.");
 
61
 
 
62
  return CURLE_BAD_CONTENT_ENCODING;
 
63
}
 
64
 
 
65
static CURLcode
 
66
exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
 
67
{
 
68
  inflateEnd(z);
 
69
  *zlib_init = 0;
 
70
  return result;
 
71
}
 
72
 
 
73
CURLcode
 
74
Curl_unencode_deflate_write(struct SessionHandle *data,
 
75
                            struct Curl_transfer_keeper *k,
 
76
                            ssize_t nread)
 
77
{
 
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 */
 
82
 
 
83
  /* Initialize zlib? */
 
84
  if (!k->zlib_init) {
 
85
    z->zalloc = (alloc_func)Z_NULL;
 
86
    z->zfree = (free_func)Z_NULL;
 
87
    z->opaque = 0;
 
88
    z->next_in = NULL;
 
89
    z->avail_in = 0;
 
90
    if (inflateInit(z) != Z_OK)
 
91
      return process_zlib_error(data, z);
 
92
    k->zlib_init = 1;
 
93
  }
 
94
 
 
95
  /* Set the compressed input when this function is called */
 
96
  z->next_in = (Bytef *)k->str;
 
97
  z->avail_in = (uInt)nread;
 
98
 
 
99
  /* because the buffer size is fixed, iteratively decompress
 
100
     and transfer to the client via client_write. */
 
101
  for (;;) {
 
102
    /* (re)set buffer for decompressed output for every iteration */
 
103
    z->next_out = (Bytef *)&decomp[0];
 
104
    z->avail_out = DSIZ;
 
105
 
 
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 */
 
112
        if (result)
 
113
          return exit_zlib(z, &k->zlib_init, result);
 
114
      }
 
115
 
 
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);
 
120
        else
 
121
          return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
 
122
      }
 
123
 
 
124
      /* Done with these bytes, exit */
 
125
      if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
 
126
        return result;
 
127
    }
 
128
    else {                      /* Error; exit loop, handle below */
 
129
      return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
 
130
    }
 
131
  }
 
132
}
 
133
 
 
134
/* Skip over the gzip header */
 
135
static enum {
 
136
  GZIP_OK,
 
137
  GZIP_BAD,
 
138
  GZIP_UNDERFLOW
 
139
} check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
 
140
{
 
141
  int method, flags;
 
142
  const ssize_t totallen = len;
 
143
 
 
144
  /* The shortest header is 10 bytes */
 
145
  if (len < 10)
 
146
    return GZIP_UNDERFLOW;
 
147
 
 
148
  if ((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
 
149
    return GZIP_BAD;
 
150
 
 
151
  method = data[2];
 
152
  flags = data[3];
 
153
 
 
154
  if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
 
155
    /* Can't handle this compression method or unknown flag */
 
156
    return GZIP_BAD;
 
157
  }
 
158
 
 
159
  /* Skip over time, xflags, OS code and all previous bytes */
 
160
  len -= 10;
 
161
  data += 10;
 
162
 
 
163
  if (flags & EXTRA_FIELD) {
 
164
    ssize_t extra_len;
 
165
 
 
166
    if (len < 2)
 
167
      return GZIP_UNDERFLOW;
 
168
 
 
169
    extra_len = (data[1] << 8) | data[0];
 
170
 
 
171
    if (len < (extra_len+2))
 
172
      return GZIP_UNDERFLOW;
 
173
 
 
174
    len -= (extra_len + 2);
 
175
  }
 
176
 
 
177
  if (flags & ORIG_NAME) {
 
178
    /* Skip over NUL-terminated file name */
 
179
    while (len && *data) {
 
180
      --len;
 
181
      ++data;
 
182
    }
 
183
    if (!len || *data)
 
184
      return GZIP_UNDERFLOW;
 
185
 
 
186
    /* Skip over the NUL */
 
187
    --len;
 
188
    ++data;
 
189
  }
 
190
 
 
191
  if (flags & COMMENT) {
 
192
    /* Skip over NUL-terminated comment */
 
193
    while (len && *data) {
 
194
      --len;
 
195
      ++data;
 
196
    }
 
197
    if (!len || *data)
 
198
      return GZIP_UNDERFLOW;
 
199
 
 
200
    /* Skip over the NUL */
 
201
    --len;
 
202
    ++data;
 
203
  }
 
204
 
 
205
  if (flags & HEAD_CRC) {
 
206
    if (len < 2)
 
207
      return GZIP_UNDERFLOW;
 
208
 
 
209
    len -= 2;
 
210
    data += 2;
 
211
  }
 
212
 
 
213
  *headerlen = totallen - len;
 
214
  return GZIP_OK;
 
215
}
 
216
 
 
217
CURLcode
 
218
Curl_unencode_gzip_write(struct SessionHandle *data,
 
219
                         struct Curl_transfer_keeper *k,
 
220
                         ssize_t nread)
 
221
{
 
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 */
 
226
 
 
227
  /* Initialize zlib? */
 
228
  if (!k->zlib_init) {
 
229
    z->zalloc = (alloc_func)Z_NULL;
 
230
    z->zfree = (free_func)Z_NULL;
 
231
    z->opaque = 0;
 
232
    z->next_in = NULL;
 
233
    z->avail_in = 0;
 
234
    if (inflateInit2(z, -MAX_WBITS) != Z_OK)
 
235
      return process_zlib_error(data, z);
 
236
    k->zlib_init = 1;   /* Initial call state */
 
237
  }
 
238
 
 
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.
 
244
   */
 
245
 
 
246
  /* Skip over gzip header? */
 
247
  if (k->zlib_init == 1) {
 
248
    /* Initial call state */
 
249
    ssize_t hlen;
 
250
 
 
251
    switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
 
252
    case GZIP_OK:
 
253
      z->next_in = (Bytef *)k->str + hlen;
 
254
      z->avail_in = (uInt)(nread - hlen);
 
255
      k->zlib_init = 3; /* Inflating stream state */
 
256
      break;
 
257
 
 
258
    case GZIP_UNDERFLOW:
 
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.
 
265
       */
 
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);
 
270
      }
 
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 */
 
274
      return CURLE_OK;
 
275
 
 
276
    case GZIP_BAD:
 
277
    default:
 
278
      return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
 
279
    }
 
280
 
 
281
  }
 
282
  else if (k->zlib_init == 2) {
 
283
    /* Need more gzip header data state */
 
284
    ssize_t hlen;
 
285
    unsigned char *oldblock = z->next_in;
 
286
 
 
287
    z->avail_in += nread;
 
288
    z->next_in = realloc(z->next_in, z->avail_in);
 
289
    if (z->next_in == NULL) {
 
290
      free(oldblock);
 
291
      return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
 
292
    }
 
293
    /* Append the new block of data to the previous one */
 
294
    memcpy(z->next_in + z->avail_in - nread, k->str, nread);
 
295
 
 
296
    switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
 
297
    case GZIP_OK:
 
298
      /* This is the zlib stream data */
 
299
      free(z->next_in);
 
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 */
 
304
      break;
 
305
 
 
306
    case GZIP_UNDERFLOW:
 
307
      /* We still don't have any data to inflate! */
 
308
      return CURLE_OK;
 
309
 
 
310
    case GZIP_BAD:
 
311
    default:
 
312
      free(z->next_in);
 
313
      return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
 
314
    }
 
315
 
 
316
  }
 
317
  else {
 
318
    /* Inflating stream state */
 
319
    z->next_in = (Bytef *)k->str;
 
320
    z->avail_in = (uInt)nread;
 
321
  }
 
322
 
 
323
  if (z->avail_in == 0) {
 
324
    /* We don't have any data to inflate; wait until next time */
 
325
    return CURLE_OK;
 
326
  }
 
327
 
 
328
  /* because the buffer size is fixed, iteratively decompress and transfer to
 
329
     the client via client_write. */
 
330
  for (;;) {
 
331
    /* (re)set buffer for decompressed output for every iteration */
 
332
    z->next_out = (Bytef *)&decomp[0];
 
333
    z->avail_out = DSIZ;
 
334
 
 
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 */
 
341
        if (result)
 
342
          return exit_zlib(z, &k->zlib_init, result);
 
343
      }
 
344
 
 
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);
 
350
        else
 
351
          return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
 
352
      }
 
353
 
 
354
      /* Done with these bytes, exit */
 
355
      if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
 
356
        return result;
 
357
    }
 
358
    else {                      /* Error; exit loop, handle below */
 
359
      return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
 
360
    }
 
361
  }
 
362
}
 
363
#endif /* HAVE_LIBZ */