diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-17 05:51:16 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-17 05:51:16 -0700 |
commit | 35376d85aaf04a80ecca6b313c323ee82e08f3df (patch) | |
tree | 6f3d2a51ffcf53078b3140cf5d0ed6e2942a6a43 /core | |
parent | f2c336e4f553f42262fd006b818ca965eeec789f (diff) | |
parent | d1d24c123d67e2568950bea6f1486e0c066eff8d (diff) |
Merge pull request #480 from rose-m/master
Messenger stores / loads nickname
Diffstat (limited to 'core')
-rw-r--r-- | core/Messenger.c | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index d88fe51a..04e4626a 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -850,8 +850,15 @@ void doMessenger(Messenger *m) | |||
850 | /* returns the size of the messenger data (for saving) */ | 850 | /* returns the size of the messenger data (for saving) */ |
851 | uint32_t Messenger_size(Messenger *m) | 851 | uint32_t Messenger_size(Messenger *m) |
852 | { | 852 | { |
853 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) | 853 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |
854 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * m->numfriends; | 854 | + sizeof(uint32_t) // nospam |
855 | + sizeof(uint32_t) // DHT size | ||
856 | + DHT_size() // DHT itself | ||
857 | + sizeof(uint32_t) // Friendlist size | ||
858 | + sizeof(Friend) * m->numfriends // Friendlist itself | ||
859 | + sizeof(uint16_t) // Own nickname length | ||
860 | + m->name_length // Own nickname | ||
861 | ; | ||
855 | } | 862 | } |
856 | 863 | ||
857 | /* save the messenger in data of size Messenger_size() */ | 864 | /* save the messenger in data of size Messenger_size() */ |
@@ -871,6 +878,11 @@ void Messenger_save(Messenger *m, uint8_t *data) | |||
871 | memcpy(data, &size, sizeof(size)); | 878 | memcpy(data, &size, sizeof(size)); |
872 | data += sizeof(size); | 879 | data += sizeof(size); |
873 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); | 880 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); |
881 | data += size; | ||
882 | uint16_t small_size = m->name_length; | ||
883 | memcpy(data, &small_size, sizeof(small_size)); | ||
884 | data += sizeof(small_size); | ||
885 | memcpy(data, m->name, small_size); | ||
874 | } | 886 | } |
875 | 887 | ||
876 | /* load the messenger from data of size length. */ | 888 | /* load the messenger from data of size length. */ |
@@ -905,7 +917,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
905 | memcpy(&size, data, sizeof(size)); | 917 | memcpy(&size, data, sizeof(size)); |
906 | data += sizeof(size); | 918 | data += sizeof(size); |
907 | 919 | ||
908 | if (length != size || length % sizeof(Friend) != 0) | 920 | if (length < size || size % sizeof(Friend) != 0) |
909 | return -1; | 921 | return -1; |
910 | 922 | ||
911 | Friend *temp = malloc(size); | 923 | Friend *temp = malloc(size); |
@@ -931,6 +943,23 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
931 | } | 943 | } |
932 | } | 944 | } |
933 | 945 | ||
946 | data += size; | ||
947 | length -= size; | ||
948 | |||
949 | uint16_t small_size; | ||
950 | |||
951 | if (length < sizeof(small_size)) | ||
952 | return -1; | ||
953 | |||
954 | memcpy(&small_size, data, sizeof(small_size)); | ||
955 | data += sizeof(small_size); | ||
956 | length -= sizeof(small_size); | ||
957 | |||
958 | if (length != small_size) | ||
959 | return -1; | ||
960 | |||
961 | setname(m, data, small_size); | ||
962 | |||
934 | free(temp); | 963 | free(temp); |
935 | return 0; | 964 | return 0; |
936 | } | 965 | } |