From 2584723d378794ef6b84296df17379eee6cf5eed Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 30 Nov 2024 23:15:42 +0000 Subject: [PATCH] cleanup: Use tox core "length" functions instead of constants. These constants can change from version to version, so we should use a runtime version of them rather than encoding the number into our binary at compile time. --- src/core/core.cpp | 34 +++++++++++++++--------------- src/core/corefile.cpp | 41 ++++++++++++++++++------------------- src/persistence/profile.cpp | 13 ++++++------ 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 5c7507f85b..a32680b237 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -824,9 +824,9 @@ ToxId Core::getSelfId() const { QMutexLocker ml{&coreLoopLock}; - uint8_t friendId[TOX_ADDRESS_SIZE] = {0x00}; - tox_self_get_address(tox.get(), friendId); - return ToxId(friendId, TOX_ADDRESS_SIZE); + std::vector friendId(tox_address_size()); + tox_self_get_address(tox.get(), friendId.data()); + return ToxId(friendId.data(), friendId.size()); } /** @@ -837,15 +837,15 @@ ToxPk Core::getSelfPublicKey() const { QMutexLocker ml{&coreLoopLock}; - uint8_t selfPk[TOX_PUBLIC_KEY_SIZE] = {0x00}; - tox_self_get_public_key(tox.get(), selfPk); - return ToxPk(selfPk); + std::vector selfPk(tox_public_key_size()); + tox_self_get_public_key(tox.get(), selfPk.data()); + return ToxPk(selfPk.data()); } QByteArray Core::getSelfDhtId() const { QMutexLocker ml{&coreLoopLock}; - QByteArray dhtKey(TOX_PUBLIC_KEY_SIZE, 0x00); + QByteArray dhtKey(tox_public_key_size(), 0x00); tox_self_get_dht_id(tox.get(), reinterpret_cast(dhtKey.data())); return dhtKey; } @@ -962,14 +962,14 @@ void Core::loadFriends() std::vector ids(friendCount); tox_self_get_friend_list(tox.get(), ids.data()); - uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00}; + std::vector friendPk(tox_public_key_size()); for (size_t i = 0; i < friendCount; ++i) { Tox_Err_Friend_Get_Public_Key keyError; - tox_friend_get_public_key(tox.get(), ids[i], friendPk, &keyError); + tox_friend_get_public_key(tox.get(), ids[i], friendPk.data(), &keyError); if (!PARSE_ERR(keyError)) { continue; } - emit friendAdded(ids[i], ToxPk(friendPk)); + emit friendAdded(ids[i], ToxPk(friendPk.data())); emit friendUsernameChanged(ids[i], getFriendUsername(ids[i])); Tox_Err_Friend_Query queryError; size_t statusMessageSize = tox_friend_get_status_message_size(tox.get(), ids[i], &queryError); @@ -1052,7 +1052,7 @@ ConferenceId Core::getConferencePersistentId(uint32_t conferenceNumber) const { QMutexLocker ml{&coreLoopLock}; - std::vector idBuff(TOX_CONFERENCE_UID_SIZE); + std::vector idBuff(tox_conference_id_size()); if (tox_conference_get_id(tox.get(), conferenceNumber, idBuff.data())) { return ConferenceId{idBuff.data()}; } else { @@ -1107,14 +1107,14 @@ ToxPk Core::getConferencePeerPk(int conferenceId, int peerId) const { QMutexLocker ml{&coreLoopLock}; - uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00}; + std::vector friendPk(tox_public_key_size()); Tox_Err_Conference_Peer_Query error; - tox_conference_peer_get_public_key(tox.get(), conferenceId, peerId, friendPk, &error); + tox_conference_peer_get_public_key(tox.get(), conferenceId, peerId, friendPk.data(), &error); if (!PARSE_ERR(error)) { return ToxPk{}; } - return ToxPk(friendPk); + return ToxPk(friendPk.data()); } /** @@ -1289,15 +1289,15 @@ ToxPk Core::getFriendPublicKey(uint32_t friendNumber) const { QMutexLocker ml{&coreLoopLock}; - uint8_t rawid[TOX_PUBLIC_KEY_SIZE]; + std::vector rawid(tox_public_key_size()); Tox_Err_Friend_Get_Public_Key error; - tox_friend_get_public_key(tox.get(), friendNumber, rawid, &error); + tox_friend_get_public_key(tox.get(), friendNumber, rawid.data(), &error); if (!PARSE_ERR(error)) { qWarning() << "getFriendPublicKey: Getting public key failed"; return ToxPk(); } - return ToxPk(rawid); + return ToxPk(rawid.data()); } /** diff --git a/src/core/corefile.cpp b/src/core/corefile.cpp index 00583a8db6..e62bacd13f 100644 --- a/src/core/corefile.cpp +++ b/src/core/corefile.cpp @@ -84,22 +84,23 @@ void CoreFile::sendAvatarFile(uint32_t friendId, const QByteArray& data) QMutexLocker locker{coreLoopLock}; uint64_t filesize = 0; - uint8_t* file_id = nullptr; - uint8_t* file_name = nullptr; + std::vector* file_id = nullptr; + std::vector* file_name = nullptr; size_t nameLength = 0; - uint8_t avatarHash[TOX_HASH_LENGTH]; + std::vector avatarHash(tox_hash_length()); if (!data.isEmpty()) { - static_assert(TOX_HASH_LENGTH <= TOX_FILE_ID_LENGTH, - "TOX_HASH_LENGTH > TOX_FILE_ID_LENGTH!"); - tox_hash(avatarHash, reinterpret_cast(data.data()), data.size()); + Q_ASSERT(tox_hash_length() <= tox_file_id_length()); + tox_hash(avatarHash.data(), reinterpret_cast(data.data()), data.size()); filesize = data.size(); - file_id = avatarHash; - file_name = avatarHash; - nameLength = TOX_HASH_LENGTH; + file_id = &avatarHash; + file_name = &avatarHash; + nameLength = tox_hash_length(); } Tox_Err_File_Send error; - const uint32_t fileNum = tox_file_send(tox, friendId, TOX_FILE_KIND_AVATAR, filesize, file_id, - file_name, nameLength, &error); + const uint32_t fileNum = + tox_file_send(tox, friendId, TOX_FILE_KIND_AVATAR, filesize, + file_id == nullptr ? nullptr : file_id->data(), + file_name == nullptr ? nullptr : file_name->data(), nameLength, &error); if (!PARSE_ERR(error)) { return; } @@ -107,7 +108,7 @@ void CoreFile::sendAvatarFile(uint32_t friendId, const QByteArray& data) ToxFile file{fileNum, friendId, "", "", filesize, ToxFile::SENDING}; file.fileKind = TOX_FILE_KIND_AVATAR; file.avatarData = data; - file.resumeFileId.resize(TOX_FILE_ID_LENGTH); + file.resumeFileId.resize(tox_file_id_length()); Tox_Err_File_Get fileGetErr; tox_file_get_file_id(tox, friendId, fileNum, reinterpret_cast(file.resumeFileId.data()), &fileGetErr); @@ -138,7 +139,7 @@ void CoreFile::sendFile(uint32_t friendId, QString filename, QString filePath, l filePath, static_cast(filesize), ToxFile::SENDING}; - file.resumeFileId.resize(TOX_FILE_ID_LENGTH); + file.resumeFileId.resize(tox_file_id_length()); Tox_Err_File_Get fileGetErr; tox_file_get_file_id(tox, friendId, fileNum, reinterpret_cast(file.resumeFileId.data()), &fileGetErr); @@ -343,16 +344,14 @@ void CoreFile::onFileReceiveCallback(Tox* tox, uint32_t friendId, uint32_t fileI PARSE_ERR(err); return; } - static_assert(TOX_HASH_LENGTH <= TOX_FILE_ID_LENGTH, - "TOX_HASH_LENGTH > TOX_FILE_ID_LENGTH!"); - uint8_t avatarHash[TOX_FILE_ID_LENGTH]; + Q_ASSERT(tox_hash_length() <= tox_file_id_length()); + std::vector avatarHash(tox_file_id_length()); Tox_Err_File_Get fileGetErr; - tox_file_get_file_id(tox, friendId, fileId, avatarHash, &fileGetErr); + tox_file_get_file_id(tox, friendId, fileId, avatarHash.data(), &fileGetErr); if (!PARSE_ERR(fileGetErr)) { return; } - QByteArray avatarBytes{static_cast(static_cast(avatarHash)), - TOX_HASH_LENGTH}; + QByteArray avatarBytes(reinterpret_cast(avatarHash.data()), tox_hash_length()); emit core->fileAvatarOfferReceived(friendId, fileId, avatarBytes, filesize); return; } @@ -370,7 +369,7 @@ void CoreFile::onFileReceiveCallback(Tox* tox, uint32_t friendId, uint32_t fileI ToxFile file{fileId, friendId, filename.getQString(), "", filesize, ToxFile::RECEIVING}; file.fileKind = kind; - file.resumeFileId.resize(TOX_FILE_ID_LENGTH); + file.resumeFileId.resize(tox_file_id_length()); Tox_Err_File_Get fileGetErr; tox_file_get_file_id(tox, friendId, fileId, reinterpret_cast(file.resumeFileId.data()), &fileGetErr); @@ -407,7 +406,7 @@ void CoreFile::handleAvatarOffer(uint32_t friendId, uint32_t fileId, bool accept ToxFile file{fileId, friendId, "", "", filesize, ToxFile::RECEIVING}; file.fileKind = TOX_FILE_KIND_AVATAR; - file.resumeFileId.resize(TOX_FILE_ID_LENGTH); + file.resumeFileId.resize(tox_file_id_length()); Tox_Err_File_Get getErr; tox_file_get_file_id(tox, friendId, fileId, reinterpret_cast(file.resumeFileId.data()), &getErr); diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index bb324bdf62..c5be559ece 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -533,12 +533,11 @@ QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted) QByteArray idData = ownerStr.toUtf8(); QByteArray pubkeyData = core->getSelfPublicKey().getByteArray(); - constexpr int hashSize = TOX_PUBLIC_KEY_SIZE; - static_assert(hashSize >= crypto_generichash_BYTES_MIN && hashSize <= crypto_generichash_BYTES_MAX, - "Hash size not supported by libsodium"); - static_assert(hashSize >= crypto_generichash_KEYBYTES_MIN - && hashSize <= crypto_generichash_KEYBYTES_MAX, - "Key size not supported by libsodium"); + const uint32_t hashSize = tox_public_key_size(); + Q_ASSERT_X(hashSize >= crypto_generichash_BYTES_MIN && hashSize <= crypto_generichash_BYTES_MAX, + "avatarPath", "Hash size not supported by libsodium"); + Q_ASSERT_X(hashSize >= crypto_generichash_KEYBYTES_MIN && hashSize <= crypto_generichash_KEYBYTES_MAX, + "avatarPath", "Key size not supported by libsodium"); QByteArray hash(hashSize, 0); crypto_generichash(reinterpret_cast(hash.data()), hashSize, reinterpret_cast(idData.data()), idData.size(), @@ -746,7 +745,7 @@ void Profile::saveAvatar(const ToxPk& owner, const QByteArray& avatar) QByteArray Profile::getAvatarHash(const ToxPk& owner) { QByteArray pic = loadAvatarData(owner); - QByteArray avatarHash(TOX_HASH_LENGTH, 0); + QByteArray avatarHash(tox_hash_length(), 0); tox_hash(reinterpret_cast(avatarHash.data()), reinterpret_cast(pic.data()), pic.size()); return avatarHash;