summaryrefslogtreecommitdiff
path: root/toxcore/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/util.c')
-rw-r--r--toxcore/util.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/toxcore/util.c b/toxcore/util.c
index 28f7d26e..51b31e7b 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -44,10 +44,12 @@ void id_cpy(uint8_t *dest, uint8_t *src)
44} 44}
45 45
46int load_state(load_state_callback_func load_state_callback, void *outer, 46int load_state(load_state_callback_func load_state_callback, void *outer,
47 uint8_t *data, uint32_t length, uint16_t cookie_inner) 47 uint8_t *data, uint32_t length, uint16_t cookie_inner)
48{ 48{
49 if (!load_state_callback || !data) { 49 if (!load_state_callback || !data) {
50#ifdef DEBUG
50 fprintf(stderr, "load_state() called with invalid args.\n"); 51 fprintf(stderr, "load_state() called with invalid args.\n");
52#endif
51 return -1; 53 return -1;
52 } 54 }
53 55
@@ -55,6 +57,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
55 uint16_t type; 57 uint16_t type;
56 uint32_t length_sub, cookie_type; 58 uint32_t length_sub, cookie_type;
57 uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2; 59 uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2;
60
58 while (length > size_head) { 61 while (length > size_head) {
59 length_sub = *(uint32_t *)data; 62 length_sub = *(uint32_t *)data;
60 cookie_type = *(uint32_t *)(data + size32); 63 cookie_type = *(uint32_t *)(data + size32);
@@ -63,17 +66,22 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
63 66
64 if (length < length_sub) { 67 if (length < length_sub) {
65 /* file truncated */ 68 /* file truncated */
69#ifdef DEBUG
66 fprintf(stderr, "state file too short: %u < %u\n", length, length_sub); 70 fprintf(stderr, "state file too short: %u < %u\n", length, length_sub);
71#endif
67 return -1; 72 return -1;
68 } 73 }
69 74
70 if ((cookie_type >> 16) != cookie_inner) { 75 if ((cookie_type >> 16) != cookie_inner) {
71 /* something is not matching up in a bad way, give up */ 76 /* something is not matching up in a bad way, give up */
77#ifdef DEBUG
72 fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner); 78 fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner);
79#endif
73 return -1; 80 return -1;
74 } 81 }
75 82
76 type = cookie_type & 0xFFFF; 83 type = cookie_type & 0xFFFF;
84
77 if (-1 == load_state_callback(outer, data, length_sub, type)) 85 if (-1 == load_state_callback(outer, data, length_sub, type))
78 return -1; 86 return -1;
79 87