Skip to content

Instantly share code, notes, and snippets.

@HamedMasafi
Created March 22, 2020 06:29
Show Gist options
  • Save HamedMasafi/b715ea15540303309ed5d0876485b32a to your computer and use it in GitHub Desktop.
Save HamedMasafi/b715ea15540303309ed5d0876485b32a to your computer and use it in GitHub Desktop.
QByteArray Firefox::parseJsonlz4RecoveryFilePath(const QString &recoveryFilePath)
{
const char mozlz4_magic[] = {109, 111, 122, 76, 122, 52, 48, 0}; /* "mozLz40\0" */
const int decomp_size = 4; /* 4 bytes size come after the header */
const size_t magic_size = sizeof mozlz4_magic;
char *encryptedData = nullptr;
char *decryptedData = nullptr;
int readSize = 0;
size_t outputBufferSize = 0;
QFile f(recoveryFilePath);
f.open(QIODevice::ReadOnly);
QByteArray encryptedByteData = f.readAll();
f.close();
if (!encryptedByteData.isEmpty()) {
encryptedData = encryptedByteData.data();
readSize = encryptedByteData.size();
} else {
qDebug() << "[FirefoxUtils::parseJsonlz4RecoveryFilePath] Empty file";
return QByteArray();
}
if (readSize < magic_size + decomp_size || memcmp(mozlz4_magic, encryptedData, magic_size) != 0) {
qDebug() << "[FirefoxUtils::parseJsonlz4RecoveryFilePath] Unsupported file format: " + recoveryFilePath;
return QByteArray();
}
size_t i = 0;
for (i = magic_size; i < magic_size + decomp_size; i++) {
outputBufferSize += (unsigned char) encryptedData[i] << (8 * (i - magic_size));
}
if (!(decryptedData = (char *) malloc(outputBufferSize))) {
qDebug() << "[FirefoxUtils::parseJsonlz4RecoveryFilePath] Failed to allocate a buffer for an output.";
return QByteArray();
}
int decryptedDataSize = LZ4_decompress_safe(encryptedData + i, decryptedData, (int) (readSize - i), (int) outputBufferSize);
if (decryptedDataSize < 0) {
qDebug() << "[FirefoxUtils::parseJsonlz4RecoveryFilePath] Failed to decompress a file: " + recoveryFilePath;
free(decryptedData);
return QByteArray();
}
QByteArray qDecryptedData = QByteArray(decryptedData);
free(decryptedData);
int indexOfLastProperChar = qDecryptedData.lastIndexOf(QLatin1String("}"));
QByteArray cutData = qDecryptedData.left(indexOfLastProperChar + 1);
// qDebug() << "[FirefoxUtils::parseJsonlz4RecoveryFilePath] Successfully decompressed a file: " + recoveryFilePath;
// // Debug: write to a D:/fx.json file
// QString filename = "/home/karol/Documents/fx.json";
// QFile file(filename);
// if (file.open(QIODevice::ReadWrite)) {
// QTextStream stream(&file);
// stream << cutData;
// }
return cutData;
}
HEADERS += \
$$PWD/lz4/lib/lz4.h
SOURCES += \
$$PWD/lz4/lib/lz4.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment