Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: Use tox core "length" functions instead of constants. #174

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,9 @@ ToxId Core::getSelfId() const
{
QMutexLocker<QRecursiveMutex> ml{&coreLoopLock};

uint8_t friendId[TOX_ADDRESS_SIZE] = {0x00};
tox_self_get_address(tox.get(), friendId);
return ToxId(friendId, TOX_ADDRESS_SIZE);
std::vector<uint8_t> friendId(tox_address_size());
tox_self_get_address(tox.get(), friendId.data());
return ToxId(friendId.data(), friendId.size());
}

/**
Expand All @@ -837,15 +837,15 @@ ToxPk Core::getSelfPublicKey() const
{
QMutexLocker<QRecursiveMutex> ml{&coreLoopLock};

uint8_t selfPk[TOX_PUBLIC_KEY_SIZE] = {0x00};
tox_self_get_public_key(tox.get(), selfPk);
return ToxPk(selfPk);
std::vector<uint8_t> selfPk(tox_public_key_size());
tox_self_get_public_key(tox.get(), selfPk.data());
return ToxPk(selfPk.data());
}

QByteArray Core::getSelfDhtId() const
{
QMutexLocker<QRecursiveMutex> 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<uint8_t*>(dhtKey.data()));
return dhtKey;
}
Expand Down Expand Up @@ -962,14 +962,14 @@ void Core::loadFriends()

std::vector<uint32_t> ids(friendCount);
tox_self_get_friend_list(tox.get(), ids.data());
uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00};
std::vector<uint8_t> 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);
Expand Down Expand Up @@ -1052,7 +1052,7 @@ ConferenceId Core::getConferencePersistentId(uint32_t conferenceNumber) const
{
QMutexLocker<QRecursiveMutex> ml{&coreLoopLock};

std::vector<uint8_t> idBuff(TOX_CONFERENCE_UID_SIZE);
std::vector<uint8_t> idBuff(tox_conference_id_size());
if (tox_conference_get_id(tox.get(), conferenceNumber, idBuff.data())) {
return ConferenceId{idBuff.data()};
} else {
Expand Down Expand Up @@ -1107,14 +1107,14 @@ ToxPk Core::getConferencePeerPk(int conferenceId, int peerId) const
{
QMutexLocker<QRecursiveMutex> ml{&coreLoopLock};

uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00};
std::vector<uint8_t> 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());
}

/**
Expand Down Expand Up @@ -1289,15 +1289,15 @@ ToxPk Core::getFriendPublicKey(uint32_t friendNumber) const
{
QMutexLocker<QRecursiveMutex> ml{&coreLoopLock};

uint8_t rawid[TOX_PUBLIC_KEY_SIZE];
std::vector<uint8_t> 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());
}

/**
Expand Down
51 changes: 25 additions & 26 deletions src/core/corefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,32 @@ void CoreFile::sendAvatarFile(uint32_t friendId, const QByteArray& data)
{
QMutexLocker<QRecursiveMutex> locker{coreLoopLock};

uint32_t fileNum;
uint64_t filesize = 0;
uint8_t* file_id = nullptr;
uint8_t* file_name = nullptr;
size_t nameLength = 0;
uint8_t avatarHash[TOX_HASH_LENGTH];
std::vector<uint8_t> 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<const uint8_t*>(data.data()), data.size());
Q_ASSERT(tox_hash_length() <= tox_file_id_length());
tox_hash(avatarHash.data(), reinterpret_cast<const uint8_t*>(data.data()), data.size());
filesize = data.size();
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);
if (!PARSE_ERR(error)) {
return;

Tox_Err_File_Send error;
fileNum = tox_file_send(tox, friendId, TOX_FILE_KIND_AVATAR, filesize, avatarHash.data(),
avatarHash.data(), avatarHash.size(), &error);
if (!PARSE_ERR(error)) {
return;
}
} else {
Tox_Err_File_Send error;
fileNum = tox_file_send(tox, friendId, TOX_FILE_KIND_AVATAR, 0, nullptr, nullptr, 0, &error);
if (!PARSE_ERR(error)) {
return;
}
}

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<uint8_t*>(file.resumeFileId.data()), &fileGetErr);
Expand Down Expand Up @@ -138,7 +139,7 @@ void CoreFile::sendFile(uint32_t friendId, QString filename, QString filePath, l
filePath,
static_cast<uint64_t>(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<uint8_t*>(file.resumeFileId.data()), &fileGetErr);
Expand Down Expand Up @@ -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<uint8_t> 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<const char*>(static_cast<const void*>(avatarHash)),
TOX_HASH_LENGTH};
QByteArray avatarBytes(reinterpret_cast<const char*>(avatarHash.data()), tox_hash_length());
emit core->fileAvatarOfferReceived(friendId, fileId, avatarBytes, filesize);
return;
}
Expand All @@ -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<uint8_t*>(file.resumeFileId.data()), &fileGetErr);
Expand Down Expand Up @@ -407,7 +406,7 @@ void CoreFile::handleAvatarOffer(uint32_t friendId, uint32_t fileId, bool accept

ToxFile file{fileId, friendId, "<avatar>", "", 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<uint8_t*>(file.resumeFileId.data()), &getErr);
Expand Down
13 changes: 6 additions & 7 deletions src/persistence/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t*>(hash.data()), hashSize,
reinterpret_cast<uint8_t*>(idData.data()), idData.size(),
Expand Down Expand Up @@ -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<uint8_t*>(avatarHash.data()), reinterpret_cast<uint8_t*>(pic.data()),
pic.size());
return avatarHash;
Expand Down
Loading