]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-zip.c
Output some debug messages only if DEBUG_ZIP is #define'd.
[ngircd-alex.git] / src / ngircd / conn-zip.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2006 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.11 2006/07/23 15:19:20 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 GLOBAL bool
86 Zip_Buffer( CONN_ID Idx, char *Data, size_t Len )
87 {
88         /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
89         * Es wird true bei Erfolg, sonst false geliefert. */
90
91         assert( Idx > NONE );
92         assert( Data != NULL );
93         assert( Len > 0 );
94         assert( Len <= ZWRITEBUFFER_LEN );
95
96         if (Len > ZWRITEBUFFER_LEN)
97                 return false;
98
99         if ( array_bytes( &My_Connections[Idx].zip.wbuf ) >= ZWRITEBUFFER_LEN ) {
100                 /* compression buffer is full, flush */
101                 if( ! Zip_Flush( Idx )) return false;
102         }
103
104         return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
105 } /* Zip_Buffer */
106
107
108 GLOBAL bool
109 Zip_Flush( CONN_ID Idx )
110 {
111         /* Daten komprimieren und in Schreibpuffer kopieren.
112         * Es wird true bei Erfolg, sonst false geliefert. */
113
114         int result;
115         unsigned char zipbuf[WRITEBUFFER_LEN];
116         int zipbuf_used = 0;
117         z_stream *out;
118
119         out = &My_Connections[Idx].zip.out;
120
121         out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
122         if (!out->next_in)
123                 return false;
124
125         out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
126
127         out->next_out = zipbuf;
128         out->avail_out = (uInt)sizeof zipbuf;
129
130 #ifdef DEBUG_ZIP
131         Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
132                 out->avail_in, out->avail_out);
133 #endif
134         result = deflate( out, Z_SYNC_FLUSH );
135         if(( result != Z_OK ) || ( out->avail_in > 0 ))
136         {
137                 Log( LOG_ALERT, "Compression error: code %d!?", result );
138                 Conn_Close( Idx, "Compression error!", NULL, false );
139                 return false;
140         }
141
142         assert(out->avail_out <= WRITEBUFFER_LEN);
143         zipbuf_used = WRITEBUFFER_LEN - out->avail_out;
144 #ifdef DEBUG_ZIP
145         Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
146 #endif
147         if (!array_catb(&My_Connections[Idx].wbuf,
148                         (char *)zipbuf, (size_t) zipbuf_used))
149                 return false;
150
151         My_Connections[Idx].bytes_out += zipbuf_used;
152         My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf); 
153         array_trunc(&My_Connections[Idx].zip.wbuf);
154
155         return true;
156 } /* Zip_Flush */
157
158
159 GLOBAL bool
160 Unzip_Buffer( CONN_ID Idx )
161 {
162         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
163         * wird false geliefert, ansonsten true. Der Fall, dass keine
164         * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
165
166         int result;
167         unsigned char unzipbuf[READBUFFER_LEN];
168         int unzipbuf_used = 0;
169         unsigned int z_rdatalen;
170         unsigned int in_len;
171         
172         z_stream *in;
173
174         assert( Idx > NONE );
175
176         z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
177         if (z_rdatalen == 0)
178                 return true;
179
180         in = &My_Connections[Idx].zip.in;
181         
182         in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
183         if (!in->next_in)
184                 return false;
185
186         in->avail_in = z_rdatalen;
187         in->next_out = unzipbuf;
188         in->avail_out = (uInt)sizeof unzipbuf;
189
190 #ifdef DEBUG_ZIP
191         Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
192                 in->avail_in, in->avail_out);
193 #endif
194         result = inflate( in, Z_SYNC_FLUSH );
195         if( result != Z_OK )
196         {
197                 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 );
198                 Conn_Close( Idx, "Decompression error!", NULL, false );
199                 return false;
200         }
201
202         assert(z_rdatalen >= in->avail_in);
203         in_len = z_rdatalen - in->avail_in;
204         unzipbuf_used = READBUFFER_LEN - in->avail_out;
205 #ifdef DEBUG_ZIP
206         Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
207                 in->avail_out, unzipbuf_used);
208 #endif
209         assert(unzipbuf_used <= READBUFFER_LEN);
210         if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
211                         (size_t)unzipbuf_used))
212                 return false;
213
214         if( in->avail_in > 0 ) {
215                 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
216         } else {
217                 array_trunc( &My_Connections[Idx].zip.rbuf );
218                 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
219         }
220
221         return true;
222 } /* Unzip_Buffer */
223
224
225 GLOBAL long
226 Zip_SendBytes( CONN_ID Idx )
227 {
228         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
229
230         assert( Idx > NONE );
231         return My_Connections[Idx].zip.bytes_out;
232 } /* Zip_SendBytes */
233
234
235 GLOBAL long
236 Zip_RecvBytes( CONN_ID Idx )
237 {
238         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
239
240         assert( Idx > NONE );
241         return My_Connections[Idx].zip.bytes_in;
242 } /* Zip_RecvBytes */
243
244
245 #endif
246
247
248 /* -eof- */