File indexing completed on 2024-06-16 05:01:51

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
0004 ** All rights reserved.
0005 ** Contact: Nokia Corporation (qt-info@nokia.com)
0006 **
0007 ** This file is part of the Qt Messaging Framework.
0008 **
0009 ** $QT_BEGIN_LICENSE:LGPL$
0010 ** GNU Lesser General Public License Usage
0011 ** This file may be used under the terms of the GNU Lesser General Public
0012 ** License version 2.1 as published by the Free Software Foundation and
0013 ** appearing in the file LICENSE.LGPL included in the packaging of this
0014 ** file. Please review the following information to ensure the GNU Lesser
0015 ** General Public License version 2.1 requirements will be met:
0016 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
0017 **
0018 ** In addition, as a special exception, Nokia gives you certain additional
0019 ** rights. These rights are described in the Nokia Qt LGPL Exception
0020 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
0021 **
0022 ** GNU General Public License Usage
0023 ** Alternatively, this file may be used under the terms of the GNU General
0024 ** Public License version 3.0 as published by the Free Software Foundation
0025 ** and appearing in the file LICENSE.GPL included in the packaging of this
0026 ** file. Please review the following information to ensure the GNU General
0027 ** Public License version 3.0 requirements will be met:
0028 ** http://www.gnu.org/copyleft/gpl.html.
0029 **
0030 ** Other Usage
0031 ** Alternatively, this file may be used in accordance with the terms and
0032 ** conditions contained in a signed written agreement between you and Nokia.
0033 **
0034 **
0035 **
0036 **
0037 **
0038 ** $QT_END_LICENSE$
0039 **
0040 ****************************************************************************/
0041 
0042 #ifndef STREAMS_RFC1951_H
0043 #define STREAMS_RFC1951_H
0044 
0045 #include <QIODevice>
0046 
0047 #include <zlib.h>
0048 
0049 namespace Streams {
0050 
0051 /* From RFC4978 The IMAP COMPRESS:   
0052    "When using the zlib library (see [RFC1951]), the functions
0053    deflateInit2(), deflate(), inflateInit2(), and inflate() suffice to
0054    implement this extension.  The windowBits value must be in the range
0055    -8 to -15, or else deflateInit2() uses the wrong format.
0056    deflateParams() can be used to improve compression rate and resource
0057    use.  The Z_FULL_FLUSH argument to deflate() can be used to clear the
0058    dictionary (the receiving peer does not need to do anything)."
0059    
0060    Total zlib mem use is 176KB plus a 'few kilobytes' per connection that uses COMPRESS:
0061    96KB for deflate, 24KB for 3x8KB buffers, 32KB plus a 'few' kilobytes for inflate.
0062 */
0063 
0064 class Rfc1951Compressor
0065 {
0066 public:
0067     explicit Rfc1951Compressor(int chunkSize = 8192);
0068     ~Rfc1951Compressor();
0069 
0070     bool write(QIODevice *out, QByteArray *in);
0071 
0072 private:
0073     int _chunkSize;
0074     z_stream _zStream;
0075     char *_buffer;
0076 };
0077 
0078 class Rfc1951Decompressor
0079 {
0080 public:
0081     explicit Rfc1951Decompressor(int chunkSize = 8192);
0082     ~Rfc1951Decompressor();
0083 
0084     bool consume(QIODevice *in);
0085     bool canReadLine() const;
0086     QByteArray readLine();
0087     QByteArray read(qint64 maxSize);
0088 
0089 private:
0090     int _chunkSize;
0091     z_stream _zStream;
0092     QByteArray _inBuffer;
0093     char *_stagingBuffer;
0094     QByteArray _output;
0095 };
0096 
0097 }
0098 
0099 #endif