7 #include "localdatabasefile.h"
8 #include "checkphishingurlutil.h"
9 #include "updatedatabaseinfo.h"
10 #include "webengineviewer_debug.h"
12 #include <QElapsedTimer>
15 using namespace WebEngineViewer;
17 WEBENGINEVIEWER_EXPORT
int webengineview_LocalDataBaseFile = 1000 * 60 * 60;
19 class WebEngineViewer::LocalDataBaseFilePrivate
22 LocalDataBaseFilePrivate(
const QString &filename, LocalDataBaseFile *qq)
34 uchar *mData =
nullptr;
36 LocalDataBaseFile *
const q;
40 bool 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());
57 void LocalDataBaseFilePrivate::close()
66 bool LocalDataBaseFilePrivate::reload()
68 qCDebug(WEBENGINEVIEWER_LOG) <<
"Reload File" << mFile.fileName();
73 LocalDataBaseFile::LocalDataBaseFile(
const QString &filename)
74 : d(new WebEngineViewer::LocalDataBaseFilePrivate(filename, this))
79 LocalDataBaseFile::~LocalDataBaseFile() =
default;
81 void LocalDataBaseFile::close()
86 bool LocalDataBaseFile::isValid()
const
91 quint16 LocalDataBaseFile::getUint16(
int offset)
const
93 return *
reinterpret_cast<quint16 *
>(d->mData + offset);
96 quint32 LocalDataBaseFile::getUint32(
int offset)
const
98 return *
reinterpret_cast<quint32 *
>(d->mData + offset);
101 quint64 LocalDataBaseFile::getUint64(
int offset)
const
103 return *
reinterpret_cast<quint64 *
>(d->mData + offset);
106 const char *LocalDataBaseFile::getCharStar(
int offset)
const
108 return reinterpret_cast<const char *
>(d->mData + offset);
111 bool LocalDataBaseFile::reload()
124 const int numHash = getUint64(4);
125 int end = numHash - 1;
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());
139 qCWarning(WEBENGINEVIEWER_LOG) <<
"search " << hashToSearch.
toBase64() <<
" begin " <<
begin <<
" end " <<
end <<
" hashCharStar"
146 }
else if (cmp > 0) {
151 }
while (begin <= end);
153 return previousValue;
156 bool LocalDataBaseFile::shouldCheck()
const
159 if (d->mLastCheck.isValid() && d->mLastCheck.elapsed() < webengineview_LocalDataBaseFile) {
162 d->mLastCheck.start();
166 bool LocalDataBaseFile::checkFileChanged()
168 bool somethingChanged =
false;
170 if (!fileInfo.exists() || fileInfo.lastModified() > d->mMtime) {
173 somethingChanged =
true;
175 if (somethingChanged) {
178 return somethingChanged;
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;
199 bool LocalDataBaseFile::fileExists()
const
202 return fileInfo.exists();