Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/tox_unpack.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2022 The TokTok team.
3
 */
4
5
#include "tox_unpack.h"
6
7
#include <stdint.h>
8
9
#include "bin_unpack.h"
10
#include "tox.h"
11
12
non_null()
13
static bool tox_conference_type_from_int(uint32_t value, Tox_Conference_Type *out)
14
4
{
15
4
    switch (value) {
16
2
        case TOX_CONFERENCE_TYPE_TEXT: {
17
2
            *out = TOX_CONFERENCE_TYPE_TEXT;
18
2
            return true;
19
0
        }
20
21
1
        case TOX_CONFERENCE_TYPE_AV: {
22
1
            *out = TOX_CONFERENCE_TYPE_AV;
23
1
            return true;
24
0
        }
25
26
1
        default: {
27
1
            *out = TOX_CONFERENCE_TYPE_TEXT;
28
1
            return false;
29
0
        }
30
4
    }
31
4
}
32
bool tox_conference_type_unpack(Tox_Conference_Type *val, Bin_Unpack *bu)
33
5
{
34
5
    uint32_t u32;
35
5
    return bin_unpack_u32(bu, &u32)
36
5
           && tox_conference_type_from_int(u32, val);
37
5
}
38
39
non_null()
40
static bool tox_connection_from_int(uint32_t value, Tox_Connection *out)
41
133
{
42
133
    switch (value) {
43
65
        case TOX_CONNECTION_NONE: {
44
65
            *out = TOX_CONNECTION_NONE;
45
65
            return true;
46
0
        }
47
48
50
        case TOX_CONNECTION_TCP: {
49
50
            *out = TOX_CONNECTION_TCP;
50
50
            return true;
51
0
        }
52
53
14
        case TOX_CONNECTION_UDP: {
54
14
            *out = TOX_CONNECTION_UDP;
55
14
            return true;
56
0
        }
57
58
4
        default: {
59
4
            *out = TOX_CONNECTION_NONE;
60
4
            return false;
61
0
        }
62
133
    }
63
133
}
64
65
bool tox_connection_unpack(Tox_Connection *val, Bin_Unpack *bu)
66
137
{
67
137
    uint32_t u32;
68
137
    return bin_unpack_u32(bu, &u32)
69
137
           && tox_connection_from_int(u32, val);
70
137
}
71
72
non_null()
73
static bool tox_file_control_from_int(uint32_t value, Tox_File_Control *out)
74
45
{
75
45
    switch (value) {
76
16
        case TOX_FILE_CONTROL_RESUME: {
77
16
            *out = TOX_FILE_CONTROL_RESUME;
78
16
            return true;
79
0
        }
80
81
12
        case TOX_FILE_CONTROL_PAUSE: {
82
12
            *out = TOX_FILE_CONTROL_PAUSE;
83
12
            return true;
84
0
        }
85
86
16
        case TOX_FILE_CONTROL_CANCEL: {
87
16
            *out = TOX_FILE_CONTROL_CANCEL;
88
16
            return true;
89
0
        }
90
91
1
        default: {
92
1
            *out = TOX_FILE_CONTROL_RESUME;
93
1
            return false;
94
0
        }
95
45
    }
96
45
}
97
98
bool tox_file_control_unpack(Tox_File_Control *val, Bin_Unpack *bu)
99
46
{
100
46
    uint32_t u32;
101
46
    return bin_unpack_u32(bu, &u32)
102
46
           && tox_file_control_from_int(u32, val);
103
46
}
104
105
non_null()
106
static bool tox_message_type_from_int(uint32_t value, Tox_Message_Type *out)
107
46
{
108
46
    switch (value) {
109
15
        case TOX_MESSAGE_TYPE_NORMAL: {
110
15
            *out = TOX_MESSAGE_TYPE_NORMAL;
111
15
            return true;
112
0
        }
113
114
27
        case TOX_MESSAGE_TYPE_ACTION: {
115
27
            *out = TOX_MESSAGE_TYPE_ACTION;
116
27
            return true;
117
0
        }
118
119
4
        default: {
120
4
            *out = TOX_MESSAGE_TYPE_NORMAL;
121
4
            return false;
122
0
        }
123
46
    }
124
46
}
125
126
bool tox_message_type_unpack(Tox_Message_Type *val, Bin_Unpack *bu)
127
51
{
128
51
    uint32_t u32;
129
51
    return bin_unpack_u32(bu, &u32)
130
51
           && tox_message_type_from_int(u32, val);
131
51
}
132
133
non_null()
134
static bool tox_user_status_from_int(uint32_t value, Tox_User_Status *out)
135
47
{
136
47
    switch (value) {
137
29
        case TOX_USER_STATUS_NONE: {
138
29
            *out = TOX_USER_STATUS_NONE;
139
29
            return true;
140
0
        }
141
142
12
        case TOX_USER_STATUS_AWAY: {
143
12
            *out = TOX_USER_STATUS_AWAY;
144
12
            return true;
145
0
        }
146
147
5
        case TOX_USER_STATUS_BUSY: {
148
5
            *out = TOX_USER_STATUS_BUSY;
149
5
            return true;
150
0
        }
151
152
1
        default: {
153
1
            *out = TOX_USER_STATUS_NONE;
154
1
            return false;
155
0
        }
156
47
    }
157
47
}
158
159
bool tox_user_status_unpack(Tox_User_Status *val, Bin_Unpack *bu)
160
48
{
161
48
    uint32_t u32;
162
48
    return bin_unpack_u32(bu, &u32)
163
48
           && tox_user_status_from_int(u32, val);
164
48
}
165
166
non_null()
167
static bool tox_group_privacy_state_from_int(uint32_t value, Tox_Group_Privacy_State *out)
168
25
{
169
25
  switch (value) {
170
11
    case TOX_GROUP_PRIVACY_STATE_PUBLIC: {
171
11
      *out = TOX_GROUP_PRIVACY_STATE_PUBLIC;
172
11
      return true;
173
0
    }
174
12
    case TOX_GROUP_PRIVACY_STATE_PRIVATE: {
175
12
      *out = TOX_GROUP_PRIVACY_STATE_PRIVATE;
176
12
      return true;
177
0
    }
178
2
    default: {
179
2
      *out = TOX_GROUP_PRIVACY_STATE_PUBLIC;
180
2
      return false;
181
0
    }
182
25
  }
183
25
}
184
bool tox_group_privacy_state_unpack(Tox_Group_Privacy_State *val, Bin_Unpack *bu)
185
27
{
186
27
    uint32_t u32;
187
27
    return bin_unpack_u32(bu, &u32)
188
27
           && tox_group_privacy_state_from_int(u32, val);
189
27
}
190
non_null()
191
static bool tox_group_voice_state_from_int(uint32_t value, Tox_Group_Voice_State *out)
192
35
{
193
35
  switch (value) {
194
10
    case TOX_GROUP_VOICE_STATE_ALL: {
195
10
      *out = TOX_GROUP_VOICE_STATE_ALL;
196
10
      return true;
197
0
    }
198
14
    case TOX_GROUP_VOICE_STATE_MODERATOR: {
199
14
      *out = TOX_GROUP_VOICE_STATE_MODERATOR;
200
14
      return true;
201
0
    }
202
10
    case TOX_GROUP_VOICE_STATE_FOUNDER: {
203
10
      *out = TOX_GROUP_VOICE_STATE_FOUNDER;
204
10
      return true;
205
0
    }
206
1
    default: {
207
1
      *out = TOX_GROUP_VOICE_STATE_ALL;
208
1
      return false;
209
0
    }
210
35
  }
211
35
}
212
bool tox_group_voice_state_unpack(Tox_Group_Voice_State *val, Bin_Unpack *bu)
213
36
{
214
36
    uint32_t u32;
215
36
    return bin_unpack_u32(bu, &u32)
216
36
           && tox_group_voice_state_from_int(u32, val);
217
36
}
218
219
non_null()
220
static bool tox_group_topic_lock_from_int(uint32_t value, Tox_Group_Topic_Lock *out)
221
37
{
222
37
  switch (value) {
223
13
    case TOX_GROUP_TOPIC_LOCK_ENABLED: {
224
13
      *out = TOX_GROUP_TOPIC_LOCK_ENABLED;
225
13
      return true;
226
0
    }
227
23
    case TOX_GROUP_TOPIC_LOCK_DISABLED: {
228
23
      *out = TOX_GROUP_TOPIC_LOCK_DISABLED;
229
23
      return true;
230
0
    }
231
1
    default: {
232
1
      *out = TOX_GROUP_TOPIC_LOCK_ENABLED;
233
1
      return false;
234
0
    }
235
37
  }
236
37
}
237
bool tox_group_topic_lock_unpack(Tox_Group_Topic_Lock *val, Bin_Unpack *bu)
238
38
{
239
38
    uint32_t u32;
240
38
    return bin_unpack_u32(bu, &u32)
241
38
           && tox_group_topic_lock_from_int(u32, val);
242
38
}
243
244
non_null()
245
static bool tox_group_join_fail_from_int(uint32_t value, Tox_Group_Join_Fail *out)
246
32
{
247
32
  switch (value) {
248
11
    case TOX_GROUP_JOIN_FAIL_PEER_LIMIT: {
249
11
      *out = TOX_GROUP_JOIN_FAIL_PEER_LIMIT;
250
11
      return true;
251
0
    }
252
13
    case TOX_GROUP_JOIN_FAIL_INVALID_PASSWORD: {
253
13
      *out = TOX_GROUP_JOIN_FAIL_INVALID_PASSWORD;
254
13
      return true;
255
0
    }
256
7
    case TOX_GROUP_JOIN_FAIL_UNKNOWN: {
257
7
      *out = TOX_GROUP_JOIN_FAIL_UNKNOWN;
258
7
      return true;
259
0
    }
260
1
    default: {
261
1
      *out = TOX_GROUP_JOIN_FAIL_PEER_LIMIT;
262
1
      return false;
263
0
    }
264
32
  }
265
32
}
266
bool tox_group_join_fail_unpack(Tox_Group_Join_Fail *val, Bin_Unpack *bu)
267
33
{
268
33
    uint32_t u32;
269
33
    return bin_unpack_u32(bu, &u32)
270
33
           && tox_group_join_fail_from_int(u32, val);
271
33
}
272
273
non_null()
274
static bool tox_group_mod_event_from_int(uint32_t value, Tox_Group_Mod_Event *out)
275
46
{
276
46
  switch (value) {
277
12
    case TOX_GROUP_MOD_EVENT_KICK: {
278
12
      *out = TOX_GROUP_MOD_EVENT_KICK;
279
12
      return true;
280
0
    }
281
11
    case TOX_GROUP_MOD_EVENT_OBSERVER: {
282
11
      *out = TOX_GROUP_MOD_EVENT_OBSERVER;
283
11
      return true;
284
0
    }
285
11
    case TOX_GROUP_MOD_EVENT_USER: {
286
11
      *out = TOX_GROUP_MOD_EVENT_USER;
287
11
      return true;
288
0
    }
289
10
    case TOX_GROUP_MOD_EVENT_MODERATOR: {
290
10
      *out = TOX_GROUP_MOD_EVENT_MODERATOR;
291
10
      return true;
292
0
    }
293
2
    default: {
294
2
      *out = TOX_GROUP_MOD_EVENT_KICK;
295
2
      return false;
296
0
    }
297
46
  }
298
46
}
299
bool tox_group_mod_event_unpack(Tox_Group_Mod_Event *val, Bin_Unpack *bu)
300
47
{
301
47
    uint32_t u32;
302
47
    return bin_unpack_u32(bu, &u32)
303
47
           && tox_group_mod_event_from_int(u32, val);
304
47
}
305
306
non_null()
307
static bool tox_group_exit_type_from_int(uint32_t value, Tox_Group_Exit_Type *out)
308
59
{
309
59
  switch (value) {
310
8
    case TOX_GROUP_EXIT_TYPE_QUIT: {
311
8
      *out = TOX_GROUP_EXIT_TYPE_QUIT;
312
8
      return true;
313
0
    }
314
30
    case TOX_GROUP_EXIT_TYPE_TIMEOUT: {
315
30
      *out = TOX_GROUP_EXIT_TYPE_TIMEOUT;
316
30
      return true;
317
0
    }
318
3
    case TOX_GROUP_EXIT_TYPE_DISCONNECTED: {
319
3
      *out = TOX_GROUP_EXIT_TYPE_DISCONNECTED;
320
3
      return true;
321
0
    }
322
10
    case TOX_GROUP_EXIT_TYPE_SELF_DISCONNECTED: {
323
10
      *out = TOX_GROUP_EXIT_TYPE_SELF_DISCONNECTED;
324
10
      return true;
325
0
    }
326
3
    case TOX_GROUP_EXIT_TYPE_KICK: {
327
3
      *out = TOX_GROUP_EXIT_TYPE_KICK;
328
3
      return true;
329
0
    }
330
4
    case TOX_GROUP_EXIT_TYPE_SYNC_ERROR: {
331
4
      *out = TOX_GROUP_EXIT_TYPE_SYNC_ERROR;
332
4
      return true;
333
0
    }
334
1
    default: {
335
1
      *out = TOX_GROUP_EXIT_TYPE_QUIT;
336
1
      return false;
337
0
    }
338
59
  }
339
59
}
340
bool tox_group_exit_type_unpack(Tox_Group_Exit_Type *val, Bin_Unpack *bu)
341
60
{
342
60
    uint32_t u32;
343
60
    return bin_unpack_u32(bu, &u32)
344
60
           && tox_group_exit_type_from_int(u32, val);
345
60
}