]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-zip.c
Implement the IRC command "SERVLIST"
[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
15 #include "portab.h"
16
17 #define CONN_MODULE
18
19
20 #ifdef ZLIB
21
22 /* enable more zlib related debug messages: */
23 /* #define DEBUG_ZLIB */
24
25 static char UNUSED id[] = "$Id: conn-zip.c,v 1.16 2007/05/17 23:34:24 alex Exp $";
26
27 #include "imp.h"
28 #include <assert.h>
29 #include <string.h>
30 #include <zlib.h>
31
32 #include "conn.h"
33 #include "conn-func.h"
34 #include "log.h"
35
36 #include "array.h"
37 #include "exp.h"
38 #include "conn-zip.h"
39
40
41 GLOBAL bool
42 Zip_InitConn( CONN_ID Idx )
43 {
44         /* Kompression fuer Link initialisieren */
45
46         assert( Idx > NONE );
47
48         My_Connections[Idx].zip.in.avail_in = 0;
49         My_Connections[Idx].zip.in.total_in = 0;
50         My_Connections[Idx].zip.in.total_out = 0;
51         My_Connections[Idx].zip.in.zalloc = NULL;
52         My_Connections[Idx].zip.in.zfree = NULL;
53         My_Connections[Idx].zip.in.data_type = Z_ASCII;
54
55         if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
56         {
57                 /* Fehler! */
58                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
59                 return false;
60         }
61
62         My_Connections[Idx].zip.out.total_in = 0;
63         My_Connections[Idx].zip.out.total_in = 0;
64         My_Connections[Idx].zip.out.zalloc = NULL;
65         My_Connections[Idx].zip.out.zfree = NULL;
66         My_Connections[Idx].zip.out.data_type = Z_ASCII;
67
68         if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
69         {
70                 /* Fehler! */
71                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
72                 return false;
73         }
74
75         My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
76         My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
77
78         Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
79         Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
80
81         return true;
82 } /* Zip_InitConn */
83
84
85 /**
86  * Copy data to the compression buffer of a connection. We do collect
87  * some data there until it's full so that we can achieve better
88  * compression ratios.
89  * If the (pre-)compression buffer is full, we try to flush it ("actually
90  * compress some data") and to add the new (uncompressed) data afterwards.
91  * @param Idx Connection handle.
92  * @param Data Pointer to the data.
93  * @param Len Length of the data to add.
94  * @return true on success, false otherwise. */
95 GLOBAL bool
96 Zip_Buffer( CONN_ID Idx, char *Data, size_t Len )
97 {
98         size_t buflen;
99
100         assert( Idx > NONE );
101         assert( Data != NULL );
102         assert( Len > 0 );
103
104         buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
105         if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
106                 /* compression buffer is full, flush */
107                 if( ! Zip_Flush( Idx )) return false;
108         }
109
110         /* check again; if zip buf is still too large do not append data:
111          * otherwise the zip wbuf would grow too large */
112         buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
113         if (buflen + Len >= WRITEBUFFER_SLINK_LEN)
114                 return false;
115
116         return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
117 } /* Zip_Buffer */
118
119
120 /**
121  * Compress data in ZIP buffer and move result to the write buffer of
122  * the connection.
123  * @param Idx Connection handle.
124  * @retrun 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 overvlow!?");
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 GLOBAL bool
188 Unzip_Buffer( CONN_ID Idx )
189 {
190         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
191         * wird false geliefert, ansonsten true. Der Fall, dass keine
192         * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
193
194         int result;
195         unsigned char unzipbuf[READBUFFER_LEN];
196         int unzipbuf_used = 0;
197         unsigned int z_rdatalen;
198         unsigned int in_len;
199         
200         z_stream *in;
201
202         assert( Idx > NONE );
203
204         z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
205         if (z_rdatalen == 0)
206                 return true;
207
208         in = &My_Connections[Idx].zip.in;
209
210         in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
211         assert(in->next_in != NULL);
212
213         in->avail_in = z_rdatalen;
214         in->next_out = unzipbuf;
215         in->avail_out = (uInt)sizeof unzipbuf;
216
217 #ifdef DEBUG_ZIP
218         Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
219                 in->avail_in, in->avail_out);
220 #endif
221         result = inflate( in, Z_SYNC_FLUSH );
222         if( result != Z_OK )
223         {
224                 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 );
225                 Conn_Close( Idx, "Decompression error!", NULL, false );
226                 return false;
227         }
228
229         assert(z_rdatalen >= in->avail_in);
230         in_len = z_rdatalen - in->avail_in;
231         unzipbuf_used = READBUFFER_LEN - in->avail_out;
232 #ifdef DEBUG_ZIP
233         Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
234                 in->avail_out, unzipbuf_used);
235 #endif
236         assert(unzipbuf_used <= READBUFFER_LEN);
237         if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
238                         (size_t)unzipbuf_used))
239                 return false;
240
241         if( in->avail_in > 0 ) {
242                 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
243         } else {
244                 array_trunc( &My_Connections[Idx].zip.rbuf );
245                 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
246         }
247
248         return true;
249 } /* Unzip_Buffer */
250
251
252 GLOBAL long
253 Zip_SendBytes( CONN_ID Idx )
254 {
255         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
256
257         assert( Idx > NONE );
258         return My_Connections[Idx].zip.bytes_out;
259 } /* Zip_SendBytes */
260
261
262 GLOBAL long
263 Zip_RecvBytes( CONN_ID Idx )
264 {
265         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
266
267         assert( Idx > NONE );
268         return My_Connections[Idx].zip.bytes_in;
269 } /* Zip_RecvBytes */
270
271
272 #endif
273
274
275 /* -eof- */