Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/events/friend_name.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_struct.h"
19
20
/*****************************************************
21
 *
22
 * :: struct and accessors
23
 *
24
 *****************************************************/
25
26
struct Tox_Event_Friend_Name {
27
    uint32_t friend_number;
28
    uint8_t *_Nullable name;
29
    uint32_t name_length;
30
};
31
32
static void tox_event_friend_name_set_friend_number(Tox_Event_Friend_Name *_Nonnull friend_name, uint32_t friend_number)
33
434
{
34
434
    assert(friend_name != nullptr);
35
434
    friend_name->friend_number = friend_number;
36
434
}
37
uint32_t tox_event_friend_name_get_friend_number(const Tox_Event_Friend_Name *friend_name)
38
0
{
39
0
    assert(friend_name != nullptr);
40
0
    return friend_name->friend_number;
41
0
}
42
43
static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *_Nonnull friend_name,
44
        const Memory *_Nonnull mem, const uint8_t *_Nullable name, uint32_t name_length)
45
435
{
46
435
    assert(friend_name != nullptr);
47
435
    if (friend_name->name != nullptr) {
48
0
        mem_delete(mem, friend_name->name);
49
0
        friend_name->name = nullptr;
50
0
        friend_name->name_length = 0;
51
0
    }
52
53
435
    if (name == nullptr) {
54
0
        assert(name_length == 0);
55
0
        return true;
56
0
    }
57
58
435
    if (name_length == 0) {
59
428
        friend_name->name = nullptr;
60
428
        friend_name->name_length = 0;
61
428
        return true;
62
428
    }
63
64
7
    uint8_t *name_copy = (uint8_t *)mem_balloc(mem, name_length);
65
66
7
    if (name_copy == nullptr) {
67
0
        return false;
68
0
    }
69
70
7
    memcpy(name_copy, name, name_length);
71
7
    friend_name->name = name_copy;
72
7
    friend_name->name_length = name_length;
73
7
    return true;
74
7
}
75
uint32_t tox_event_friend_name_get_name_length(const Tox_Event_Friend_Name *friend_name)
76
0
{
77
0
    assert(friend_name != nullptr);
78
0
    return friend_name->name_length;
79
0
}
80
const uint8_t *tox_event_friend_name_get_name(const Tox_Event_Friend_Name *friend_name)
81
0
{
82
0
    assert(friend_name != nullptr);
83
0
    return friend_name->name;
84
0
}
85
86
static void tox_event_friend_name_construct(Tox_Event_Friend_Name *_Nonnull friend_name)
87
462
{
88
462
    *friend_name = (Tox_Event_Friend_Name) {
89
462
        0
90
462
    };
91
462
}
92
static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *_Nonnull friend_name, const Memory *_Nonnull mem)
93
462
{
94
462
    mem_delete(mem, friend_name->name);
95
462
}
96
97
bool tox_event_friend_name_pack(
98
    const Tox_Event_Friend_Name *event, Bin_Pack *bp)
99
38
{
100
38
    return bin_pack_array(bp, 2)
101
38
           && bin_pack_u32(bp, event->friend_number)
102
38
           && bin_pack_bin(bp, event->name, event->name_length);
103
38
}
104
105
static bool tox_event_friend_name_unpack_into(Tox_Event_Friend_Name *_Nonnull event, Bin_Unpack *_Nonnull bu)
106
27
{
107
27
    assert(event != nullptr);
108
27
    if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
109
1
        return false;
110
1
    }
111
112
26
    return bin_unpack_u32(bu, &event->friend_number)
113
26
           && bin_unpack_bin(bu, &event->name, &event->name_length);
114
27
}
115
116
/*****************************************************
117
 *
118
 * :: new/free/add/get/size/unpack
119
 *
120
 *****************************************************/
