conn-zip.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2007 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. */
  13. #include "portab.h"
  14. #define CONN_MODULE
  15. #ifdef ZLIB
  16. /* enable more zlib related debug messages: */
  17. /* #define DEBUG_ZLIB */
  18. static char UNUSED id[] = "$Id: conn-zip.c,v 1.11.2.1 2007/05/18 22:11:19 alex Exp $";
  19. #include "imp.h"
  20. #include <assert.h>
  21. #include <string.h>
  22. #include <zlib.h>
  23. #include "conn.h"
  24. #include "conn-func.h"
  25. #include "log.h"
  26. #include "array.h"
  27. #include "exp.h"
  28. #include "conn-zip.h"
  29. GLOBAL bool
  30. Zip_InitConn( CONN_ID Idx )
  31. {
  32. /* Kompression fuer Link initialisieren */
  33. assert( Idx > NONE );
  34. My_Connections[Idx].zip.in.avail_in = 0;
  35. My_Connections[Idx].zip.in.total_in = 0;
  36. My_Connections[Idx].zip.in.total_out = 0;
  37. My_Connections[Idx].zip.in.zalloc = NULL;
  38. My_Connections[Idx].zip.in.zfree = NULL;
  39. My_Connections[Idx].zip.in.data_type = Z_ASCII;
  40. if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
  41. {
  42. /* Fehler! */
  43. Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
  44. return false;
  45. }
  46. My_Connections[Idx].zip.out.total_in = 0;
  47. My_Connections[Idx].zip.out.total_in = 0;
  48. My_Connections[Idx].zip.out.zalloc = NULL;
  49. My_Connections[Idx].zip.out.zfree = NULL;
  50. My_Connections[Idx].zip.out.data_type = Z_ASCII;
  51. if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
  52. {
  53. /* Fehler! */
  54. Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
  55. return false;
  56. }
  57. My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
  58. My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
  59. Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
  60. Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
  61. return true;
  62. } /* Zip_InitConn */
  63. /**
  64. * Copy data to the compression buffer of a connection. We do collect
  65. * some data there until it's full so that we can achieve better
  66. * compression ratios.
  67. * If the (pre-)compression buffer is full, we try to flush it ("actually
  68. * compress some data") and to add the new (uncompressed) data afterwards.
  69. * @param Idx Connection handle.
  70. * @param Data Pointer to the data.
  71. * @param Len Length of the data to add.
  72. * @return true on success, false otherwise. */
  73. GLOBAL bool
  74. Zip_Buffer( CONN_ID Idx, char *Data, size_t Len )
  75. {
  76. size_t buflen;
  77. assert( Idx > NONE );
  78. assert( Data != NULL );
  79. assert( Len > 0 );
  80. buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
  81. if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
  82. /* compression buffer is full, flush */
  83. if( ! Zip_Flush( Idx )) return false;
  84. }
  85. /* check again; if zip buf is still too large do not append data:
  86. * otherwise the zip wbuf would grow too large */
  87. buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
  88. if (buflen + Len >= WRITEBUFFER_SLINK_LEN)
  89. return false;
  90. return array_catb(&My_Connections[Idx].zip.wbuf, Data, Len);
  91. } /* Zip_Buffer */
  92. /**
  93. * Compress data in ZIP buffer and move result to the write buffer of
  94. * the connection.
  95. * @param Idx Connection handle.
  96. * @retrun true on success, false otherwise.
  97. */
  98. GLOBAL bool
  99. Zip_Flush( CONN_ID Idx )
  100. {
  101. int result;
  102. unsigned char zipbuf[WRITEBUFFER_SLINK_LEN];
  103. int zipbuf_used = 0;
  104. z_stream *out;
  105. out = &My_Connections[Idx].zip.out;
  106. out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
  107. if (!out->avail_in)
  108. return true; /* nothing to do. */
  109. out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
  110. assert(out->next_in != NULL);
  111. out->next_out = zipbuf;
  112. out->avail_out = (uInt)sizeof zipbuf;
  113. #ifdef DEBUG_ZIP
  114. Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
  115. out->avail_in, out->avail_out);
  116. #endif
  117. result = deflate( out, Z_SYNC_FLUSH );
  118. if(( result != Z_OK ) || ( out->avail_in > 0 ))
  119. {
  120. Log( LOG_ALERT, "Compression error: code %d!?", result );
  121. Conn_Close( Idx, "Compression error!", NULL, false );
  122. return false;
  123. }
  124. if (out->avail_out <= 0) {
  125. /* Not all data was compressed, because data became
  126. * bigger while compressing it. */
  127. Log (LOG_ALERT, "Compression error: buffer overvlow!?");
  128. Conn_Close(Idx, "Compression error!", NULL, false);
  129. return false;
  130. }
  131. assert(out->avail_out <= WRITEBUFFER_SLINK_LEN);
  132. zipbuf_used = WRITEBUFFER_SLINK_LEN - out->avail_out;
  133. #ifdef DEBUG_ZIP
  134. Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
  135. #endif
  136. if (!array_catb(&My_Connections[Idx].wbuf,
  137. (char *)zipbuf, (size_t) zipbuf_used)) {
  138. Log (LOG_ALERT, "Compression error: can't copy data!?");
  139. Conn_Close(Idx, "Compression error!", NULL, false);
  140. return false;
  141. }
  142. My_Connections[Idx].bytes_out += zipbuf_used;
  143. My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf);
  144. array_trunc(&My_Connections[Idx].zip.wbuf);
  145. return true;
  146. } /* Zip_Flush */
  147. GLOBAL bool
  148. Unzip_Buffer( CONN_ID Idx )
  149. {
  150. /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
  151. * wird false geliefert, ansonsten true. Der Fall, dass keine
  152. * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
  153. int result;
  154. unsigned char unzipbuf[READBUFFER_LEN];
  155. int unzipbuf_used = 0;
  156. unsigned int z_rdatalen;
  157. unsigned int in_len;
  158. z_stream *in;
  159. assert( Idx > NONE );
  160. z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
  161. if (z_rdatalen == 0)
  162. return true;
  163. in = &My_Connections[Idx].zip.in;
  164. in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
  165. assert(in->next_in != NULL);
  166. in->avail_in = z_rdatalen;
  167. in->next_out = unzipbuf;
  168. in->avail_out = (uInt)sizeof unzipbuf;
  169. #ifdef DEBUG_ZIP
  170. Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
  171. in->avail_in, in->avail_out);
  172. #endif
  173. result = inflate( in, Z_SYNC_FLUSH );
  174. if( result != Z_OK )
  175. {
  176. 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 );
  177. Conn_Close( Idx, "Decompression error!", NULL, false );
  178. return false;
  179. }
  180. assert(z_rdatalen >= in->avail_in);
  181. in_len = z_rdatalen - in->avail_in;
  182. unzipbuf_used = READBUFFER_LEN - in->avail_out;
  183. #ifdef DEBUG_ZIP
  184. Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
  185. in->avail_out, unzipbuf_used);
  186. #endif
  187. assert(unzipbuf_used <= READBUFFER_LEN);
  188. if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
  189. (size_t)unzipbuf_used))
  190. return false;
  191. if( in->avail_in > 0 ) {
  192. array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
  193. } else {
  194. array_trunc( &My_Connections[Idx].zip.rbuf );
  195. My_Connections[Idx].zip.bytes_in += unzipbuf_used;
  196. }
  197. return true;
  198. } /* Unzip_Buffer */
  199. GLOBAL long
  200. Zip_SendBytes( CONN_ID Idx )
  201. {
  202. /* Anzahl gesendeter Bytes (komprimiert!) liefern */
  203. assert( Idx > NONE );
  204. return My_Connections[Idx].zip.bytes_out;
  205. } /* Zip_SendBytes */
  206. GLOBAL long
  207. Zip_RecvBytes( CONN_ID Idx )
  208. {
  209. /* Anzahl gesendeter Bytes (komprimiert!) liefern */
  210. assert( Idx > NONE );
  211. return My_Connections[Idx].zip.bytes_in;
  212. } /* Zip_RecvBytes */
  213. #endif
  214. /* -eof- */