diff options
author | irungentoo <irungentoo@gmail.com> | 2014-02-23 10:23:55 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-02-23 10:23:55 -0500 |
commit | f3becc62e84bb7c29b5d366523abdab6eeee8a31 (patch) | |
tree | ba9067dfdfd92d8fc158412aa57c4776234337b7 /toxcore | |
parent | 50d2b45541923ecd6c911a2acb6288c7632cc8e7 (diff) |
Strings now no longer need to be NULL terminated.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 52 | ||||
-rw-r--r-- | toxcore/friend_requests.c | 6 | ||||
-rw-r--r-- | toxcore/group_chats.c | 4 | ||||
-rw-r--r-- | toxcore/tox.h | 12 |
4 files changed, 46 insertions, 28 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index e3dcf2a1..da326b88 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -690,7 +690,7 @@ static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t | |||
690 | if (friend_not_valid(m, friendnumber)) | 690 | if (friend_not_valid(m, friendnumber)) |
691 | return -1; | 691 | return -1; |
692 | 692 | ||
693 | uint8_t *newstatus = calloc(length, 1); | 693 | uint8_t *newstatus = calloc(length + 1, 1); |
694 | memcpy(newstatus, status, length); | 694 | memcpy(newstatus, status, length); |
695 | free(m->friendlist[friendnumber].statusmessage); | 695 | free(m->friendlist[friendnumber].statusmessage); |
696 | m->friendlist[friendnumber].statusmessage = newstatus; | 696 | m->friendlist[friendnumber].statusmessage = newstatus; |
@@ -957,10 +957,12 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m | |||
957 | if (i == -1) | 957 | if (i == -1) |
958 | return; | 958 | return; |
959 | 959 | ||
960 | message[length - 1] = 0; /* Force NULL terminator */ | 960 | uint8_t message_terminated[length + 1]; |
961 | memcpy(message_terminated, message, length); | ||
962 | message_terminated[length] = 0; /* Force NULL terminator */ | ||
961 | 963 | ||
962 | if (m->group_message) | 964 | if (m->group_message) |
963 | (*m->group_message)(m, i, peer_number, message, length, m->group_message_userdata); | 965 | (*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata); |
964 | } | 966 | } |
965 | 967 | ||
966 | static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata) | 968 | static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata) |
@@ -971,10 +973,12 @@ static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *ac | |||
971 | if (i == -1) | 973 | if (i == -1) |
972 | return; | 974 | return; |
973 | 975 | ||
974 | action[length - 1] = 0; /* Force NULL terminator */ | 976 | uint8_t action_terminated[length + 1]; |
977 | memcpy(action_terminated, action, length); | ||
978 | action_terminated[length] = 0; /* Force NULL terminator */ | ||
975 | 979 | ||
976 | if (m->group_action) | 980 | if (m->group_action) |
977 | (*m->group_action)(m, i, peer_number, action, length, m->group_action_userdata); | 981 | (*m->group_action)(m, i, peer_number, action_terminated, length, m->group_action_userdata); |
978 | } | 982 | } |
979 | 983 | ||
980 | static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata) | 984 | static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata) |
@@ -1912,13 +1916,15 @@ void do_friends(Messenger *m) | |||
1912 | break; | 1916 | break; |
1913 | 1917 | ||
1914 | /* Make sure the NULL terminator is present. */ | 1918 | /* Make sure the NULL terminator is present. */ |
1915 | data[data_length - 1] = 0; | 1919 | uint8_t data_terminated[data_length + 1]; |
1920 | memcpy(data_terminated, data, data_length); | ||
1921 | data_terminated[data_length] = 0; | ||
1916 | 1922 | ||
1917 | /* inform of namechange before we overwrite the old name */ | 1923 | /* inform of namechange before we overwrite the old name */ |
1918 | if (m->friend_namechange) | 1924 | if (m->friend_namechange) |
1919 | m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); | 1925 | m->friend_namechange(m, i, data_terminated, data_length, m->friend_namechange_userdata); |
1920 | 1926 | ||
1921 | memcpy(m->friendlist[i].name, data, data_length); | 1927 | memcpy(m->friendlist[i].name, data_terminated, data_length + 1); |
1922 | m->friendlist[i].name_length = data_length; | 1928 | m->friendlist[i].name_length = data_length; |
1923 | 1929 | ||
1924 | break; | 1930 | break; |
@@ -1928,13 +1934,16 @@ void do_friends(Messenger *m) | |||
1928 | if (data_length == 0 || data_length > MAX_STATUSMESSAGE_LENGTH) | 1934 | if (data_length == 0 || data_length > MAX_STATUSMESSAGE_LENGTH) |
1929 | break; | 1935 | break; |
1930 | 1936 | ||
1931 | data[data_length - 1] = 0; /* Make sure the NULL terminator is present. */ | 1937 | /* Make sure the NULL terminator is present. */ |
1938 | uint8_t data_terminated[data_length + 1]; | ||
1939 | memcpy(data_terminated, data, data_length); | ||
1940 | data_terminated[data_length] = 0; | ||
1932 | 1941 | ||
1933 | if (m->friend_statusmessagechange) | 1942 | if (m->friend_statusmessagechange) |
1934 | m->friend_statusmessagechange(m, i, data, data_length, | 1943 | m->friend_statusmessagechange(m, i, data_terminated, data_length, |
1935 | m->friend_statuschange_userdata); | 1944 | m->friend_statuschange_userdata); |
1936 | 1945 | ||
1937 | set_friend_statusmessage(m, i, data, data_length); | 1946 | set_friend_statusmessage(m, i, data_terminated, data_length); |
1938 | break; | 1947 | break; |
1939 | } | 1948 | } |
1940 | 1949 | ||
@@ -1974,14 +1983,17 @@ void do_friends(Messenger *m) | |||
1974 | uint8_t *message = data + message_id_length; | 1983 | uint8_t *message = data + message_id_length; |
1975 | uint16_t message_length = data_length - message_id_length; | 1984 | uint16_t message_length = data_length - message_id_length; |
1976 | 1985 | ||
1977 | message[message_length - 1] = 0;/* Make sure the NULL terminator is present. */ | 1986 | /* Make sure the NULL terminator is present. */ |
1987 | uint8_t message_terminated[message_length + 1]; | ||
1988 | memcpy(message_terminated, message, message_length); | ||
1989 | message_terminated[message_length] = 0; | ||
1978 | 1990 | ||
1979 | if (m->friendlist[i].receives_read_receipts) { | 1991 | if (m->friendlist[i].receives_read_receipts) { |
1980 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); | 1992 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); |
1981 | } | 1993 | } |
1982 | 1994 | ||
1983 | if (m->friend_message) | 1995 | if (m->friend_message) |
1984 | (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); | 1996 | (*m->friend_message)(m, i, message_terminated, message_length, m->friend_message_userdata); |
1985 | 1997 | ||
1986 | break; | 1998 | break; |
1987 | } | 1999 | } |
@@ -1996,14 +2008,17 @@ void do_friends(Messenger *m) | |||
1996 | uint8_t *action = data + message_id_length; | 2008 | uint8_t *action = data + message_id_length; |
1997 | uint16_t action_length = data_length - message_id_length; | 2009 | uint16_t action_length = data_length - message_id_length; |
1998 | 2010 | ||
1999 | action[action_length - 1] = 0;/* Make sure the NULL terminator is present. */ | 2011 | /* Make sure the NULL terminator is present. */ |
2012 | uint8_t action_terminated[action_length + 1]; | ||
2013 | memcpy(action_terminated, action, action_length); | ||
2014 | action_terminated[action_length] = 0; | ||
2000 | 2015 | ||
2001 | if (m->friendlist[i].receives_read_receipts) { | 2016 | if (m->friendlist[i].receives_read_receipts) { |
2002 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); | 2017 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); |
2003 | } | 2018 | } |
2004 | 2019 | ||
2005 | if (m->friend_action) | 2020 | if (m->friend_action) |
2006 | (*m->friend_action)(m, i, action, action_length, m->friend_action_userdata); | 2021 | (*m->friend_action)(m, i, action_terminated, action_length, m->friend_action_userdata); |
2007 | 2022 | ||
2008 | break; | 2023 | break; |
2009 | } | 2024 | } |
@@ -2062,10 +2077,13 @@ void do_friends(Messenger *m) | |||
2062 | m->friendlist[i].file_receiving[filenumber].size = filesize; | 2077 | m->friendlist[i].file_receiving[filenumber].size = filesize; |
2063 | m->friendlist[i].file_receiving[filenumber].transferred = 0; | 2078 | m->friendlist[i].file_receiving[filenumber].transferred = 0; |
2064 | 2079 | ||
2065 | data[data_length - 1] = 0; /* Force NULL terminate file name. */ | 2080 | /* Force NULL terminate file name. */ |
2081 | uint8_t filename_terminated[data_length - 1 - sizeof(uint64_t) + 1]; | ||
2082 | memcpy(filename_terminated, data + 1 + sizeof(uint64_t), data_length - 1 - sizeof(uint64_t)); | ||
2083 | filename_terminated[data_length - 1 - sizeof(uint64_t)] = 0; | ||
2066 | 2084 | ||
2067 | if (m->file_sendrequest) | 2085 | if (m->file_sendrequest) |
2068 | (*m->file_sendrequest)(m, i, filenumber, filesize, data + 1 + sizeof(uint64_t), data_length - 1 - sizeof(uint64_t), | 2086 | (*m->file_sendrequest)(m, i, filenumber, filesize, filename_terminated, data_length - 1 - sizeof(uint64_t), |
2069 | m->file_sendrequest_userdata); | 2087 | m->file_sendrequest_userdata); |
2070 | 2088 | ||
2071 | break; | 2089 | break; |
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 9ac72097..987b1a4a 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c | |||
@@ -141,9 +141,11 @@ static int friendreq_handlepacket(void *object, uint8_t *source_pubkey, uint8_t | |||
141 | 141 | ||
142 | addto_receivedlist(fr, source_pubkey); | 142 | addto_receivedlist(fr, source_pubkey); |
143 | 143 | ||
144 | packet[length - 1] = 0; /* Force NULL terminator. */ | 144 | uint8_t message[length - 4 + 1]; |
145 | memcpy(message, packet + 4, length - 4); | ||
146 | message[sizeof(message) - 1] = 0; /* Be sure the message is null terminated. */ | ||
145 | 147 | ||
146 | (*fr->handle_friendrequest)(source_pubkey, packet + 4, length - 4, fr->handle_friendrequest_userdata); | 148 | (*fr->handle_friendrequest)(source_pubkey, message, length - 4, fr->handle_friendrequest_userdata); |
147 | return 0; | 149 | return 0; |
148 | } | 150 | } |
149 | 151 | ||
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 1ec8ede5..22cac286 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c | |||
@@ -335,7 +335,7 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name) | |||
335 | 335 | ||
336 | static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len) | 336 | static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len) |
337 | { | 337 | { |
338 | if (contents_len > MAX_NICK_BYTES || contents_len == 0) | 338 | if (contents_len >= MAX_NICK_BYTES || contents_len == 0) |
339 | return; | 339 | return; |
340 | 340 | ||
341 | /* same name as already stored? */ | 341 | /* same name as already stored? */ |
@@ -345,7 +345,7 @@ static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t c | |||
345 | 345 | ||
346 | memcpy(chat->group[peernum].nick, contents, contents_len); | 346 | memcpy(chat->group[peernum].nick, contents, contents_len); |
347 | /* Force null termination */ | 347 | /* Force null termination */ |
348 | chat->group[peernum].nick[contents_len - 1] = 0; | 348 | chat->group[peernum].nick[contents_len] = 0; |
349 | chat->group[peernum].nick_len = contents_len; | 349 | chat->group[peernum].nick_len = contents_len; |
350 | 350 | ||
351 | if (chat->peer_namelistchange != NULL) | 351 | if (chat->peer_namelistchange != NULL) |
diff --git a/toxcore/tox.h b/toxcore/tox.h index 17bd4e1a..8fb03af3 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -97,13 +97,11 @@ TOX_USERSTATUS; | |||
97 | typedef struct Tox Tox; | 97 | typedef struct Tox Tox; |
98 | #endif | 98 | #endif |
99 | 99 | ||
100 | /* NOTE: Strings in Tox are all UTF-8, also the last byte in all strings must be NULL (0). | 100 | /* NOTE: Strings in Tox are all UTF-8, (This means that there is no terminating NULL character.) |
101 | * | 101 | * |
102 | * The length when passing those strings to the core includes that NULL character. | 102 | * The exact buffer you send will be received at the other end without modification. |
103 | * | 103 | * |
104 | * The length of all strings returned by the core include the NULL character. | 104 | * Do not treat Tox strings as C strings. |
105 | * | ||
106 | * If you send non NULL terminated strings Tox will force NULL terminates them when it receives them. | ||
107 | */ | 105 | */ |
108 | 106 | ||
109 | /* return TOX_FRIEND_ADDRESS_SIZE byte address to give to others. | 107 | /* return TOX_FRIEND_ADDRESS_SIZE byte address to give to others. |