7#include "createdatabasefilejob.h"
8#include "checkphishingurlutil.h"
9#include "localdatabasefile.h"
10#include "riceencodingdecoder.h"
11#include "webengineviewer_debug.h"
13#include <QCryptographicHash>
16using namespace WebEngineViewer;
18class WebEngineViewer::CreateDatabaseFileJobPrivate
28 void createBinaryFile();
29 void generateFile(
bool fullUpdate);
30 WebEngineViewer::UpdateDataBaseInfo mInfoDataBase;
36void CreateDatabaseFileJobPrivate::createFileFromFullUpdate(
const QList<Addition> &additionList)
39 const quint16 major = WebEngineViewer::CheckPhishingUrlUtil::majorVersion();
40 const quint16 minor = WebEngineViewer::CheckPhishingUrlUtil::minorVersion();
42 qint64 hashStartPosition = mFile.
write(
reinterpret_cast<const char *
>(&major),
sizeof(major));
43 hashStartPosition += mFile.
write(
reinterpret_cast<const char *
>(&minor),
sizeof(minor));
47 for (
const Addition &add : additionList) {
48 switch (
add.compressionType) {
49 case UpdateDataBaseInfo::RawCompression: {
52 for (
int i = 0; i < uncompressed.
size();) {
58 tmp.prefixSize =
add.prefixSize;
62 hashStartPosition += 8;
63 if (m.
size() !=
add.prefixSize) {
64 qCWarning(WEBENGINEVIEWER_LOG) <<
"hash string: " << m <<
" hash string size: " << m.
size();
69 case UpdateDataBaseInfo::RiceCompression: {
71 qCWarning(WEBENGINEVIEWER_LOG) <<
"Rice compression still not implemented";
72 const QList<quint32> listRice = WebEngineViewer::RiceEncodingDecoder::decodeRiceHashesDelta(
add.riceDeltaEncoding);
73 qDebug() <<
" listRice" << listRice;
76 case UpdateDataBaseInfo::UnknownCompression:
77 qCWarning(WEBENGINEVIEWER_LOG) <<
"Unknown compression type in addition element";
81 const quint64 numberOfElement = itemToStore.
count();
82 hashStartPosition += mFile.
write(
reinterpret_cast<const char *
>(&numberOfElement),
sizeof(numberOfElement));
86 std::sort(itemToStore.
begin(), itemToStore.
end(), Addition::lessThan);
88 quint64 tmpPos = hashStartPosition;
90 for (
const Addition &add : std::as_const(itemToStore)) {
91 mFile.
write(
reinterpret_cast<const char *
>(&tmpPos),
sizeof(tmpPos));
92 tmpPos +=
add.prefixSize + 1;
97 for (
const Addition &add : std::as_const(itemToStore)) {
100 newSsha256 +=
add.hashString;
106 const bool checkSumCorrect = (mInfoDataBase.sha256 == newSsha256Value.
toBase64());
107 if (!checkSumCorrect) {
108 qCWarning(WEBENGINEVIEWER_LOG) <<
" newSsha256Value different from sha256 : " << newSsha256Value.
toBase64() <<
" from server " << mInfoDataBase.sha256;
110 Q_EMIT q->finished(checkSumCorrect, mInfoDataBase.newClientState, mInfoDataBase.minimumWaitDuration);
113void CreateDatabaseFileJobPrivate::generateFile(
bool fullUpdate)
115 qCDebug(WEBENGINEVIEWER_LOG) <<
" void CreateDatabaseFileJobPrivate::generateFile(bool fullUpdate)" << fullUpdate;
119 qCWarning(WEBENGINEVIEWER_LOG) <<
"Impossible to remove database file " << mFileName;
124 qCWarning(WEBENGINEVIEWER_LOG) <<
"Impossible to open database file " << mFileName;
128 createFileFromFullUpdate(mInfoDataBase.additionList);
130 WebEngineViewer::LocalDataBaseFile localeFile(mFileName);
131 if (!localeFile.fileExists()) {
132 qCWarning(WEBENGINEVIEWER_LOG) <<
"Impossible to create partial update as file doesn't exist";
139 removeElementFromDataBase(mInfoDataBase.removalList, oldDataBaseAddition);
141 oldDataBaseAddition += additionList;
147 qCWarning(WEBENGINEVIEWER_LOG) <<
"Impossible to remove database file " << mFileName;
152 qCWarning(WEBENGINEVIEWER_LOG) <<
"Impossible to open database file " << mFileName;
156 createFileFromFullUpdate(oldDataBaseAddition);
163 for (
const Removal &removeItem : removalList) {
164 switch (removeItem.compressionType) {
165 case UpdateDataBaseInfo::RawCompression:
166 for (
int id : std::as_const(removeItem.indexes)) {
170 case UpdateDataBaseInfo::RiceCompression:
171 indexToRemove = WebEngineViewer::RiceEncodingDecoder::decodeRiceIndiceDelta(removeItem.riceDeltaEncoding);
173 case UpdateDataBaseInfo::UnknownCompression:
174 qCWarning(WEBENGINEVIEWER_LOG) <<
" Unknown compression type defined in removal elements. It's a bug!";
179 std::sort(indexToRemove.
begin(), indexToRemove.
end());
180 for (
int i = (indexToRemove.
count() - 1); i >= 0; --i) {
181 oldDataBaseAddition.
remove(indexToRemove.
at(i));
185void CreateDatabaseFileJobPrivate::createBinaryFile()
187 switch (mInfoDataBase.responseType) {
188 case UpdateDataBaseInfo::Unknown:
189 qCWarning(WEBENGINEVIEWER_LOG) <<
" Response Type of database info is \"unknown\". It's a bug!";
191 case UpdateDataBaseInfo::FullUpdate:
192 case UpdateDataBaseInfo::PartialUpdate:
193 generateFile((mInfoDataBase.responseType == UpdateDataBaseInfo::FullUpdate));
199CreateDatabaseFileJob::CreateDatabaseFileJob(
QObject *parent)
201 , d(new WebEngineViewer::CreateDatabaseFileJobPrivate(this))
205CreateDatabaseFileJob::~CreateDatabaseFileJob() =
default;
207bool CreateDatabaseFileJob::canStart()
const
209 return !d->mFileName.isEmpty() && d->mInfoDataBase.isValid();
212void CreateDatabaseFileJob::setUpdateDataBaseInfo(
const UpdateDataBaseInfo &infoDataBase)
214 d->mInfoDataBase = infoDataBase;
217void CreateDatabaseFileJob::start()
223 d->createBinaryFile();
227void CreateDatabaseFileJob::setFileName(
const QString &filename)
229 d->mFileName = filename;
232#include "moc_createdatabasefilejob.cpp"
The CreateDatabaseFileJob class.
const char * constData() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
qsizetype size() const const
QByteArray toBase64(Base64Options options) const const
QByteArray hash(QByteArrayView data, Algorithm method)
bool exists(const QString &fileName)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
void setFileName(const QString &name)
virtual void close() override
qint64 write(const QByteArray &data)
const_reference at(qsizetype i) const const
qsizetype count() const const
void remove(qsizetype i, qsizetype n)