| 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 "rcfs.h" |
| 19 | |
| 20 | #ifdef _LZO2_SHARED |
| 21 | #include <lzo/lzo1x.h> |
| 22 | #else |
| 23 | #include "lzo.h" |
| 24 | #endif |
| 25 | |
| 26 | namespace FS { |
| 27 | |
| 28 | rinode RCFS::createNode(int offset) { |
| 29 | QNXStream stream(_file); |
| 30 | _file->seek(_offset + offset + 4); |
| 31 | rinode ind; |
| 32 | stream >> ind.mode >> ind.nameoffset >> ind.offset >> ind.size >> ind.time; |
| 33 | _file->seek(_offset + ind.nameoffset); |
| 34 | ind.name = QString(_file->readLine(QNX6_MAX_CHARS)); |
| 35 | if (ind.name == "") |
| 36 | ind.name = "."; |
| 37 | return ind; |
| 38 | } |
| 39 | |
| 40 | QString RCFS::generateName(QString imageExt) { |
| 41 | _file->seek(_offset + 8); |
| 42 | QString board = "rcfs"; |
| 43 | QString variant = "unk"; |
| 44 | QString cpu = "unk"; |
| 45 | QString version = "unk"; |
| 46 | |
| 47 | if (_file->readLine(4).startsWith("fs-")) { |
| 48 | _file->seek(_offset + 0x1038); |
| 49 | QNXStream stream(_file); |
| 50 | READ_TMP(qint32, offset); |
| 51 | rinode dotnode = createNode(offset); |
| 52 | for (int i = 0; i < dotnode.size / 0x20; i++) { |
| 53 | rinode slashdotnode = createNode(dotnode.offset + (i * 0x20)); |
| 54 | if (slashdotnode.name == "etc") { |
| 55 | for (int i = 0; i < slashdotnode.size / 0x20; i++) { |
| 56 | rinode node = createNode(slashdotnode.offset + (i * 0x20)); |
| 57 | if (node.name == "os.version" || node.name == "radio.version") { |
| 58 | QByteArray versionData = extractFile(_offset + node.offset, node.size, node.mode); |
| 59 | version = QString(versionData).simplified(); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | if (slashdotnode.name.endsWith(".tdf")) { |
| 64 | QByteArray boardData = extractFile(_offset + slashdotnode.offset, slashdotnode.size, slashdotnode.mode); |
| 65 | foreach (QString config, QString(boardData).split('\n')) { |
| 66 | if (config.startsWith("CPU=")) { |
| 67 | cpu = config.split('=').last().remove('"'); |
| 68 | } else if (config.startsWith("BOARD=")) { |
| 69 | board = config.split('=').last().remove('"'); |
| 70 | if (board != "radio") |
| 71 | board.prepend("os."); |
| 72 | } else if (config.startsWith("BOARD_CONFIG=") || |
| 73 | config.startsWith("RADIO_BOARD_CONFIG=")) { |
| 74 | variant = config.split('=').last().remove('"'); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | QString name = QString("%1.%2.%3.%4") |
| 82 | .arg(board) |
| 83 | .arg(variant) |
| 84 | .arg(version) |
| 85 | .arg(cpu); |
| 86 | if (imageExt.isEmpty()) |
| 87 | return uniqueDir(name); |
| 88 | |
| 89 | return uniqueFile(name + imageExt); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // Returning the array of data might be dangerous if it's huge. Consider taking a var instead |
| 94 | QByteArray RCFS::extractFile(qint64 node_offset, int node_size, int node_mode) |
| 95 | { |
| 96 | QByteArray ret; |
| 97 | _file->seek(node_offset); |
| 98 | QNXStream stream(_file); |
| 99 | if (node_mode & QCFM_IS_LZO_COMPRESSED) { |
| 100 | READ_TMP(int, next); |
| 101 | int chunks = (next - 4) / 4; |
| 102 | QList<int> sizes, offsets; |
| 103 | offsets.append(next); |
| 104 | for (int s = 0; s < chunks; s++) { |
| 105 | stream >> next; |
| 106 | offsets.append(next); |
| 107 | sizes.append(offsets[s+1] - offsets[s]); |
| 108 | } |
| 109 | char* buffer = new char[node_size]; |
| 110 | foreach(int size, sizes) { |
| 111 | char* readData = new char[size]; |
| 112 | _file->read(readData, size); |
| 113 | size_t write_len = 0x4000; |
| 114 | lzo1x_decompress_safe(reinterpret_cast<const unsigned char*>(readData), size, reinterpret_cast<unsigned char*>(buffer), &write_len, nullptr); |
| 115 | ret.append(buffer, write_len); |
| 116 | delete [] readData; |
| 117 | } |
| 118 | delete [] buffer; |
| 119 | } |
| 120 | else { |
| 121 | for (qint64 i = node_size; i > 0;) { |
| 122 | QByteArray data = _file->read(qMin(BUFFER_LEN, i)); |
| 123 | i -= data.size(); |
| 124 | ret.append(data); |
| 125 | } |
| 126 | } |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | void RCFS::extractDir(int offset, int numNodes, QString basedir, qint64 _offset) |
| 131 | { |
| 132 | QNXStream stream(_file); |
| 133 | QDir mainDir(basedir); |
| 134 | for (int i = 0; i < numNodes; i++) { |
| 135 | rinode node = createNode(offset + (i * 0x20)); |
| 136 | node.path_to = basedir; |
| 137 | QString absName = node.path_to + "/" + node.name; |
| 138 | if (node.mode & QCFM_IS_DIRECTORY) { |
| 139 | mainDir.mkpath(node.name); |
| 140 | if (node.size > 0) |
| 141 | extractDir(node.offset, node.size / 0x20, absName, _offset); |
| 142 | } |
| 143 | else { |
| 144 | _file->seek(node.offset + _offset); |
| 145 | if (node.mode & QCFM_IS_SYMLINK) { |
| 146 | #ifdef _WIN32 |
| 147 | QString lnkName = absName + ".lnk"; |
| 148 | QFile::link(node.path_to + "/" + _file->readLine(QNX6_MAX_CHARS), lnkName); |
| 149 | fixFileTime(lnkName, node.time); |
| 150 | #else |
| 151 | QFile::link(node.path_to + "/" + _file->readLine(QNX6_MAX_CHARS), absName); |
| 152 | #endif |
| 153 | continue; |
| 154 | } |
| 155 | if (node.mode & QCFM_IS_LZO_COMPRESSED) { |
| 156 | QFile newFile(absName); |
| 157 | newFile.open(QFile::WriteOnly); |
| 158 | READ_TMP(int, next); |
| 159 | int chunks = (next - 4) / 4; |
| 160 | QList<int> sizes, offsets; |
| 161 | offsets.append(next); |
| 162 | for (int s = 0; s < chunks; s++) { |
| 163 | stream >> next; |
| 164 | offsets.append(next); |
| 165 | sizes.append(offsets[s+1] - offsets[s]); |
| 166 | } |
| 167 | char* buffer = new char[node.size]; |
| 168 | foreach(int size, sizes) { |
| 169 | char* readData = new char[size]; |
| 170 | _file->read(readData, size); |
| 171 | size_t write_len = 0x4000; |
| 172 | lzo1x_decompress_safe(reinterpret_cast<const unsigned char*>(readData), size, reinterpret_cast<unsigned char*>(buffer), &write_len, nullptr); |
| 173 | newFile.write(buffer, (qint64)write_len); |
| 174 | increaseCurSize(size); // Uncompressed size |
| 175 | delete [] readData; |
| 176 | } |
| 177 | delete [] buffer; |
| 178 | newFile.close(); |
| 179 | } else { |
| 180 | writeFile(absName, node.size, true); |
| 181 | } |
| 182 | #ifdef _WIN32 |
| 183 | fixFileTime(absName, node.time); |
| 184 | #endif |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | bool RCFS::createContents() { |
| 190 | _file->seek(_offset + 0x1038); |
| 191 | QNXStream stream(_file); |
| 192 | READ_TMP(qint32, offset); |
| 193 | extractDir(offset, 1, _path, _offset); |
| 194 | |
| 195 | // Display result |
| 196 | QDesktopServices::openUrl(QUrl(_path)); |
| 197 | return true; |
| 198 | } |
| 199 | } |