Vidalia 0.2.12

ZlibByteArray.h

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 **
00010 **                     *       *       *
00011 ** 
00012 **  Zlib support in this class is derived from Tor's torgzip.[ch].
00013 **  Tor is distributed under this license:
00014 ** 
00015 **    Copyright (c) 2001-2004, Roger Dingledine
00016 **    Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
00017 **
00018 **   Redistribution and use in source and binary forms, with or without
00019 **   modification, are permitted provided that the following conditions are
00020 **   met:
00021 **
00022 **     * Redistributions of source code must retain the above copyright
00023 **       notice, this list of conditions and the following disclaimer.
00024 **
00025 **     * Redistributions in binary form must reproduce the above
00026 **       copyright notice, this list of conditions and the following disclaimer
00027 **       in the documentation and/or other materials provided with the
00028 **       distribution.
00029 ** 
00030 **     * Neither the names of the copyright owners nor the names of its
00031 **       contributors may be used to endorse or promote products derived from
00032 **       this software without specific prior written permission.
00033 **
00034 **    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00035 **    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00036 **    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00037 **    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00038 **    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00039 **    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00040 **    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00041 **    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00042 **    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00043 **    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00044 **    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00045 */
00046 
00047 /*
00048 ** \file ZlibByteArray.h
00049 ** \brief Wrapper around QByteArray that adds compression capabilities
00050 */
00051 
00052 #ifndef _ZLIBBYTEARRAY_H
00053 #define _ZLIBBYTEARRAY_H
00054 
00055 #include <QByteArray>
00056 #include <QString>
00057 
00058 
00059 class ZlibByteArray : public QByteArray
00060 {
00061 public:
00062   /** Available compression methods. */
00063   enum CompressionMethod {
00064     None,   /**< No compression method. */
00065     Gzip,   /**< Gzip compression method. */
00066     Zlib    /**< Zlib compression method. */
00067   };
00068   
00069   /** Constructor. */
00070   ZlibByteArray(QByteArray data); 
00071   
00072   /** Compresses the current contents of this object using <b>method</b>. */
00073   QByteArray compress(const CompressionMethod method = Zlib,
00074                       QString *errmsg = 0) const;
00075   /** Compreses the contents of <b>in</b> using <b>method</b>. */
00076   static QByteArray compress(const QByteArray in, 
00077                              const CompressionMethod method = Zlib,
00078                              QString *errmsg = 0);
00079   /** Uncompresses the current contents of this object using <b>method</b>. */
00080   QByteArray uncompress(CompressionMethod method = Zlib,
00081                         QString *errmsg = 0) const;
00082   /** Uncompresses the contents of <b>in</b> using <b>method</b>. */
00083   static QByteArray uncompress(const QByteArray in,
00084                                const CompressionMethod method = Zlib,
00085                                QString *errmsg = 0);
00086 
00087   /** Returns true if the Zlib compression library is available and usable. */
00088   static bool isZlibAvailable();
00089   /** Returns true iff we support gzip-based compression. Otherwise, we need to
00090     * use zlib. */
00091   static bool isGzipSupported();
00092 
00093 private:
00094   /** Return the 'bits' value to tell zlib to use <b>method</b>.*/
00095   static int methodBits(CompressionMethod method);
00096   /** Returns a string description of <b>method</b>. */
00097   static QString methodString(CompressionMethod method);
00098 };
00099 
00100 #endif
00101