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