Deflate.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "Deflate.h"
00025 #include "3rdParty/zlib/zlib.h"
00026
00030
00031
00032
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 }