7#include "localdatabasefile.h"
8#include "checkphishingurlutil.h"
9#include "updatedatabaseinfo.h"
10#include "webengineviewer_debug.h"
12#include <QElapsedTimer>
15using namespace WebEngineViewer;
17WEBENGINEVIEWER_EXPORT
int webengineview_LocalDataBaseFile = 1000 * 60 * 60;
19class WebEngineViewer::LocalDataBaseFilePrivate
22 LocalDataBaseFilePrivate(
const QString &filename, LocalDataBaseFile *qq)
32 QElapsedTimer mLastCheck;
34 uchar *mData =
nullptr;
36 LocalDataBaseFile *
const q;
40bool LocalDataBaseFilePrivate::load()
45 mData = mFile.map(0, mFile.size());
47 const int major = q->getUint16(0);
48 const int minor = q->getUint16(2);
51 mValid = (major == WebEngineViewer::CheckPhishingUrlUtil::majorVersion() && minor == WebEngineViewer::CheckPhishingUrlUtil::minorVersion());
53 mMtime = QFileInfo(mFile).lastModified();
57void LocalDataBaseFilePrivate::close()
66bool LocalDataBaseFilePrivate::reload()
68 qCDebug(WEBENGINEVIEWER_LOG) <<
"Reload File" << mFile.fileName();
73LocalDataBaseFile::LocalDataBaseFile(
const QString &filename)
74 : d(new WebEngineViewer::LocalDataBaseFilePrivate(filename, this))
79LocalDataBaseFile::~LocalDataBaseFile() =
default;
81void LocalDataBaseFile::close()
86bool LocalDataBaseFile::isValid()
const
91quint16 LocalDataBaseFile::getUint16(
int offset)
const
93 return *
reinterpret_cast<quint16 *
>(d->mData + offset);
96quint32 LocalDataBaseFile::getUint32(
int offset)
const
98 return *
reinterpret_cast<quint32 *
>(d->mData + offset);
101quint64 LocalDataBaseFile::getUint64(
int offset)
const
103 return *
reinterpret_cast<quint64 *
>(d->mData + offset);
106const char *LocalDataBaseFile::getCharStar(
int offset)
const
108 return reinterpret_cast<const char *
>(d->mData + offset);
111bool LocalDataBaseFile::reload()
116QByteArray LocalDataBaseFile::searchHash(
const QByteArray &hashToSearch)
124 const int numHash = getUint64(4);
125 int end = numHash - 1;
126 QByteArray previousValue;
128 QByteArray currentValue;
130 int posListOffset = 12;
132 previousValue = currentValue;
133 const int medium = (
begin +
end) / 2;
134 const int off = posListOffset + 8 * medium;
135 const int hashOffset = getUint64(off);
136 const char *hashCharStar = getCharStar(hashOffset);
137 const int cmp = qstrcmp(hashCharStar, hashToSearch.
constData());
138 currentValue = QByteArray(hashCharStar);
139 qCWarning(WEBENGINEVIEWER_LOG) <<
"search " << hashToSearch.
toBase64() <<
" begin " <<
begin <<
" end " <<
end <<
" hashCharStar"
146 }
else if (cmp > 0) {
151 }
while (begin <= end);
153 return previousValue;
156bool LocalDataBaseFile::shouldCheck()
const
159 if (d->mLastCheck.isValid() && d->mLastCheck.elapsed() < webengineview_LocalDataBaseFile) {
162 d->mLastCheck.start();
166bool LocalDataBaseFile::checkFileChanged()
168 bool somethingChanged =
false;
169 QFileInfo fileInfo(d->mFile);
170 if (!fileInfo.exists() || fileInfo.lastModified() > d->mMtime) {
173 somethingChanged =
true;
175 if (somethingChanged) {
178 return somethingChanged;
181QList<WebEngineViewer::Addition> LocalDataBaseFile::extractAllInfo()
const
183 QList<WebEngineViewer::Addition> lst;
184 quint64 numberOfElement = getUint64(4);
187 for (quint64 i = 0; i < numberOfElement; ++i) {
188 const quint64 value = getUint64(index);
190 tmp.hashString = QByteArray(getCharStar(value));
191 tmp.prefixSize = tmp.hashString.
size();
192 tmp.compressionType = WebEngineViewer::UpdateDataBaseInfo::RawCompression;
199bool LocalDataBaseFile::fileExists()
const
201 QFileInfo fileInfo(d->mFile);
202 return fileInfo.exists();
const QList< QKeySequence > & begin()
const QList< QKeySequence > & end()
const char * constData() const const
qsizetype size() const const
QByteArray toBase64(Base64Options options) const const
void append(QList< T > &&value)
void reserve(qsizetype size)