conn-zip.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  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. #define CONN_MODULE
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * Connection compression using ZLIB
  16. */
  17. /* Additionan debug messages related to ZIP compression: 0=off / 1=on */
  18. #define DEBUG_ZIP 0
  19. #ifdef ZLIB
  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 "conn-zip.h"
  28. GLOBAL bool
  29. Zip_InitConn( CONN_ID Idx )
  30. {
  31. /* initialize zlib compression on this link */
  32. assert( Idx > NONE );
  33. My_Connections[Idx].zip.in.avail_in = 0;
  34. My_Connections[Idx].zip.in.total_in = 0;
  35. My_Connections[Idx].zip.in.total_out = 0;
  36. My_Connections[Idx].zip.in.zalloc = NULL;
  37. My_Connections[Idx].zip.in.zfree = NULL;
  38. My_Connections[Idx].zip.in.data_type = Z_ASCII;
  39. if (inflateInit( &My_Connections[Idx].zip.in ) != Z_OK) {
  40. Log(LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx);
  41. return false;
  42. }
  43. My_Connections[Idx].zip.out.total_in = 0;
  44. My_Connections[Idx].zip.out.total_in = 0;
  45. My_Connections[Idx].zip.out.zalloc = NULL;
  46. My_Connections[Idx].zip.out.zfree = NULL;
  47. My_Connections[Idx].zip.out.data_type = Z_ASCII;
  48. if (deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK) {
  49. Log(LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx);
  50. return false;
  51. }
  52. My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
  53. My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
  54. Log(LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx);
  55. Conn_OPTION_ADD( &My_Connections[Idx], CONN_ZIP );
  56. return true;
  57. } /* Zip_InitConn */
  58. /**
  59. * Copy data to the compression buffer of a connection. We do collect
  60. * some data there until it's full so that we can achieve better
  61. * compression ratios.
  62. * If the (pre-)compression buffer is full, we try to flush it ("actually
  63. * compress some data") and to add the new (uncompressed) data afterwards.
  64. * This function closes the connection on error.
  65. * @param Idx Connection handle.
  66. * @param Data Pointer to the data.
  67. * @param Len Length of the data to add.
  68. * @return true on success, false otherwise.
  69. */
  70. GLOBAL bool
  71. Zip_Buffer( CONN_ID Idx, const char *Data, size_t Len )
  72. {
  73. size_t buflen;
  74. assert( Idx > NONE );
  75. assert( Data != NULL );
  76. assert( Len > 0 );
  77. buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
  78. if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
  79. /* compression buffer is full, flush */
  80. if( ! Zip_Flush( Idx )) return false;
  81. }
  82. /* check again; if zip buf is still too large do not append data:
  83. * otherwise the zip wbuf would grow too large */
  84. buflen = array_bytes(&My_Connections[Idx].zip.wbuf);
  85. if (buflen + Len >= WRITEBUFFER_SLINK_LEN) {
  86. Log(LOG_ALERT, "Zip Write buffer space exhausted: %lu bytes", buflen + Len);
  87. Conn_Close(Idx, "Zip Write buffer space exhausted", NULL, false);
  88. return false;
  89. }
  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. * This function closes the connection on error.
  96. * @param Idx Connection handle.
  97. * @return true on success, false otherwise.
  98. */
  99. GLOBAL bool
  100. Zip_Flush( CONN_ID Idx )
  101. {
  102. int result;
  103. unsigned char zipbuf[WRITEBUFFER_SLINK_LEN];
  104. int zipbuf_used = 0;
  105. z_stream *out;
  106. out = &My_Connections[Idx].zip.out;
  107. out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
  108. if (!out->avail_in)
  109. return true; /* nothing to do. */
  110. out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
  111. assert(out->next_in != NULL);
  112. out->next_out = zipbuf;
  113. out->avail_out = (uInt)sizeof zipbuf;
  114. #if DEBUG_ZIP
  115. Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
  116. out->avail_in, out->avail_out);
  117. #endif
  118. result = deflate( out, Z_SYNC_FLUSH );
  119. if(( result != Z_OK ) || ( out->avail_in > 0 ))
  120. {
  121. Log( LOG_ALERT, "Compression error: code %d!?", result );
  122. Conn_Close( Idx, "Compression error!", NULL, false );
  123. return false;
  124. }
  125. if (out->avail_out <= 0) {
  126. /* Not all data was compressed, because data became
  127. * bigger while compressing it. */
  128. Log(LOG_ALERT, "Compression error: buffer overflow!?");
  129. Conn_Close(Idx, "Compression error!", NULL, false);
  130. return false;
  131. }
  132. assert(out->avail_out <= WRITEBUFFER_SLINK_LEN);
  133. zipbuf_used = WRITEBUFFER_SLINK_LEN - out->avail_out;
  134. #if DEBUG_ZIP
  135. Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
  136. #endif
  137. if (!array_catb(&My_Connections[Idx].wbuf,
  138. (char *)zipbuf, (size_t) zipbuf_used)) {
  139. Log (LOG_ALERT, "Compression error: can't copy data!?");
  140. Conn_Close(Idx, "Compression error!", NULL, false);
  141. return false;
  142. }
  143. My_Connections[Idx].bytes_out += zipbuf_used;
  144. My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf);
  145. array_trunc(&My_Connections[Idx].zip.wbuf);
  146. return true;
  147. } /* Zip_Flush */
  148. /**
  149. * uncompress data and copy it to read buffer.
  150. * Returns true if data has been unpacked or no
  151. * compressed data is currently pending in the zread buffer.
  152. * This function closes the connection on error.
  153. * @param Idx Connection handle.
  154. * @return true on success, false otherwise.
  155. */
  156. GLOBAL bool
  157. Unzip_Buffer( CONN_ID Idx )
  158. {
  159. int result;
  160. unsigned char unzipbuf[READBUFFER_LEN];
  161. int unzipbuf_used = 0;
  162. unsigned int z_rdatalen;
  163. unsigned int in_len;
  164. z_stream *in;
  165. assert( Idx > NONE );
  166. z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
  167. if (z_rdatalen == 0)
  168. return true;
  169. in = &My_Connections[Idx].zip.in;
  170. in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
  171. assert(in->next_in != NULL);
  172. in->avail_in = z_rdatalen;
  173. in->next_out = unzipbuf;
  174. in->avail_out = (uInt)sizeof unzipbuf;
  175. #if DEBUG_ZIP
  176. Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
  177. in->avail_in, in->avail_out);
  178. #endif
  179. result = inflate( in, Z_SYNC_FLUSH );
  180. if( result != Z_OK )
  181. {
  182. 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);
  183. Conn_Close(Idx, "Decompression error!", NULL, false);
  184. return false;
  185. }
  186. assert(z_rdatalen >= in->avail_in);
  187. in_len = z_rdatalen - in->avail_in;
  188. unzipbuf_used = READBUFFER_LEN - in->avail_out;
  189. #if DEBUG_ZIP
  190. Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
  191. in->avail_out, unzipbuf_used);
  192. #endif
  193. assert(unzipbuf_used <= READBUFFER_LEN);
  194. if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
  195. (size_t)unzipbuf_used)) {
  196. Log (LOG_ALERT, "Decompression error: can't copy data!?");
  197. Conn_Close(Idx, "Decompression error!", NULL, false);
  198. return false;
  199. }
  200. if( in->avail_in > 0 ) {
  201. array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
  202. } else {
  203. array_trunc( &My_Connections[Idx].zip.rbuf );
  204. My_Connections[Idx].zip.bytes_in += unzipbuf_used;
  205. }
  206. return true;
  207. } /* Unzip_Buffer */
  208. /**
  209. * @param Idx Connection handle.
  210. * @return amount of sent (compressed) bytes
  211. */
  212. GLOBAL long
  213. Zip_SendBytes( CONN_ID Idx )
  214. {
  215. assert( Idx > NONE );
  216. return My_Connections[Idx].zip.bytes_out;
  217. } /* Zip_SendBytes */
  218. /**
  219. * @param Idx Connection handle.
  220. * @return amount of received (compressed) bytes
  221. */
  222. GLOBAL long
  223. Zip_RecvBytes( CONN_ID Idx )
  224. {
  225. assert( Idx > NONE );
  226. return My_Connections[Idx].zip.bytes_in;
  227. } /* Zip_RecvBytes */
  228. #endif
  229. /* -eof- */