2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2006 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 /* enable more zlib related debug messages: */
23 /* #define DEBUG_ZLIB */
25 static char UNUSED id[] = "$Id: conn-zip.c,v 1.13 2007/05/17 13:49:49 alex Exp $";
33 #include "conn-func.h"
42 Zip_InitConn( CONN_ID Idx )
44 /* Kompression fuer Link initialisieren */
48 My_Connections[Idx].zip.in.avail_in = 0;
49 My_Connections[Idx].zip.in.total_in = 0;
50 My_Connections[Idx].zip.in.total_out = 0;
51 My_Connections[Idx].zip.in.zalloc = NULL;
52 My_Connections[Idx].zip.in.zfree = NULL;
53 My_Connections[Idx].zip.in.data_type = Z_ASCII;
55 if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
58 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
62 My_Connections[Idx].zip.out.total_in = 0;
63 My_Connections[Idx].zip.out.total_in = 0;
64 My_Connections[Idx].zip.out.zalloc = NULL;
65 My_Connections[Idx].zip.out.zfree = NULL;
66 My_Connections[Idx].zip.out.data_type = Z_ASCII;
68 if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
71 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
75 My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
76 My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
78 Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
79 Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
86 Zip_Buffer( CONN_ID Idx, char *Data, size_t Len )
91 assert( Data != NULL );
94 buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
95 if (buflen >= WRITEBUFFER_LEN) {
96 /* compression buffer is full, flush */
97 if( ! Zip_Flush( Idx )) return false;
100 /* check again; if zip buf is still too large do not append data:
101 * otherwise the zip wbuf would grow too large */
102 buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
103 if (buflen >= WRITEBUFFER_LEN)
105 return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
110 * Compress data in ZIP buffer and move result to the write buffer of
112 * @param Idx Connection handle.
113 * @retrun true on success, false otherwise.
116 Zip_Flush( CONN_ID Idx )
119 unsigned char zipbuf[WRITEBUFFER_LEN];
123 out = &My_Connections[Idx].zip.out;
125 out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
129 out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
131 return true; /* nothing to do. */
133 out->next_out = zipbuf;
134 out->avail_out = (uInt)sizeof zipbuf;
137 Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
138 out->avail_in, out->avail_out);
140 result = deflate( out, Z_SYNC_FLUSH );
141 if(( result != Z_OK ) || ( out->avail_in > 0 ))
143 Log( LOG_ALERT, "Compression error: code %d!?", result );
144 Conn_Close( Idx, "Compression error!", NULL, false );
148 if (out->avail_out <= 0) {
149 /* Not all data was compressed, because data became
150 * bigger while compressing it. */
151 Log (LOG_ALERT, "Compression error: buffer overvlow!?");
152 Conn_Close(Idx, "Compression error!", NULL, false);
156 assert(out->avail_out <= WRITEBUFFER_LEN);
158 zipbuf_used = WRITEBUFFER_LEN - out->avail_out;
160 Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
162 if (!array_catb(&My_Connections[Idx].wbuf,
163 (char *)zipbuf, (size_t) zipbuf_used))
166 My_Connections[Idx].bytes_out += zipbuf_used;
167 My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf);
168 array_trunc(&My_Connections[Idx].zip.wbuf);
175 Unzip_Buffer( CONN_ID Idx )
177 /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
178 * wird false geliefert, ansonsten true. Der Fall, dass keine
179 * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
182 unsigned char unzipbuf[READBUFFER_LEN];
183 int unzipbuf_used = 0;
184 unsigned int z_rdatalen;
189 assert( Idx > NONE );
191 z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
195 in = &My_Connections[Idx].zip.in;
197 in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
201 in->avail_in = z_rdatalen;
202 in->next_out = unzipbuf;
203 in->avail_out = (uInt)sizeof unzipbuf;
206 Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
207 in->avail_in, in->avail_out);
209 result = inflate( in, Z_SYNC_FLUSH );
212 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 );
213 Conn_Close( Idx, "Decompression error!", NULL, false );
217 assert(z_rdatalen >= in->avail_in);
218 in_len = z_rdatalen - in->avail_in;
219 unzipbuf_used = READBUFFER_LEN - in->avail_out;
221 Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
222 in->avail_out, unzipbuf_used);
224 assert(unzipbuf_used <= READBUFFER_LEN);
225 if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
226 (size_t)unzipbuf_used))
229 if( in->avail_in > 0 ) {
230 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
232 array_trunc( &My_Connections[Idx].zip.rbuf );
233 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
241 Zip_SendBytes( CONN_ID Idx )
243 /* Anzahl gesendeter Bytes (komprimiert!) liefern */
245 assert( Idx > NONE );
246 return My_Connections[Idx].zip.bytes_out;
247 } /* Zip_SendBytes */
251 Zip_RecvBytes( CONN_ID Idx )
253 /* Anzahl gesendeter Bytes (komprimiert!) liefern */
255 assert( Idx > NONE );
256 return My_Connections[Idx].zip.bytes_in;
257 } /* Zip_RecvBytes */