]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-zip.c
removed Conn_UnsetOption() function. It was not used anymore.
[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.7 2005/04/25 18:37:16 fw 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 "exp.h"
34 #include "conn-zip.h"
35
36
37 GLOBAL bool
38 Zip_InitConn( CONN_ID Idx )
39 {
40         /* Kompression fuer Link initialisieren */
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         {
53                 /* Fehler! */
54                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
55                 return false;
56         }
57
58         My_Connections[Idx].zip.out.total_in = 0;
59         My_Connections[Idx].zip.out.total_in = 0;
60         My_Connections[Idx].zip.out.zalloc = NULL;
61         My_Connections[Idx].zip.out.zfree = NULL;
62         My_Connections[Idx].zip.out.data_type = Z_ASCII;
63
64         if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
65         {
66                 /* Fehler! */
67                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
68                 return false;
69         }
70
71         My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
72         My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
73
74         Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
75         Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
76
77         return true;
78 } /* Zip_InitConn */
79
80
81 GLOBAL bool
82 Zip_Buffer( CONN_ID Idx, char *Data, int Len )
83 {
84         /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
85         * Es wird true bei Erfolg, sonst false geliefert. */
86
87         assert( Idx > NONE );
88         assert( Data != NULL );
89         assert( Len > 0 );
90
91         /* Ist noch Platz im Kompressions-Puffer? */
92         if( ZWRITEBUFFER_LEN - My_Connections[Idx].zip.wdatalen < Len + 50 )
93         {
94                 /* Nein! Puffer zunaechst leeren ...*/
95                 if( ! Zip_Flush( Idx )) return false;
96         }
97
98         /* Daten kopieren */
99         memmove( My_Connections[Idx].zip.wbuf + My_Connections[Idx].zip.wdatalen, Data, Len );
100         My_Connections[Idx].zip.wdatalen += Len;
101
102         return true;
103 } /* Zip_Buffer */
104
105
106 GLOBAL bool
107 Zip_Flush( CONN_ID Idx )
108 {
109         /* Daten komprimieren und in Schreibpuffer kopieren.
110         * Es wird true bei Erfolg, sonst false geliefert. */
111
112         int result, out_len;
113         z_stream *out;
114
115         out = &My_Connections[Idx].zip.out;
116
117         out->next_in = (void *)My_Connections[Idx].zip.wbuf;
118         out->avail_in = My_Connections[Idx].zip.wdatalen;
119         out->next_out = (void *)(My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen);
120         out->avail_out = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen;
121
122         result = deflate( out, Z_SYNC_FLUSH );
123         if(( result != Z_OK ) || ( out->avail_in > 0 ))
124         {
125                 Log( LOG_ALERT, "Compression error: code %d!?", result );
126                 Conn_Close( Idx, "Compression error!", NULL, false );
127                 return false;
128         }
129
130         out_len = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - out->avail_out;
131         My_Connections[Idx].wdatalen += out_len;
132         My_Connections[Idx].bytes_out += out_len;
133         My_Connections[Idx].zip.bytes_out += My_Connections[Idx].zip.wdatalen;
134         My_Connections[Idx].zip.wdatalen = 0;
135
136         return true;
137 } /* Zip_Flush */
138
139
140 GLOBAL bool
141 Unzip_Buffer( CONN_ID Idx )
142 {
143         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
144         * wird false geliefert, ansonsten true. Der Fall, dass keine
145         * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
146
147         int result, in_len, out_len;
148         z_stream *in;
149
150         assert( Idx > NONE );
151
152         if( My_Connections[Idx].zip.rdatalen <= 0 ) return true;
153
154         in = &My_Connections[Idx].zip.in;
155
156         in->next_in = (void *)My_Connections[Idx].zip.rbuf;
157         in->avail_in = My_Connections[Idx].zip.rdatalen;
158         in->next_out = (void *)(My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen);
159         in->avail_out = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1;
160
161         result = inflate( in, Z_SYNC_FLUSH );
162         if( result != Z_OK )
163         {
164                 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 );
165                 Conn_Close( Idx, "Decompression error!", NULL, false );
166                 return false;
167         }
168
169         in_len = My_Connections[Idx].zip.rdatalen - in->avail_in;
170         out_len = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1 - in->avail_out;
171         My_Connections[Idx].rdatalen += out_len;
172
173         if( in->avail_in > 0 )
174         {
175                 /* es konnten nicht alle Daten entpackt werden, vermutlich war
176                 * im Ziel-Puffer kein Platz mehr. Umkopieren ... */
177                 My_Connections[Idx].zip.rdatalen -= in_len;
178                 memmove( My_Connections[Idx].zip.rbuf, My_Connections[Idx].zip.rbuf + in_len, My_Connections[Idx].zip.rdatalen );
179         }
180         else My_Connections[Idx].zip.rdatalen = 0;
181         My_Connections[Idx].zip.bytes_in += out_len;
182
183         return true;
184 } /* Unzip_Buffer */
185
186
187 GLOBAL long
188 Zip_SendBytes( CONN_ID Idx )
189 {
190         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
191
192         assert( Idx > NONE );
193         return My_Connections[Idx].zip.bytes_out;
194 } /* Zip_SendBytes */
195
196
197 GLOBAL long
198 Zip_RecvBytes( CONN_ID Idx )
199 {
200         /* Anzahl gesendeter Bytes (komprimiert!) liefern */
201
202         assert( Idx > NONE );
203         return My_Connections[Idx].zip.bytes_in;
204 } /* Zip_RecvBytes */
205
206
207 #endif
208
209
210 /* -eof- */