Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/events/group_topic.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2023-2025 The TokTok team.
3
 */
4
5
#include "events_alloc.h"
6
7
#include <assert.h>
8
#include <stdlib.h>
9
#include <string.h>
10
11
#include "../attributes.h"
12
#include "../bin_pack.h"
13
#include "../bin_unpack.h"
14
#include "../ccompat.h"
15
#include "../mem.h"
16
#include "../tox.h"
17
#include "../tox_event.h"
18
#include "../tox_events.h"
19
20
/*****************************************************
21
 *
22
 * :: struct and accessors
23
 *
24
 *****************************************************/
25
26
struct Tox_Event_Group_Topic {
27
    uint32_t group_number;
28
    uint32_t peer_id;
29
    uint8_t *topic;
30
    uint32_t topic_length;
31
};
32
33
static void tox_event_group_topic_set_group_number(Tox_Event_Group_Topic *_Nonnull group_topic, uint32_t group_number)
34
618
{
35
618
    assert(group_topic != nullptr);
36
618
    group_topic->group_number = group_number;
37
618
}
38
uint32_t tox_event_group_topic_get_group_number(const Tox_Event_Group_Topic *group_topic)
39
12
{
40
12
    assert(group_topic != nullptr);
41
12
    return group_topic->group_number;
42
12
}
43
44
static void tox_event_group_topic_set_peer_id(Tox_Event_Group_Topic *_Nonnull group_topic, uint32_t peer_id)
45
618
{
46
618
    assert(group_topic != nullptr);
47
618
    group_topic->peer_id = peer_id;
48
618
}
49
uint32_t tox_event_group_topic_get_peer_id(const Tox_Event_Group_Topic *group_topic)
50
0
{
51
0
    assert(group_topic != nullptr);
52
0
    return group_topic->peer_id;
53
0
}
54
55
static bool tox_event_group_topic_set_topic(Tox_Event_Group_Topic *_Nonnull group_topic,
56
        const uint8_t *_Nullable topic, uint32_t topic_length)
57
618
{
58
618
    assert(group_topic != nullptr);
59
618
    if (group_topic->topic != nullptr) {
60
0
        free(group_topic->topic);
61
0
        group_topic->topic = nullptr;
62
0
        group_topic->topic_length = 0;
63
0
    }
64
65
618
    if (topic == nullptr) {
66
0
        assert(topic_length == 0);
67
0
        return true;
68
0
    }
69
70
618
    uint8_t *topic_copy = (uint8_t *)malloc(topic_length);
71
72
618
    if (topic_copy == nullptr) {
73
1
        return false;
74
1
    }
75
76
617
    memcpy(topic_copy, topic, topic_length);
77
617
    group_topic->topic = topic_copy;
78
617
    group_topic->topic_length = topic_length;
79
617
    return true;
80
618
}
81
uint32_t tox_event_group_topic_get_topic_length(const Tox_Event_Group_Topic *group_topic)
82
581
{
83
581
    assert(group_topic != nullptr);
84
581
    return group_topic->topic_length;
85
581
}
86
const uint8_t *tox_event_group_topic_get_topic(const Tox_Event_Group_Topic *group_topic)
87
581
{
88
581
    assert(group_topic != nullptr);
89
581
    return group_topic->topic;
90
581
}
91
92
static void tox_event_group_topic_construct(Tox_Event_Group_Topic *_Nonnull group_topic)
93
639
{
94
639
    *group_topic = (Tox_Event_Group_Topic) {
95
639
        0
96
639
    };
97
639
}
98
static void tox_event_group_topic_destruct(Tox_Event_Group_Topic *_Nonnull group_topic, const Memory *_Nonnull mem)
99
639
{
100
639
    free(group_topic->topic);
101
639
}
102
103
bool tox_event_group_topic_pack(
104
    const Tox_Event_Group_Topic *event, Bin_Pack *bp)
105
20
{
106
20
    return bin_pack_array(bp, 3)
107
20
           && bin_pack_u32(bp, event->group_number)
108
20
           && bin_pack_u32(bp, event->peer_id)
109
20
           && bin_pack_bin(bp, event->topic, event->topic_length);
110
20
}
111
112
static bool tox_event_group_topic_unpack_into(Tox_Event_Group_Topic *_Nonnull event, Bin_Unpack *_Nonnull bu)
113
20
{
114
20
    assert(event != nullptr);
115
20
    if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
116
1
        return false;
117
1
    }
