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.2 2002/12/30 17:15:06 alex Exp $";
30 #include "conn-func.h"
38 Zip_InitConn( CONN_ID Idx )
40 /* Kompression fuer Link initialisieren */
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;
51 if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
54 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
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;
64 if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
67 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
71 My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
72 My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
74 Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
75 Conn_SetOption( Idx, CONN_ZIP );
82 Zip_Buffer( CONN_ID Idx, CHAR *Data, INT Len )
84 /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
85 * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
88 assert( Data != NULL );
91 /* Ist noch Platz im Kompressions-Puffer? */
92 if( ZWRITEBUFFER_LEN - My_Connections[Idx].zip.wdatalen < Len + 50 )
94 /* Nein! Puffer zunaechst leeren ...*/
95 if( ! Zip_Flush( Idx )) return FALSE;
99 memmove( My_Connections[Idx].zip.wbuf + My_Connections[Idx].zip.wdatalen, Data, Len );
100 My_Connections[Idx].zip.wdatalen += Len;
107 Zip_Flush( CONN_ID Idx )
109 /* Daten komprimieren und in Schreibpuffer kopieren.
110 * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
115 out = &My_Connections[Idx].zip.out;
117 out->next_in = My_Connections[Idx].zip.wbuf;
118 out->avail_in = My_Connections[Idx].zip.wdatalen;
119 out->next_out = My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen;
120 out->avail_out = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen;
122 result = deflate( out, Z_SYNC_FLUSH );
123 if(( result != Z_OK ) || ( out->avail_in > 0 ))
125 Log( LOG_ALERT, "Compression error: code %d!?", result );
126 Conn_Close( Idx, "Compression error!", NULL, FALSE );
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;
141 Unzip_Buffer( CONN_ID Idx )
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! */
147 INT result, in_len, out_len;
150 assert( Idx > NONE );
152 if( My_Connections[Idx].zip.rdatalen <= 0 ) return TRUE;
154 in = &My_Connections[Idx].zip.in;
156 in->next_in = My_Connections[Idx].zip.rbuf;
157 in->avail_in = My_Connections[Idx].zip.rdatalen;
158 in->next_out = My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen;
159 in->avail_out = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1;
161 result = inflate( in, Z_SYNC_FLUSH );
164 Log( LOG_ALERT, "Decompression error: code %d (ni=%d, ai=%d, no=%d, ao=%d)!?", result, in->next_in, in->avail_in, in->next_out, in->avail_out );
165 Conn_Close( Idx, "Decompression error!", NULL, FALSE );
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;
173 if( in->avail_in > 0 )
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 );
180 else My_Connections[Idx].zip.rdatalen = 0;
181 My_Connections[Idx].zip.bytes_in += out_len;
188 Zip_SendBytes( CONN_ID Idx )
190 /* Anzahl gesendeter Bytes (komprimiert!) liefern */
192 assert( Idx > NONE );
193 return My_Connections[Idx].zip.bytes_out;
194 } /* Zip_SendBytes */
198 Zip_RecvBytes( CONN_ID Idx )
200 /* Anzahl gesendeter Bytes (komprimiert!) liefern */
202 assert( Idx > NONE );
203 return My_Connections[Idx].zip.bytes_in;
204 } /* Zip_RecvBytes */