ff7tk  0.02
Toolkit for making FF7 Tools
GZIP.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  ** Makou Reactor Final Fantasy VII Field Script Editor
3  ** Copyright (C) 2009-2012 Arzel Jérôme <myst6re@gmail.com>
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 3 of the License, or
8  ** (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17  ****************************************************************************/
18 #include "GZIP.h"
19 #include <zlib.h>
20 #undef compress // conflict with GZIP::compress
21 
22 QByteArray GZIP::decompress(const QByteArray &data, int decSize, Strategy strategy)
23 {
24  return decompress(data.constData(), data.size(), decSize, strategy);
25 }
26 
27 QByteArray GZIP::compress(const QByteArray &ungzip, int level, Strategy strategy)
28 {
29  return compress(ungzip.constData(), ungzip.size(), level, strategy);
30 }
31 
32 QByteArray GZIP::decompress(const char *data, int size, int decSize, Strategy strategy)
33 {
34  QTemporaryFile temp;
35  if(!temp.open()) {
36  return QByteArray();
37  }
38  temp.write(data, size);
39  temp.close();
40 
41  return decompress(temp.fileName(), decSize, strategy);
42 }
43 
44 QByteArray GZIP::compress(const char *ungzip, int size, int level, Strategy strategy)
45 {
46  QString tempPath = QDir::tempPath() + "/qt_temp.gz";
47 
48  gzFile file2 = gzopen(tempPath.toLatin1(), gzMode("w", level, strategy).toLatin1());
49  if(!file2) {
50  return QByteArray();
51  }
52  gzwrite(file2, ungzip, size);
53  gzclose(file2);
54  QFile finalFile(tempPath);
55  if(!finalFile.open(QIODevice::ReadOnly)) {
56  return QByteArray();
57  }
58 
59  QByteArray data = finalFile.readAll();
60  finalFile.remove();
61 
62  return data;
63 }
64 
65 QByteArray GZIP::decompress(const QString &path, int decSize, Strategy strategy)
66 {
67  Q_UNUSED(decSize);
68  QByteArray ungzip;
69 
70  gzFile file = gzopen(path.toLatin1(), gzMode("r", -1, strategy).toLatin1());
71  if(!file) {
72  return QByteArray();
73  }
74  char buffer[10000];
75  int r;
76  while((r = gzread(file, buffer, 10000)) > 0) {
77  ungzip.append(buffer, r);
78  }
79  gzclose(file);
80 
81  return ungzip;
82 }
83 
85 {
86  switch (strategy) {
87  case StrategyDefault: return '*';
88  case StrategyFiltered: return 'f';
89  case StrategyHuffmanOnly: return 'h';
90  case StrategyRle: return 'R';
91  case StrategyFixed: return 'F';
92  }
93  return '*';
94 }
95 
96 QString GZIP::gzMode(const char *mode, int level, Strategy strategy)
97 {
98  QString m(mode);
99  if (level >= 0 && level <= 9) {
100  m.append(QString::number(level));
101  }
102  char s = strategyToChar(strategy);
103  if (s != '*') {
104  m.append(s);
105  }
106  return m;
107 }
static QString gzMode(const char *mode, int level=-1, Strategy strategy=StrategyDefault)
Definition: GZIP.cpp:96
static QByteArray compress(const QByteArray &ungzip, int level=-1, Strategy strategy=StrategyDefault)
Definition: GZIP.cpp:27
Strategy
Definition: GZIP.h:26
static char strategyToChar(Strategy strategy)
Definition: GZIP.cpp:84
static QByteArray decompress(const QByteArray &data, int decSize, Strategy strategy=StrategyDefault)
Definition: GZIP.cpp:22