2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
11 * Connection compression using ZLIB
22 static char UNUSED id[] = "$Id: conn-zip.c,v 1.10 2006/05/10 21:24:01 alex Exp $";
30 #include "conn-func.h"
39 Zip_InitConn( CONN_ID Idx )
41 /* Kompression fuer Link initialisieren */
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;
52 if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
55 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
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;
65 if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
68 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
72 My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
73 My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
75 Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
76 Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
83 Zip_Buffer( CONN_ID Idx, char *Data, size_t Len )
85 /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
86 * Es wird true bei Erfolg, sonst false geliefert. */
89 assert( Data != NULL );
91 assert( Len <= ZWRITEBUFFER_LEN );
93 if (Len > ZWRITEBUFFER_LEN)
96 if ( array_bytes( &My_Connections[Idx].zip.wbuf ) >= ZWRITEBUFFER_LEN ) {
97 /* compression buffer is full, flush */
98 if( ! Zip_Flush( Idx )) return false;
101 return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
106 Zip_Flush( CONN_ID Idx )
108 /* Daten komprimieren und in Schreibpuffer kopieren.
109 * Es wird true bei Erfolg, sonst false geliefert. */
112 unsigned char zipbuf[WRITEBUFFER_LEN];
116 out = &My_Connections[Idx].zip.out;
118 out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
122 out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
124 out->next_out = zipbuf;
125 out->avail_out = (uInt)sizeof zipbuf;
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 ))
131 Log( LOG_ALERT, "Compression error: code %d!?", result );
132 Conn_Close( Idx, "Compression error!", NULL, false );
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))
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);
152 Unzip_Buffer( CONN_ID Idx )
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! */
159 unsigned char unzipbuf[READBUFFER_LEN];
160 int unzipbuf_used = 0;
161 unsigned int z_rdatalen;
166 assert( Idx > NONE );
168 z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
172 in = &My_Connections[Idx].zip.in;
174 in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
178 in->avail_in = z_rdatalen;
179 in->next_out = unzipbuf;
180 in->avail_out = (uInt)sizeof unzipbuf;
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 );
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 );
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))
200 if( in->avail_in > 0 ) {
201 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
203 array_trunc( &My_Connections[Idx].zip.rbuf );
204 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
212 Zip_SendBytes( CONN_ID Idx )
214 /* Anzahl gesendeter Bytes (komprimiert!) liefern */
216 assert( Idx > NONE );
217 return My_Connections[Idx].zip.bytes_out;
218 } /* Zip_SendBytes */
222 Zip_RecvBytes( CONN_ID Idx )
224 /* Anzahl gesendeter Bytes (komprimiert!) liefern */
226 assert( Idx > NONE );
227 return My_Connections[Idx].zip.bytes_in;
228 } /* Zip_RecvBytes */