]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-zip.c
conn-zip: fix error handling
[ngircd-alex.git] / src / ngircd / conn-zip.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2007 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * Connection compression using ZLIB
12  */
13
14 #include "portab.h"
15
16 #define CONN_MODULE
17
18 #ifdef ZLIB
19
20 /* enable more zlib related debug messages: */
21 /* #define DEBUG_ZLIB */
22
23 #include "imp.h"
24 #include <assert.h>
25 #include <string.h>
26 #include <zlib.h>
27
28 #include "conn.h"
29 #include "conn-func.h"
30 #include "log.h"
31
32 #include "array.h"
33 #include "exp.h"
34 #include "conn-zip.h"
35
36
37 GLOBAL bool
38 Zip_InitConn( CONN_ID Idx )
39 {
40         /* initialize zlib compression on this link */
41
42         assert( Idx > NONE );
43
44         My_Connections[Idx].zip.in.avail_in = 0;
45         My_Connections[Idx].zip.in.total_in = 0;
46         My_Connections[Idx].zip.in.total_out = 0;
47         My_Connections[Idx].zip.in.zalloc = NULL;
48         My_Connections[Idx].zip.in.zfree = NULL;
49         My_Connections[Idx].zip.in.data_type = Z_ASCII;
50
51         if (inflateInit( &My_Connections[Idx].zip.in ) != Z_OK) {
52                 Log(LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx);
53                 return false;
54         }
55
56         My_Connections[Idx].zip.out.total_in = 0;
57         My_Connections[Idx].zip.out.total_in = 0;
58         My_Connections[Idx].zip.out.zalloc = NULL;
59         My_Connections[Idx].zip.out.zfree = NULL;
60         My_Connections[Idx].zip.out.data_type = Z_ASCII;
61
62         if (deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK) {
63                 Log(LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx);
64                 return false;
65         }
66
67         My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
68         My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
69
70         Log(LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx);
71         Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
72
73         return true;
74 } /* Zip_InitConn */
75
76
77 /**
78  * Copy data to the compression buffer of a connection. We do collect
79  * some data there until it's full so that we can achieve better
80  * compression ratios.
81  * If the (pre-)compression buffer is full, we try to flush it ("actually
82  * compress some data") and to add the new (uncompressed) data afterwards.
83  * This function closes the connection on error.
84  * @param Idx Connection handle.
85  * @param Data Pointer to the data.
86  * @param Len Length of the data to add.
87  * @return true on success, false otherwise.
88  */
89 GLOBAL bool
90 Zip_Buffer( CONN_ID Idx, const char *Data, size_t Len )
91 {
92         size_t buflen;
93
94         assert( Idx > NONE );
95         assert( Data != NULL );
96         assert( Len > 0 );
97
98         buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
99         if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
100                 /* compression buffer is full, flush */
101                 if( ! Zip_Flush( Idx )) return false;
102         }
103
104         /* check again; if zip buf is still too large do not append data:
105          * otherwise the zip wbuf would grow too large */
106         buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
107         if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
108                 Log(LOG_ALERT, "Zip Write Buffer overflow: %lu bytes\n", buflen + Len);
109                 Conn_Close(Idx, "Zip Write buffer overflow", NULL, false);
110                 return false;
111         }
112         return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
113 } /* Zip_Buffer */
114
115
116 /**
117  * Compress data in ZIP buffer and move result to the write buffer of
118  * the connection.
119  * This function closes the connection on error.
120  * @param Idx Connection handle.
121  * @return true on success, false otherwise.
122  */
123 GLOBAL bool
124 Zip_Flush( CONN_ID Idx )
125 {
126         int result;
127         unsigned char zipbuf[WRITEBUFFER_SLINK_LEN];
128         int zipbuf_used = 0;
129         z_stream *out;
130
131         out = &My_Connections[Idx].zip.out;
132
133         out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
134         if (!out->avail_in)
135                 return true;    /* nothing to do. */
136
137         out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
138         assert(out->next_in != NULL);
139
140         out->next_out = zipbuf;
141         out->avail_out = (uInt)sizeof zipbuf;
142
143 #ifdef DEBUG_ZIP
144         Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
145                 out->avail_in, out->avail_out);
146 #endif
147         result = deflate( out, Z_SYNC_FLUSH );
148         if(( result != Z_OK ) || ( out->avail_in > 0 ))
149         {
150                 Log( LOG_ALERT, "Compression error: code %d!?", result );
151                 Conn_Close( Idx, "Compression error!", NULL, false );
152                 return false;
153         }
154
155         if (out->avail_out <= 0) {
156                 /* Not all data was compressed, because data became
157                  * bigger while compressing it. */
158                 Log (LOG_ALERT, "Compression error: buffer overvlow!?");
159                 Conn_Close(Idx, "Compression error!", NULL, false);
160                 return false;
161         }
162
163         assert(out->avail_out <= WRITEBUFFER_SLINK_LEN);
164
165         zipbuf_used = WRITEBUFFER_SLINK_LEN - out->avail_out;
166 #ifdef DEBUG_ZIP
167         Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
168 #endif
169         if (!array_catb(&My_Connections[Idx].wbuf,
170                         (char *)zipbuf, (size_t) zipbuf_used)) {
171                 Log (LOG_ALERT, "Compression error: can't copy data!?");
172                 Conn_Close(Idx, "Compression error!", NULL, false);
173                 return false;
174         }
175
176         My_Connections[Idx].bytes_out += zipbuf_used;
177         My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf); 
178         array_trunc(&My_Connections[Idx].zip.wbuf);
179
180         return true;
181 } /* Zip_Flush */
182
183
184 /**
185  * uncompress data and copy it to read buffer.
186  * Returns true if data has been unpacked or no
187  * compressed data is currently pending in the zread buffer.
188  * This function closes the connection on error.
189  * @param Idx Connection handle.
190  * @return true on success, false otherwise.
191  */
192 GLOBAL bool
193 Unzip_Buffer( CONN_ID Idx )
194 {
195         int result;
196         unsigned char unzipbuf[READBUFFER_LEN];
197         int unzipbuf_used = 0;
198         unsigned int z_rdatalen;
199         unsigned int in_len;
200         
201         z_stream *in;
202
203         assert( Idx > NONE );
204
205         z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
206         if (z_rdatalen == 0)
207                 return true;
208
209         in = &My_Connections[Idx].zip.in;
210
211         in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
212         assert(in->next_in != NULL);
213
214         in->avail_in = z_rdatalen;
215         in->next_out = unzipbuf;
216         in->avail_out = (uInt)sizeof unzipbuf;
217
218 #ifdef DEBUG_ZIP
219         Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
220                 in->avail_in, in->avail_out);
221 #endif
222         result = inflate( in, Z_SYNC_FLUSH );
223         if( result != Z_OK )
224         {
225                 Log(LOG_ALERT, "Decompression error: %s (code=%d, ni=%d, ai=%d, no=%d, ao=%d)!?", in->msg, result, in->next_in, in->avail_in, in->next_out, in->avail_out);
226                 Conn_Close(Idx, "Decompression error!", NULL, false);
227                 return false;
228         }
229
230         assert(z_rdatalen >= in->avail_in);
231         in_len = z_rdatalen - in->avail_in;
232         unzipbuf_used = READBUFFER_LEN - in->avail_out;
233 #ifdef DEBUG_ZIP
234         Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
235                 in->avail_out, unzipbuf_used);
236 #endif
237         assert(unzipbuf_used <= READBUFFER_LEN);
238         if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
239                         (size_t)unzipbuf_used)) {
240                 Log (LOG_ALERT, "Decompression error: can't copy data!?");
241                 Conn_Close(Idx, "Decompression error!", NULL, false);
242                 return false;
243         }
244         if( in->avail_in > 0 ) {
245                 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
246         } else {
247                 array_trunc( &My_Connections[Idx].zip.rbuf );
248                 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
249         }
250
251         return true;
252 } /* Unzip_Buffer */
253
254
255 /**
256  * @param Idx Connection handle.
257  * @return amount of sent (compressed) bytes
258  */
259 GLOBAL long
260 Zip_SendBytes( CONN_ID Idx )
261 {
262         assert( Idx > NONE );
263         return My_Connections[Idx].zip.bytes_out;
264 } /* Zip_SendBytes */
265
266
267 /**
268  * @param Idx Connection handle.
269  * @return amount of received (compressed) bytes
270  */
271 GLOBAL long
272 Zip_RecvBytes( CONN_ID Idx )
273 {
274         assert( Idx > NONE );
275         return My_Connections[Idx].zip.bytes_in;
276 } /* Zip_RecvBytes */
277
278
279 #endif
280
281
282 /* -eof- */