Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/events/conference_invite.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2023-2026 The TokTok team.
3
 */
4
5
#include "events_alloc.h"
6
7
#include <assert.h>
8
#include <string.h>
9
10
#include "../attributes.h"
11
#include "../bin_pack.h"
12
#include "../bin_unpack.h"
13
#include "../ccompat.h"
14
#include "../mem.h"
15
#include "../tox.h"
16
#include "../tox_event.h"
17
#include "../tox_events.h"
18
#include "../tox_pack.h"
19
#include "../tox_struct.h"
20
#include "../tox_unpack.h"
21
22
/*****************************************************
23
 *
24
 * :: struct and accessors
25
 *
26
 *****************************************************/
27
28
struct Tox_Event_Conference_Invite {
29
    uint32_t friend_number;
30
    Tox_Conference_Type type;
31
    uint8_t *_Nullable cookie;
32
    uint32_t cookie_length;
33
};
34
35
static void tox_event_conference_invite_set_friend_number(Tox_Event_Conference_Invite *_Nonnull conference_invite, uint32_t friend_number)
36
28
{
37
28
    assert(conference_invite != nullptr);
38
28
    conference_invite->friend_number = friend_number;
39
28
}
40
uint32_t tox_event_conference_invite_get_friend_number(const Tox_Event_Conference_Invite *conference_invite)
41
28
{
42
28
    assert(conference_invite != nullptr);
43
28
    return conference_invite->friend_number;
44
28
}
45
46
static void tox_event_conference_invite_set_type(Tox_Event_Conference_Invite *_Nonnull conference_invite, Tox_Conference_Type type)
47
28
{
48
28
    assert(conference_invite != nullptr);
49
28
    conference_invite->type = type;
50
28
}
51
Tox_Conference_Type tox_event_conference_invite_get_type(const Tox_Event_Conference_Invite *conference_invite)
52
0
{
53
0
    assert(conference_invite != nullptr);
54
0
    return conference_invite->type;
55
0
}
56
57
static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *_Nonnull conference_invite,
58
        const Memory *_Nonnull mem, const uint8_t *_Nullable cookie, uint32_t cookie_length)
59
28
{
60
28
    assert(conference_invite != nullptr);
61
28
    if (conference_invite->cookie != nullptr) {
62
0
        mem_delete(mem, conference_invite->cookie);
63
0
        conference_invite->cookie = nullptr;
64
0
        conference_invite->cookie_length = 0;
65
0
    }
66
67
28
    if (cookie == nullptr) {
68
0
        assert(cookie_length == 0);
69
0
        return true;
70
0
    }
71
72
28
    if (cookie_length == 0) {
73
0
        conference_invite->cookie = nullptr;
74
0
        conference_invite->cookie_length = 0;
75
0
        return true;
76
0
    }
77
78
28
    uint8_t *cookie_copy = (uint8_t *)mem_balloc(mem, cookie_length);
79
80
28
    if (cookie_copy == nullptr) {
81
0
        return false;
82
0
    }
83
84
28
    memcpy(cookie_copy, cookie, cookie_length);
85
28
    conference_invite->cookie = cookie_copy;
86
28
    conference_invite->cookie_length = cookie_length;
87
28
    return true;
88
28
}
89
uint32_t tox_event_conference_invite_get_cookie_length(const Tox_Event_Conference_Invite *conference_invite)
90
28
{
91
28
    assert(conference_invite != nullptr);
92
28
    return conference_invite->cookie_length;
93
28
}
94
const uint8_t *tox_event_conference_invite_get_cookie(const Tox_Event_Conference_Invite *conference_invite)
95
28
{
96
28
    assert(conference_invite != nullptr);
97
28
    return conference_invite->cookie;
98
28
}
99
100
static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *_Nonnull conference_invite)
101
52
{
102
52
    *conference_invite = (Tox_Event_Conference_Invite) {
103
52
        0
104
52
    };
105
52
}
106
static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *_Nonnull conference_invite, const Memory *_Nonnull mem)
107
52
{
108
52
    mem_delete(mem, conference_invite->cookie);
109
52
}
110
111
bool tox_event_conference_invite_pack(
112
    const Tox_Event_Conference_Invite *event, Bin_Pack *bp)
113
24
{
114
24
    return bin_pack_array(bp, 3)
115
24
           && bin_pack_u32(bp, event->friend_number)
116
24
           && tox_conference_type_pack(event->type, bp)
117
24
           && bin_pack_bin(bp, event->cookie, event->cookie_length);
118
24
}
119
120
static bool tox_event_conference_invite_unpack_into(Tox_Event_Conference_Invite *_Nonnull event, Bin_Unpack *_Nonnull bu)
121
24
{
122
24
    assert(event != nullptr);
123
24
    if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
124
1
        return false;
125
1
    }
126
127
23
    return bin_unpack_u32(bu, &event->friend_number)