121
122
const Tox_Event_Friend_Name *tox_event_get_friend_name(const Tox_Event *event)
123
0
{
124
0
    return event->type == TOX_EVENT_FRIEND_NAME ? event->data.friend_name : nullptr;
125
0
}
126
127
Tox_Event_Friend_Name *tox_event_friend_name_new(const Memory *mem)
128
463
{
129
463
    Tox_Event_Friend_Name *const friend_name =
130
463
        (Tox_Event_Friend_Name *)mem_alloc(mem, sizeof(Tox_Event_Friend_Name));
131
132
463
    if (friend_name == nullptr) {
133
1
        return nullptr;
134
1
    }
135
136
462
    tox_event_friend_name_construct(friend_name);
137
462
    return friend_name;
138
463
}
139
140
void tox_event_friend_name_free(Tox_Event_Friend_Name *friend_name, const Memory *mem)
141
463
{
142
463
    if (friend_name != nullptr) {
143
462
        tox_event_friend_name_destruct(friend_name, mem);
144
462
    }
145
463
    mem_delete(mem, friend_name);
146
463
}
147
148
static Tox_Event_Friend_Name *_Nullable tox_events_add_friend_name(Tox_Events *_Nonnull events, const Memory *_Nonnull mem)
149
435
{
150
435
    Tox_Event_Friend_Name *const friend_name = tox_event_friend_name_new(mem);
151
152
435
    if (friend_name == nullptr) {
153
0
        return nullptr;
154
0
    }
155
156
435
    Tox_Event event;
157
435
    event.type = TOX_EVENT_FRIEND_NAME;
158
435
    event.data.friend_name = friend_name;
159
160
435
    if (!tox_events_add(events, &event)) {
161
0
        tox_event_friend_name_free(friend_name, mem);
162
0
        return nullptr;
163
0
    }
164
435
    return friend_name;
165
435
}
166
167
bool tox_event_friend_name_unpack(
168
    Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem)
169
28
{
170
28
    assert(event != nullptr);
171
28
    assert(*event == nullptr);
172
28
    *event = tox_event_friend_name_new(mem);
173
174
28
    if (*event == nullptr) {
175
1
        return false;
176
1
    }
177
178
27
    return tox_event_friend_name_unpack_into(*event, bu);
179
28
}
180
181
static Tox_Event_Friend_Name *_Nullable tox_event_friend_name_alloc(Tox_Events_State *_Nonnull state)
182
435
{
183
435
    if (state->events == nullptr) {
184
0
        return nullptr;
185
0
    }
186
187
435
    Tox_Event_Friend_Name *friend_name = tox_events_add_friend_name(state->events, state->mem);
188
189
435
    if (friend_name == nullptr) {
190
0
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
191
0
        return nullptr;
192
0
    }
193
194
435
    return friend_name;
195
435
}
196
197
/*****************************************************
198
 *
199
 * :: event handler
200
 *
201
 *****************************************************/
202
203
void tox_events_handle_friend_name(
204
    Tox *_Nonnull tox,
205
    uint32_t friend_number,
206
    const uint8_t *_Nullable name, size_t length,
207
    void *_Nullable user_data)
208
435
{
209
435
    Tox_Events_State *state = tox_events_alloc(user_data);
210
435
    Tox_Event_Friend_Name *friend_name = tox_event_friend_name_alloc(state);
211
212
435
    if (friend_name == nullptr) {
213
0
        return;
214
0
    }
215
216
435
    tox_event_friend_name_set_friend_number(friend_name, friend_number);
217
435
    if (!tox_event_friend_name_set_name(friend_name, state->mem, name, length)) {
218
0
        tox_event_friend_name_free(friend_name, state->mem);
219
0
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
220
0
        return;
221
0
    }
222
435
}
223
224
void tox_events_handle_friend_name_dispatch(Tox *tox, const Tox_Event_Friend_Name *event, void *user_data)
225
0
{
226
0
    if (tox->friend_name_callback == nullptr) {
227
0
        return;
228
0
    }
229
230
0
    tox_unlock(tox);
231
0
    tox->friend_name_callback(tox, event->friend_number, event->name, event->name_length, user_data);
232
0
    tox_lock(tox);
233
0
}