Skip to content

Commit 3b271fe

Browse files
committed
fix buffer issue on group images (should fix decoding problems)
1 parent 3275bdc commit 3b271fe

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/core/core.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,9 @@ void Core::onNgcGroupCustomPacket(Tox* tox, uint32_t group_number, uint32_t peer
913913
{
914914
// HINT: ok we have a group file
915915
auto peerPk = core->getGroupPeerPk((Settings::NGC_GROUPNUM_OFFSET + group_number), peer_id);
916+
QByteArray image_data_bytes = QByteArray(reinterpret_cast<const char*>(data + header_len), (length - header_len));
916917
emit core->groupMessageReceivedImage((Settings::NGC_GROUPNUM_OFFSET + group_number),
917-
peer_id, (data + header_len),
918+
peer_id, image_data_bytes,
918919
(length - header_len), false,
919920
static_cast<int>(Widget::MessageHasIdType::NGC_MSG_ID));
920921
}

src/core/core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public slots:
187187
void emptyGroupCreated(int groupnumber, const GroupId groupId, const QString& title = QString());
188188
void groupInviteReceived(const GroupInvite& inviteInfo);
189189
void groupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction, bool isPrivate = false, const int hasIdType = 0);
190-
void groupMessageReceivedImage(int groupnumber, int peernumber, const uint8_t *data, size_t length, bool isAction, const int hasIdType = 0);
190+
void groupMessageReceivedImage(int groupnumber, int peernumber, const QByteArray& image_bytes, size_t length, bool isAction, const int hasIdType = 0);
191191
void groupSyncHistoryReqReceived(int groupnumber, int peernumber, ToxPk peerPk);
192192
void groupNamelistChanged(int groupnumber, int peernumber, uint8_t change);
193193
void groupPeerlistChanged(int groupnumber);

src/widget/widget.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cassert>
2323

2424
#include <QClipboard>
25+
#include <QCryptographicHash>
2526
#include <QDebug>
2627
#include <QDesktopServices>
2728
#include <QDesktopWidget>
@@ -2073,7 +2074,7 @@ void Widget::onGroupMessageReceived(int groupnumber, int peernumber, const QStri
20732074
groupMessageDispatchers[groupId]->onMessageReceived(author, isAction, isPrivate, message, hasIdType);
20742075
}
20752076

2076-
void Widget::onGroupMessageReceivedImage(int groupnumber, int peernumber, const uint8_t *image_data,
2077+
void Widget::onGroupMessageReceivedImage(int groupnumber, int peernumber, const QByteArray& image_bytes,
20772078
size_t length, bool isAction, const int hasIdType)
20782079
{
20792080
const GroupId& groupId = groupList->id2Key(groupnumber);
@@ -2084,25 +2085,28 @@ void Widget::onGroupMessageReceivedImage(int groupnumber, int peernumber, const
20842085
std::ignore = isAction;
20852086
std::ignore = hasIdType;
20862087

2087-
QByteArray image_data_bytes = QByteArray(reinterpret_cast<const char*>(image_data), length);
2088-
// qDebug() << "onGroupMessageReceivedImage:image_data_bytes:"
2089-
// << QString::fromUtf8(image_data_bytes.toHex()).toUpper().rightJustified((length * 2), '0')
2088+
QByteArray shaBa = QCryptographicHash::hash(image_bytes, QCryptographicHash::Sha256);
2089+
QString sha256hash = QString::fromUtf8(shaBa.toHex()).toUpper();
2090+
qDebug() << "onGroupMessageReceivedImage:SHA256:" << sha256hash << "length" << length;
2091+
2092+
// qDebug() << "onGroupMessageReceivedImage:image_bytes:"
2093+
// << QString::fromUtf8(image_bytes.toHex()).toUpper().rightJustified((length * 2), '0')
20902094
// << "len:" << length;
20912095

20922096
QPixmap mpixmap;
2093-
bool result = mpixmap.loadFromData(image_data_bytes);
2097+
bool result = mpixmap.loadFromData(image_bytes);
20942098
if (!result)
20952099
{
20962100
qDebug() << "onGroupMessageReceivedImage:loadFromData:trying with hardcoded WEBP type";
2097-
result = mpixmap.loadFromData(image_data_bytes, "WEBP");
2101+
result = mpixmap.loadFromData(image_bytes, "WEBP");
20982102
}
20992103
qDebug() << "onGroupMessageReceivedImage:loadFromData:res=" << result;
21002104

21012105
//if (result)
21022106
//{
21032107
// HINT: image could be loaded OK, so save hex data into DB
21042108
// add message even if image could not be loaded
2105-
QString message = QString::fromUtf8(image_data_bytes.toHex()).toUpper().rightJustified((length * 2), '0') + QString(":") + QString("___");
2109+
QString message = QString::fromUtf8(image_bytes.toHex()).toUpper().rightJustified((length * 2), '0') + QString(":") + QString("___");
21062110
groupMessageDispatchers[groupId]->onMessageReceived(author, isAction, false, message, hasIdType);
21072111
//}
21082112
}

src/widget/widget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public slots:
207207
void onGroupInviteReceived(const GroupInvite& inviteInfo);
208208
void onGroupInviteAccepted(const GroupInvite& inviteInfo);
209209
void onGroupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction, bool isPrivate = false, const int hasIdType = 0);
210-
void onGroupMessageReceivedImage(int groupnumber, int peernumber, const uint8_t *image_data, size_t length, bool isAction, const int hasIdType = 0);
210+
void onGroupMessageReceivedImage(int groupnumber, int peernumber, const QByteArray& image_bytes, size_t length, bool isAction, const int hasIdType = 0);
211211
void onGroupSyncHistoryReqReceived(int groupnumber, int peernumber, ToxPk peerPk);
212212
void onGroupPeerlistChanged(uint32_t groupnumber);
213213
void onGroupPeerNameChanged(uint32_t groupnumber, const ToxPk& peerPk, const QString& newName);

0 commit comments

Comments
 (0)