Debian Codegraph @

src/splitter.cpp

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 #include "splitter.h"
19
20
21 // This is our entry point for extraction that will determine which filetype we started with.
22 // In general we have a container (.exe, .bar, .zip) which may contain further containers.
23 // Underneath this we may have disk images with partition tables (.signed)
24 // And below this we can have filesystem images (.rcfs, .qnx6, .ifs)
25 // Although, the user could make us enter at any point!
26 void Splitter::processExtractWrapper() {
27 extracting = true;
28 progressInfo.clear();
29 partitionInfo.clear();
30 QFileInfo fileInfo(selectedFile);
31
32 // We can probably use a better method, similar to processExtractType, for all files!
33 if (fileInfo.suffix() == "exe")
34 processExtractAutoloader();
35 else if (fileInfo.suffix() == "signed")
36 processExtractSigned();
37 else if (fileInfo.suffix() == "bar" || fileInfo.suffix() == "zip")
38 processExtractBar();
39 else // Assume it is a rcfs/qnx6/ifs
40 processExtractType();
41
42 // Now we should be done parsing through the entire file and its components
43 // So run through the partition Info we collected
44 // First gather sizes so we can have an established goal in the UI
45 for (int i = 0; i < partitionInfo.count(); i++) {
46 if (partitionInfo[i].size <= 65536) // Too small to have anything of value
47 partitionInfo.removeAt(i--);
48 else
49 maxSize += partitionInfo[i].size;
50 }
51
52 // All files will be extracted relative to the given container file
53 QString baseDir = QFileInfo(selectedFile).absolutePath();
54 foreach(PartitionInfo info, partitionInfo) {
55 int unique = newProgressInfo(info.size);
56 // If we are extracting FS images (only type supported for this method right now), then we want to create a filesystem type
57 QFileSystem* fs = createTypedFileSystem(selectedFile, info.dev, info.type, info.offset, info.size, baseDir);
58 if (fs == nullptr)
59 continue;
60 connect(fs, &QFileSystem::sizeChanged, [=] (qint64 delta) {
61 updateCurProgress(unique, fs->curSize, delta);
62 });
63 // TODO: This should be cleaner
64 if (info.type == FS_QNX6) {
65 qobject_cast<FS::QNX6*>(fs)->extractApps = extractApps;
66 // We need to make Splitter a QML-exposed class, then it's nicer to push these signals
67 //QObject::connect(fs, SIGNAL(currentNameChanged(QString)), this, SLOT()));
68 }
69 if (extractImage)
70 fs->extractImage();
71 else
72 fs->extractContents();
73
74 delete fs;
75 }
76 emit finished();
77
78 cleanDevHandle();
79 partitionInfo.clear();
80 progressInfo.clear();
81 }
82
83 // Process an Autoloader with the aim of extracting files
84 void Splitter::processExtractAutoloader() {
85 // We hardcode this only to speed it up. It isn't required and may cause issues later on.
86 #define START_CAP_SEARCH 0x400000
87 #define END_CAP_SEARCH 0x1000000
88 QFile* autoloaderFile = new QFile(selectedFile);
89 devHandle.append(autoloaderFile);
90 autoloaderFile->open(QIODevice::ReadOnly);
91 read = 0;
92 maxSize = 1;
93 int findHeader = 0;
94 autoloaderFile->seek(START_CAP_SEARCH);
95 for (int b = START_CAP_SEARCH; b < END_CAP_SEARCH; )
96 {
97 QByteArray tmp = autoloaderFile->read(BUFFER_LEN);
98 for (int i = 0; i < BUFFER_LEN - 12; i++) {
99 if (tmp.at(i) == (char)0x9C && tmp.at(i+1) == (char)0xD5 && tmp.at(i+2) == (char)0xC5 && tmp.at(i+3) == (char)0x97 &&
100 tmp.at(i+8) == (char)0x9C && tmp.at(i+9) == (char)0xD5 && tmp.at(i+10) == (char)0xC5 && tmp.at(i+11) == (char)0x97) {
101 findHeader = b+i+20;
102 }
103 }
104 b += tmp.size();
105 }
106
107 if (!findHeader) {
108 return die(tr("Was not a Blackberry Autoloader file."));
109 }
110
111 qint64 files;
112 QList<qint64> offsets;
113 QNXStream dataStream(autoloaderFile);
114 autoloaderFile->seek(findHeader);
115
116 // Search for offset table
117 for (int attempts = 0; attempts < 32; attempts++) {
118 qint64 tmp;
119 dataStream >> tmp;
120 if ((tmp - autoloaderFile->pos()) < 500 && (tmp - autoloaderFile->pos()) > -500) {
121 autoloaderFile->seek(autoloaderFile->pos() - 16);
122 dataStream >> files;
123 break;
124 }
125 }
126
127 if (files < 1 || files > 20) {
128 return die(tr("Unknown Blackberry Autoloader file."));
129 }
130
131 // Collect offsets
132 for (int i = 0; i < files; i++) {
133 offsets.append(0); dataStream >> offsets[i];
134 }
135 offsets.append(autoloaderFile->size()); // End of file
136
137 // Create sizes and files
138 QString baseName = selectedFile;
139 baseName.chop(4);
140 // TODO: Use detection based on header. OS, Radio and PINList notify their header (but not RFOS?).
141 // Not a priority as the size indication is 100% accurate as long as it is actually an Autoloader
142 for (int i = 0; i < files; i++) {
143 QString filename = baseName;
144 qint64 size = offsets[i+1] - offsets[i];
145
146 // Detection on type
147 int type = 0;
148 for (int index = 0; index < 1000 - 32; index += 4) {
149 autoloaderFile->seek(offsets[i] + index);
150 if (autoloaderFile->read(4) == QByteArray("pfcq",4)) {
151 autoloaderFile->seek(offsets[i] + index + 12);
152 qint8 typeChar;
153 dataStream >> typeChar;
154 if (typeChar == 5) {
155 type |= PACKED_FILE_USER;
156 } else if (typeChar == 6) {
157 type |= PACKED_FILE_OS;
158 } else if (typeChar == 12) { // Always seem to have 10 as well, sometimes 11
159 type |= PACKED_FILE_RADIO;
160 } else if (typeChar == 8) {
161 type |= PACKED_FILE_IFS;
162 break;
163 }
164
165 }
166 }
167 // Detect by size alone
168 if (type == 0) {
169 if (size > 1024*1024*70)
170 type = PACKED_FILE_OS;
171 else if (size > 1024*1024*5)
172 type = PACKED_FILE_RADIO;
173 else
174 type = PACKED_FILE_PINLIST;
175 }
176
177 // Append strings
178 if (type & PACKED_FILE_USER)
179 filename += "@User";
180 if (type & PACKED_FILE_OS)
181 filename += "@OS";
182 if (type & PACKED_FILE_RADIO)
183 filename += "@Radio";
184 if (type & PACKED_FILE_IFS)
185 filename += "@IFS";
186 if (type & PACKED_FILE_PINLIST)
187 filename += "@PINList";
188
189 if (splitting) {
190 if (option & type)
191 {
192 maxSize += size;
193 QString unique = "";
194 for (int i = 0; QFile::exists(filename + unique + ".signed"); i++)
195 unique = QString(".%1").arg(i);
196 tmpFile.append(new QFile(filename+".signed"));
197 }
198 else
199 tmpFile.append(nullptr);
200 } else if (extracting) {
201 if (type != PACKED_FILE_PINLIST)
202 processExtract(autoloaderFile, size, offsets[i]);
203 }
204 }
205 if (splitting) {
206 // Write them out
207 for (int i = 0; i < files; i++) {
208 if (tmpFile.at(i) == nullptr)
209 continue;
210 autoloaderFile->seek(offsets[i]);
211 tmpFile.at(i)->open(QIODevice::WriteOnly);
212 tmpFile.at(i)->resize(offsets[i+1] - offsets[i]);
213 QByteArray tmp;
214 for (qint64 b = offsets[i]; b < offsets[i+1]; ) {
215 qint64 read_len = qMin(BUFFER_LEN, offsets[i+1] - b);
216 tmp = autoloaderFile->read(read_len);
217 tmpFile.at(i)->write(tmp);
218 b += updateProgress(tmp.size());
219 }
220 tmpFile.at(i)->close();
221 }
222 foreach (QFile* file, tmpFile)
223 file->deleteLater();
224 tmpFile.clear();
225 autoloaderFile->close();
226 delete autoloaderFile;
227 autoloaderFile = nullptr;
228 }
229 }
230
231 // Process a Disk Image with the aim of extracting files
232 void Splitter::processExtractSigned()
233 {
234 QFile* file = new QFile(selectedFile); // Gets cleaned up later
235 devHandle.append(file); // By this
236 if (!file->open(QIODevice::ReadOnly)) {
237 return die("Could not open " + selectedFile);
238 }
239 processExtract(file, file->size(), 0);
240 }
241
242 // Process a zip container with the aim of extracting files
243 void Splitter::processExtractBar() {
244 QuaZip barFile(selectedFile);
245 barFile.open(QuaZip::mdUnzip);
246 foreach (QString signedName, barFile.getFileNameList()) {
247 if (QFileInfo(signedName).suffix() == "signed") {
248 // Create a new internal QuaZip instance so we can successfully close the search instance but leave devHandle open
249 QuaZipFile* signedFile = new QuaZipFile(selectedFile, signedName);
250 devHandle.append(signedFile);
251 barFile.setCurrentFile(signedName);
252 signedFile->open(QIODevice::ReadOnly);
253 if (splitting) {
254 qint64 size = signedFile->size();
255 int type = 0;
256 if (size > 1024 * 1024 * 120)
257 type = PACKED_FILE_OS;
258 else if (size > 1024 * 1024 * 5)
259 type = PACKED_FILE_RADIO | PACKED_FILE_IFS; // Could be either
260 else
261 type = PACKED_FILE_PINLIST;
262 if (option & type) {
263 read = 0;
264 maxSize = size;
265 progressChanged(0);
266 QFile outputSigned(QFileInfo(selectedFile).canonicalPath() + "/" + signedName);
267 outputSigned.open(QIODevice::WriteOnly);
268 outputSigned.resize(size);
269 QByteArray tmp;
270 for (qint64 b = size; b > 0; ) {
271 qint64 read_len = qMin(BUFFER_LEN, b);
272 tmp = signedFile->read(read_len);
273 outputSigned.write(tmp);
274 b -= updateProgress(tmp.size());
275 }
276 outputSigned.close();
277 }
278 } else if (extracting) {
279 progressChanged(0);
280 if (signedFile->size() > 1024*1024*5)
281 processExtract(signedFile, signedFile->size(), 0);
282 }
283 }
284 }
285 barFile.close();
286 }
287
288 // Process a Filesystem Image with the aim of extracting files
289 void Splitter::processExtractType() {
290 QFile* imageFile = new QFile(selectedFile); // Gets cleaned up later
291 devHandle.append(imageFile); // By this
292 if (!imageFile->open(QIODevice::ReadOnly)) {
293 return die("Could not open " + selectedFile);
294 }
295
296 partitionInfo.append(PartitionInfo(imageFile, 0, imageFile->size()));
297 }
298
299 QFileSystem* Splitter::createTypedFileSystem(QString name, QIODevice* dev, QFileSystemType type, qint64 offset, qint64 size, QString baseDir) {
300 if (type == FS_RCFS)
301 return new FS::RCFS(name, dev, offset, size, baseDir);
302 else if (type == FS_QNX6)
303 return new FS::QNX6(name, dev, offset, size, baseDir);
304 else if (type == FS_IFS)
305 return new FS::IFS(name, dev, offset, size, baseDir);
306
307 return nullptr;
308 }
309
310 void Splitter::processExtract(QIODevice* dev, qint64 signedSize, qint64 signedPos)
311 {
312 if (signedPos > 0)
313 dev->seek(signedPos);
314 if (dev->read(4) != QByteArray("mfcq", 4)) {
315 QMessageBox::information(nullptr, "Error", "Was not a Blackberry .signed image.");
316 return;
317 }
318 QList<PartitionInfo> partInfo;
319 dev->seek(signedPos+12);
320
321 // We are now at the partition table
322 QByteArray partitionTable = dev->read(4000);
323 QBuffer buffer(&partitionTable);
324 buffer.open(QIODevice::ReadOnly);
325 QNXStream tableStream(&buffer);
326 int numPartitions, firstOffset, unknown, startSearchOffset;
327 tableStream >> numPartitions >> firstOffset;
328 tableStream >> unknown >> startSearchOffset;
329
330 if (numPartitions > 15) {
331 QMessageBox::information(nullptr, "Error", "Bad partition table.");
332 return;
333 }
334 // Possible issue with other Signed? So far this is always zero
335 /*qint64 signedOffset;
336 for (int i = startSearchOffset; i < 1000 - 32; i+=4) {
337 dev->seek(signedPos+i);
338 signedOffset = -1;
339 if (dev->read(4) == QByteArray("pfcq",4) && partitionTable.at(i) == 5) { // PFCQ User FS
340 int nextVal;
341 buffer.seek(i+8);
342 tableStream >> nextVal;
343 qDebug() << nextVal;
344 if (nextVal == 3) {
345 qDebug() << "Got a WEIRD offset";
346 int blockOffset, endOffset;
347 buffer.seek(i+44);
348 tableStream >> blockOffset;
349 buffer.seek(i+56);
350 tableStream >> endOffset;
351 signedOffset = endOffset - blockOffset*16;
352 break;
353 } else if (nextVal == 1) {
354 signedOffset = 0;
355 break;
356 }
357 }
358 }*/
359
360 partInfo.append(PartitionInfo(dev, signedPos + firstOffset));
361 for (int i = startSearchOffset; i < 1000 - 32; i += 4) {
362 dev->seek(signedPos+i);
363 if (dev->read(4) == QByteArray("pfcq",4)) {
364 qint64 blocks = 0;
365 int count = partitionTable.at(i+8);
366 for (int j = 0; j < count; j++) {
367 buffer.seek(i + j*16 + 44);
368 int blockCount;
369 tableStream >> blockCount;
370 blocks += blockCount;
371 }
372 partInfo.last().size = blocks * (qint64)65536;
373 partInfo.append(PartitionInfo(dev, partInfo.last().offset + partInfo.last().size));
374 }
375 }
376 partInfo.last().size = signedPos + signedSize - partInfo.last().offset;
377 if (partInfo.last().size < 65536)
378 partInfo.removeLast();
379
380 // Add to the main partition list
381 foreach(PartitionInfo info, partInfo) {
382 if (info.type == FS_UNKNOWN)
383 continue;
384 if (extractTypes & info.type) {
385 partitionInfo.append(info);
386 }
387 }
388 }