]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-zip.c
Removed some line feeds in debug statements.
[ngircd-alex.git] / src / ngircd / conn-zip.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by 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 static char UNUSED id[] = "$Id: conn-zip.c,v 1.9 2005/08/02 22:48:57 alex Exp $";
23
24 #include "imp.h"
25 #include <assert.h>
26 #include <string.h>
27 #include <zlib.h>
28
29 #include "conn.h"
30 #include "conn-func.h"
31 #include "log.h"
32
33 #include "array.h"
34 #include "exp.h"
35 #include "conn-zip.h"
36
37
38 GLOBAL bool
39 Zip_InitConn( CONN_ID Idx )
40 {
41         /* Kompression fuer Link initialisieren */
42
43         assert( Idx > NONE );
44
45         My_Connections[Idx].zip.in.avail_in = 0;
46         My_Connections[Idx].zip.in.total_in = 0;
47         My_Connections[Idx].zip.in.total_out = 0;
48         My_Connections[Idx].zip.in.zalloc = NULL;
49         My_Connections[Idx].zip.in.zfree = NULL;
50         My_Connections[Idx].zip.in.data_type = Z_ASCII;
51
52         if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
53         {
54                 /* Fehler! */
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         {
67                 /* Fehler! */
68                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
69                 return false;
70         }
71
72         My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
73         My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
74
75         Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
76         Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
77
78         return true;
79 } /* Zip_InitConn */
80
81
82 GLOBAL bool
83 Zip_Buffer( CONN_ID Idx, char *Data, int Len )
84 {
85         /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
86         * Es wird true bei Erfolg, sonst false geliefert. */
87
88         assert( Idx > NONE );
89         assert( Data != NULL );
90         assert( Len > 0 );
91         assert( Len <= ZWRITEBUFFER_LEN );
92
93         if (Len < 0 || Len > ZWRITEBUFFER_LEN) return false;
94
95         if ( array_bytes( &My_Connections[Idx].zip.wbuf ) >= ZWRITEBUFFER_LEN ) {
96                 /* compression buffer is full, flush */
97                 if( ! Zip_Flush( Idx )) return false;
98         }
99
100         return array_catb( &My_Connections[Idx].zip.wbuf, Data, Len );
101 } /* Zip_Buffer */
102
103
104 GLOBAL bool
105 Zip_Flush( CONN_ID Idx )
106 {
107         /* Daten komprimieren und in Schreibpuffer kopieren.
108         * Es wird true bei Erfolg, sonst false geliefert. */
109
110         int result;
111         unsigned char zipbuf[WRITEBUFFER_LEN];
112         unsigned int zipbuf_used = 0;
113         z_stream *out;
114
115         out = &My_Connections[Idx].zip.out;
116
117         out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
118         assert(out->next_in);
119         if (!out->next_in)
120                 return false;
121
122         out->avail_in = array_bytes(&My_Connections[Idx].zip.wbuf);
123
124         out->next_out = zipbuf;
125         out->avail_out = sizeof zipbuf;
126
127         Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d", out->avail_in, out->avail_out);
128         result = deflate( out, Z_SYNC_FLUSH );
129         if(( result != Z_OK ) || ( out->avail_in > 0 ))
130         {
131                 Log( LOG_ALERT, "Compression error: code %d!?", result );
132                 Conn_Close( Idx, "Compression error!", NULL, false );
133                 return false;
134         }
135
136         assert(out->avail_out <= WRITEBUFFER_LEN);
137         zipbuf_used = WRITEBUFFER_LEN - out->avail_out;
138         Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
139         if (!array_catb( &My_Connections[Idx].wbuf, (char*) zipbuf, zipbuf_used ))
140                 return false;
141
142         My_Connections[Idx].bytes_out += zipbuf_used;
143         My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf); 
144         array_trunc(&My_Connections[Idx].zip.wbuf);
145
146         return true;
147 } /* Zip_Flush */
148
149
150 GLOBAL bool
151 Unzip_Buffer( CONN_ID Idx )
152 {
153         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
154         * wird false geliefert, ansonsten true. Der Fall, dass keine
155         * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
156
157         int result;
158         unsigned char unzipbuf[READBUFFER_LEN];
159         unsigned int unzipbuf_used = 0;
160         unsigned int z_rdatalen;
161         unsigned int in_len;
162         
163         z_stream *in;
164
165         assert( Idx > NONE );
166
167         z_rdatalen = array_bytes(&My_Connections[Idx].zip.rbuf);
168         if (z_rdatalen == 0)
169                 return true;
170
171         in = &My_Connections[Idx].zip.in;
172         
173         in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
174         assert(in->next_in);
175         if (!in->next_in)
176                 return false;
177
178         in->avail_in = z_rdatalen;
179         in->next_out = unzipbuf;
180         in->avail_out = sizeof unzipbuf;
181
182         Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d", in->avail_in, in->avail_out);
183         result = inflate( in, Z_SYNC_FLUSH );
184         if( result != Z_OK )
185         {
186                 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 );
187                 Conn_Close( Idx, "Decompression error!", NULL, false );
188                 return false;
189         }
190
191         assert(z_rdatalen >= in->avail_in);
192         in_len = z_rdatalen - in->avail_in;
193         unzipbuf_used = READBUFFER_LEN - in->avail_out;
194         Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,  in->avail_out, unzipbuf_used);
195         assert(unzipbuf_used <= READBUFFER_LEN);
196         if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf, unzipbuf_used))
197                 return false;
198
199         if( in->avail_in > 0 ) {
200                 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
201         } else {
202                 array_trunc( &My_Connections[Idx].zip.rbuf );
203                 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
204         }
205
206         return true;
207 } /* Unzip_Buffer */
208
209
210 GLOBAL long
211 Zip_SendBytes( CONN_ID Idx )
212 {
213         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
214
215         assert( Idx > NONE );
216         return My_Connections[Idx].zip.bytes_out;
217 } /* Zip_SendBytes */
218
219
220 GLOBAL long
221 Zip_RecvBytes( CONN_ID Idx )
222 {
223         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
224
225         assert( Idx > NONE );
226         return My_Connections[Idx].zip.bytes_in;
227 } /* Zip_RecvBytes */
228
229
230 #endif
231
232
233 /* -eof- */