summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c17
-rw-r--r--toxcore/Messenger.c35
-rw-r--r--toxcore/util.c10
-rw-r--r--toxcore/util.h1
4 files changed, 27 insertions, 36 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 0b866940..fcd15686 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -1304,8 +1304,6 @@ int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size)
1304#define DHT_STATE_TYPE_FRIENDS 1 1304#define DHT_STATE_TYPE_FRIENDS 1
1305#define DHT_STATE_TYPE_CLIENTS 2 1305#define DHT_STATE_TYPE_CLIENTS 2
1306 1306
1307typedef uint16_t statelensub_t;
1308
1309/* Get the size of the DHT (for saving). */ 1307/* Get the size of the DHT (for saving). */
1310uint32_t DHT_size(DHT *dht) 1308uint32_t DHT_size(DHT *dht)
1311{ 1309{
@@ -1314,26 +1312,25 @@ uint32_t DHT_size(DHT *dht)
1314 if (dht->close_clientlist[i].timestamp != 0) 1312 if (dht->close_clientlist[i].timestamp != 0)
1315 num++; 1313 num++;
1316 1314
1317 uint32_t size32 = sizeof(uint32_t), lengthsublen = sizeof(statelensub_t); 1315 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
1318 uint32_t sizesubhead = lengthsublen + size32;
1319 return size32 1316 return size32
1320 + sizesubhead + sizeof(DHT_Friend) * dht->num_friends 1317 + sizesubhead + sizeof(DHT_Friend) * dht->num_friends
1321 + sizesubhead + sizeof(Client_data) * num; 1318 + sizesubhead + sizeof(Client_data) * num;
1322} 1319}
1323 1320
1324static uint8_t *z_state_save_subheader(uint8_t *data, statelensub_t len, uint16_t type) 1321static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
1325{ 1322{
1326 *(statelensub_t *)data = len; 1323 uint32_t *data32 = (uint32_t *)data;
1327 data += sizeof(statelensub_t); 1324 data32[0] = len;
1328 *(uint32_t *)data = (DHT_STATE_COOKIE_TYPE << 16) | type; 1325 data32[1] = (DHT_STATE_COOKIE_TYPE << 16) | type;
1329 data += sizeof(uint32_t); 1326 data += sizeof(uint32_t) * 2;
1330 return data; 1327 return data;
1331} 1328}
1332 1329
1333/* Save the DHT in data where data is an array of size DHT_size(). */ 1330/* Save the DHT in data where data is an array of size DHT_size(). */
1334void DHT_save(DHT *dht, uint8_t *data) 1331void DHT_save(DHT *dht, uint8_t *data)
1335{ 1332{
1336 statelensub_t len; 1333 uint32_t len;
1337 uint16_t type; 1334 uint16_t type;
1338 *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL; 1335 *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL;
1339 data += sizeof(uint32_t); 1336 data += sizeof(uint32_t);
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 682cb1be..2fbb1d3f 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1361,13 +1361,10 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length)
1361#define MESSENGER_STATE_TYPE_FRIENDS 3 1361#define MESSENGER_STATE_TYPE_FRIENDS 3
1362#define MESSENGER_STATE_TYPE_NAME 4 1362#define MESSENGER_STATE_TYPE_NAME 4
1363 1363
1364typedef uint16_t statelensub_t;
1365
1366/* return size of the messenger data (for saving) */ 1364/* return size of the messenger data (for saving) */
1367uint32_t Messenger_size(Messenger *m) 1365uint32_t Messenger_size(Messenger *m)
1368{ 1366{
1369 uint32_t size32 = sizeof(uint32_t), lengthsublen = sizeof(statelensub_t); 1367 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
1370 uint32_t sizesubhead = lengthsublen + size32;
1371 return size32 * 2 // global cookie 1368 return size32 * 2 // global cookie
1372 + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 1369 + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
1373 + sizesubhead + DHT_size(m->dht) // DHT 1370 + sizesubhead + DHT_size(m->dht) // DHT
@@ -1376,19 +1373,19 @@ uint32_t Messenger_size(Messenger *m)
1376 ; 1373 ;
1377} 1374}
1378 1375
1379static uint8_t *z_state_save_subheader(uint8_t *data, statelensub_t len, uint16_t type) 1376static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
1380{ 1377{
1381 *(statelensub_t *)data = len; 1378 uint32_t *data32 = (uint32_t *)data;
1382 data += sizeof(statelensub_t); 1379 data32[0] = len;
1383 *(uint32_t *)data = (MESSENGER_STATE_COOKIE_TYPE << 16) | type; 1380 data32[1] = (MESSENGER_STATE_COOKIE_TYPE << 16) | type;
1384 data += sizeof(uint32_t); 1381 data += sizeof(uint32_t) * 2;
1385 return data; 1382 return data;
1386} 1383}
1387 1384
1388/* Save the messenger in data of size Messenger_size(). */ 1385/* Save the messenger in data of size Messenger_size(). */
1389void Messenger_save(Messenger *m, uint8_t *data) 1386void Messenger_save(Messenger *m, uint8_t *data)
1390{ 1387{
1391 statelensub_t len; 1388 uint32_t len;
1392 uint16_t type; 1389 uint16_t type;
1393 uint32_t *data32, size32 = sizeof(uint32_t); 1390 uint32_t *data32, size32 = sizeof(uint32_t);
1394 1391
@@ -1484,17 +1481,15 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le
1484int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) 1481int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
1485{ 1482{
1486 uint32_t cookie_len = 2 * sizeof(uint32_t); 1483 uint32_t cookie_len = 2 * sizeof(uint32_t);
1487 if (length > cookie_len) { 1484 if (length < cookie_len)
1488 uint32_t *data32 = (uint32_t *)data; 1485 return -1;
1489 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) {
1490 return load_state(messenger_load_state_callback, m, data + cookie_len,
1491 length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
1492 }
1493
1494 /* old state file or too short */
1495 }
1496 1486
1497 return Messenger_load_old(m, data, length); 1487 uint32_t *data32 = (uint32_t *)data;
1488 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL))
1489 return load_state(messenger_load_state_callback, m, data + cookie_len,
1490 length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
1491 else /* old state file */
1492 return Messenger_load_old(m, data, length);
1498} 1493}
1499 1494
1500/* Allocate and return a list of valid friend id's. List must be freed by the 1495/* Allocate and return a list of valid friend id's. List must be freed by the
diff --git a/toxcore/util.c b/toxcore/util.c
index 55b51709..653e8d5f 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -56,13 +56,13 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
56 return -1; 56 return -1;
57 } 57 }
58 58
59 state_length_sub_t length_sub; 59
60 uint16_t type; 60 uint16_t type;
61 uint32_t size32 = sizeof(uint32_t), length_sub_len = sizeof(state_length_sub_t); 61 uint32_t length_sub, cookie_type;
62 uint32_t size_head = length_sub_len + size32, cookie_type; 62 uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2;
63 while (length > size_head) { 63 while (length > size_head) {
64 length_sub = *(state_length_sub_t *)data; 64 length_sub = *(uint32_t *)data;
65 cookie_type = *(uint32_t *)(data + length_sub_len); 65 cookie_type = *(uint32_t *)(data + size32);
66 data += size_head; 66 data += size_head;
67 length -= size_head; 67 length -= size_head;
68 68
diff --git a/toxcore/util.h b/toxcore/util.h
index 41f558c1..00482862 100644
--- a/toxcore/util.h
+++ b/toxcore/util.h
@@ -14,7 +14,6 @@ bool ipp_eq(IP_Port a, IP_Port b);
14bool id_eq(uint8_t *dest, uint8_t *src); 14bool id_eq(uint8_t *dest, uint8_t *src);
15void id_cpy(uint8_t *dest, uint8_t *src); 15void id_cpy(uint8_t *dest, uint8_t *src);
16 16
17typedef uint16_t state_length_sub_t;
18typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len, uint16_t type); 17typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len, uint16_t type);
19int load_state(load_state_callback_func load_state_callback, void *outer, 18int load_state(load_state_callback_func load_state_callback, void *outer,
20 uint8_t *data, uint32_t length, uint16_t cookie_inner); 19 uint8_t *data, uint32_t length, uint16_t cookie_inner);