]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-zip.c
Use some more specific data types (e. g. pid_t vs. int), make "SPLint" happy :-)
[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.10 2006/05/10 21:24:01 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, size_t 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 > ZWRITEBUFFER_LEN)
94                 return false;
95
96         if ( array_bytes( &My_Connections[Idx].zip.wbuf ) >= ZWRITEBUFFER_LEN ) {
97                 /* compression buffer is full, flush */
98                 if( ! Zip_Flush( Idx )) return false;
99         }
100
101         return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
102 } /* Zip_Buffer */
103
104
105 GLOBAL bool
106 Zip_Flush( CONN_ID Idx )
107 {
108         /* Daten komprimieren und in Schreibpuffer kopieren.
109         * Es wird true bei Erfolg, sonst false geliefert. */
110
111         int result;
112         unsigned char zipbuf[WRITEBUFFER_LEN];
113         int zipbuf_used = 0;
114         z_stream *out;
115
116         out = &My_Connections[Idx].zip.out;
117
118         out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
119         if (!out->next_in)
120                 return false;
121
122         out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
123
124         out->next_out = zipbuf;
125         out->avail_out = (uInt)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,
140                         (char *)zipbuf, (size_t) zipbuf_used))
141                 return false;
142
143         My_Connections[Idx].bytes_out += zipbuf_used;
144         My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf); 
145         array_trunc(&My_Connections[Idx].zip.wbuf);
146
147         return true;
148 } /* Zip_Flush */
149
150
151 GLOBAL bool
152 Unzip_Buffer( CONN_ID Idx )
153 {
154         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
155         * wird false geliefert, ansonsten true. Der Fall, dass keine
156         * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
157
158         int result;
159         unsigned char unzipbuf[READBUFFER_LEN];
160         int unzipbuf_used = 0;
161         unsigned int z_rdatalen;
162         unsigned int in_len;
163         
164         z_stream *in;
165
166         assert( Idx > NONE );
167
168         z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
169         if (z_rdatalen == 0)
170                 return true;
171
172         in = &My_Connections[Idx].zip.in;
173         
174         in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
175         if (!in->next_in)
176                 return false;
177
178         in->avail_in = z_rdatalen;
179         in->next_out = unzipbuf;
180         in->avail_out = (uInt)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,
197                         (size_t)unzipbuf_used))
198                 return false;
199
200         if( in->avail_in > 0 ) {
201                 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
202         } else {
203                 array_trunc( &My_Connections[Idx].zip.rbuf );
204                 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
205         }
206
207         return true;
208 } /* Unzip_Buffer */
209
210
211 GLOBAL long
212 Zip_SendBytes( CONN_ID Idx )
213 {
214         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
215
216         assert( Idx > NONE );
217         return My_Connections[Idx].zip.bytes_out;
218 } /* Zip_SendBytes */
219
220
221 GLOBAL long
222 Zip_RecvBytes( CONN_ID Idx )
223 {
224         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
225
226         assert( Idx > NONE );
227         return My_Connections[Idx].zip.bytes_in;
228 } /* Zip_RecvBytes */
229
230
231 #endif
232
233
234 /* -eof- */