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