Deflate.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "Deflate.h"
00025 #include "3rdParty/zlib/zlib.h"
00026 
00030 /* ===========================================================================
00031      If the default memLevel or windowBits for deflateInit() is changed, then
00032    this function needs to be updated.
00033  */
00034 ulong ZEXPORT compressBound (ulong sourceLen)
00035 {
00036     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
00037 }
00038 
00040 QByteArray  Utils::Encodings::Deflate::compress (const QByteArray &rawData, int compressionLevel)
00041 {
00042     QByteArray compressedData;
00043     ulong compressedSize = ::compressBound (rawData.length());
00044 
00045     if (compressionLevel < -1 || compressionLevel > 9)
00046         compressionLevel = -1;
00047 
00048     compressedData.resize (compressedSize);
00049     int res = ::compress2 (reinterpret_cast <uchar*> (compressedData.data()),
00050                            &compressedSize,
00051                            reinterpret_cast <const uchar*> (rawData.data()),
00052                            rawData.length(),
00053                            compressionLevel);
00054     if (res != Z_OK)
00055         return QByteArray();
00056 
00057     compressedData.resize (compressedSize);
00058     return compressedData;
00059 }
00060 
00062 
00069 QByteArray  Utils::Encodings::Deflate::unCompress (const QByteArray &compressedData)
00070 {
00071     QByteArray uncompressedData;
00072     ulong uncompressedSize = compressedData.size() * 2;
00073 
00074     int res = Z_OK;
00075     do {
00076         uncompressedData.resize (uncompressedSize);
00077         res = ::uncompress (reinterpret_cast <uchar *> (uncompressedData.data()),
00078                             &uncompressedSize,
00079                             reinterpret_cast <const uchar *> (compressedData.data()),
00080                             compressedData.size());
00081         if (res == Z_OK)
00082             break;
00083         if (res != Z_BUF_ERROR)
00084             return QByteArray();
00085 
00086         uncompressedSize *= 2;
00087     } while (true);
00088 
00089     uncompressedData.resize (uncompressedSize);
00090     return uncompressedData;
00091 }