118
119
19
    return bin_unpack_u32(bu, &event->group_number)
120
19
           && bin_unpack_u32(bu, &event->peer_id)
121
19
           && bin_unpack_bin(bu, &event->topic, &event->topic_length);
122
20
}
123
124
/*****************************************************
125
 *
126
 * :: new/free/add/get/size/unpack
127
 *
128
 *****************************************************/
129
130
const Tox_Event_Group_Topic *tox_event_get_group_topic(const Tox_Event *event)
131
0
{
132
0
    return event->type == TOX_EVENT_GROUP_TOPIC ? event->data.group_topic : nullptr;
133
0
}
134
135
Tox_Event_Group_Topic *tox_event_group_topic_new(const Memory *mem)
136
641
{
137
641
    Tox_Event_Group_Topic *const group_topic =
138
641
        (Tox_Event_Group_Topic *)mem_alloc(mem, sizeof(Tox_Event_Group_Topic));
139
140
641
    if (group_topic == nullptr) {
141
2
        return nullptr;
142
2
    }
143
144
639
    tox_event_group_topic_construct(group_topic);
145
639
    return group_topic;
146
641
}
147
148
void tox_event_group_topic_free(Tox_Event_Group_Topic *group_topic, const Memory *mem)
149
640
{
150
640
    if (group_topic != nullptr) {
151
639
        tox_event_group_topic_destruct(group_topic, mem);
152
639
    }
153
640
    mem_delete(mem, group_topic);
154
640
}
155
156
static Tox_Event_Group_Topic *tox_events_add_group_topic(Tox_Events *_Nonnull events, const Memory *_Nonnull mem)
157
620
{
158
620
    Tox_Event_Group_Topic *const group_topic = tox_event_group_topic_new(mem);
159
160
620
    if (group_topic == nullptr) {
161
1
        return nullptr;
162
1
    }
163
164
619
    Tox_Event event;
165
619
    event.type = TOX_EVENT_GROUP_TOPIC;
166
619
    event.data.group_topic = group_topic;
167
168
619
    if (!tox_events_add(events, &event)) {
169
1
        tox_event_group_topic_free(group_topic, mem);
170
1
        return nullptr;
171
1
    }
172
618
    return group_topic;
173
619
}
174
175
bool tox_event_group_topic_unpack(
176
    Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem)
177
21
{
178
21
    assert(event != nullptr);
179
21
    assert(*event == nullptr);
180
21
    *event = tox_event_group_topic_new(mem);
181
182
21
    if (*event == nullptr) {
183
1
        return false;
184
1
    }
185
186
20
    return tox_event_group_topic_unpack_into(*event, bu);
187
21
}
188
189
static Tox_Event_Group_Topic *tox_event_group_topic_alloc(void *_Nonnull user_data)
190
621
{
191
621
    Tox_Events_State *state = tox_events_alloc(user_data);
192
621
    assert(state != nullptr);
193
194
621
    if (state->events == nullptr) {
195
1
        return nullptr;
196
1
    }
197
198
620
    Tox_Event_Group_Topic *group_topic = tox_events_add_group_topic(state->events, state->mem);
199
200
620
    if (group_topic == nullptr) {
201
2
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
202
2
        return nullptr;
203
2
    }
204
205
618
    return group_topic;
206
620
}
207
208
/*****************************************************
209
 *
210
 * :: event handler
211
 *
212
 *****************************************************/
213
214
void tox_events_handle_group_topic(
215
    Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, size_t topic_length,
216
    void *user_data)
217
621
{
218
621
    Tox_Event_Group_Topic *group_topic = tox_event_group_topic_alloc(user_data);
219
220
621
    if (group_topic == nullptr) {
221
3
        return;
222
3
    }
223
224
618
    tox_event_group_topic_set_group_number(group_topic, group_number);
225
618
    tox_event_group_topic_set_peer_id(group_topic, peer_id);
226
618
    tox_event_group_topic_set_topic(group_topic, topic, topic_length);
227
618
}