2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2007 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
20 /* enable more zlib related debug messages: */
21 /* #define DEBUG_ZLIB */
29 #include "conn-func.h"
38 Zip_InitConn( CONN_ID Idx )
40 /* initialize zlib compression on this link */
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 Log(LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx);
56 My_Connections[Idx].zip.out.total_in = 0;
57 My_Connections[Idx].zip.out.total_in = 0;
58 My_Connections[Idx].zip.out.zalloc = NULL;
59 My_Connections[Idx].zip.out.zfree = NULL;
60 My_Connections[Idx].zip.out.data_type = Z_ASCII;
62 if (deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK) {
63 Log(LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx);
67 My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
68 My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
70 Log(LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx);
71 Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
78 * Copy data to the compression buffer of a connection. We do collect
79 * some data there until it's full so that we can achieve better
81 * If the (pre-)compression buffer is full, we try to flush it ("actually
82 * compress some data") and to add the new (uncompressed) data afterwards.
83 * This function closes the connection on error.
84 * @param Idx Connection handle.
85 * @param Data Pointer to the data.
86 * @param Len Length of the data to add.
87 * @return true on success, false otherwise.
90 Zip_Buffer( CONN_ID Idx, const char *Data, size_t Len )
95 assert( Data != NULL );
98 buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
99 if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
100 /* compression buffer is full, flush */
101 if( ! Zip_Flush( Idx )) return false;
104 /* check again; if zip buf is still too large do not append data:
105 * otherwise the zip wbuf would grow too large */
106 buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
107 if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
108 Log(LOG_ALERT, "Zip Write Buffer overflow: %lu bytes\n", buflen + Len);
109 Conn_Close(Idx, "Zip Write buffer overflow", NULL, false);
112 return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
117 * Compress data in ZIP buffer and move result to the write buffer of
119 * This function closes the connection on error.
120 * @param Idx Connection handle.
121 * @return true on success, false otherwise.
124 Zip_Flush( CONN_ID Idx )
127 unsigned char zipbuf[WRITEBUFFER_SLINK_LEN];
131 out = &My_Connections[Idx].zip.out;
133 out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
135 return true; /* nothing to do. */
137 out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
138 assert(out->next_in != NULL);
140 out->next_out = zipbuf;
141 out->avail_out = (uInt)sizeof zipbuf;
144 Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
145 out->avail_in, out->avail_out);
147 result = deflate( out, Z_SYNC_FLUSH );
148 if(( result != Z_OK ) || ( out->avail_in > 0 ))
150 Log( LOG_ALERT, "Compression error: code %d!?", result );
151 Conn_Close( Idx, "Compression error!", NULL, false );
155 if (out->avail_out <= 0) {
156 /* Not all data was compressed, because data became
157 * bigger while compressing it. */
158 Log (LOG_ALERT, "Compression error: buffer overvlow!?");
159 Conn_Close(Idx, "Compression error!", NULL, false);
163 assert(out->avail_out <= WRITEBUFFER_SLINK_LEN);
165 zipbuf_used = WRITEBUFFER_SLINK_LEN - out->avail_out;
167 Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
169 if (!array_catb(&My_Connections[Idx].wbuf,
170 (char *)zipbuf, (size_t) zipbuf_used)) {
171 Log (LOG_ALERT, "Compression error: can't copy data!?");
172 Conn_Close(Idx, "Compression error!", NULL, false);
176 My_Connections[Idx].bytes_out += zipbuf_used;
177 My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf);
178 array_trunc(&My_Connections[Idx].zip.wbuf);
185 * uncompress data and copy it to read buffer.
186 * Returns true if data has been unpacked or no
187 * compressed data is currently pending in the zread buffer.
188 * This function closes the connection on error.
189 * @param Idx Connection handle.
190 * @return true on success, false otherwise.
193 Unzip_Buffer( CONN_ID Idx )
196 unsigned char unzipbuf[READBUFFER_LEN];
197 int unzipbuf_used = 0;
198 unsigned int z_rdatalen;
203 assert( Idx > NONE );
205 z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
209 in = &My_Connections[Idx].zip.in;
211 in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
212 assert(in->next_in != NULL);
214 in->avail_in = z_rdatalen;
215 in->next_out = unzipbuf;
216 in->avail_out = (uInt)sizeof unzipbuf;
219 Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
220 in->avail_in, in->avail_out);
222 result = inflate( in, Z_SYNC_FLUSH );
225 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);
226 Conn_Close(Idx, "Decompression error!", NULL, false);
230 assert(z_rdatalen >= in->avail_in);
231 in_len = z_rdatalen - in->avail_in;
232 unzipbuf_used = READBUFFER_LEN - in->avail_out;
234 Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
235 in->avail_out, unzipbuf_used);
237 assert(unzipbuf_used <= READBUFFER_LEN);
238 if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
239 (size_t)unzipbuf_used)) {
240 Log (LOG_ALERT, "Decompression error: can't copy data!?");
241 Conn_Close(Idx, "Decompression error!", NULL, false);
244 if( in->avail_in > 0 ) {
245 array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
247 array_trunc( &My_Connections[Idx].zip.rbuf );
248 My_Connections[Idx].zip.bytes_in += unzipbuf_used;
256 * @param Idx Connection handle.
257 * @return amount of sent (compressed) bytes
260 Zip_SendBytes( CONN_ID Idx )
262 assert( Idx > NONE );
263 return My_Connections[Idx].zip.bytes_out;
264 } /* Zip_SendBytes */
268 * @param Idx Connection handle.
269 * @return amount of received (compressed) bytes
272 Zip_RecvBytes( CONN_ID Idx )
274 assert( Idx > NONE );
275 return My_Connections[Idx].zip.bytes_in;
276 } /* Zip_RecvBytes */