Debian Codegraph @

src/fs/qnx6.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 "qnx6.h"
19
20 namespace FS {
21
22 qint64 QNX6::findIndexFromSig(unsigned char* signature, int startFrom, int distanceFrom, unsigned int maxBlocks, int num) {
23 if (startFrom != -1)
24 _file->seek(startFrom);
25 int readlen = BUFFER_LEN;
26 while (!_file->atEnd() && (maxBlocks-- != 0))
27 {
28 QByteArray tmp = _file->read(readlen);
29 if (tmp.size() < 0)
30 break;
31 for (int i = 0; i < tmp.size(); i++)
32 {
33 bool found = true;
34 for (int j = 0; j < num; j++)
35 if ((unsigned char)tmp[i+j] != signature[j]) {
36 found = false;
37 break;
38 }
39 if (found)
40 return (qint64)(_file->pos() - tmp.size() + i + distanceFrom);
41 }
42 }
43 return 0;
44 }
45
46 qinode QNX6::createNode(int node) {
47 QNXStream stream(_file);
48 qinode ind;
49 qint64 base = findNode(node);
50 _file->seek(base);
51 stream >> ind.size;
52 _file->seek(base + 0x10);
53 stream >> ind.time;
54 _file->seek(base + 0x20);
55 stream >> ind.perms;
56 stream.skipRawData(2);
57 int sector;
58 for (int i = 0; i < 16; i++)
59 {
60 stream >> sector;
61 if (sector != -1)
62 ind.sectors.append(sector);
63 }
64 // No sectors appears to be caused by empty files. These need to be extracted
65 if (ind.sectors.count() == 0) {
66 ind.sectors.append(0);
67 }
68
69 stream >> ind.tiers;
70 return ind;
71 }
72
73 QPair<int, QString> QNX6::nodeInfo(QNXStream* stream, qint64 offset) {
74 _file->seek(offset);
75 QPair<int, QString> ret = {0, ""};
76 ret.first = stream->grabInt();
77 if (ret.first == 0)
78 return ret;
79 int count = stream->grabUChar();
80 if (count == 0xFF)
81 {
82 _file->seek(offset + 0x8);
83 int item = stream->grabInt();
84 if (item > lfn.count())
85 item = 1;
86 _file->seek(findSector(lfn.at(item)));
87 count = stream->grabUShort();
88 }
89 ret.second = _file->read(count);
90 return ret;
91 }
92
93 // Specialised function which takes a directory and finds META-INF/MANIFEST.MF and grabs its data
94 void QNX6::extractManifest(int nodenum) {
95 QNXStream stream(_file);
96 foreach(int num, createNode(nodenum).sectors)
97 {
98 for (int i = 0; i < 0x1000; i += 0x20)
99 {
100 QPair<int, QString> info = nodeInfo(&stream, findSector(num) + i);
101 if (info.second == "META-INF") {
102 foreach(int num, createNode(info.first).sectors)
103 {
104 for (int i = 0; i < 0x1000; i += 0x20) {
105 info = nodeInfo(&stream, findSector(num) + i);
106 if (info.second == "MANIFEST.MF") {
107 qinode ind = createNode(info.first);
108 QList<int> sections;
109 if (ind.tiers == 0 && (ind.sectors[0] > 0)) {
110 foreach(int sector, ind.sectors) {
111 if (sector != -1)
112 sections.append(sector);
113 }
114 } else if (ind.tiers > 0 ) {
115 foreach (int sector, ind.sectors) {
116 if (sector == -1) break;
117 _file->seek(findSector(sector));
118 for (int j = 0; j < 0x400; j++)
119 {
120 stream >> sector;
121 if (sector > 0)
122 sections.append(sector);
123 }
124 }
125 if (ind.tiers == 2) {
126 QList<int> nodes = sections;
127 sections.clear();
128 foreach (int fn, nodes) {
129 if (fn == -1) break;
130 _file->seek(findSector(fn));
131 for (int tmpx = 0; tmpx < 1024; tmpx++) {
132 stream >> fn;
133 if (fn > 0) sections.append(fn);
134 }
135 }
136 }
137 }
138 QByteArray manifestDump;
139 if (ind.size != 0) {
140 foreach(int section, sections)
141 {
142 _file->seek(findSector(section));
143 int len = sectorSize;
144 if (section == sections.last() && (ind.size % sectorSize))
145 len = ind.size % sectorSize;
146 manifestDump.append(_file->read(len));
147 }
148 }
149
150 QString name = "";
151 QString version = "";
152 QString arch = "";
153 QString strSigned = "";
154 foreach(QByteArray manifestString, manifestDump.split('\n')) {
155 QString tmp = QString(manifestString).simplified();
156 if (tmp.startsWith("Package-Name:")) {
157 name = tmp.split(": ").last().split('.').last();
158 }
159 else if (tmp.startsWith("Package-Version:")) {
160 version = tmp.split(": ").last();
161 }
162 else if (tmp.startsWith("Package-Architecture:")) {
163 arch = tmp.split(": ").last();
164 }
165 else if (tmp.startsWith("Package-Author-Certificate-Hash:")) {
166 strSigned = "+signed";
167 }
168 else if (tmp.startsWith("Archive-Asset-Name:")) {
169 manifestApps.append(tmp.split(": ").last());
170 }
171 }
172 if (currentZip == nullptr && name != "") {
173 currentZip = new QuaZip(QString("%1/%2-%3-nto+%4%5.bar").arg(_path).arg(name).arg(version).arg(arch).arg(strSigned));
174 emit currentNameChanged(QString("%1-%2").arg(name).arg(version));
175 currentZip->open(QuaZip::mdCreate);
176 }
177 // Leave now
178 return;
179 }
180 }
181 }
182 return;
183 }
184 }
185 }
186 }
187
188 void QNX6::extractDir(int nodenum, QString basedir, int tier)
189 {
190 QDir mainDir;
191 if (!extractApps)
192 mainDir.mkdir(basedir);
193
194 QNXStream stream(_file);
195 qinode ind = createNode(nodenum);
196 foreach(int num, ind.sectors)
197 {
198 for (int i = 0; i < 0x80; i++)
199 {
200 // TODO: Nice place to check if we want to quit
201 QPair<int, QString> info = nodeInfo(&stream, findSector(num) + (i * 0x20));
202 if (info.second == "." || info.second == "..")
203 continue;
204
205 qinode ind2 = createNode(info.first);
206
207 if (ind2.perms & QCFM_IS_DIRECTORY)
208 {
209 if (extractApps) {
210 if (info.second == "apps" && tier == 0) {
211 mainDir.mkdir(basedir);
212 extractDir(info.first, basedir, 1);
213 continue;
214 }
215 else if (tier == 1) {
216 currentZip = nullptr;
217 extractManifest(info.first);
218 if (currentZip != nullptr) {
219 extractDir(info.first, "", 2);
220 currentZip->close();
221 manifestApps.clear();
222 delete currentZip;
223 }
224 continue;
225 } else if (tier == 2) {
226 extractDir(info.first, info.second, 3);
227 continue;
228 }
229 }
230 extractDir(info.first, basedir + "/" + info.second, tier ? tier + 1 : 0);
231 continue;
232 }
233 if (extractApps) {
234 if (tier <= 1)
235 continue;
236 QString thisFile = (tier == 2) ? info.second : (basedir + "/" + info.second);
237 if (!manifestApps.isEmpty() && !(tier == 3 && basedir == "META-INF") && !manifestApps.contains(thisFile))
238 continue;
239 }
240 QList<int> sections;
241 if (ind2.tiers == 0 && (ind2.sectors[0] > 0)) {
242 foreach(int sector, ind2.sectors) {
243 if (sector != -1)
244 sections.append(sector);
245 }
246 } else if (ind2.tiers > 0 ) {
247 foreach (int sector, ind2.sectors) {
248 if (sector == -1) break;
249 _file->seek(findSector(sector));
250 for (int j = 0; j < 0x400; j++)
251 {
252 stream >> sector;
253 if (sector > 0)
254 sections.append(sector);
255 }
256 }
257 if (ind2.tiers == 2) {
258 QList<int> nodes = sections;
259 sections.clear();
260 foreach (int fn, nodes) {
261 if (fn == -1) break;
262 _file->seek(findSector(fn));
263 for (int tmpx = 0; tmpx < 1024; tmpx++) {
264 stream >> fn;
265 if (fn > 0)
266 sections.append(fn);
267 }
268 }
269 }
270 }
271
272 QuaZipFile* zipFile = 0;
273 QFile* newFile = 0;
274 if (extractApps)
275 {
276 Q_ASSERT(currentZip != nullptr);
277 zipFile = new QuaZipFile(currentZip);
278 QuaZipNewInfo newInfo((tier == 2) ? info.second : (basedir + "/" + info.second));
279 newInfo.setPermissions(QFileDevice::Permission(0x7774));
280 newInfo.dateTime.setTime_t(ind.time);
281 zipFile->open(QIODevice::WriteOnly, newInfo);
282 } else {
283 newFile = new QFile(basedir + "/" + info.second);
284 newFile->open(QIODevice::WriteOnly);
285 }
286 if (ind2.size != 0) {
287 foreach(int section, sections)
288 {
289 _file->seek(findSector(section));
290 int len = sectorSize;
291 if (section == sections.last() && (ind2.size % sectorSize))
292 len = ind2.size % sectorSize;
293 QByteArray tmp = _file->read(len);
294 increaseCurSize(tmp.size());
295 if (extractApps)
296 zipFile->write(tmp);
297 else
298 newFile->write(tmp);
299 }
300 }
301 if (extractApps) {
302 Q_ASSERT(zipFile->isOpen());
303 zipFile->close();
304 delete zipFile;
305 }
306 else {
307 newFile->close();
308 #ifdef _WIN32
309 fixFileTime(newFile->fileName(), ind.time);
310 #endif
311 delete newFile;
312 }
313 }
314 }
315 }
316
317 bool QNX6::createContents() {
318 _file->seek(_offset+8);
319 QNXStream stream(_file);
320 READ_TMP(unsigned char, typeQNX); // 0x10 = no offset; 0x08 = has offset
321 unsigned char qnx6Sig[] = {0x22, 0x11, 0x19, 0x68};
322 unsigned char fsSig[] = {0xDD, 0xEE, 0xE6, 0x97};
323 if ( (findIndexFromSig(qnx6Sig, -1, 0, 1)) == 0) { return false; }
324 if ( (_offset = findIndexFromSig(fsSig, -1, 0)) == 0) { return false; }
325 _file->seek(_offset+48);
326 stream >> sectorSize;
327 if (sectorSize % 512) { return false; }
328 _offset += sectorSize;
329
330 // Find sectorOffset
331 qinode ind2 = createNode(1);
332 // Try all offsets
333 if (typeQNX == 0x10)
334 {
335 sectorOffset = 0;
336 _file->seek((ind2.sectors[0] - sectorOffset)*sectorSize + _offset);
337 READ_TMP(int, offsetCheck);
338 if (offsetCheck != 1)
339 typeQNX = 8;
340 }
341 if (typeQNX != 0x10) {
342 for (sectorOffset = 0x6320; sectorOffset < 0x6400; sectorOffset += 0x10)
343 {
344 _file->seek((ind2.sectors[0] - sectorOffset)*sectorSize + _offset);
345 READ_TMP(int, offsetCheck);
346 if (offsetCheck == 1)
347 break;
348 }
349 }
350
351 for (qint64 s = _offset - 0xF10; true; s+=4)
352 {
353 _file->seek(s);
354 READ_TMP(int, next);
355 if (next == -1) break;
356 _file->seek(findSector(next));
357 for (int i = 0; i < 0x400; i++) {
358 stream >> next;
359 if (next > 0)
360 lfn.append(next);
361 }
362 }
363 extractDir(1, _path, 0);
364 emit currentNameChanged("");
365 QDesktopServices::openUrl(QUrl(_path));
366 return true;
367 }
368
369 }
370