content_encoding.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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 https://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  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
25 #ifdef HAVE_LIBZ
26 
27 #include "urldata.h"
28 #include <curl/curl.h>
29 #include "sendf.h"
30 #include "content_encoding.h"
31 #include "strdup.h"
32 #include "curl_memory.h"
33 #include "memdebug.h"
34 
35 /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
36  (doing so will reduce code size slightly). */
37 #define OLD_ZLIB_SUPPORT 1
38 
39 #define DSIZ CURL_MAX_WRITE_SIZE /* 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 voidpf
53 zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
54 {
55  (void) opaque;
56  /* not a typo, keep it calloc() */
57  return (voidpf) calloc(items, size);
58 }
59 
60 static void
61 zfree_cb(voidpf opaque, voidpf ptr)
62 {
63  (void) opaque;
64  free(ptr);
65 }
66 
67 static CURLcode
68 process_zlib_error(struct connectdata *conn, z_stream *z)
69 {
70  struct Curl_easy *data = conn->data;
71  if(z->msg)
72  failf(data, "Error while processing content unencoding: %s",
73  z->msg);
74  else
75  failf(data, "Error while processing content unencoding: "
76  "Unknown failure within decompression software.");
77 
79 }
80 
81 static CURLcode
82 exit_zlib(z_stream *z, zlibInitState *zlib_init, CURLcode result)
83 {
84  inflateEnd(z);
85  *zlib_init = ZLIB_UNINIT;
86  return result;
87 }
88 
89 static CURLcode
90 inflate_stream(struct connectdata *conn,
91  struct SingleRequest *k)
92 {
93  int allow_restart = 1;
94  z_stream *z = &k->z; /* zlib state structure */
95  uInt nread = z->avail_in;
96  Bytef *orig_in = z->next_in;
97  int status; /* zlib status */
98  CURLcode result = CURLE_OK; /* Curl_client_write status */
99  char *decomp; /* Put the decompressed data here. */
100 
101  /* Dynamically allocate a buffer for decompression because it's uncommonly
102  large to hold on the stack */
103  decomp = malloc(DSIZ);
104  if(decomp == NULL) {
105  return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
106  }
107 
108  /* because the buffer size is fixed, iteratively decompress and transfer to
109  the client via client_write. */
110  for(;;) {
111  /* (re)set buffer for decompressed output for every iteration */
112  z->next_out = (Bytef *)decomp;
113  z->avail_out = DSIZ;
114 
115  status = inflate(z, Z_SYNC_FLUSH);
116  if(status == Z_OK || status == Z_STREAM_END) {
117  allow_restart = 0;
118  if((DSIZ - z->avail_out) && (!k->ignorebody)) {
119  result = Curl_client_write(conn, CLIENTWRITE_BODY, decomp,
120  DSIZ - z->avail_out);
121  /* if !CURLE_OK, clean up, return */
122  if(result) {
123  free(decomp);
124  return exit_zlib(z, &k->zlib_init, result);
125  }
126  }
127 
128  /* Done? clean up, return */
129  if(status == Z_STREAM_END) {
130  free(decomp);
131  if(inflateEnd(z) == Z_OK)
132  return exit_zlib(z, &k->zlib_init, result);
133  return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
134  }
135 
136  /* Done with these bytes, exit */
137 
138  /* status is always Z_OK at this point! */
139  if(z->avail_in == 0) {
140  free(decomp);
141  return result;
142  }
143  }
144  else if(allow_restart && status == Z_DATA_ERROR) {
145  /* some servers seem to not generate zlib headers, so this is an attempt
146  to fix and continue anyway */
147 
148  (void) inflateEnd(z); /* don't care about the return code */
149  if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
150  free(decomp);
151  return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
152  }
153  z->next_in = orig_in;
154  z->avail_in = nread;
155  allow_restart = 0;
156  continue;
157  }
158  else { /* Error; exit loop, handle below */
159  free(decomp);
160  return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
161  }
162  }
163  /* Will never get here */
164 }
165 
166 CURLcode
168  struct SingleRequest *k,
169  ssize_t nread)
170 {
171  z_stream *z = &k->z; /* zlib state structure */
172 
173  /* Initialize zlib? */
174  if(k->zlib_init == ZLIB_UNINIT) {
175  memset(z, 0, sizeof(z_stream));
176  z->zalloc = (alloc_func)zalloc_cb;
177  z->zfree = (free_func)zfree_cb;
178 
179  if(inflateInit(z) != Z_OK)
180  return process_zlib_error(conn, z);
181  k->zlib_init = ZLIB_INIT;
182  }
183 
184  /* Set the compressed input when this function is called */
185  z->next_in = (Bytef *)k->str;
186  z->avail_in = (uInt)nread;
187 
188  /* Now uncompress the data */
189  return inflate_stream(conn, k);
190 }
191 
192 #ifdef OLD_ZLIB_SUPPORT
193 /* Skip over the gzip header */
194 static enum {
195  GZIP_OK,
196  GZIP_BAD,
197  GZIP_UNDERFLOW
198 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
199 {
200  int method, flags;
201  const ssize_t totallen = len;
202 
203  /* The shortest header is 10 bytes */
204  if(len < 10)
205  return GZIP_UNDERFLOW;
206 
207  if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
208  return GZIP_BAD;
209 
210  method = data[2];
211  flags = data[3];
212 
213  if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
214  /* Can't handle this compression method or unknown flag */
215  return GZIP_BAD;
216  }
217 
218  /* Skip over time, xflags, OS code and all previous bytes */
219  len -= 10;
220  data += 10;
221 
222  if(flags & EXTRA_FIELD) {
223  ssize_t extra_len;
224 
225  if(len < 2)
226  return GZIP_UNDERFLOW;
227 
228  extra_len = (data[1] << 8) | data[0];
229 
230  if(len < (extra_len + 2))
231  return GZIP_UNDERFLOW;
232 
233  len -= (extra_len + 2);
234  data += (extra_len + 2);
235  }
236 
237  if(flags & ORIG_NAME) {
238  /* Skip over NUL-terminated file name */
239  while(len && *data) {
240  --len;
241  ++data;
242  }
243  if(!len || *data)
244  return GZIP_UNDERFLOW;
245 
246  /* Skip over the NUL */
247  --len;
248  ++data;
249  }
250 
251  if(flags & COMMENT) {
252  /* Skip over NUL-terminated comment */
253  while(len && *data) {
254  --len;
255  ++data;
256  }
257  if(!len || *data)
258  return GZIP_UNDERFLOW;
259 
260  /* Skip over the NUL */
261  --len;
262  }
263 
264  if(flags & HEAD_CRC) {
265  if(len < 2)
266  return GZIP_UNDERFLOW;
267 
268  len -= 2;
269  }
270 
271  *headerlen = totallen - len;
272  return GZIP_OK;
273 }
274 #endif
275 
276 CURLcode
278  struct SingleRequest *k,
279  ssize_t nread)
280 {
281  z_stream *z = &k->z; /* zlib state structure */
282 
283  /* Initialize zlib? */
284  if(k->zlib_init == ZLIB_UNINIT) {
285  memset(z, 0, sizeof(z_stream));
286  z->zalloc = (alloc_func)zalloc_cb;
287  z->zfree = (free_func)zfree_cb;
288 
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);
293  }
294  k->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
295  }
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);
300  }
301  k->zlib_init = ZLIB_INIT; /* Initial call state */
302  }
303  }
304 
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);
311  }
312 
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_WRITE_ERROR);
317 
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.
324  *
325  * This is only required with zlib versions < 1.2.0.4 as newer versions
326  * can handle the gzip header themselves.
327  */
328 
329  switch(k->zlib_init) {
330  /* Skip over gzip header? */
331  case ZLIB_INIT:
332  {
333  /* Initial call state */
334  ssize_t hlen;
335 
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;
342 
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.
350  */
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);
355  }
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;
360 
361  case GZIP_BAD:
362  default:
363  return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
364  }
365 
366  }
367  break;
368 
369  case ZLIB_GZIP_HEADER:
370  {
371  /* Need more gzip header data state */
372  ssize_t hlen;
373  z->avail_in += (uInt)nread;
374  z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
375  if(z->next_in == NULL) {
376  return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
377  }
378  /* Append the new block of data to the previous one */
379  memcpy(z->next_in + z->avail_in - nread, k->str, nread);
380 
381  switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
382  case GZIP_OK:
383  /* This is the zlib stream data */
384  free(z->next_in);
385  /* Don't point into the malloced block since we just freed it */
386  z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
387  z->avail_in = (uInt)(z->avail_in - hlen);
388  k->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
389  break;
390 
391  case GZIP_UNDERFLOW:
392  /* We still don't have any data to inflate! */
393  return CURLE_OK;
394 
395  case GZIP_BAD:
396  default:
397  free(z->next_in);
398  return exit_zlib(z, &k->zlib_init, process_zlib_error(conn, z));
399  }
400 
401  }
402  break;
403 
404  case ZLIB_GZIP_INFLATING:
405  default:
406  /* Inflating stream state */
407  z->next_in = (Bytef *)k->str;
408  z->avail_in = (uInt)nread;
409  break;
410  }
411 
412  if(z->avail_in == 0) {
413  /* We don't have any data to inflate; wait until next time */
414  return CURLE_OK;
415  }
416 
417  /* We've parsed the header, now uncompress the data */
418  return inflate_stream(conn, k);
419 #endif
420 }
421 
422 void Curl_unencode_cleanup(struct connectdata *conn)
423 {
424  struct Curl_easy *data = conn->data;
425  struct SingleRequest *k = &data->req;
426  z_stream *z = &k->z;
427  if(k->zlib_init != ZLIB_UNINIT)
428  (void) exit_zlib(z, &k->zlib_init, CURLE_OK);
429 }
430 
431 #endif /* HAVE_LIBZ */
#define free(ptr)
Definition: curl_memory.h:130
#define CLIENTWRITE_BODY
Definition: sendf.h:50
#define failf
Definition: sendf.h:48
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
#define malloc(size)
Definition: curl_memory.h:124
UNITTEST_START int result
Definition: unit1304.c:49
size_t len
Definition: curl_sasl.c:55
memcpy(filename, filename1, strlen(filename1))
struct SingleRequest req
Definition: urldata.h:1761
bool ignorebody
Definition: urldata.h:586
UNITTEST_START struct Curl_easy data
Definition: unit1399.c:82
void * Curl_saferealloc(void *ptr, size_t size)
Definition: strdup.c:93
CURLcode Curl_unencode_gzip_write(struct connectdata *conn, struct SingleRequest *k, ssize_t nread)
Definition: curl.h:455
CURLcode Curl_client_write(struct connectdata *conn, int type, char *ptr, size_t len)
Definition: sendf.c:624
#define ssize_t
Definition: config-win32.h:382
char * str
Definition: urldata.h:550
#define Curl_unencode_cleanup(x)
size_t size
Definition: unit1302.c:52
struct Curl_message msg
Definition: urldata.h:1744
CURLcode Curl_unencode_deflate_write(struct connectdata *conn, struct SingleRequest *req, ssize_t nread)
Definition: debug.c:29
#define calloc(nbelem, size)
Definition: curl_memory.h:126
struct Curl_easy * data
Definition: urldata.h:791


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08