updated source code boilerplate/header
[platform/upstream/curl.git] / lib / content_encoding.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2002, 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$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #ifdef HAVE_LIBZ
27
28 #include "urldata.h"
29 #include <curl/curl.h>
30 #include <curl/types.h>
31 #include "sendf.h"
32
33 #define DSIZ 4096               /* buffer size for decompressed data */
34
35
36 static CURLcode
37 process_zlib_error(struct SessionHandle *data, z_stream *z)
38 {
39   if (z->msg)
40     failf (data, "Error while processing content unencoding.\n%s",
41            z->msg);
42   else
43     failf (data, "Error while processing content unencoding.\n"
44            "Unknown failure within decompression software.");
45
46   return CURLE_BAD_CONTENT_ENCODING;
47 }
48
49 static CURLcode
50 exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
51 {
52   inflateEnd(z);
53   *zlib_init = 0;
54   return result;
55 }
56
57 CURLcode
58 Curl_unencode_deflate_write(struct SessionHandle *data, 
59                             struct Curl_transfer_keeper *k,
60                             ssize_t nread)
61 {
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 */
66               
67   /* Initialize zlib? */
68   if (!k->zlib_init) {
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);
74     k->zlib_init = 1;
75   }
76
77   /* Set the compressed input when this fucntion is called */
78   z->next_in = (Bytef *)k->str;
79   z->avail_in = nread;
80
81   /* because the buffer size is fixed, iteratively decompress
82      and transfer to the client via client_write. */
83   for (;;) {
84     /* (re)set buffer for decompressed output for every iteration */
85     z->next_out = (Bytef *)&decomp[0];
86     z->avail_out = DSIZ;
87
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, 
91                                  DSIZ - z->avail_out);
92       /* if !CURLE_OK, clean up, return */
93       if (result) {              
94         return exit_zlib(z, &k->zlib_init, result);
95       }
96
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);
101         else
102           return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
103       }
104
105       /* Done with these bytes, exit */
106       if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0) 
107         return result;
108     }
109     else {                      /* Error; exit loop, handle below */
110       return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
111     }
112   }
113 }
114 #endif /* HAVE_LIBZ */
115
116 /*
117  * local variables:
118  * eval: (load-file "../curl-mode.el")
119  * end:
120  * vim600: fdm=marker
121  * vim: et sw=2 ts=2 sts=2 tw=78
122  */