Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 05134fc

Browse files
committed
complete main lib code cleanup stage 1
1 parent 82ecf6a commit 05134fc

23 files changed

+143
-136
lines changed

src/datasync/cryptocontroller.cpp

+35-22
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ template <template<class> class TScheme, class TCipher>
5252
class StandardCipherScheme : public CryptoController::CipherScheme
5353
{
5454
public:
55-
typedef TScheme<TCipher> Scheme;
55+
using Scheme = TScheme<TCipher>;
5656

5757
QByteArray name() const override;
5858
quint32 defaultKeyLength() const override;
@@ -130,7 +130,7 @@ QStringList CryptoController::allKeystoreKeys()
130130
QStringList CryptoController::availableKeystoreKeys()
131131
{
132132
QStringList keys;
133-
for(auto key : factory->allKeys()) {
133+
for(const auto &key : factory->allKeys()) {
134134
if(factory->isAvailable(key))
135135
keys.append(key);
136136
}
@@ -211,7 +211,7 @@ void CryptoController::acquireStore(bool existing)
211211
logDebug() << "Created keystore instance of type" << provider;
212212
}
213213

214-
void CryptoController::loadKeyMaterial(const QUuid &deviceId)
214+
void CryptoController::loadKeyMaterial(QUuid deviceId)
215215
{
216216
try {
217217
ensureStoreOpen();
@@ -259,7 +259,7 @@ void CryptoController::clearKeyMaterial()
259259
emit fingerprintChanged(QByteArray());
260260
}
261261

262-
void CryptoController::deleteKeyMaterial(const QUuid &deviceId)
262+
void CryptoController::deleteKeyMaterial(QUuid deviceId)
263263
{
264264
//remove all saved keys
265265
auto keyDir = keysDir();
@@ -291,7 +291,8 @@ void CryptoController::createPrivateKeys(const QByteArray &nonce)
291291
{
292292
try {
293293
if(_asymCrypto->rng().CanIncorporateEntropy())
294-
_asymCrypto->rng().IncorporateEntropy(reinterpret_cast<const byte*>(nonce.constData()), nonce.size());
294+
_asymCrypto->rng().IncorporateEntropy(reinterpret_cast<const byte*>(nonce.constData()),
295+
static_cast<size_t>(nonce.size()));
295296

296297
//generate private signature and encryption keys
297298
_asymCrypto->generate(static_cast<Setup::SignatureScheme>(defaults().property(Defaults::SignScheme).toInt()),
@@ -315,7 +316,7 @@ void CryptoController::createPrivateKeys(const QByteArray &nonce)
315316
}
316317
}
317318

318-
void CryptoController::storePrivateKeys(const QUuid &deviceId) const
319+
void CryptoController::storePrivateKeys(QUuid deviceId) const
319320
{
320321
try {
321322
ensureStoreOpen();
@@ -364,8 +365,9 @@ tuple<quint32, QByteArray, QByteArray> CryptoController::encryptData(const QByte
364365
{
365366
try {
366367
auto info = getInfo(_localCipher);
367-
QByteArray salt(info.scheme->ivLength(), Qt::Uninitialized);
368-
_asymCrypto->rng().GenerateBlock(reinterpret_cast<byte*>(salt.data()), salt.size());
368+
QByteArray salt(static_cast<int>(info.scheme->ivLength()), Qt::Uninitialized);
369+
_asymCrypto->rng().GenerateBlock(reinterpret_cast<byte*>(salt.data()),
370+
static_cast<size_t>(salt.size()));
369371

370372
auto cipher = encryptImpl(info, salt, plain);
371373

@@ -457,7 +459,8 @@ void CryptoController::decryptSecretKey(quint32 keyIndex, const QByteArray &sche
457459

458460
CipherInfo info;
459461
createScheme(scheme, info.scheme);
460-
info.key.Assign(reinterpret_cast<const byte*>(key.constData()), key.size());
462+
info.key.Assign(reinterpret_cast<const byte*>(key.constData()),
463+
static_cast<size_t>(key.size()));
461464

462465
_loadedChiphers.insert(keyIndex, info);
463466
if(grantInit) {//only set key index. Storing is done a step later for granting
@@ -582,14 +585,18 @@ tuple<QByteArray, QByteArray, SecByteBlock> CryptoController::generateExportKey(
582585
auto pw = password.toUtf8();
583586

584587
//create a salt
585-
salt.resize(info.scheme->ivLength());
586-
_asymCrypto->rng().GenerateBlock(reinterpret_cast<byte*>(salt.data()), salt.size());
588+
salt.resize(static_cast<int>(info.scheme->ivLength()));
589+
_asymCrypto->rng().GenerateBlock(reinterpret_cast<byte*>(salt.data()),
590+
static_cast<size_t>(salt.size()));
587591

588592
//generate the key
589593
PKCS5_PBKDF2_HMAC<SHA3_256> keydev;
590594
keydev.DeriveKey(info.key.data(), info.key.size(),
591-
PwPurpose, reinterpret_cast<const byte*>(pw.constData()), pw.size(),
592-
reinterpret_cast<const byte*>(salt.constData()), salt.size(), PwRounds);
595+
PwPurpose, reinterpret_cast<const byte*>(pw.constData()),
596+
static_cast<size_t>(pw.size()),
597+
reinterpret_cast<const byte*>(salt.constData()),
598+
static_cast<size_t>(salt.size()),
599+
PwRounds);
593600
}
594601

595602
return make_tuple(info.scheme->name(), salt, info.key);
@@ -613,8 +620,11 @@ SecByteBlock CryptoController::recoverExportKey(const QByteArray &scheme, const
613620
//generate the key
614621
PKCS5_PBKDF2_HMAC<SHA3_256> keydev;
615622
keydev.DeriveKey(info.key.data(), info.key.size(),
616-
PwPurpose, reinterpret_cast<const byte*>(pw.constData()), pw.size(),
617-
reinterpret_cast<const byte*>(salt.constData()), salt.size(), PwRounds);
623+
PwPurpose, reinterpret_cast<const byte*>(pw.constData()),
624+
static_cast<size_t>(pw.size()),
625+
reinterpret_cast<const byte*>(salt.constData()),
626+
static_cast<size_t>(salt.size()),
627+
PwRounds);
618628

619629
return info.key;
620630
} catch(CppException &e) {
@@ -662,7 +672,7 @@ void CryptoController::verifyImportCmac(const QByteArray &scheme, const SecByteB
662672
CipherInfo info;
663673
createScheme(scheme, info.scheme);
664674
info.key = key;
665-
return verifyCmacImpl(info, data, mac);
675+
verifyCmacImpl(info, data, mac);
666676
} catch(CppException &e) {
667677
throw CryptoException(defaults(),
668678
QStringLiteral("Failed to verify cmac for import data"),
@@ -680,7 +690,7 @@ void CryptoController::verifyImportCmacForCrypto(const QByteArray &scheme, const
680690
CipherInfo info;
681691
createScheme(scheme, info.scheme);
682692
info.key = key;
683-
return verifyCmacImpl(info, trustMessage, mac);
693+
verifyCmacImpl(info, trustMessage, mac);
684694
} catch(CppException &e) {
685695
throw CryptoException(defaults(),
686696
QStringLiteral("Failed to verify cmac for crypto keys"),
@@ -829,8 +839,9 @@ const CryptoController::CipherInfo &CryptoController::getInfo(quint32 keyIndex)
829839
keyFile.close();
830840

831841
auto key = _asymCrypto->decrypt(encData);
832-
info.key.Assign(reinterpret_cast<const byte*>(key.constData()), key.size());
833-
memset(key.data(), 0, key.size());
842+
info.key.Assign(reinterpret_cast<const byte*>(key.constData()),
843+
static_cast<size_t>(key.size()));
844+
memset(key.data(), 0, static_cast<size_t>(key.size()));
834845

835846
//test if the key is of valid length
836847
if(info.key.size() != info.scheme->toKeyLength(static_cast<quint32>(info.key.size())))
@@ -866,7 +877,7 @@ void CryptoController::cleanCiphers() const
866877
auto keys = settings()->childKeys();
867878
settings()->endGroup();
868879

869-
for(auto key : keys) {
880+
for(const auto &key : qAsConst(keys)) {
870881
auto ok = false;
871882
auto keyIndex = key.toUInt(&ok);
872883
if(!ok)
@@ -910,7 +921,8 @@ QByteArray CryptoController::encryptImpl(const CryptoController::CipherInfo &inf
910921
{
911922
auto enc = info.scheme->encryptor();
912923
enc->SetKeyWithIV(info.key.data(), info.key.size(),
913-
reinterpret_cast<const byte*>(salt.constData()), salt.size());
924+
reinterpret_cast<const byte*>(salt.constData()),
925+
static_cast<size_t>(salt.size()));
914926

915927
QByteArray cipher;
916928
QByteArraySource(plain, true,
@@ -925,7 +937,8 @@ QByteArray CryptoController::decryptImpl(const CryptoController::CipherInfo &inf
925937
{
926938
auto dec = info.scheme->decryptor();
927939
dec->SetKeyWithIV(info.key.data(), info.key.size(),
928-
reinterpret_cast<const byte*>(salt.constData()), salt.size());
940+
reinterpret_cast<const byte*>(salt.constData()),
941+
static_cast<size_t>(salt.size()));
929942

930943
QByteArray plain;
931944
QByteArraySource(cipher, true,

src/datasync/cryptocontroller_p.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class Q_DATASYNC_EXPORT CryptoController : public Controller
7676

7777
//load, clear, remove key material
7878
void acquireStore(bool existing);
79-
void loadKeyMaterial(const QUuid &deviceId);
79+
void loadKeyMaterial(QUuid deviceId);
8080
void clearKeyMaterial();
81-
void deleteKeyMaterial(const QUuid &deviceId);
81+
void deleteKeyMaterial(QUuid deviceId);
8282

8383
//create and store new keys
8484
void createPrivateKeys(const QByteArray &nonce);
85-
void storePrivateKeys(const QUuid &deviceId) const;
85+
void storePrivateKeys(QUuid deviceId) const;
8686

8787
//wrapper to sign a message
8888
QByteArray serializeSignedMessage(const Message &message);

src/datasync/datastore.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ void DataStore::initStore(const QString &setupName)
3636
this, PSIG(&DataStore::dataResetted));
3737
}
3838

39-
DataStore::~DataStore() {}
39+
DataStore::~DataStore() = default;
4040

41-
qint64 DataStore::count(int metaTypeId) const
41+
qint64 DataStore::count(int metaTypeId) const //MAJOR change to uint
4242
{
43-
return d->store->count(d->typeName(metaTypeId));
43+
return static_cast<qint64>(d->store->count(d->typeName(metaTypeId)));
4444
}
4545

4646
QStringList DataStore::keys(int metaTypeId) const
@@ -50,8 +50,10 @@ QStringList DataStore::keys(int metaTypeId) const
5050

5151
QVariantList DataStore::loadAll(int metaTypeId) const
5252
{
53+
const auto allData = d->store->loadAll(d->typeName(metaTypeId));
5354
QVariantList resList;
54-
for(auto val : d->store->loadAll(d->typeName(metaTypeId)))
55+
resList.reserve(allData.size());
56+
for(const auto &val : allData)
5557
resList.append(d->serializer->deserialize(val, metaTypeId));
5658
return resList;
5759
}
@@ -134,15 +136,17 @@ void DataStore::update(int metaTypeId, QObject *object) const
134136

135137
QVariantList DataStore::search(int metaTypeId, const QString &query, SearchMode mode) const
136138
{
139+
const auto dataList= d->store->find(d->typeName(metaTypeId), query, mode);
137140
QVariantList resList;
138-
for(auto val : d->store->find(d->typeName(metaTypeId), query, mode))
141+
resList.reserve(dataList.size());
142+
for(const auto &val : dataList)
139143
resList.append(d->serializer->deserialize(val, metaTypeId));
140144
return resList;
141145
}
142146

143147
void DataStore::iterate(int metaTypeId, const function<bool (QVariant)> &iterator) const
144148
{
145-
for(auto key : keys(metaTypeId)) {
149+
for(const auto &key : keys(metaTypeId)) {
146150
if(!iterator(load(metaTypeId, key)))
147151
break;
148152
}
@@ -215,8 +219,7 @@ QString LocalStoreException::qWhat() const
215219
QString msg = Exception::qWhat() +
216220
QStringLiteral("\n\tContext: %1"
217221
"\n\tTypeName: %2")
218-
.arg(_context)
219-
.arg(QString::fromUtf8(_key.typeName));
222+
.arg(_context, QString::fromUtf8(_key.typeName));
220223
if(!_key.id.isNull())
221224
msg += QStringLiteral("\n\tKey: %1").arg(_key.id);
222225
return msg;
@@ -259,8 +262,7 @@ QString NoDataException::qWhat() const
259262
return Exception::qWhat() +
260263
QStringLiteral("\n\tTypeName: %1"
261264
"\n\tKey: %2")
262-
.arg(QString::fromUtf8(_key.typeName))
263-
.arg(_key.id);
265+
.arg(QString::fromUtf8(_key.typeName), _key.id);
264266
}
265267

266268
void NoDataException::raise() const

src/datasync/datastoremodel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void DataStoreModel::initStore(DataStore *store)
3939
this, &DataStoreModel::storeResetted);
4040
}
4141

42-
DataStoreModel::~DataStoreModel() {}
42+
DataStoreModel::~DataStoreModel() = default;
4343

4444
DataStore *DataStoreModel::store() const
4545
{

src/datasync/defaults.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ DatabaseRef::DatabaseRef() :
145145
d(nullptr)
146146
{}
147147

148-
DatabaseRef::~DatabaseRef() {}
148+
DatabaseRef::~DatabaseRef() = default;
149149

150150
DatabaseRef::DatabaseRef(DatabaseRefPrivate *d) :
151151
d(d)
@@ -246,7 +246,7 @@ void DefaultsPrivate::clearDefaults()
246246
for(auto it = setupDefaults.constBegin(); it != setupDefaults.constEnd(); it++)
247247
weakRefs.append({it.key(), it.value().toWeakRef()});
248248
setupDefaults.clear();
249-
for(auto ref : weakRefs) {
249+
for(const auto &ref : qAsConst(weakRefs)) {
250250
#undef QTDATASYNC_LOG
251251
#define QTDATASYNC_LOG ref.second.toStrongRef()->logger
252252
if(ref.second)
@@ -269,14 +269,14 @@ QSharedPointer<DefaultsPrivate> DefaultsPrivate::obtainDefaults(const QString &s
269269
throw SetupDoesNotExistException(setupName);
270270
}
271271

272-
DefaultsPrivate::DefaultsPrivate(const QString &setupName, const QDir &storageDir, const QUrl &roAddress, const QHash<Defaults::PropertyKey, QVariant> &properties, QJsonSerializer *serializer, ConflictResolver *resolver) :
273-
setupName(setupName),
274-
storageDir(storageDir),
275-
logger(new Logger("defaults", setupName, this)),
276-
roAddress(roAddress),
272+
DefaultsPrivate::DefaultsPrivate(QString setupName, QDir storageDir, QUrl roAddress, QHash<Defaults::PropertyKey, QVariant> properties, QJsonSerializer *serializer, ConflictResolver *resolver) :
273+
setupName(std::move(setupName)),
274+
storageDir(std::move(storageDir)),
275+
logger(new Logger("defaults", this->setupName, this)),
276+
roAddress(std::move(roAddress)),
277277
serializer(serializer),
278278
resolver(resolver),
279-
properties(properties),
279+
properties(std::move(properties)),
280280
roMutex(),
281281
roNodes(),
282282
cacheInfo(nullptr),
@@ -307,16 +307,15 @@ DefaultsPrivate::DefaultsPrivate(const QString &setupName, const QDir &storageDi
307307
DefaultsPrivate::~DefaultsPrivate()
308308
{
309309
QMutexLocker _(&roMutex);
310-
for(auto node : roNodes)
310+
for(const auto &node : qAsConst(roNodes))
311311
node->deleteLater();
312312
roNodes.clear();
313313
}
314314

315315
QSqlDatabase DefaultsPrivate::acquireDatabase()
316316
{
317317
auto name = DefaultsPrivate::DatabaseName
318-
.arg(setupName)
319-
.arg(QString::number(reinterpret_cast<quint64>(QThread::currentThread()), 16));
318+
.arg(setupName, QString::number(reinterpret_cast<quint64>(QThread::currentThread()), 16));
320319
if((dbRefHash.localData()[setupName])++ == 0) {
321320
auto database = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), name);
322321
database.setDatabaseName(storageDir.absoluteFilePath(QStringLiteral("store.db")));
@@ -362,8 +361,7 @@ void DefaultsPrivate::releaseDatabase()
362361
{
363362
if(--(dbRefHash.localData()[setupName]) == 0) {
364363
auto name = DefaultsPrivate::DatabaseName
365-
.arg(setupName)
366-
.arg(QString::number(reinterpret_cast<quint64>(QThread::currentThread()), 16));
364+
.arg(setupName, QString::number(reinterpret_cast<quint64>(QThread::currentThread()), 16));
367365
QSqlDatabase::database(name).close();
368366
QSqlDatabase::removeDatabase(name);
369367
}
@@ -417,7 +415,7 @@ void DefaultsPrivate::makePassive()
417415
// ------------- PRIVATE IMPLEMENTATION DatabaseRef -------------
418416

419417
DatabaseRefPrivate::DatabaseRefPrivate(QSharedPointer<DefaultsPrivate> defaultsPrivate, QObject *object) :
420-
_defaultsPrivate(defaultsPrivate),
418+
_defaultsPrivate(std::move(defaultsPrivate)),
421419
_object(object),
422420
_database()
423421
{

src/datasync/defaults_p.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ class Q_DATASYNC_EXPORT DefaultsPrivate : public QObject
5757
static void clearDefaults();
5858
static QSharedPointer<DefaultsPrivate> obtainDefaults(const QString &setupName);
5959

60-
DefaultsPrivate(const QString &setupName,
61-
const QDir &storageDir,
62-
const QUrl &roAddress,
63-
const QHash<Defaults::PropertyKey, QVariant> &properties,
60+
DefaultsPrivate(QString setupName,
61+
QDir storageDir,
62+
QUrl roAddress,
63+
QHash<Defaults::PropertyKey, QVariant> properties,
6464
QJsonSerializer *serializer,
6565
ConflictResolver *resolver);
6666
~DefaultsPrivate() override;

src/datasync/emitteradapter.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ EmitterAdapter::EmitterAdapter(QObject *changeEmitter, QSharedPointer<CacheInfo>
66
QObject(origin),
77
_isPrimary(changeEmitter->metaObject()->inherits(&ChangeEmitter::staticMetaObject)),
88
_emitterBackend(changeEmitter),
9-
_cache(cacheInfo)
9+
_cache(std::move(cacheInfo))
1010
{
1111
if(_isPrimary) {
1212
connect(_emitterBackend, SIGNAL(dataChanged(QObject*,QtDataSync::ObjectKey,bool)),
1313
this, SLOT(dataChangedImpl(QObject*,QtDataSync::ObjectKey,bool)),
1414
Qt::QueuedConnection);
1515
connect(_emitterBackend, SIGNAL(dataResetted(QObject*)),
16-
this, SLOT(dataResettedImpl(QObject*,QByteArray)),
16+
this, SLOT(dataResettedImpl(QObject*)),
1717
Qt::QueuedConnection);
1818
} else {
1919
connect(_emitterBackend, SIGNAL(remoteDataChanged(QtDataSync::ObjectKey,bool)),
2020
this, SLOT(remoteDataChangedImpl(QtDataSync::ObjectKey,bool)),
2121
Qt::QueuedConnection);
2222
connect(_emitterBackend, SIGNAL(remoteDataResetted()),
23-
this, SLOT(remoteDataResettedImpl(QByteArray)),
23+
this, SLOT(remoteDataResettedImpl()),
2424
Qt::QueuedConnection);
2525
}
2626
}
@@ -151,7 +151,7 @@ void EmitterAdapter::dropCached()
151151
return;
152152

153153
QWriteLocker _(&_cache->lock);
154-
return _cache->cache.clear();
154+
_cache->cache.clear();
155155
}
156156

157157
void EmitterAdapter::dataChangedImpl(QObject *origin, const ObjectKey &key, bool deleted)

0 commit comments

Comments
 (0)