diff options
author | Maxim Biro <nurupo.contributions@gmail.com> | 2013-08-09 16:56:11 -0400 |
---|---|---|
committer | Maxim Biro <nurupo.contributions@gmail.com> | 2013-08-09 17:03:37 -0400 |
commit | 0f8eea89fab7e3e219819d9c7eee65b454ddab7e (patch) | |
tree | 5bef065a697f64e36ac518e30b986e9af05be887 /core | |
parent | 9dd691d106f3a77e4c679df5d71023ab6a4f4316 (diff) |
Introduced data pointer and data_length
Diffstat (limited to 'core')
-rw-r--r-- | core/Messenger.c | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 58e6085f..89fc290b 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -571,55 +571,62 @@ static void doFriends(void) | |||
571 | friendlist[i].userstatus_sent = 1; | 571 | friendlist[i].userstatus_sent = 1; |
572 | } | 572 | } |
573 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 573 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); |
574 | uint8_t packet_id = temp[0]; | ||
575 | uint8_t* data = temp + 1; | ||
576 | int data_length = len - 1; | ||
574 | if (len > 0) { | 577 | if (len > 0) { |
575 | switch (temp[0]) { | 578 | switch (packet_id) { |
576 | case PACKET_ID_NICKNAME: { | 579 | case PACKET_ID_NICKNAME: { |
577 | if (len >= MAX_NAME_LENGTH + 1 || len == 1) | 580 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) |
578 | break; | 581 | break; |
579 | if(friend_namechange_isset) | 582 | if(friend_namechange_isset) |
580 | friend_namechange(i, temp + 1, len - 1); | 583 | friend_namechange(i, data, data_length); |
581 | memcpy(friendlist[i].name, temp + 1, len - 1); | 584 | memcpy(friendlist[i].name, data, data_length); |
582 | friendlist[i].name[len - 2] = 0; /* make sure the NULL terminator is present. */ | 585 | friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ |
583 | break; | 586 | break; |
584 | } | 587 | } |
585 | case PACKET_ID_STATUSMESSAGE: { | 588 | case PACKET_ID_STATUSMESSAGE: { |
586 | if (len < 2) | 589 | if (data_length == 0) |
587 | break; | 590 | break; |
588 | uint8_t *status = calloc(MIN(len - 1, MAX_STATUSMESSAGE_LENGTH), 1); | 591 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); |
589 | memcpy(status, temp + 1, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH)); | 592 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
590 | if (friend_statusmessagechange_isset) | 593 | if (friend_statusmessagechange_isset) |
591 | friend_statusmessagechange(i, status, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH)); | 594 | friend_statusmessagechange(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
592 | set_friend_statusmessage(i, status, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH)); | 595 | set_friend_statusmessage(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
593 | free(status); | 596 | free(status); |
594 | break; | 597 | break; |
595 | } | 598 | } |
596 | case PACKET_ID_USERSTATUS: { | 599 | case PACKET_ID_USERSTATUS: { |
597 | if (len != 2) | 600 | if (data_length != 1) |
598 | break; | 601 | break; |
599 | USERSTATUS status = temp[1]; | 602 | USERSTATUS status = data[0]; |
600 | if (friend_userstatuschange_isset) | 603 | if (friend_userstatuschange_isset) |
601 | friend_userstatuschange(i, status); | 604 | friend_userstatuschange(i, status); |
602 | set_friend_userstatus(i, status); | 605 | set_friend_userstatus(i, status); |
603 | break; | 606 | break; |
604 | } | 607 | } |
605 | case PACKET_ID_MESSAGE: { | 608 | case PACKET_ID_MESSAGE: { |
609 | uint8_t *message_id = data; | ||
610 | uint8_t message_id_length = 4; | ||
611 | uint8_t *message = data + message_id_length; | ||
612 | uint16_t message_length = data_length - message_id_length; | ||
606 | if (friendlist[i].receives_read_receipts) { | 613 | if (friendlist[i].receives_read_receipts) { |
607 | write_cryptpacket_id(i, PACKET_ID_RECEIPT, temp + 1, 4); | 614 | write_cryptpacket_id(i, PACKET_ID_RECEIPT, message_id, message_id_length); |
608 | } | 615 | } |
609 | if (friend_message_isset) | 616 | if (friend_message_isset) |
610 | (*friend_message)(i, temp + 5, len - 5); | 617 | (*friend_message)(i, message, message_length); |
611 | break; | 618 | break; |
612 | } | 619 | } |
613 | case PACKET_ID_ACTION: { | 620 | case PACKET_ID_ACTION: { |
614 | if (friend_action_isset) | 621 | if (friend_action_isset) |
615 | (*friend_action)(i, temp + 1, len - 1); | 622 | (*friend_action)(i, data, data_length); |
616 | break; | 623 | break; |
617 | } | 624 | } |
618 | case PACKET_ID_RECEIPT: { | 625 | case PACKET_ID_RECEIPT: { |
619 | uint32_t msgid; | 626 | uint32_t msgid; |
620 | if (len < 1 + sizeof(msgid)) | 627 | if (data_length < sizeof(msgid)) |
621 | break; | 628 | break; |
622 | memcpy(&msgid, temp + 1, sizeof(msgid)); | 629 | memcpy(&msgid, data, sizeof(msgid)); |
623 | msgid = ntohl(msgid); | 630 | msgid = ntohl(msgid); |
624 | if (read_receipt_isset) | 631 | if (read_receipt_isset) |
625 | (*read_receipt)(i, msgid); | 632 | (*read_receipt)(i, msgid); |