128
23
           && tox_conference_type_unpack(&event->type, bu)
129
23
           && bin_unpack_bin(bu, &event->cookie, &event->cookie_length);
130
24
}
131
132
/*****************************************************
133
 *
134
 * :: new/free/add/get/size/unpack
135
 *
136
 *****************************************************/
137
138
const Tox_Event_Conference_Invite *tox_event_get_conference_invite(const Tox_Event *event)
139
0
{
140
0
    return event->type == TOX_EVENT_CONFERENCE_INVITE ? event->data.conference_invite : nullptr;
141
0
}
142
143
Tox_Event_Conference_Invite *tox_event_conference_invite_new(const Memory *mem)
144
53
{
145
53
    Tox_Event_Conference_Invite *const conference_invite =
146
53
        (Tox_Event_Conference_Invite *)mem_alloc(mem, sizeof(Tox_Event_Conference_Invite));
147
148
53
    if (conference_invite == nullptr) {
149
1
        return nullptr;
150
1
    }
151
152
52
    tox_event_conference_invite_construct(conference_invite);
153
52
    return conference_invite;
154
53
}
155
156
void tox_event_conference_invite_free(Tox_Event_Conference_Invite *conference_invite, const Memory *mem)
157
53
{
158
53
    if (conference_invite != nullptr) {
159
52
        tox_event_conference_invite_destruct(conference_invite, mem);
160
52
    }
161
53
    mem_delete(mem, conference_invite);
162
53
}
163
164
static Tox_Event_Conference_Invite *_Nullable tox_events_add_conference_invite(Tox_Events *_Nonnull events, const Memory *_Nonnull mem)
165
28
{
166
28
    Tox_Event_Conference_Invite *const conference_invite = tox_event_conference_invite_new(mem);
167
168
28
    if (conference_invite == nullptr) {
169
0
        return nullptr;
170
0
    }
171
172
28
    Tox_Event event;
173
28
    event.type = TOX_EVENT_CONFERENCE_INVITE;
174
28
    event.data.conference_invite = conference_invite;
175
176
28
    if (!tox_events_add(events, &event)) {
177
0
        tox_event_conference_invite_free(conference_invite, mem);
178
0
        return nullptr;
179
0
    }
180
28
    return conference_invite;
181
28
}
182
183
bool tox_event_conference_invite_unpack(
184
    Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem)
185
25
{
186
25
    assert(event != nullptr);
187
25
    assert(*event == nullptr);
188
25
    *event = tox_event_conference_invite_new(mem);
189
190
25
    if (*event == nullptr) {
191
1
        return false;
192
1
    }
193
194
24
    return tox_event_conference_invite_unpack_into(*event, bu);
195
25
}
196
197
static Tox_Event_Conference_Invite *_Nullable tox_event_conference_invite_alloc(Tox_Events_State *_Nonnull state)
198
28
{
199
28
    if (state->events == nullptr) {
200
0
        return nullptr;
201
0
    }
202
203
28
    Tox_Event_Conference_Invite *conference_invite = tox_events_add_conference_invite(state->events, state->mem);
204
205
28
    if (conference_invite == nullptr) {
206
0
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
207
0
        return nullptr;
208
0
    }
209
210
28
    return conference_invite;
211
28
}
212
213
/*****************************************************
214
 *
215
 * :: event handler
216
 *
217
 *****************************************************/
218
219
void tox_events_handle_conference_invite(
220
    Tox *_Nonnull tox,
221
    uint32_t friend_number,
222
    Tox_Conference_Type type,
223
    const uint8_t *_Nullable cookie, size_t length,
224
    void *_Nullable user_data)
225
28
{
226
28
    Tox_Events_State *state = tox_events_alloc(user_data);
227
28
    Tox_Event_Conference_Invite *conference_invite = tox_event_conference_invite_alloc(state);
228
229
28
    if (conference_invite == nullptr) {
230
0
        return;
231
0
    }
232
233
28
    tox_event_conference_invite_set_friend_number(conference_invite, friend_number);
234
28
    tox_event_conference_invite_set_type(conference_invite, type);
235
28
    if (!tox_event_conference_invite_set_cookie(conference_invite, state->mem, cookie, length)) {
236
0
        tox_event_conference_invite_free(conference_invite, state->mem);
237
0
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
238
0
        return;
239
0
    }
240
28
}
241
242
void tox_events_handle_conference_invite_dispatch(Tox *tox, const Tox_Event_Conference_Invite *event, void *user_data)
243
0
{
244
0
    if (tox->conference_invite_callback == nullptr) {
245
0
        return;
246
0
    }
247
248
0
    tox_unlock(tox);
249
0
    tox->conference_invite_callback(tox, event->friend_number, event->type, event->cookie, event->cookie_length, user_data);
250
0
    tox_lock(tox);
251
0
}