]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-zip.c
Zip_Flush(): close the connection in all error cases.
[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.15 2007/05/17 15:16:47 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         size_t buflen;
89
90         assert( Idx > NONE );
91         assert( Data != NULL );
92         assert( Len > 0 );
93
94         buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
95         if (buflen >= WRITEBUFFER_LEN) {
96                 /* compression buffer is full, flush */
97                 if( ! Zip_Flush( Idx )) return false;
98         }
99
100         /* check again; if zip buf is still too large do not append data:
101          * otherwise the zip wbuf would grow too large */
102         buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
103         if (buflen >= WRITEBUFFER_LEN)
104                 return false;
105         return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
106 } /* Zip_Buffer */
107
108
109 /**
110  * Compress data in ZIP buffer and move result to the write buffer of
111  * the connection.
112  * @param Idx Connection handle.
113  * @retrun true on success, false otherwise.
114  */
115 GLOBAL bool
116 Zip_Flush( CONN_ID Idx )
117 {
118         int result;
119         unsigned char zipbuf[WRITEBUFFER_LEN];
120         int zipbuf_used = 0;
121         z_stream *out;
122
123         out = &My_Connections[Idx].zip.out;
124
125         out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
126         if (!out->avail_in)
127                 return true;    /* nothing to do. */
128
129         out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
130         assert(out->next_in != NULL);
131
132         out->next_out = zipbuf;
133         out->avail_out = (uInt)sizeof zipbuf;
134
135 #ifdef DEBUG_ZIP
136         Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
137                 out->avail_in, out->avail_out);
138 #endif
139         result = deflate( out, Z_SYNC_FLUSH );
140         if(( result != Z_OK ) || ( out->avail_in > 0 ))
141         {
142                 Log( LOG_ALERT, "Compression error: code %d!?", result );
143                 Conn_Close( Idx, "Compression error!", NULL, false );
144                 return false;
145         }
146
147         if (out->avail_out <= 0) {
148                 /* Not all data was compressed, because data became
149                  * bigger while compressing it. */
150                 Log (LOG_ALERT, "Compression error: buffer overvlow!?");
151                 Conn_Close(Idx, "Compression error!", NULL, false);
152                 return false;
153         }
154
155         assert(out->avail_out <= WRITEBUFFER_LEN);
156
157         zipbuf_used = WRITEBUFFER_LEN - out->avail_out;
158 #ifdef DEBUG_ZIP
159         Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
160 #endif
161         if (!array_catb(&My_Connections[Idx].wbuf,
162                         (char *)zipbuf, (size_t) zipbuf_used)) {
163                 Log (LOG_ALERT, "Compression error: can't copy data!?");
164                 Conn_Close(Idx, "Compression error!", NULL, false);
165                 return false;
166         }
167
168         My_Connections[Idx].bytes_out += zipbuf_used;
169         My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf); 
170         array_trunc(&My_Connections[Idx].zip.wbuf);
171
172         return true;
173 } /* Zip_Flush */
174
175
176 GLOBAL bool
177 Unzip_Buffer( CONN_ID Idx )
178 {
179         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
180         * wird false geliefert, ansonsten true. Der Fall, dass keine
181         * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
182
183         int result;
184         unsigned char unzipbuf[READBUFFER_LEN];
185         int unzipbuf_used = 0;
186         unsigned int z_rdatalen;
187         unsigned int in_len;
188         
189         z_stream *in;
190
191         assert( Idx > NONE );
192
193         z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
194         if (z_rdatalen == 0)
195                 return true;
196
197         in = &My_Connections[Idx].zip.in;
198
199         in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
200         assert(in->next_in != NULL);
201
202         in->avail_in = z_rdatalen;
203         in->next_out = unzipbuf;
204         in->avail_out = (uInt)sizeof unzipbuf;
205
206 #ifdef DEBUG_ZIP
207         Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
208                 in->avail_in, in->avail_out);
209 #endif
210         result = inflate( in, Z_SYNC_FLUSH );
211         if( result != Z_OK )
212         {
213                 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 );
214                 Conn_Close( Idx, "Decompression error!", NULL, false );
215                 return false;
216         }
217
218         assert(z_rdatalen >= in->avail_in);
219         in_len = z_rdatalen - in->avail_in;
220         unzipbuf_used = READBUFFER_LEN - in->avail_out;
221 #ifdef DEBUG_ZIP
222         Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
223                 in->avail_out, unzipbuf_used);
224 #endif
225         assert(unzipbuf_used <= READBUFFER_LEN);
226         if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
227                         (size_t)unzipbuf_used))
228                 return false;
229
230         if( in->avail_in > 0 ) {
231                 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
232         } else {
233                 array_trunc( &My_Connections[Idx].zip.rbuf );
234                 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
235         }
236
237         return true;
238 } /* Unzip_Buffer */
239
240
241 GLOBAL long
242 Zip_SendBytes( CONN_ID Idx )
243 {
244         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
245
246         assert( Idx > NONE );
247         return My_Connections[Idx].zip.bytes_out;
248 } /* Zip_SendBytes */
249
250
251 GLOBAL long
252 Zip_RecvBytes( CONN_ID Idx )
253 {
254         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
255
256         assert( Idx > NONE );
257         return My_Connections[Idx].zip.bytes_in;
258 } /* Zip_RecvBytes */
259
260
261 #endif
262
263
264 /* -eof- */