1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2002, 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.
22 ***************************************************************************/
29 #include <curl/curl.h>
30 #include <curl/types.h>
33 #define DSIZ 4096 /* buffer size for decompressed data */
37 process_zlib_error(struct SessionHandle *data, z_stream *z)
40 failf (data, "Error while processing content unencoding.\n%s",
43 failf (data, "Error while processing content unencoding.\n"
44 "Unknown failure within decompression software.");
46 return CURLE_BAD_CONTENT_ENCODING;
50 exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
58 Curl_unencode_deflate_write(struct SessionHandle *data,
59 struct Curl_transfer_keeper *k,
62 int status; /* zlib status */
63 int result; /* Curl_client_write status */
64 char decomp[DSIZ]; /* Put the decompressed data here. */
65 z_stream *z = &k->z; /* zlib state structure */
67 /* Initialize zlib? */
69 z->zalloc = (alloc_func)Z_NULL;
70 z->zfree = (free_func)Z_NULL;
71 z->opaque = 0; /* of dubious use 08/27/02 jhrg */
72 if (inflateInit(z) != Z_OK)
73 return process_zlib_error(data, z);
77 /* Set the compressed input when this fucntion is called */
78 z->next_in = (Bytef *)k->str;
81 /* because the buffer size is fixed, iteratively decompress
82 and transfer to the client via client_write. */
84 /* (re)set buffer for decompressed output for every iteration */
85 z->next_out = (Bytef *)&decomp[0];
88 status = inflate(z, Z_SYNC_FLUSH);
89 if (status == Z_OK || status == Z_STREAM_END) {
90 result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
92 /* if !CURLE_OK, clean up, return */
94 return exit_zlib(z, &k->zlib_init, result);
97 /* Done?; clean up, return */
98 if (status == Z_STREAM_END) {
99 if (inflateEnd(z) == Z_OK)
100 return exit_zlib(z, &k->zlib_init, result);
102 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
105 /* Done with these bytes, exit */
106 if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
109 else { /* Error; exit loop, handle below */
110 return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
114 #endif /* HAVE_LIBZ */
118 * eval: (load-file "../curl-mode.el")
121 * vim: et sw=2 ts=2 sts=2 tw=78