Debian Codegraph @

src/splitter.h

raw symbols SCIP
1 // Copyright (C) 2014 Sacha Refshauge
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 3.0.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 3.0 for more details.
11
12 // A copy of the GPL 3.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official GIT repository and contact information can be found at
16 // http://github.com/xsacha/Sachesi
17
18 #pragma once
19
20 #include <QObject>
21 #include <QBuffer>
22 #include <QStringList>
23 #include <QDir>
24 #include <QDataStream>
25 #include <QDebug>
26 #include <QCoreApplication>
27 #ifndef BLACKBERRY
28 #include <QMessageBox>
29 #endif
30 #include "ports.h"
31 #include "fs/qnx6.h"
32 #include "fs/rcfs.h"
33 #include "fs/ifs.h"
34 #include "autoloaderwriter.h"
35
36 enum QFileSystemType {
37 FS_UNKNOWN = 0,
38 FS_RCFS = 1,
39 FS_QNX6 = 2,
40 FS_IFS = 4,
41 };
42
43 struct ProgressInfo {
44 int unique;
45 qint64 curSize;
46 qint64 maxSize;
47 double progress;
48 };
49
50 struct PartitionInfo {
51 qint64 offset;
52 qint64 size;
53 QFileSystemType type;
54 QIODevice* dev;
55 QString image;
56 // We are grabbing a partition of a container or image
57 PartitionInfo(QIODevice* file, qint64 loc, qint64 size = 0)
58 : offset(loc)
59 , size(size)
60 , dev(file)
61 {
62 detectType(dev);
63 }
64
65 void detectType(QIODevice* device) {
66 // Check what sort of image we are dealing with
67 if (!device->isSequential())
68 device->seek(offset);
69 QByteArray header = device->read(4);
70
71 if (header == QByteArray("rimh", 4))
72 type = FS_RCFS;
73 else if (header == QByteArray::fromHex("EB109000"))
74 type = FS_QNX6;
75 else if (header == QByteArray::fromHex("FE0300EA") // Qualcomm
76 || header == QByteArray::fromHex("FE0100EA")) // OMAP
77 type = FS_IFS;
78 else
79 type = FS_UNKNOWN;
80 }
81 };
82
83 #define PACKED_FILE_USER (1 << 0)
84 #define PACKED_FILE_OS (1 << 1)
85 #define PACKED_FILE_RADIO (1 << 2)
86 #define PACKED_FILE_IFS (1 << 3)
87 #define PACKED_FILE_PINLIST (1 << 4)
88
89 class Splitter: public QObject {
90 Q_OBJECT
91
92 public:
93 Splitter(QString file) : selectedFile(file) { reset(); }
94 Splitter(QString file, int options) : option(options), selectedFile(file) { reset(); }
95 Splitter(QStringList files) : selectedFiles(files) { reset(); }
96 Splitter(QList<QUrl> urls) : selectedUrls(urls) { reset(); }
97 ~Splitter() { }
98 bool extractApps, extractImage;
99 int extractTypes;
100 public slots:
101 void reset() {
102 kill = false;
103 read = 0; maxSize = 1; emit progressChanged(0);
104 extractApps = false;
105 extractImage = false;
106 extractTypes = 0;
107 extracting = false;
108 splitting = false;
109 }
110
111 void killSplit() {
112 kill = true;
113 die();
114 }
115 void cleanDevHandle() {
116 // Cleanup all pointers we had.
117 foreach (QIODevice* dev, devHandle) {
118 // QIODevice's are automatically closed.
119 if (dev != nullptr) {
120 dev->deleteLater();
121 dev = nullptr;
122 }
123 }
124 devHandle.clear();
125 }
126
127 void die(QString message = "") {
128 qDebug() << "Tool terminated early due to unforseen circumstances.";
129 if (extracting) {
130 cleanDevHandle();
131 }
132 if (combining) {
133 newAutoloader->kill();
134 cleanDevHandle();
135 }
136 if (splitting)
137 {
138 for (int i = 0; i < tmpFile.count(); i++)
139 {
140 if (tmpFile.at(i)->isOpen())
141 {
142 tmpFile.at(i)->close();
143 delete tmpFile.at(i);
144 tmpFile.removeAt(i);
145 }
146 }
147 tmpFile.clear();
148 }
149 extracting = false;
150 combining = false;
151 splitting = false;
152 #ifndef BLACKBERRY
153 if (!message.isEmpty())
154 QMessageBox::information(nullptr, "Error", message);
155 #endif
156 emit finished();
157 }
158
159 void processSplitBar() {
160 // And it seems we need a wrapper for splitting too.
161 // Splitting is just extracting .signed, so lets integrate those after
162 splitting = true;
163 processExtractBar();
164 cleanDevHandle();
165
166 emit finished();
167 }
168
169 void processExtractBar();
170
171 void processSplitAutoloader() {
172 // Ditto with the above split bar
173 splitting = true;
174 processExtractAutoloader();
175 emit finished();
176 }
177
178 void processExtractAutoloader();
179
180 static bool compareSizes(QFileInfo i, QFileInfo j)
181 {
182 return i.size() > j.size();
183 }
184
185 void processCombine() {
186 // Convert URL list to FileInfo list
187 QList<QFileInfo> splitFiles;
188 foreach(QUrl url, selectedUrls) {
189 QFileInfo fileInfo = QFileInfo(url.toLocalFile());
190 if (fileInfo.isDir())
191 {
192 QStringList suffixOnly = fileInfo.absoluteDir().entryList(QStringList() << "*.signed" << ".bar" << ".zip");
193 foreach (QString suffix, suffixOnly) {
194 splitFiles.append(QFileInfo(fileInfo.absoluteFilePath() + "/" + suffix));
195 }
196 } else if (fileInfo.suffix() == "signed" || fileInfo.suffix() == "bar" || fileInfo.suffix() == "zip")
197 splitFiles.append(fileInfo);
198 }
199 // Sort files by size
200 qSort(splitFiles.begin(), splitFiles.end(), compareSizes);
201 // Convert FileInfo list to dev handle list
202 cleanDevHandle();
203 foreach (QFileInfo info, splitFiles) {
204 if (info.suffix() == "signed") {
205 QFile* newFile = new QFile(info.absoluteFilePath());
206 newFile->open(QIODevice::ReadOnly);
207 devHandle.append(newFile);
208 } else {
209 QuaZip barFile(info.absoluteFilePath());
210 barFile.open(QuaZip::mdUnzip);
211 foreach (QString signedName, barFile.getFileNameList()) {
212 if (QFileInfo(signedName).suffix() == "signed") {
213 // Create a new internal QuaZip instance so we can successfully close the search instance but leave devHandle open
214 QuaZipFile* signedFile = new QuaZipFile(info.absoluteFilePath(), signedName);
215 // Couldn't open file. Shouldn't ever happen so don't worry about error
216 if (!signedFile->open(QIODevice::ReadOnly)) {
217 continue;
218 }
219 devHandle.append(signedFile);
220 }
221 }
222 barFile.close();
223 }
224 }
225 if (devHandle.isEmpty())
226 return;
227 if (devHandle.count() > 6) {
228 QMessageBox::information(nullptr, "Error", "Autoloaders can only have a maximum of 6 signed files.");
229 cleanDevHandle();
230 return;
231 }
232 // All good, lets officially start creating
233 combining = true;
234 // Create new Autoloader object
235 newAutoloader = new AutoloaderWriter(devHandle);
236 connect(newAutoloader, &AutoloaderWriter::newProgress, [=](int percent) { emit this->progressChanged(percent); });
237 // This is blocking, but we are in a thread
238 newAutoloader->create(splitFiles.first().absolutePath() + "/" + splitFiles.first().completeBaseName());
239 cleanDevHandle();
240 delete newAutoloader;
241 emit finished();
242 }
243
244 void processExtractSigned();
245 void processExtract(QIODevice* dev, qint64 signedSize, qint64 signedPos);
246 void processExtractType();
247 QFileSystem* createTypedFileSystem(QString name, QIODevice* dev, QFileSystemType type, qint64 offset = 0, qint64 size = 0, QString baseDir = ".");
248
249 void processExtractWrapper();
250
251 // Old, compatibility
252 quint64 updateProgress(qint64 delta) {
253 if (delta < 0)
254 return 0;
255 read += 100 * delta;
256 emit progressChanged((int)(read / maxSize));
257 return delta;
258 }
259 int newProgressInfo(qint64 size) {
260 progressInfo.append(ProgressInfo());
261 progressInfo.last().maxSize = size;
262 return progressInfo.count() - 1;
263 }
264
265 void updateCurProgress(int unique, qint64 bytes, qint64 delta) {
266 // New, unused
267 if (progressInfo.count() <= unique)
268 return;
269 progressInfo[unique].curSize = bytes;
270 progressInfo[unique].progress = (double)(100*progressInfo[unique].curSize) / (double)progressInfo[unique].maxSize;
271
272 // Old, compatibility
273 read += 100 * delta;
274 emit progressChanged((int)(read / maxSize));
275 }
276
277 signals:
278 void finished();
279 void progressChanged(int progress);
280 void error(QString err);
281 private:
282 bool kill;
283 quint64 read, maxSize;
284 bool splitting;
285 bool combining;
286 bool extracting;
287 int option;
288 QString selectedFile;
289 QStringList selectedFiles;
290 QList<QUrl> selectedUrls;
291 QList<QFile*> tmpFile;
292
293 // New
294 QList<ProgressInfo> progressInfo;
295 QList<PartitionInfo> partitionInfo;
296 QList<QIODevice*> devHandle;
297 AutoloaderWriter* newAutoloader;
298 };