Implement streaming compression support
[quassel.git] / src / common / compressor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
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) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "compressor.h"
22
23 #include <QTcpSocket>
24 #include <QTimer>
25
26 #define MINIZ_HEADER_FILE_ONLY
27 #include "../../3rdparty/miniz/miniz.c"
28
29 const int maxBufferSize = 64 * 1024 * 1024; // protect us from zip bombs
30 const int ioBufferSize = 64 * 1024;         // chunk size for inflate/deflate; should not be too large as we preallocate that space!
31
32 Compressor::Compressor(QTcpSocket *socket, Compressor::CompressionLevel level, QObject *parent)
33     : QObject(parent),
34     _socket(socket),
35     _level(level),
36     _inflater(0),
37     _deflater(0)
38 {
39     connect(socket, SIGNAL(readyRead()), SLOT(readData()));
40
41     bool ok = true;
42     if (level != NoCompression)
43         ok = initStreams();
44
45     if (!ok) {
46         // something went wrong during initialization... but we can only emit an error after RemotePeer has connected its signal
47         QTimer::singleShot(0, this, SIGNAL(error()));
48         return;
49     }
50
51     // It's possible that more data has already arrived during the handshake, so readyRead() wouldn't be triggered.
52     // However, we want to give RemotePeer a chance to connect to our signals, so trigger this asynchronously.
53     if (socket->bytesAvailable())
54         QTimer::singleShot(0, this, SLOT(readData()));
55 }
56
57
58 Compressor::~Compressor()
59 {
60     // release resources allocated by zlib
61     if (_inflater) {
62         inflateEnd(_inflater);
63         delete _inflater;
64     }
65     if (_deflater) {
66         deflateEnd(_deflater);
67         delete _deflater;
68     }
69 }
70
71
72 bool Compressor::initStreams()
73 {
74     int zlevel;
75     switch(compressionLevel()) {
76         case BestCompression:
77             zlevel = 9;
78             break;
79         case BestSpeed:
80             zlevel = 1;
81             break;
82         default:
83             zlevel = Z_DEFAULT_COMPRESSION;
84     }
85
86     _inflater = new z_stream;
87     memset(_inflater, 0, sizeof(z_stream));
88     if (Z_OK != inflateInit(_inflater)) {
89         qWarning() << "Could not initialize the inflate stream!";
90         return false;
91     }
92
93     _deflater = new z_stream;
94     memset(_deflater, 0, sizeof(z_stream));
95     if (Z_OK != deflateInit(_deflater, zlevel)) {
96         qWarning() << "Could not intialize the deflate stream!";
97         return false;
98     }
99
100     _inputBuffer.reserve(ioBufferSize); // pre-allocate space
101     _outputBuffer.resize(ioBufferSize); // not a typo; we never change the size of this buffer anyway (we *do* for _inputBuffer!)
102
103     qDebug() << "Enabling compression...";
104
105     return true;
106 }
107
108
109
110 qint64 Compressor::bytesAvailable() const
111 {
112     return _readBuffer.size();
113 }
114
115
116 qint64 Compressor::read(char *data, qint64 maxSize)
117 {
118     if (maxSize <= 0)
119         maxSize = _readBuffer.size();
120
121     qint64 n = qMin(maxSize, (qint64)_readBuffer.size());
122     memcpy(data, _readBuffer.constData(), n);
123
124     // TODO: don't copy for every read
125     if (n == _readBuffer.size())
126         _readBuffer.clear();
127     else
128         _readBuffer = _readBuffer.mid(n);
129
130     // If there's still data left in the socket buffer, make sure to schedule a read
131     if (_socket->bytesAvailable())
132         QTimer::singleShot(0, this, SLOT(readData()));
133
134     return n;
135 }
136
137
138 // The usual usage pattern is to write a blocksize first, followed by the actual data.
139 // By setting NoFlush, one can indicate that the write buffer should not immediately be
140 // written, which should make things a bit more efficient.
141 qint64 Compressor::write(const char *data, qint64 count, WriteBufferHint flush)
142 {
143     int pos = _writeBuffer.size();
144     _writeBuffer.resize(pos + count);
145     memcpy(_writeBuffer.data() + pos, data, count);
146
147     if (flush != NoFlush)
148         writeData();
149
150     return count;
151 }
152
153
154 void Compressor::readData()
155 {
156     // don't try to read more data if we're already closing
157     if (_socket->state() !=  QAbstractSocket::ConnectedState)
158         return;
159
160     if (!_socket->bytesAvailable() || _readBuffer.size() >= maxBufferSize)
161         return;
162
163     if (compressionLevel() == NoCompression) {
164         _readBuffer.append(_socket->read(maxBufferSize - _readBuffer.size()));
165         emit readyRead();
166         return;
167     }
168
169     // We let zlib directly append to the readBuffer, which means we pre-allocate extra space for ioBufferSize.
170     // Afterwards, we'll shrink the buffer appropriately. Since shrinking should not reallocate, the readBuffer's
171     // capacity should over time adapt to the largest message sizes we encounter. However, this is not a bad thing
172     // considering that otherwise (using an intermediate buffer) we'd copy around data for every single message.
173     // TODO: Benchmark if it would still make sense to squeeze the buffer from time to time (e.g. after initial sync)!
174
175     while (_socket->bytesAvailable() && _readBuffer.size() + ioBufferSize < maxBufferSize && _inputBuffer.size() < ioBufferSize) {
176         _readBuffer.resize(_readBuffer.size() + ioBufferSize);
177         _inputBuffer.append(_socket->read(ioBufferSize - _inputBuffer.size()));
178
179         _inflater->next_in = reinterpret_cast<const unsigned char *>(_inputBuffer.constData());
180         _inflater->avail_in = _inputBuffer.size();
181         _inflater->next_out = reinterpret_cast<unsigned char *>(_readBuffer.data() + _readBuffer.size() - ioBufferSize);
182         _inflater->avail_out = ioBufferSize;
183
184         const unsigned char *orig_out = _inflater->next_out; // so we see if we have actually produced any output
185
186         int status = inflate(_inflater, Z_SYNC_FLUSH); // get as much data as possible
187
188         // adjust input and output buffers
189         _readBuffer.resize(_inflater->next_out - reinterpret_cast<const unsigned char *>(_readBuffer.constData()));
190         if (_inflater->avail_in > 0)
191             memmove(_inputBuffer.data(), _inflater->next_in, _inflater->avail_in);
192         _inputBuffer.resize(_inflater->avail_in);
193
194         if (_inflater->next_out != orig_out)
195             emit readyRead();
196
197         switch(status) {
198             case Z_NEED_DICT:
199             case Z_DATA_ERROR:
200             case Z_MEM_ERROR:
201             case Z_STREAM_ERROR:
202                 qWarning() << "Error while decompressing stream:" << status;
203                 emit error(StreamError);
204                 return;
205             case Z_BUF_ERROR:
206                 // means that we need more input to continue, so this is not an actual error
207                 return;
208             case Z_STREAM_END:
209                 qWarning() << "Reached end of zlib stream!"; // this should really never happen
210                 return;
211             default:
212                 // just try to get more out of the stream
213                 break;
214         }
215     }
216     //qDebug() << "inflate in:" << _inflater->total_in << "out:" << _inflater->total_out << "ratio:" << (double)_inflater->total_in/_inflater->total_out;
217 }
218
219
220 void Compressor::writeData()
221 {
222     if (compressionLevel() == NoCompression) {
223         _socket->write(_writeBuffer);
224         _writeBuffer.clear();
225         return;
226     }
227
228     _deflater->next_in = reinterpret_cast<const unsigned char *>(_writeBuffer.constData());
229     _deflater->avail_in = _writeBuffer.size();
230
231     int status;
232     do {
233         _deflater->next_out = reinterpret_cast<unsigned char *>(_outputBuffer.data());
234         _deflater->avail_out = ioBufferSize;
235         status = deflate(_deflater, Z_PARTIAL_FLUSH);
236         if (status != Z_OK && status != Z_BUF_ERROR) {
237             qWarning() << "Error while compressing stream:" << status;
238             emit error(StreamError);
239             return;
240         }
241
242         if (_deflater->avail_out == ioBufferSize)
243             continue; // nothing to write here
244
245         if (!_socket->write(_outputBuffer.constData(), ioBufferSize - _deflater->avail_out)) {
246             qWarning() << "Error while writing to socket:" << _socket->errorString();
247             emit error(DeviceError);
248             return;
249         }
250     } while (_deflater->avail_out == 0); // the output buffer being full is the only reason we should have to loop here!
251
252     if (_deflater->avail_in > 0) {
253         qWarning() << "Oops, something weird happened: data still remaining in write buffer!";
254         emit error(StreamError);
255     }
256
257     _writeBuffer.resize(0);
258
259     //qDebug() << "deflate in:" << _deflater->total_in << "out:" << _deflater->total_out << "ratio:" << (double)_deflater->total_out/_deflater->total_in;
260 }
261
262
263 void Compressor::flush()
264 {
265     if (compressionLevel() == NoCompression && _socket->state() == QAbstractSocket::ConnectedState)
266         _socket->flush();
267
268     // FIXME: missing impl for enabled compression; but then we're not using this method yet
269 }