diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 49 | ||||
-rw-r--r-- | toxcore/Messenger.h | 61 | ||||
-rw-r--r-- | toxcore/tox.c | 4 | ||||
-rw-r--r-- | toxcore/tox.h | 40 |
4 files changed, 86 insertions, 68 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 3d96f6ca..99b95f67 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -576,10 +576,10 @@ int m_set_userstatus(Messenger *m, uint8_t status) | |||
576 | 576 | ||
577 | int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t length) | 577 | int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t length) |
578 | { | 578 | { |
579 | if (length > MAX_AVATAR_DATA_LENGTH) | 579 | if (length > AVATAR_MAX_DATA_LENGTH) |
580 | return -1; | 580 | return -1; |
581 | 581 | ||
582 | if (format == AVATARFORMAT_NONE) { | 582 | if (format == AVATAR_FORMAT_NONE) { |
583 | free(m->avatar_data); | 583 | free(m->avatar_data); |
584 | m->avatar_data = NULL; | 584 | m->avatar_data = NULL; |
585 | m->avatar_data_length = 0; | 585 | m->avatar_data_length = 0; |
@@ -632,15 +632,18 @@ int m_get_self_avatar(const Messenger *m, uint8_t *format, uint8_t *buf, uint32_ | |||
632 | return 0; | 632 | return 0; |
633 | } | 633 | } |
634 | 634 | ||
635 | int m_avatar_hash(uint8_t *hash, const uint8_t *data, const uint32_t datalen) | 635 | int m_hash(uint8_t *hash, const uint8_t *data, const uint32_t datalen) |
636 | { | 636 | { |
637 | if (hash == NULL) | 637 | if (hash == NULL) |
638 | return -1; | 638 | return -1; |
639 | 639 | ||
640 | crypto_hash_sha256(hash, data, datalen); | 640 | return crypto_hash_sha256(hash, data, datalen); |
641 | return 0; | ||
642 | } | 641 | } |
643 | 642 | ||
643 | int m_avatar_hash(uint8_t *hash, const uint8_t *data, const uint32_t datalen) | ||
644 | { | ||
645 | return m_hash(hash, data, datalen); | ||
646 | } | ||
644 | 647 | ||
645 | int m_request_avatar_info(const Messenger *m, const int32_t friendnumber) | 648 | int m_request_avatar_info(const Messenger *m, const int32_t friendnumber) |
646 | { | 649 | { |
@@ -673,10 +676,10 @@ int m_request_avatar_data(const Messenger *m, const int32_t friendnumber) | |||
673 | if (friend_not_valid(m, friendnumber)) | 676 | if (friend_not_valid(m, friendnumber)) |
674 | return -1; | 677 | return -1; |
675 | 678 | ||
676 | AVATARRECEIVEDATA *avrd = m->friendlist[friendnumber].avatar_recv_data; | 679 | AVATAR_RECEIVEDATA *avrd = m->friendlist[friendnumber].avatar_recv_data; |
677 | 680 | ||
678 | if (avrd == NULL) { | 681 | if (avrd == NULL) { |
679 | avrd = calloc(sizeof(AVATARRECEIVEDATA), 1); | 682 | avrd = calloc(sizeof(AVATAR_RECEIVEDATA), 1); |
680 | 683 | ||
681 | if (avrd == NULL) | 684 | if (avrd == NULL) |
682 | return -1; | 685 | return -1; |
@@ -693,9 +696,9 @@ int m_request_avatar_data(const Messenger *m, const int32_t friendnumber) | |||
693 | avrd->started = 0; | 696 | avrd->started = 0; |
694 | avrd->bytes_received = 0; | 697 | avrd->bytes_received = 0; |
695 | avrd->total_length = 0; | 698 | avrd->total_length = 0; |
696 | avrd->format = AVATARFORMAT_NONE; | 699 | avrd->format = AVATAR_FORMAT_NONE; |
697 | 700 | ||
698 | return send_avatar_data_control(m, friendnumber, AVATARDATACONTROL_REQ); | 701 | return send_avatar_data_control(m, friendnumber, AVATAR_DATACONTROL_REQ); |
699 | } | 702 | } |
700 | 703 | ||
701 | 704 | ||
@@ -2006,7 +2009,7 @@ Messenger *new_messenger(Messenger_Options *options) | |||
2006 | m->net = new_networking(ip, TOX_PORT_DEFAULT); | 2009 | m->net = new_networking(ip, TOX_PORT_DEFAULT); |
2007 | } | 2010 | } |
2008 | 2011 | ||
2009 | m->avatar_format = AVATARFORMAT_NONE; | 2012 | m->avatar_format = AVATAR_FORMAT_NONE; |
2010 | m->avatar_data = NULL; | 2013 | m->avatar_data = NULL; |
2011 | 2014 | ||
2012 | if (m->net == NULL) { | 2015 | if (m->net == NULL) { |
@@ -2159,17 +2162,17 @@ static int handle_avatar_data_control(Messenger *m, uint32_t friendnumber, | |||
2159 | LOGGER_DEBUG("Error: PACKET_ID_AVATAR_DATA_CONTROL with bad " | 2162 | LOGGER_DEBUG("Error: PACKET_ID_AVATAR_DATA_CONTROL with bad " |
2160 | "data_length = %u, friendnumber = %u", | 2163 | "data_length = %u, friendnumber = %u", |
2161 | data_length, friendnumber); | 2164 | data_length, friendnumber); |
2162 | send_avatar_data_control(m, friendnumber, AVATARDATACONTROL_ERROR); | 2165 | send_avatar_data_control(m, friendnumber, AVATAR_DATACONTROL_ERROR); |
2163 | return -1; /* Error */ | 2166 | return -1; /* Error */ |
2164 | } | 2167 | } |
2165 | 2168 | ||
2166 | LOGGER_DEBUG("friendnumber = %u, op = %u", friendnumber, data[0]); | 2169 | LOGGER_DEBUG("friendnumber = %u, op = %u", friendnumber, data[0]); |
2167 | 2170 | ||
2168 | switch (data[0]) { | 2171 | switch (data[0]) { |
2169 | case AVATARDATACONTROL_REQ: { | 2172 | case AVATAR_DATACONTROL_REQ: { |
2170 | 2173 | ||
2171 | /* Check data transfer limits for this friend */ | 2174 | /* Check data transfer limits for this friend */ |
2172 | AVATARSENDDATA *const avsd = &(m->friendlist[friendnumber].avatar_send_data); | 2175 | AVATAR_SENDDATA *const avsd = &(m->friendlist[friendnumber].avatar_send_data); |
2173 | 2176 | ||
2174 | if (avsd->bytes_sent >= AVATAR_DATA_TRANSFER_LIMIT) { | 2177 | if (avsd->bytes_sent >= AVATAR_DATA_TRANSFER_LIMIT) { |
2175 | /* User reached data limit. Check timeout */ | 2178 | /* User reached data limit. Check timeout */ |
@@ -2183,7 +2186,7 @@ static int handle_avatar_data_control(Messenger *m, uint32_t friendnumber, | |||
2183 | /* Friend still rate-limitted. Send an error and stops. */ | 2186 | /* Friend still rate-limitted. Send an error and stops. */ |
2184 | LOGGER_DEBUG("Avatar data transfer limit reached. " | 2187 | LOGGER_DEBUG("Avatar data transfer limit reached. " |
2185 | "friendnumber = %u", friendnumber); | 2188 | "friendnumber = %u", friendnumber); |
2186 | send_avatar_data_control(m, friendnumber, AVATARDATACONTROL_ERROR); | 2189 | send_avatar_data_control(m, friendnumber, AVATAR_DATACONTROL_ERROR); |
2187 | return 0; | 2190 | return 0; |
2188 | } | 2191 | } |
2189 | } | 2192 | } |
@@ -2211,12 +2214,12 @@ static int handle_avatar_data_control(Messenger *m, uint32_t friendnumber, | |||
2211 | if (!ret) { | 2214 | if (!ret) { |
2212 | /* Something went wrong, try to signal the error so the friend | 2215 | /* Something went wrong, try to signal the error so the friend |
2213 | * can clear up the state. */ | 2216 | * can clear up the state. */ |
2214 | send_avatar_data_control(m, friendnumber, AVATARDATACONTROL_ERROR); | 2217 | send_avatar_data_control(m, friendnumber, AVATAR_DATACONTROL_ERROR); |
2215 | return 0; | 2218 | return 0; |
2216 | } | 2219 | } |
2217 | 2220 | ||
2218 | /* User have no avatar data, nothing more to do. */ | 2221 | /* User have no avatar data, nothing more to do. */ |
2219 | if (m->avatar_format == AVATARFORMAT_NONE) | 2222 | if (m->avatar_format == AVATAR_FORMAT_NONE) |
2220 | return 0; | 2223 | return 0; |
2221 | 2224 | ||
2222 | /* Send the actual avatar data. */ | 2225 | /* Send the actual avatar data. */ |
@@ -2241,7 +2244,7 @@ static int handle_avatar_data_control(Messenger *m, uint32_t friendnumber, | |||
2241 | LOGGER_DEBUG("write_cryptpacket_id failed. ret = %d, " | 2244 | LOGGER_DEBUG("write_cryptpacket_id failed. ret = %d, " |
2242 | "friendnumber = %u, offset = %u", | 2245 | "friendnumber = %u, offset = %u", |
2243 | ret, friendnumber, offset); | 2246 | ret, friendnumber, offset); |
2244 | send_avatar_data_control(m, friendnumber, AVATARDATACONTROL_ERROR); | 2247 | send_avatar_data_control(m, friendnumber, AVATAR_DATACONTROL_ERROR); |
2245 | return -1; | 2248 | return -1; |
2246 | } | 2249 | } |
2247 | } | 2250 | } |
@@ -2249,7 +2252,7 @@ static int handle_avatar_data_control(Messenger *m, uint32_t friendnumber, | |||
2249 | return 0; | 2252 | return 0; |
2250 | } | 2253 | } |
2251 | 2254 | ||
2252 | case AVATARDATACONTROL_ERROR: { | 2255 | case AVATAR_DATACONTROL_ERROR: { |
2253 | if (m->friendlist[friendnumber].avatar_recv_data) { | 2256 | if (m->friendlist[friendnumber].avatar_recv_data) { |
2254 | /* We were receiving the data, sender detected an error | 2257 | /* We were receiving the data, sender detected an error |
2255 | (eg. changing avatar) and asked us to stop. */ | 2258 | (eg. changing avatar) and asked us to stop. */ |
@@ -2276,7 +2279,7 @@ static int handle_avatar_data_start(Messenger *m, uint32_t friendnumber, | |||
2276 | return -1; | 2279 | return -1; |
2277 | } | 2280 | } |
2278 | 2281 | ||
2279 | AVATARRECEIVEDATA *avrd = m->friendlist[friendnumber].avatar_recv_data; | 2282 | AVATAR_RECEIVEDATA *avrd = m->friendlist[friendnumber].avatar_recv_data; |
2280 | 2283 | ||
2281 | if (avrd == NULL) { | 2284 | if (avrd == NULL) { |
2282 | LOGGER_DEBUG("Received an unrequested DATA_START, friendnumber = %u", | 2285 | LOGGER_DEBUG("Received an unrequested DATA_START, friendnumber = %u", |
@@ -2307,7 +2310,7 @@ static int handle_avatar_data_start(Messenger *m, uint32_t friendnumber, | |||
2307 | friendnumber, avrd->format, avrd->total_length, | 2310 | friendnumber, avrd->format, avrd->total_length, |
2308 | avrd->bytes_received); | 2311 | avrd->bytes_received); |
2309 | 2312 | ||
2310 | if (avrd->total_length > MAX_AVATAR_DATA_LENGTH) { | 2313 | if (avrd->total_length > AVATAR_MAX_DATA_LENGTH) { |
2311 | /* Invalid data length. Stops. */ | 2314 | /* Invalid data length. Stops. */ |
2312 | LOGGER_DEBUG("Error: total_length > MAX_AVATAR_DATA_LENGTH, " | 2315 | LOGGER_DEBUG("Error: total_length > MAX_AVATAR_DATA_LENGTH, " |
2313 | "friendnumber = %u", friendnumber); | 2316 | "friendnumber = %u", friendnumber); |
@@ -2317,7 +2320,7 @@ static int handle_avatar_data_start(Messenger *m, uint32_t friendnumber, | |||
2317 | return 0; | 2320 | return 0; |
2318 | } | 2321 | } |
2319 | 2322 | ||
2320 | if (avrd->format == AVATARFORMAT_NONE || avrd->total_length == 0) { | 2323 | if (avrd->format == AVATAR_FORMAT_NONE || avrd->total_length == 0) { |
2321 | /* No real data to receive. Run callback function and finish. */ | 2324 | /* No real data to receive. Run callback function and finish. */ |
2322 | LOGGER_DEBUG("format == NONE, friendnumber = %u", friendnumber); | 2325 | LOGGER_DEBUG("format == NONE, friendnumber = %u", friendnumber); |
2323 | 2326 | ||
@@ -2343,7 +2346,7 @@ static int handle_avatar_data_push(Messenger *m, uint32_t friendnumber, | |||
2343 | { | 2346 | { |
2344 | LOGGER_DEBUG("friendnumber = %u, data_length = %u", friendnumber, data_length); | 2347 | LOGGER_DEBUG("friendnumber = %u, data_length = %u", friendnumber, data_length); |
2345 | 2348 | ||
2346 | AVATARRECEIVEDATA *avrd = m->friendlist[friendnumber].avatar_recv_data; | 2349 | AVATAR_RECEIVEDATA *avrd = m->friendlist[friendnumber].avatar_recv_data; |
2347 | 2350 | ||
2348 | if (avrd == NULL) { | 2351 | if (avrd == NULL) { |
2349 | /* No active transfer. It must be an error or a malicious request, | 2352 | /* No active transfer. It must be an error or a malicious request, |
@@ -2363,7 +2366,7 @@ static int handle_avatar_data_push(Messenger *m, uint32_t friendnumber, | |||
2363 | uint32_t new_length = avrd->bytes_received + data_length; | 2366 | uint32_t new_length = avrd->bytes_received + data_length; |
2364 | 2367 | ||
2365 | if (new_length > avrd->total_length | 2368 | if (new_length > avrd->total_length |
2366 | || new_length >= MAX_AVATAR_DATA_LENGTH) { | 2369 | || new_length >= AVATAR_MAX_DATA_LENGTH) { |
2367 | /* Invalid data length due to error or malice. Stops. */ | 2370 | /* Invalid data length due to error or malice. Stops. */ |
2368 | LOGGER_DEBUG("Invalid data length. friendnumber = %u, " | 2371 | LOGGER_DEBUG("Invalid data length. friendnumber = %u, " |
2369 | "new_length = %u, avrd->total_length = %u", | 2372 | "new_length = %u, avrd->total_length = %u", |
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index a107d80f..e6877002 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -36,8 +36,8 @@ | |||
36 | #define MAX_NAME_LENGTH 128 | 36 | #define MAX_NAME_LENGTH 128 |
37 | /* TODO: this must depend on other variable. */ | 37 | /* TODO: this must depend on other variable. */ |
38 | #define MAX_STATUSMESSAGE_LENGTH 1007 | 38 | #define MAX_STATUSMESSAGE_LENGTH 1007 |
39 | #define MAX_AVATAR_DATA_LENGTH 16384 | 39 | #define AVATAR_MAX_DATA_LENGTH 16384 |
40 | #define AVATAR_HASH_LENGTH 32 | 40 | #define AVATAR_HASH_LENGTH crypto_hash_sha256_BYTES |
41 | 41 | ||
42 | 42 | ||
43 | #define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) | 43 | #define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) |
@@ -121,7 +121,7 @@ enum { | |||
121 | #define AVATAR_DATA_MAX_CHUNK_SIZE (MAX_CRYPTO_DATA_SIZE-1) | 121 | #define AVATAR_DATA_MAX_CHUNK_SIZE (MAX_CRYPTO_DATA_SIZE-1) |
122 | 122 | ||
123 | /* Per-friend data limit for avatar data requests */ | 123 | /* Per-friend data limit for avatar data requests */ |
124 | #define AVATAR_DATA_TRANSFER_LIMIT (10*MAX_AVATAR_DATA_LENGTH) | 124 | #define AVATAR_DATA_TRANSFER_LIMIT (10*AVATAR_MAX_DATA_LENGTH) |
125 | #define AVATAR_DATA_TRANSFER_TIMEOUT (60) /* 164kB every 60 seconds is not a lot */ | 125 | #define AVATAR_DATA_TRANSFER_TIMEOUT (60) /* 164kB every 60 seconds is not a lot */ |
126 | 126 | ||
127 | 127 | ||
@@ -137,40 +137,40 @@ typedef enum { | |||
137 | } | 137 | } |
138 | USERSTATUS; | 138 | USERSTATUS; |
139 | 139 | ||
140 | /* AVATARFORMAT - | 140 | /* AVATAR_FORMAT - |
141 | * Data formats for user avatar images | 141 | * Data formats for user avatar images |
142 | */ | 142 | */ |
143 | typedef enum { | 143 | typedef enum { |
144 | AVATARFORMAT_NONE, | 144 | AVATAR_FORMAT_NONE = 0, |
145 | AVATARFORMAT_PNG | 145 | AVATAR_FORMAT_PNG |
146 | } | 146 | } |
147 | AVATARFORMAT; | 147 | AVATAR_FORMAT; |
148 | 148 | ||
149 | /* AVATARDATACONTROL | 149 | /* AVATAR_DATACONTROL |
150 | * To control avatar data requests (PACKET_ID_AVATAR_DATA_CONTROL) | 150 | * To control avatar data requests (PACKET_ID_AVATAR_DATA_CONTROL) |
151 | */ | 151 | */ |
152 | typedef enum { | 152 | typedef enum { |
153 | AVATARDATACONTROL_REQ, | 153 | AVATAR_DATACONTROL_REQ, |
154 | AVATARDATACONTROL_ERROR | 154 | AVATAR_DATACONTROL_ERROR |
155 | } | 155 | } |
156 | AVATARDATACONTROL; | 156 | AVATAR_DATACONTROL; |
157 | 157 | ||
158 | typedef struct { | 158 | typedef struct { |
159 | uint8_t started; | 159 | uint8_t started; |
160 | AVATARFORMAT format; | 160 | AVATAR_FORMAT format; |
161 | uint8_t hash[AVATAR_HASH_LENGTH]; | 161 | uint8_t hash[AVATAR_HASH_LENGTH]; |
162 | uint32_t total_length; | 162 | uint32_t total_length; |
163 | uint32_t bytes_received; | 163 | uint32_t bytes_received; |
164 | uint8_t data[MAX_AVATAR_DATA_LENGTH]; | 164 | uint8_t data[AVATAR_MAX_DATA_LENGTH]; |
165 | } | 165 | } |
166 | AVATARRECEIVEDATA; | 166 | AVATAR_RECEIVEDATA; |
167 | 167 | ||
168 | typedef struct { | 168 | typedef struct { |
169 | /* Fields only used to limit the network usage from a given friend */ | 169 | /* Fields only used to limit the network usage from a given friend */ |
170 | uint32_t bytes_sent; /* Total bytes send to this user */ | 170 | uint32_t bytes_sent; /* Total bytes send to this user */ |
171 | uint64_t last_reset; /* Time the data counter was last reset */ | 171 | uint64_t last_reset; /* Time the data counter was last reset */ |
172 | } | 172 | } |
173 | AVATARSENDDATA; | 173 | AVATAR_SENDDATA; |
174 | 174 | ||
175 | 175 | ||
176 | struct File_Transfers { | 176 | struct File_Transfers { |
@@ -230,8 +230,8 @@ typedef struct { | |||
230 | int invited_groups[MAX_INVITED_GROUPS]; | 230 | int invited_groups[MAX_INVITED_GROUPS]; |
231 | uint16_t invited_groups_num; | 231 | uint16_t invited_groups_num; |
232 | 232 | ||
233 | AVATARSENDDATA avatar_send_data; | 233 | AVATAR_SENDDATA avatar_send_data; |
234 | AVATARRECEIVEDATA *avatar_recv_data; // We are receiving avatar data from this friend. | 234 | AVATAR_RECEIVEDATA *avatar_recv_data; // We are receiving avatar data from this friend. |
235 | 235 | ||
236 | struct { | 236 | struct { |
237 | int (*function)(void *object, const uint8_t *data, uint32_t len); | 237 | int (*function)(void *object, const uint8_t *data, uint32_t len); |
@@ -264,7 +264,7 @@ typedef struct Messenger { | |||
264 | 264 | ||
265 | USERSTATUS userstatus; | 265 | USERSTATUS userstatus; |
266 | 266 | ||
267 | AVATARFORMAT avatar_format; | 267 | AVATAR_FORMAT avatar_format; |
268 | uint8_t *avatar_data; | 268 | uint8_t *avatar_data; |
269 | uint32_t avatar_data_length; | 269 | uint32_t avatar_data_length; |
270 | uint8_t avatar_hash[AVATAR_HASH_LENGTH]; | 270 | uint8_t avatar_hash[AVATAR_HASH_LENGTH]; |
@@ -509,7 +509,7 @@ uint8_t m_get_self_userstatus(const Messenger *m); | |||
509 | * Notice that the library treats the image as raw data and does not interpret it by any way. | 509 | * Notice that the library treats the image as raw data and does not interpret it by any way. |
510 | * | 510 | * |
511 | * Arguments: | 511 | * Arguments: |
512 | * format - Avatar image format or NONE for user with no avatar (see AVATARFORMAT); | 512 | * format - Avatar image format or NONE for user with no avatar (see AVATAR_FORMAT); |
513 | * data - pointer to the avatar data (may be NULL it the format is NONE); | 513 | * data - pointer to the avatar data (may be NULL it the format is NONE); |
514 | * length - length of image data. Must be <= MAX_AVATAR_DATA_LENGTH. | 514 | * length - length of image data. Must be <= MAX_AVATAR_DATA_LENGTH. |
515 | * | 515 | * |
@@ -528,7 +528,7 @@ int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t len | |||
528 | * If any of the pointers format, buf, length, and hash are NULL, that particular field will be ignored. | 528 | * If any of the pointers format, buf, length, and hash are NULL, that particular field will be ignored. |
529 | * | 529 | * |
530 | * Arguments: | 530 | * Arguments: |
531 | * format - destination pointer to the avatar image format (see AVATARFORMAT); | 531 | * format - destination pointer to the avatar image format (see AVATAR_FORMAT); |
532 | * buf - destination buffer to the image data. Must have at least 'maxlen' bytes; | 532 | * buf - destination buffer to the image data. Must have at least 'maxlen' bytes; |
533 | * length - destination pointer to the image data length; | 533 | * length - destination pointer to the image data length; |
534 | * maxlen - length of the destination buffer 'buf'; | 534 | * maxlen - length of the destination buffer 'buf'; |
@@ -541,8 +541,23 @@ int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t len | |||
541 | int m_get_self_avatar(const Messenger *m, uint8_t *format, uint8_t *buf, uint32_t *length, uint32_t maxlen, | 541 | int m_get_self_avatar(const Messenger *m, uint8_t *format, uint8_t *buf, uint32_t *length, uint32_t maxlen, |
542 | uint8_t *hash); | 542 | uint8_t *hash); |
543 | 543 | ||
544 | /* Generates a cryptographic hash of the given data. | ||
545 | * This function may be used by clients for any purpose, but is provided primarily for | ||
546 | * validating cached avatars. | ||
547 | * This function is a wrapper to internal message-digest functions. | ||
548 | * | ||
549 | * Arguments: | ||
550 | * hash - destination buffer for the hash data, it must be exactly crypto_hash_sha256_BYTES bytes long. | ||
551 | * data - data to be hashed; | ||
552 | * datalen - length of the data; | ||
553 | * | ||
554 | * returns 0 on success | ||
555 | * returns -1 on failure. | ||
556 | */ | ||
557 | int m_hash(uint8_t *hash, const uint8_t *data, const uint32_t datalen); | ||
558 | |||
544 | /* Generates a cryptographic hash of the given avatar data. | 559 | /* Generates a cryptographic hash of the given avatar data. |
545 | * This function is a wrapper to internal message-digest functions and specifically provided | 560 | * This function is a wrapper to m_hash and specifically provided |
546 | * to generate hashes from user avatars that may be memcmp()ed with the values returned by the | 561 | * to generate hashes from user avatars that may be memcmp()ed with the values returned by the |
547 | * other avatar functions. It is specially important to validate cached avatars. | 562 | * other avatar functions. It is specially important to validate cached avatars. |
548 | * | 563 | * |
@@ -694,7 +709,7 @@ void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Mess | |||
694 | * Function format is: | 709 | * Function format is: |
695 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, void *userdata) | 710 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, void *userdata) |
696 | * | 711 | * |
697 | * where 'format' is the avatar image format (see AVATARFORMAT) and 'hash' is the hash of | 712 | * where 'format' is the avatar image format (see AVATAR_FORMAT) and 'hash' is the hash of |
698 | * the avatar data for caching purposes and it is exactly AVATAR_HASH_LENGTH long. If the | 713 | * the avatar data for caching purposes and it is exactly AVATAR_HASH_LENGTH long. If the |
699 | * image format is NONE, the hash is zeroed. | 714 | * image format is NONE, the hash is zeroed. |
700 | * | 715 | * |
@@ -710,7 +725,7 @@ void m_callback_avatar_info(Messenger *m, void (*function)(Messenger *m, int32_t | |||
710 | * Function format is: | 725 | * Function format is: |
711 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, uint8_t *data, uint32_t datalen, void *userdata) | 726 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, uint8_t *data, uint32_t datalen, void *userdata) |
712 | * | 727 | * |
713 | * where 'format' is the avatar image format (see AVATARFORMAT); 'hash' is the | 728 | * where 'format' is the avatar image format (see AVATAR_FORMAT); 'hash' is the |
714 | * locally-calculated cryptographic hash of the avatar data and it is exactly | 729 | * locally-calculated cryptographic hash of the avatar data and it is exactly |
715 | * AVATAR_HASH_LENGTH long; 'data' is the avatar image data and 'datalen' is the length | 730 | * AVATAR_HASH_LENGTH long; 'data' is the avatar image data and 'datalen' is the length |
716 | * of such data. | 731 | * of such data. |
diff --git a/toxcore/tox.c b/toxcore/tox.c index b44f0ee6..21854de4 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -826,9 +826,9 @@ int tox_get_self_avatar(const Tox *tox, uint8_t *format, uint8_t *buf, uint32_t | |||
826 | return m_get_self_avatar(m, format, buf, length, maxlen, hash); | 826 | return m_get_self_avatar(m, format, buf, length, maxlen, hash); |
827 | } | 827 | } |
828 | 828 | ||
829 | int tox_avatar_hash(const Tox *tox, uint8_t *hash, const uint8_t *data, const uint32_t datalen) | 829 | int tox_hash(const Tox *tox, uint8_t *hash, const uint8_t *data, const uint32_t datalen) |
830 | { | 830 | { |
831 | return m_avatar_hash(hash, data, datalen); | 831 | return m_hash(hash, data, datalen); |
832 | } | 832 | } |
833 | 833 | ||
834 | int tox_request_avatar_info(const Tox *tox, const int32_t friendnumber) | 834 | int tox_request_avatar_info(const Tox *tox, const int32_t friendnumber) |
diff --git a/toxcore/tox.h b/toxcore/tox.h index a5efee34..d285e3fc 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -37,8 +37,8 @@ extern "C" { | |||
37 | #define TOX_MAX_MESSAGE_LENGTH 1368 | 37 | #define TOX_MAX_MESSAGE_LENGTH 1368 |
38 | #define TOX_MAX_STATUSMESSAGE_LENGTH 1007 | 38 | #define TOX_MAX_STATUSMESSAGE_LENGTH 1007 |
39 | #define TOX_CLIENT_ID_SIZE 32 | 39 | #define TOX_CLIENT_ID_SIZE 32 |
40 | #define TOX_MAX_AVATAR_DATA_LENGTH 16384 | 40 | #define TOX_AVATAR_MAX_DATA_LENGTH 16384 |
41 | #define TOX_AVATAR_HASH_LENGTH 32 | 41 | #define TOX_HASH_LENGTH /*crypto_hash_sha256_BYTES*/ 32 |
42 | 42 | ||
43 | #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) | 43 | #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) |
44 | 44 | ||
@@ -73,14 +73,14 @@ typedef enum { | |||
73 | TOX_USERSTATUS; | 73 | TOX_USERSTATUS; |
74 | 74 | ||
75 | 75 | ||
76 | /* AVATARFORMAT - | 76 | /* AVATAR_FORMAT - |
77 | * Data formats for user avatar images | 77 | * Data formats for user avatar images |
78 | */ | 78 | */ |
79 | typedef enum { | 79 | typedef enum { |
80 | TOX_AVATARFORMAT_NONE, | 80 | TOX_AVATAR_FORMAT_NONE = 0, |
81 | TOX_AVATARFORMAT_PNG | 81 | TOX_AVATAR_FORMAT_PNG |
82 | } | 82 | } |
83 | TOX_AVATARFORMAT; | 83 | TOX_AVATAR_FORMAT; |
84 | 84 | ||
85 | #ifndef __TOX_DEFINED__ | 85 | #ifndef __TOX_DEFINED__ |
86 | #define __TOX_DEFINED__ | 86 | #define __TOX_DEFINED__ |
@@ -538,7 +538,7 @@ uint32_t tox_get_chatlist(const Tox *tox, int *out_list, uint32_t list_size); | |||
538 | * Function format is: | 538 | * Function format is: |
539 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, void *userdata) | 539 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, void *userdata) |
540 | * | 540 | * |
541 | * where 'format' is the avatar image format (see TOX_AVATARFORMAT) and 'hash' is the hash of | 541 | * where 'format' is the avatar image format (see TOX_AVATAR_FORMAT) and 'hash' is the hash of |
542 | * the avatar data for caching purposes and it is exactly TOX_AVATAR_HASH_LENGTH long. If the | 542 | * the avatar data for caching purposes and it is exactly TOX_AVATAR_HASH_LENGTH long. If the |
543 | * image format is NONE, the hash is zeroed. | 543 | * image format is NONE, the hash is zeroed. |
544 | * | 544 | * |
@@ -554,7 +554,7 @@ void tox_callback_avatar_info(Tox *tox, void (*function)(Tox *tox, int32_t, uint | |||
554 | * Function format is: | 554 | * Function format is: |
555 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, uint8_t *data, uint32_t datalen, void *userdata) | 555 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, uint8_t *data, uint32_t datalen, void *userdata) |
556 | * | 556 | * |
557 | * where 'format' is the avatar image format (see TOX_AVATARFORMAT); 'hash' is the | 557 | * where 'format' is the avatar image format (see TOX_AVATAR_FORMAT); 'hash' is the |
558 | * locally-calculated cryptographic hash of the avatar data and it is exactly | 558 | * locally-calculated cryptographic hash of the avatar data and it is exactly |
559 | * TOX_AVATAR_HASH_LENGTH long; 'data' is the avatar image data and 'datalen' is the length | 559 | * TOX_AVATAR_HASH_LENGTH long; 'data' is the avatar image data and 'datalen' is the length |
560 | * of such data. | 560 | * of such data. |
@@ -577,9 +577,9 @@ void tox_callback_avatar_data(Tox *tox, void (*function)(Tox *tox, int32_t, uint | |||
577 | * Notice that the library treats the image as raw data and does not interpret it by any way. | 577 | * Notice that the library treats the image as raw data and does not interpret it by any way. |
578 | * | 578 | * |
579 | * Arguments: | 579 | * Arguments: |
580 | * format - Avatar image format or NONE for user with no avatar (see TOX_AVATARFORMAT); | 580 | * format - Avatar image format or NONE for user with no avatar (see TOX_AVATAR_FORMAT); |
581 | * data - pointer to the avatar data (may be NULL it the format is NONE); | 581 | * data - pointer to the avatar data (may be NULL it the format is NONE); |
582 | * length - length of image data. Must be <= TOX_MAX_AVATAR_DATA_LENGTH. | 582 | * length - length of image data. Must be <= TOX_AVATAR_MAX_DATA_LENGTH. |
583 | * | 583 | * |
584 | * returns 0 on success | 584 | * returns 0 on success |
585 | * returns -1 on failure. | 585 | * returns -1 on failure. |
@@ -597,7 +597,7 @@ int tox_set_avatar(Tox *tox, uint8_t format, const uint8_t *data, uint32_t lengt | |||
597 | * If any of the pointers format, buf, length, and hash are NULL, that particular field will be ignored. | 597 | * If any of the pointers format, buf, length, and hash are NULL, that particular field will be ignored. |
598 | * | 598 | * |
599 | * Arguments: | 599 | * Arguments: |
600 | * format - destination pointer to the avatar image format (see TOX_AVATARFORMAT); | 600 | * format - destination pointer to the avatar image format (see TOX_AVATAR_FORMAT); |
601 | * buf - destination buffer to the image data. Must have at least 'maxlen' bytes; | 601 | * buf - destination buffer to the image data. Must have at least 'maxlen' bytes; |
602 | * length - destination pointer to the image data length; | 602 | * length - destination pointer to the image data length; |
603 | * maxlen - length of the destination buffer 'buf'; | 603 | * maxlen - length of the destination buffer 'buf'; |
@@ -611,21 +611,21 @@ int tox_get_self_avatar(const Tox *tox, uint8_t *format, uint8_t *buf, uint32_t | |||
611 | uint8_t *hash); | 611 | uint8_t *hash); |
612 | 612 | ||
613 | 613 | ||
614 | /* Generates a cryptographic hash of the given avatar data. | 614 | /* Generates a cryptographic hash of the given data. |
615 | * This function is a wrapper to internal message-digest functions and specifically provided | 615 | * This function may be used by clients for any purpose, but is provided primarily for |
616 | * to generate hashes from user avatars that may be memcmp()ed with the values returned by the | 616 | * validating cached avatars. This use is highly recommended to avoid unnecessary avatar |
617 | * other avatar functions. It is specially important to validate cached avatars. | 617 | * updates. |
618 | * This function is a wrapper to internal message-digest functions. | ||
618 | * | 619 | * |
619 | * Arguments: | 620 | * Arguments: |
620 | * hash - destination buffer for the hash data, it must be exactly TOX_AVATAR_HASH_LENGTH bytes long. | 621 | * hash - destination buffer for the hash data, it must be exactly TOX_HASH_LENGTH bytes long. |
621 | * data - avatar image data; | 622 | * data - data to be hashed; |
622 | * datalen - length of the avatar image data; it must be <= TOX_MAX_AVATAR_DATA_LENGTH. | 623 | * datalen - length of the data; for avatars, should be TOX_AVATAR_MAX_DATA_LENGTH |
623 | * | 624 | * |
624 | * returns 0 on success | 625 | * returns 0 on success |
625 | * returns -1 on failure. | 626 | * returns -1 on failure. |
626 | */ | 627 | */ |
627 | int tox_avatar_hash(const Tox *tox, uint8_t *hash, const uint8_t *data, const uint32_t datalen); | 628 | int tox_hash(const Tox *tox, uint8_t *hash, const uint8_t *data, const uint32_t datalen); |
628 | |||
629 | 629 | ||
630 | /* Request avatar information from a friend. | 630 | /* Request avatar information from a friend. |
631 | * Asks a friend to provide their avatar information (image format and hash). The friend may | 631 | * Asks a friend to provide their avatar information (image format and hash). The friend may |