summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorRobin Lindén <mail+gitlab@robinlinden.eu>2016-11-16 02:10:38 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-11-21 00:28:33 +0000
commit6e1a01bd1662f9ccb834ebdd706f753ca22ff2cf (patch)
tree98a9076073f5dd9cd894410bbe918a47de048b9b /toxcore
parent75a33d220106f66e3abe80409a10cb55130fc457 (diff)
Made saveformat platform-independent.
Fixes #215.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c127
1 files changed, 118 insertions, 9 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 77a19f30..d4c4526e 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -2633,6 +2633,7 @@ void do_messenger(Messenger *m, void *userdata)
2633 2633
2634#define SAVED_FRIEND_REQUEST_SIZE 1024 2634#define SAVED_FRIEND_REQUEST_SIZE 1024
2635#define NUM_SAVED_PATH_NODES 8 2635#define NUM_SAVED_PATH_NODES 8
2636
2636struct SAVED_FRIEND { 2637struct SAVED_FRIEND {
2637 uint8_t status; 2638 uint8_t status;
2638 uint8_t real_pk[crypto_box_PUBLICKEYBYTES]; 2639 uint8_t real_pk[crypto_box_PUBLICKEYBYTES];
@@ -2647,20 +2648,82 @@ struct SAVED_FRIEND {
2647 uint64_t last_seen_time; 2648 uint64_t last_seen_time;
2648}; 2649};
2649 2650
2651static uint32_t friend_size()
2652{
2653 uint32_t data = 0;
2654 const struct SAVED_FRIEND temp = { 0 };
2655
2656#define VALUE_MEMBER(NAME) data += sizeof(temp.NAME)
2657#define ARRAY_MEMBER(NAME) data += sizeof(temp.NAME)
2658
2659 // Exactly the same in friend_load, friend_save, and friend_size
2660 VALUE_MEMBER(status);
2661 ARRAY_MEMBER(real_pk);
2662 ARRAY_MEMBER(info);
2663 data++; // padding
2664 VALUE_MEMBER(info_size);
2665 ARRAY_MEMBER(name);
2666 data++; // padding
2667 VALUE_MEMBER(name_length);
2668 ARRAY_MEMBER(statusmessage);
2669 VALUE_MEMBER(statusmessage_length);
2670 VALUE_MEMBER(userstatus);
2671 data += 3; // padding
2672 VALUE_MEMBER(friendrequest_nospam);
2673 VALUE_MEMBER(last_seen_time);
2674
2675#undef VALUE_MEMBER
2676#undef ARRAY_MEMBER
2677
2678 return data;
2679}
2680
2650static uint32_t saved_friendslist_size(const Messenger *m) 2681static uint32_t saved_friendslist_size(const Messenger *m)
2651{ 2682{
2652 return count_friendlist(m) * sizeof(struct SAVED_FRIEND); 2683 return count_friendlist(m) * friend_size();
2684}
2685
2686static uint8_t *friend_save(const struct SAVED_FRIEND *temp, uint8_t *data)
2687{
2688#define VALUE_MEMBER(NAME) \
2689 memcpy(data, &temp->NAME, sizeof(temp->NAME)); \
2690 data += sizeof(temp->NAME)
2691
2692#define ARRAY_MEMBER(NAME) \
2693 memcpy(data, temp->NAME, sizeof(temp->NAME)); \
2694 data += sizeof(temp->NAME)
2695
2696 // Exactly the same in friend_load, friend_save, and friend_size
2697 VALUE_MEMBER(status);
2698 ARRAY_MEMBER(real_pk);
2699 ARRAY_MEMBER(info);
2700 data++; // padding
2701 VALUE_MEMBER(info_size);
2702 ARRAY_MEMBER(name);
2703 data++; // padding
2704 VALUE_MEMBER(name_length);
2705 ARRAY_MEMBER(statusmessage);
2706 VALUE_MEMBER(statusmessage_length);
2707 VALUE_MEMBER(userstatus);
2708 data += 3; // padding
2709 VALUE_MEMBER(friendrequest_nospam);
2710 VALUE_MEMBER(last_seen_time);
2711
2712#undef VALUE_MEMBER
2713#undef ARRAY_MEMBER
2714
2715 return data;
2653} 2716}
2654 2717
2655static uint32_t friends_list_save(const Messenger *m, uint8_t *data) 2718static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
2656{ 2719{
2657 uint32_t i; 2720 uint32_t i;
2658 uint32_t num = 0; 2721 uint32_t num = 0;
2722 uint8_t *cur_data = data;
2659 2723
2660 for (i = 0; i < m->numfriends; i++) { 2724 for (i = 0; i < m->numfriends; i++) {
2661 if (m->friendlist[i].status > 0) { 2725 if (m->friendlist[i].status > 0) {
2662 struct SAVED_FRIEND temp; 2726 struct SAVED_FRIEND temp = { 0 };
2663 memset(&temp, 0, sizeof(struct SAVED_FRIEND));
2664 temp.status = m->friendlist[i].status; 2727 temp.status = m->friendlist[i].status;
2665 memcpy(temp.real_pk, m->friendlist[i].real_pk, crypto_box_PUBLICKEYBYTES); 2728 memcpy(temp.real_pk, m->friendlist[i].real_pk, crypto_box_PUBLICKEYBYTES);
2666 2729
@@ -2686,26 +2749,72 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
2686 memcpy(&temp.last_seen_time, last_seen_time, sizeof(uint64_t)); 2749 memcpy(&temp.last_seen_time, last_seen_time, sizeof(uint64_t));
2687 } 2750 }
2688 2751
2689 memcpy(data + num * sizeof(struct SAVED_FRIEND), &temp, sizeof(struct SAVED_FRIEND)); 2752 uint8_t *next_data = friend_save(&temp, cur_data);
2753#ifdef TOX_DEBUG
2754 assert(next_data - cur_data == friend_size());
2755 assert(memcmp(cur_data, &temp, friend_size()) == 0);
2756#endif
2757 cur_data = next_data;
2690 num++; 2758 num++;
2691 } 2759 }
2692 } 2760 }
2693 2761
2694 return num * sizeof(struct SAVED_FRIEND); 2762#ifdef TOX_DEBUG
2763 assert(cur_data - data == num * friend_size());
2764#endif
2765 return cur_data - data;
2766}
2767
2768static const uint8_t *friend_load(struct SAVED_FRIEND *temp, const uint8_t *data)
2769{
2770#define VALUE_MEMBER(NAME) \
2771 memcpy(&temp->NAME, data, sizeof(temp->NAME)); \
2772 data += sizeof(temp->NAME)
2773
2774#define ARRAY_MEMBER(NAME) \
2775 memcpy(temp->NAME, data, sizeof(temp->NAME)); \
2776 data += sizeof(temp->NAME)
2777
2778 // Exactly the same in friend_load, friend_save, and friend_size
2779 VALUE_MEMBER(status);
2780 ARRAY_MEMBER(real_pk);
2781 ARRAY_MEMBER(info);
2782 data++; // padding
2783 VALUE_MEMBER(info_size);
2784 ARRAY_MEMBER(name);
2785 data++; // padding
2786 VALUE_MEMBER(name_length);
2787 ARRAY_MEMBER(statusmessage);
2788 VALUE_MEMBER(statusmessage_length);
2789 VALUE_MEMBER(userstatus);
2790 data += 3; // padding
2791 VALUE_MEMBER(friendrequest_nospam);
2792 VALUE_MEMBER(last_seen_time);
2793
2794#undef VALUE_MEMBER
2795#undef ARRAY_MEMBER
2796
2797 return data;
2695} 2798}
2696 2799
2697static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length) 2800static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length)
2698{ 2801{
2699 if (length % sizeof(struct SAVED_FRIEND) != 0) { 2802 if (length % friend_size() != 0) {
2700 return -1; 2803 return -1;
2701 } 2804 }
2702 2805
2703 uint32_t num = length / sizeof(struct SAVED_FRIEND); 2806 uint32_t num = length / friend_size();
2704 uint32_t i; 2807 uint32_t i;
2808 const uint8_t *cur_data = data;
2705 2809
2706 for (i = 0; i < num; ++i) { 2810 for (i = 0; i < num; ++i) {
2707 struct SAVED_FRIEND temp; 2811 struct SAVED_FRIEND temp = { 0 };
2708 memcpy(&temp, data + i * sizeof(struct SAVED_FRIEND), sizeof(struct SAVED_FRIEND)); 2812 const uint8_t *next_data = friend_load(&temp, cur_data);
2813#ifdef TOX_DEBUG
2814 assert(next_data - cur_data == friend_size());
2815 assert(memcmp(&temp, cur_data, friend_size()) == 0);
2816#endif
2817 cur_data = next_data;
2709 2818
2710 if (temp.status >= 3) { 2819 if (temp.status >= 3) {
2711 int fnum = m_addfriend_norequest(m, temp.real_pk); 2820 int fnum = m_addfriend_norequest(m, temp.real_pk);