Blob


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 */
15 #include "portab.h"
17 #define CONN_MODULE
20 #ifdef ZLIB
22 static char UNUSED id[] = "$Id: conn-zip.c,v 1.5 2004/04/25 13:55:36 alex Exp $";
24 #include "imp.h"
25 #include <assert.h>
26 #include <string.h>
27 #include <zlib.h>
29 #include "conn.h"
30 #include "conn-func.h"
31 #include "log.h"
33 #include "exp.h"
34 #include "conn-zip.h"
37 GLOBAL BOOLEAN
38 Zip_InitConn( CONN_ID Idx )
39 {
40 /* Kompression fuer Link initialisieren */
42 assert( Idx > NONE );
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 )
52 {
53 /* Fehler! */
54 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
55 return FALSE;
56 }
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 )
65 {
66 /* Fehler! */
67 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
68 return FALSE;
69 }
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 );
77 return TRUE;
78 } /* Zip_InitConn */
81 GLOBAL BOOLEAN
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. */
87 assert( Idx > NONE );
88 assert( Data != NULL );
89 assert( Len > 0 );
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 }
98 /* Daten kopieren */
99 memmove( My_Connections[Idx].zip.wbuf + My_Connections[Idx].zip.wdatalen, Data, Len );
100 My_Connections[Idx].zip.wdatalen += Len;
102 return TRUE;
103 } /* Zip_Buffer */
106 GLOBAL BOOLEAN
107 Zip_Flush( CONN_ID Idx )
109 /* Daten komprimieren und in Schreibpuffer kopieren.
110 * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
112 INT result, out_len;
113 z_stream *out;
115 out = &My_Connections[Idx].zip.out;
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;
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 );
127 return 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;
136 return TRUE;
137 } /* Zip_Flush */
140 GLOBAL BOOLEAN
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;
148 z_stream *in;
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 = (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;
161 result = inflate( in, Z_SYNC_FLUSH );
162 if( result != Z_OK )
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;
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;
183 return TRUE;
184 } /* Unzip_Buffer */
187 GLOBAL LONG
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 */
197 GLOBAL LONG
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 */
207 #endif
210 /* -eof- */