summaryrefslogtreecommitdiff
path: root/toxcore/util.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-09 13:40:36 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-09 20:36:39 +0000
commitc8697ccc20e12b0f4a2394c10f01ce147eeca269 (patch)
tree88a2fbf4455b9fdfc58d15865340b1a184191483 /toxcore/util.c
parent751d0948abfd69629d32fabb1f505ff7ad7fb078 (diff)
Move `load_state` and its helper functions to their own module.
Diffstat (limited to 'toxcore/util.c')
-rw-r--r--toxcore/util.c79
1 files changed, 0 insertions, 79 deletions
diff --git a/toxcore/util.c b/toxcore/util.c
index 57d1762e..aa2786dd 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -92,85 +92,6 @@ void host_to_net(uint8_t *num, uint16_t numbytes)
92#endif 92#endif
93} 93}
94 94
95uint16_t lendian_to_host16(uint16_t lendian)
96{
97#ifdef WORDS_BIGENDIAN
98 return (lendian << 8) | (lendian >> 8);
99#else
100 return lendian;
101#endif
102}
103
104void host_to_lendian32(uint8_t *dest, uint32_t num)
105{
106#ifdef WORDS_BIGENDIAN
107 num = ((num << 8) & 0xFF00FF00) | ((num >> 8) & 0xFF00FF);
108 num = (num << 16) | (num >> 16);
109#endif
110 memcpy(dest, &num, sizeof(uint32_t));
111}
112
113void lendian_to_host32(uint32_t *dest, const uint8_t *lendian)
114{
115 uint32_t d;
116 memcpy(&d, lendian, sizeof(uint32_t));
117#ifdef WORDS_BIGENDIAN
118 d = ((d << 8) & 0xFF00FF00) | ((d >> 8) & 0xFF00FF);
119 d = (d << 16) | (d >> 16);
120#endif
121 *dest = d;
122}
123
124/* state load/save */
125int load_state(load_state_callback_func load_state_callback, const Logger *log, void *outer,
126 const uint8_t *data, uint32_t length, uint16_t cookie_inner)
127{
128 if (!load_state_callback || !data) {
129 LOGGER_ERROR(log, "load_state() called with invalid args.\n");
130 return -1;
131 }
132
133
134 uint32_t length_sub, cookie_type;
135 uint32_t size_head = sizeof(uint32_t) * 2;
136
137 while (length >= size_head) {
138 lendian_to_host32(&length_sub, data);
139 lendian_to_host32(&cookie_type, data + sizeof(length_sub));
140 data += size_head;
141 length -= size_head;
142
143 if (length < length_sub) {
144 /* file truncated */
145 LOGGER_ERROR(log, "state file too short: %u < %u\n", length, length_sub);
146 return -1;
147 }
148
149 if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) {
150 /* something is not matching up in a bad way, give up */
151 LOGGER_ERROR(log, "state file garbled: %04x != %04x\n", (cookie_type >> 16), cookie_inner);
152 return -1;
153 }
154
155 const uint16_t type = lendian_to_host16(cookie_type & 0xFFFF);
156 const int ret = load_state_callback(outer, data, length_sub, type);
157
158 if (ret == -1) {
159 return -1;
160 }
161
162 /* -2 means end of save. */
163 if (ret == -2) {
164 return 0;
165 }
166
167 data += length_sub;
168 length -= length_sub;
169 }
170
171 return length == 0 ? 0 : -1;
172}
173
174int create_recursive_mutex(pthread_mutex_t *mutex) 95int create_recursive_mutex(pthread_mutex_t *mutex)
175{ 96{
176 pthread_mutexattr_t attr; 97 pthread_mutexattr_t attr;